Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,74 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import joblib
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
-
|
| 7 |
-
# 1. Load Models & Components
|
| 8 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 9 |
-
print("Loading Models...")
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
en_classifier = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "logreg_model.joblib"))
|
| 14 |
-
en_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "label_encoder.joblib"))
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
def predict_emotion(text, language):
|
| 31 |
-
if not text.strip():
|
| 32 |
-
return "Please enter some text."
|
| 33 |
-
|
| 34 |
try:
|
| 35 |
-
if language == "English":
|
| 36 |
-
X =
|
| 37 |
-
pred =
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 53 |
-
# 3. Gradio Interface
|
| 54 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 55 |
-
|
| 56 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 57 |
-
gr.Markdown("# ๐ Multi-Language Emotion Detector")
|
| 58 |
-
gr.Markdown("Select a language and enter your text to analyze the emotion.")
|
| 59 |
-
|
| 60 |
-
with gr.Row():
|
| 61 |
-
with gr.Column():
|
| 62 |
-
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=3)
|
| 63 |
-
lang_dropdown = gr.Dropdown(
|
| 64 |
-
choices=["English", "Sinhala", "Tamil"],
|
| 65 |
-
value="English",
|
| 66 |
-
label="Select Language"
|
| 67 |
-
)
|
| 68 |
-
submit_btn = gr.Button("Analyze Emotion", variant="primary")
|
| 69 |
-
|
| 70 |
-
with gr.Column():
|
| 71 |
-
output_label = gr.Label(label="Predicted Emotion")
|
| 72 |
-
|
| 73 |
-
# This defines the API logic automatically
|
| 74 |
-
submit_btn.click(
|
| 75 |
-
fn=predict_emotion,
|
| 76 |
-
inputs=[input_text, lang_dropdown],
|
| 77 |
-
outputs=output_label,
|
| 78 |
-
api_name="predict" # This creates the /predict API endpoint
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
gr.Examples(
|
| 82 |
-
examples=[
|
| 83 |
-
["I am very happy today!", "English"],
|
| 84 |
-
["เถธเถธ เถ
เถฏ เถเทเถฉเถเท เทเถญเทเถงเทเถฑเท", "Sinhala"],
|
| 85 |
-
["เฎจเฎพเฎฉเฏ เฎเฎฉเฏเฎฑเฏ เฎฎเฎฟเฎเฎตเฏเฎฎเฏ เฎฎเฎเฎฟเฎดเฏเฎเฏเฎเฎฟเฎฏเฎพเฎ เฎเฎฐเฏเฎเฏเฎเฎฟเฎฑเฏเฎฉเฏ", "Tamil"]
|
| 86 |
-
],
|
| 87 |
-
inputs=[input_text, lang_dropdown]
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
if __name__ == "__main__":
|
| 91 |
-
demo.launch()
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import joblib
|
| 4 |
+
import logging
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 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():
|
| 35 |
+
load_models()
|
| 36 |
+
|
| 37 |
+
class PredictRequest(BaseModel):
|
| 38 |
+
text: str
|
| 39 |
+
language: str # "English", "Sinhala", or "Tamil"
|
| 40 |
+
|
| 41 |
+
@app.get("/")
|
| 42 |
+
def root():
|
| 43 |
+
return {"status": "ok", "message": "Emotion Detector API is running"}
|
| 44 |
+
|
| 45 |
+
@app.post("/predict")
|
| 46 |
+
def predict(req: PredictRequest):
|
| 47 |
+
if not req.text.strip():
|
| 48 |
+
return {"error": "Text cannot be empty"}
|
| 49 |
+
|
| 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 |
+
|
| 72 |
except Exception as e:
|
| 73 |
+
logging.error(f"Prediction error: {e}")
|
| 74 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|