Spaces:
Sleeping
Sleeping
catch error
Browse files- main_sentiment.py +24 -20
main_sentiment.py
CHANGED
@@ -32,23 +32,27 @@ def read_root():
|
|
32 |
@app.post("/predict")
|
33 |
def predict(text:str):
|
34 |
"prediction endpoint, classifying tweets"
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
32 |
@app.post("/predict")
|
33 |
def predict(text:str):
|
34 |
"prediction endpoint, classifying tweets"
|
35 |
+
try:
|
36 |
+
text = preprocess(text)
|
37 |
+
|
38 |
+
# PyTorch-based models
|
39 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
40 |
+
output = model(**encoded_input)
|
41 |
+
scores = output[0][0].detach().numpy()
|
42 |
+
scores = softmax(scores)
|
43 |
+
|
44 |
+
#Process scores
|
45 |
+
ranking = np.argsort(scores)
|
46 |
+
ranking = ranking[::-1]
|
47 |
+
predicted_label = config.id2label[ranking[0]]
|
48 |
+
predicted_score = scores[ranking[0]]
|
49 |
+
|
50 |
+
|
51 |
+
return {"text":text,
|
52 |
+
"predicted_label":predicted_label,
|
53 |
+
"confidence_score":predicted_score
|
54 |
+
}
|
55 |
+
except Exception as e:
|
56 |
+
return {
|
57 |
+
"error": e
|
58 |
+
}
|