Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,36 @@
|
|
1 |
-
import
|
2 |
-
from transformers import TFBertForSequenceClassification
|
3 |
-
from flask import Flask, request, jsonify
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
model_name =
|
9 |
-
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
inputs = preprocess_data(data)
|
16 |
-
# Make prediction
|
17 |
-
predictions = model.predict(inputs)
|
18 |
-
# Postprocess and return results
|
19 |
-
results = postprocess_predictions(predictions)
|
20 |
-
return jsonify(results)
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
pass
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from transformers import TFBertForSequenceClassification, BertTokenizerFast
|
|
|
3 |
|
4 |
+
def load_model(model_name):
|
5 |
+
try:
|
6 |
+
# Load TensorFlow model from Hugging Face
|
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 |
+
def load_tokenizer(model_name):
|
14 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'))
|
15 |
+
return tokenizer
|
16 |
|
17 |
+
def predict(text, model, tokenizer):
|
18 |
+
inputs = tokenizer(text, return_tensors="tf")
|
19 |
+
outputs = model(**inputs)
|
20 |
+
return outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
def main():
|
23 |
+
model_name = os.getenv('MODEL_PATH')
|
24 |
+
if model_name is None:
|
25 |
+
raise ValueError("MODEL_PATH environment variable not set or is None")
|
26 |
|
27 |
+
model = load_model(model_name)
|
28 |
+
tokenizer = load_tokenizer(model_name)
|
|
|
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()
|