Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,33 +8,27 @@ from transformers import pipeline
|
|
| 8 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
-
|
| 12 |
models = None
|
| 13 |
|
| 14 |
def load_models():
|
| 15 |
global models
|
| 16 |
if models is not None:
|
| 17 |
return
|
| 18 |
-
|
| 19 |
logging.info("Loading models...")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
en_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/English_LR_Model_New", "label_encoder.joblib"))
|
| 25 |
|
| 26 |
-
# Sinhala Model (unchanged)
|
| 27 |
si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
|
| 28 |
si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
|
| 29 |
si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
|
| 30 |
|
| 31 |
-
# Tamil Model (unchanged)
|
| 32 |
tamil_pipe = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model", device=-1)
|
| 33 |
|
| 34 |
models = (en_vectorizer, en_classifier, en_label_encoder,
|
| 35 |
si_vectorizer, si_classifier, si_label_encoder, tamil_pipe)
|
| 36 |
-
|
| 37 |
-
logging.info("✅ All models loaded successfully.")
|
| 38 |
|
| 39 |
@app.on_event("startup")
|
| 40 |
def startup_event():
|
|
@@ -56,25 +50,22 @@ def predict(req: PredictRequest):
|
|
| 56 |
en_vec, en_clf, en_le, si_vec, si_clf, si_le, tamil_pipe = models
|
| 57 |
|
| 58 |
try:
|
| 59 |
-
if req.language
|
| 60 |
X = en_vec.transform([req.text])
|
| 61 |
pred = en_clf.predict(X)[0]
|
| 62 |
emotion = en_le.inverse_transform([pred])[0]
|
| 63 |
return {"emotion": emotion, "language": "English"}
|
| 64 |
|
| 65 |
-
elif req.language
|
| 66 |
X = si_vec.transform([req.text])
|
| 67 |
pred = si_clf.predict(X)[0]
|
| 68 |
emotion = si_le.inverse_transform([pred])[0]
|
| 69 |
return {"emotion": emotion, "language": "Sinhala"}
|
| 70 |
|
| 71 |
-
elif req.language
|
| 72 |
res = tamil_pipe(req.text)[0]
|
| 73 |
-
return {
|
| 74 |
-
|
| 75 |
-
"confidence": round(res["score"], 3),
|
| 76 |
-
"language": "Tamil"
|
| 77 |
-
}
|
| 78 |
else:
|
| 79 |
return {"error": f"Unsupported language: {req.language}"}
|
| 80 |
|
|
|
|
| 8 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
| 9 |
|
| 10 |
app = FastAPI()
|
|
|
|
| 11 |
models = None
|
| 12 |
|
| 13 |
def load_models():
|
| 14 |
global models
|
| 15 |
if models is not None:
|
| 16 |
return
|
|
|
|
| 17 |
logging.info("Loading models...")
|
| 18 |
|
| 19 |
+
en_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "tfidf_vectorizer.joblib"))
|
| 20 |
+
en_classifier = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "logreg_model.joblib"))
|
| 21 |
+
en_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "label_encoder.joblib"))
|
|
|
|
| 22 |
|
|
|
|
| 23 |
si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
|
| 24 |
si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
|
| 25 |
si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
|
| 26 |
|
|
|
|
| 27 |
tamil_pipe = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model", device=-1)
|
| 28 |
|
| 29 |
models = (en_vectorizer, en_classifier, en_label_encoder,
|
| 30 |
si_vectorizer, si_classifier, si_label_encoder, tamil_pipe)
|
| 31 |
+
logging.info("✅ All models loaded.")
|
|
|
|
| 32 |
|
| 33 |
@app.on_event("startup")
|
| 34 |
def startup_event():
|
|
|
|
| 50 |
en_vec, en_clf, en_le, si_vec, si_clf, si_le, tamil_pipe = models
|
| 51 |
|
| 52 |
try:
|
| 53 |
+
if req.language == "English":
|
| 54 |
X = en_vec.transform([req.text])
|
| 55 |
pred = en_clf.predict(X)[0]
|
| 56 |
emotion = en_le.inverse_transform([pred])[0]
|
| 57 |
return {"emotion": emotion, "language": "English"}
|
| 58 |
|
| 59 |
+
elif req.language == "Sinhala":
|
| 60 |
X = si_vec.transform([req.text])
|
| 61 |
pred = si_clf.predict(X)[0]
|
| 62 |
emotion = si_le.inverse_transform([pred])[0]
|
| 63 |
return {"emotion": emotion, "language": "Sinhala"}
|
| 64 |
|
| 65 |
+
elif req.language == "Tamil":
|
| 66 |
res = tamil_pipe(req.text)[0]
|
| 67 |
+
return {"emotion": res["label"], "confidence": round(res["score"], 3), "language": "Tamil"}
|
| 68 |
+
|
|
|
|
|
|
|
|
|
|
| 69 |
else:
|
| 70 |
return {"error": f"Unsupported language: {req.language}"}
|
| 71 |
|