Spaces:
Sleeping
Sleeping
Commit
·
8286e02
1
Parent(s):
4370811
json error fix
Browse files- main.py +4 -2
- model_utils.py +2 -9
main.py
CHANGED
@@ -4,6 +4,8 @@ import sys
|
|
4 |
import os
|
5 |
from fastapi import FastAPI
|
6 |
from model_utils import load_model_and_weights, single_predict
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
@@ -23,12 +25,12 @@ async def root():
|
|
23 |
"happens on this show every 0 seconds. I don't think that Days of our lives is a good show, but I seem to be addicted to it "
|
24 |
"anyway. PROPNAME is so nice and her and LOCNAME are finally together, but probably not for long because there is")
|
25 |
predictions = single_predict(model, test_text)
|
26 |
-
return
|
27 |
|
28 |
@app.get("/predict")
|
29 |
async def predict_personality_get(text: str):
|
30 |
predictions = single_predict(model, text)
|
31 |
-
return
|
32 |
|
33 |
if __name__ == "__main__":
|
34 |
import uvicorn
|
|
|
4 |
import os
|
5 |
from fastapi import FastAPI
|
6 |
from model_utils import load_model_and_weights, single_predict
|
7 |
+
import json
|
8 |
+
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
|
|
25 |
"happens on this show every 0 seconds. I don't think that Days of our lives is a good show, but I seem to be addicted to it "
|
26 |
"anyway. PROPNAME is so nice and her and LOCNAME are finally together, but probably not for long because there is")
|
27 |
predictions = single_predict(model, test_text)
|
28 |
+
return JSONResponse(content=predictions)
|
29 |
|
30 |
@app.get("/predict")
|
31 |
async def predict_personality_get(text: str):
|
32 |
predictions = single_predict(model, text)
|
33 |
+
return JSONResponse(content=predictions)
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
import uvicorn
|
model_utils.py
CHANGED
@@ -74,24 +74,17 @@ def tokenize_text(text, hugging_model='roberta-base'):
|
|
74 |
return x
|
75 |
|
76 |
def single_predict(model, text, traits=['cAGR', 'cCON', 'cEXT', 'cOPN', 'cNEU']):
|
77 |
-
print("predict function-----")
|
78 |
traits_scores = dict()
|
79 |
predicted_labels = dict()
|
80 |
x = tokenize_text([text])
|
81 |
logits = model.predict(x, verbose=0).logits
|
82 |
-
print("logits function-----")
|
83 |
probs = tf.math.sigmoid(logits).numpy()
|
84 |
-
print("sigmoid function-----")
|
85 |
predictions = np.where(probs > 0.5, 1, 0)
|
86 |
-
print("predictions function------")
|
87 |
for t, s in zip(traits, probs[0]):
|
88 |
-
traits_scores[t] = s
|
89 |
-
print("t, s in")
|
90 |
for t, l in zip(traits, predictions[0]):
|
91 |
-
predicted_labels[t] = l
|
92 |
-
print("t, l in")
|
93 |
final_dic = {'probability': traits_scores, 'predicted_label': predicted_labels}
|
94 |
-
print("end predict function------")
|
95 |
return final_dic
|
96 |
|
97 |
def load_model_and_weights(hugging_model='roberta-base', output_folder='.'):
|
|
|
74 |
return x
|
75 |
|
76 |
def single_predict(model, text, traits=['cAGR', 'cCON', 'cEXT', 'cOPN', 'cNEU']):
|
|
|
77 |
traits_scores = dict()
|
78 |
predicted_labels = dict()
|
79 |
x = tokenize_text([text])
|
80 |
logits = model.predict(x, verbose=0).logits
|
|
|
81 |
probs = tf.math.sigmoid(logits).numpy()
|
|
|
82 |
predictions = np.where(probs > 0.5, 1, 0)
|
|
|
83 |
for t, s in zip(traits, probs[0]):
|
84 |
+
traits_scores[t] = float(s) # Convert numpy.float32 to Python float
|
|
|
85 |
for t, l in zip(traits, predictions[0]):
|
86 |
+
predicted_labels[t] = int(l) # Convert numpy.int64 to Python int
|
|
|
87 |
final_dic = {'probability': traits_scores, 'predicted_label': predicted_labels}
|
|
|
88 |
return final_dic
|
89 |
|
90 |
def load_model_and_weights(hugging_model='roberta-base', output_folder='.'):
|