Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,85 @@
|
|
| 1 |
-
|
| 2 |
import joblib
|
| 3 |
from huggingface_hub import hf_hub_download
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# ── Model repositories ────────────────────────────────────────
|
| 7 |
+
EN_REPO = "E-motionAssistant/Englsih_Trained_Model_LR"
|
| 8 |
+
SI_REPO = "E-motionAssistant/SInhala_Text_Emotion_Recognition_Model"
|
| 9 |
+
TA_REPO = "E-motionAssistant/Tamil_Emotion_Recognition_Model"
|
| 10 |
+
|
| 11 |
+
# ── English (your LR model) ───────────────────────────────────
|
| 12 |
+
@gr.cache
|
| 13 |
+
def load_en():
|
| 14 |
+
vec = joblib.load(hf_hub_download(EN_REPO, "tfidf_vectorizer.joblib"))
|
| 15 |
+
clf = joblib.load(hf_hub_download(EN_REPO, "logreg_model.joblib"))
|
| 16 |
+
le = joblib.load(hf_hub_download(EN_REPO, "label_encoder.joblib"))
|
| 17 |
+
return vec, clf, le
|
| 18 |
+
|
| 19 |
+
en_vec, en_clf, en_le = load_en()
|
| 20 |
+
|
| 21 |
+
def predict_en(text):
|
| 22 |
+
if not text.strip():
|
| 23 |
+
return "කරුණාකර යමක් ලියන්න"
|
| 24 |
+
X = en_vec.transform([text])
|
| 25 |
+
pred = en_clf.predict(X)[0]
|
| 26 |
+
emotion = en_le.inverse_transform([pred])[0]
|
| 27 |
+
return f"**English:** {emotion}"
|
| 28 |
+
|
| 29 |
+
# ── Sinhala & Tamil (finetuned transformers) ──────────────────
|
| 30 |
+
@gr.cache_resource
|
| 31 |
+
def load_si():
|
| 32 |
+
return pipeline("text-classification", model=SI_REPO)
|
| 33 |
+
|
| 34 |
+
@gr.cache_resource
|
| 35 |
+
def load_ta():
|
| 36 |
+
return pipeline("text-classification", model=TA_REPO)
|
| 37 |
+
|
| 38 |
+
si_pipe = load_si()
|
| 39 |
+
ta_pipe = load_ta()
|
| 40 |
+
|
| 41 |
+
def predict_si(text):
|
| 42 |
+
if not text.strip():
|
| 43 |
+
return "කරුණාකර යමක් ලියන්න"
|
| 44 |
+
res = si_pipe(text)[0]
|
| 45 |
+
label = res['label']
|
| 46 |
+
score = res['score']
|
| 47 |
+
return f"**Sinhala:** {label} ({score:.3f})"
|
| 48 |
+
|
| 49 |
+
def predict_ta(text):
|
| 50 |
+
if not text.strip():
|
| 51 |
+
return "கருணையுடன் உரையை உள்ளிடவும்"
|
| 52 |
+
res = ta_pipe(text)[0]
|
| 53 |
+
label = res['label']
|
| 54 |
+
score = res['score']
|
| 55 |
+
return f"**Tamil:** {label} ({score:.3f})"
|
| 56 |
+
|
| 57 |
+
# ── Interface ─────────────────────────────────────────────────
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("# Multilingual Emotion Recognition\nEnglish • සිංහල • தமிழ்")
|
| 60 |
+
|
| 61 |
+
with gr.TabbedInterface(
|
| 62 |
+
[
|
| 63 |
+
gr.Interface(
|
| 64 |
+
fn=predict_en,
|
| 65 |
+
inputs=gr.Textbox(label="English", lines=3),
|
| 66 |
+
outputs="markdown",
|
| 67 |
+
examples=["I am very sad today", "This is so fun!", "headache ugh"]
|
| 68 |
+
),
|
| 69 |
+
gr.Interface(
|
| 70 |
+
fn=predict_si,
|
| 71 |
+
inputs=gr.Textbox(label="සිංහල", lines=3),
|
| 72 |
+
outputs="markdown",
|
| 73 |
+
examples=["මම ගොඩක් දුකින් ඉන්නවා", "අද ගොඩක් සතුටුයි", "හිසරදයි බයයි"]
|
| 74 |
+
),
|
| 75 |
+
gr.Interface(
|
| 76 |
+
fn=predict_ta,
|
| 77 |
+
inputs=gr.Textbox(label="தமிழ்", lines=3),
|
| 78 |
+
outputs="markdown",
|
| 79 |
+
examples=["இன்று ரொம்ப சோர்வாக இருக்கிறேன்", "மிகவும் சந்தோஷமாக இருக்கு", "தலைவலி கொடுமை"]
|
| 80 |
+
)
|
| 81 |
+
],
|
| 82 |
+
tab_names=["English", "සිංහල", "தமிழ்"]
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|