Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,24 @@
|
|
1 |
import os
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'))
|
8 |
-
except OSError:
|
9 |
-
# Fallback to PyTorch model if TensorFlow fails
|
10 |
-
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'), from_pt=True)
|
11 |
-
return model
|
12 |
|
13 |
-
|
14 |
-
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'))
|
15 |
-
return tokenizer
|
16 |
|
17 |
-
def
|
18 |
-
|
19 |
-
outputs = model(**inputs)
|
20 |
-
return outputs
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
# Example prediction
|
31 |
-
text = "Sample input text"
|
32 |
-
result = predict(text, model, tokenizer)
|
33 |
-
print(result)
|
34 |
-
|
35 |
-
if __name__ == "__main__":
|
36 |
-
main()
|
|
|
1 |
import os
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import tensorflow as tf
|
5 |
|
6 |
+
load_dotenv()
|
7 |
+
api_key = os.getenv('HF_API_KEY')
|
8 |
+
model_path = os.getenv('MODEL_PATH')
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
app = Flask(__name__)
|
|
|
|
|
11 |
|
12 |
+
def load_model():
|
13 |
+
return tf.keras.models.load_model(model_path)
|
|
|
|
|
14 |
|
15 |
+
@app.route('/predict', methods=['POST'])
|
16 |
+
def predict():
|
17 |
+
data = request.get_json()
|
18 |
+
text = data['text']
|
19 |
+
model = load_model()
|
20 |
+
prediction = model.predict([text])
|
21 |
+
return jsonify(prediction.tolist())
|
22 |
|
23 |
+
if __name__ == '__main__':
|
24 |
+
app.run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|