Raemih commited on
Commit
6dbcfca
ยท
verified ยท
1 Parent(s): 9d3f6cb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -79
app.py CHANGED
@@ -1,91 +1,74 @@
1
- import gradio as gr
 
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
- # English (LR)
12
- en_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "tfidf_vectorizer.joblib"))
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
- # Sinhala (LR)
17
- si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
18
- si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
19
- si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
 
20
 
21
- # Tamil (Transformers)
22
- tamil_pipe = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model")
 
23
 
24
- print("All models loaded successfully!")
 
 
25
 
26
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
27
- # 2. Prediction Functions
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 = en_vectorizer.transform([text])
37
- pred = en_classifier.predict(X)[0]
38
- return en_label_encoder.inverse_transform([pred])[0]
39
-
40
- elif language == "Sinhala":
41
- X = si_vectorizer.transform([text])
42
- pred = si_classifier.predict(X)[0]
43
- return si_label_encoder.inverse_transform([pred])[0]
44
-
45
- elif language == "Tamil":
46
- res = tamil_pipe(text)[0]
47
- return f"{res['label']} (Confidence: {res['score']:.3f})"
48
-
 
 
 
 
 
49
  except Exception as e:
50
- return f"Error: {str(e)}"
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)}