ShanukaB commited on
Commit
efd64a8
·
verified ·
1 Parent(s): 8b38023

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -16
app.py CHANGED
@@ -1,30 +1,79 @@
1
- # app.py — Space 4
2
- # requirements.txt: transformers, torch, gradio
3
-
4
  import gradio as gr
5
- from transformers import pipeline
 
 
 
 
 
 
6
 
7
- english_clf = pipeline("text-classification", model="E-motionAssistant/Englsih_Trained_Model_LR") # fix full name
8
- sinhala_clf = pipeline("text-classification", model="E-motionAssistant/SInhala_Text_Emotion_Recognition_Model") # ⚠ fix full name
9
- tamil_clf = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model")
 
 
 
 
 
 
 
 
 
10
 
11
  def classify_english(text):
12
- return english_clf(text)[0]
 
 
 
 
 
13
 
14
  def classify_sinhala(text):
15
- return sinhala_clf(text)[0]
 
 
 
 
 
16
 
17
  def classify_tamil(text):
18
- return tamil_clf(text)[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- with gr.Blocks() as demo:
21
- gr.TabbedInterface(
22
  [
23
- gr.Interface(fn=classify_english, inputs=gr.Textbox(label="English Text"), outputs=gr.JSON(), title="English"),
24
- gr.Interface(fn=classify_sinhala, inputs=gr.Textbox(label="Sinhala Text"), outputs=gr.JSON(), title="Sinhala"),
25
- gr.Interface(fn=classify_tamil, inputs=gr.Textbox(label="Tamil Text"), outputs=gr.JSON(), title="Tamil"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ],
27
- ["English Emotion", "Sinhala Emotion", "Tamil Emotion"]
28
  )
29
 
30
  demo.launch()
 
 
 
 
1
  import gradio as gr
2
+ import joblib
3
+ from huggingface_hub import hf_hub_download
4
+
5
+ # ── Your model repositories (fix typos if needed later)
6
+ EN_REPO = "E-motionAssistant/Englsih_Trained_Model_LR"
7
+ SI_REPO = "E-motionAssistant/SInhala_Text_Emotion_Recognition_Model"
8
+ TA_REPO = "E-motionAssistant/Tamil_Emotion_Recognition_Model"
9
 
10
+ # Load function (cached by Gradio)
11
+ @gr.cache_resource
12
+ def load_model(repo_id):
13
+ tfidf = joblib.load(hf_hub_download(repo_id, "tfidf_vectorizer.joblib"))
14
+ clf = joblib.load(hf_hub_download(repo_id, "logreg_model.joblib"))
15
+ le = joblib.load(hf_hub_download(repo_id, "label_encoder.joblib"))
16
+ return tfidf, clf, le
17
+
18
+ # Load all three (will download only once)
19
+ en_vec, en_clf, en_le = load_model(EN_REPO)
20
+ si_vec, si_clf, si_le = load_model(SI_REPO)
21
+ ta_vec, ta_clf, ta_le = load_model(TA_REPO)
22
 
23
  def classify_english(text):
24
+ if not text.strip():
25
+ return {"error": "කරුණාකර ටෙක්ස්ට් එකක් ඇතුලත් කරන්න"}
26
+ X = en_vec.transform([text])
27
+ pred = en_clf.predict(X)[0]
28
+ emotion = en_le.inverse_transform([pred])[0]
29
+ return {"emotion": emotion, "confidence": float(en_clf.predict_proba(X).max())}
30
 
31
  def classify_sinhala(text):
32
+ if not text.strip():
33
+ return {"error": "කරුණාකර ටෙක්ස්ට් එකක් ඇතුලත් කරන්න"}
34
+ X = si_vec.transform([text])
35
+ pred = si_clf.predict(X)[0]
36
+ emotion = si_le.inverse_transform([pred])[0]
37
+ return {"emotion": emotion, "confidence": float(si_clf.predict_proba(X).max())}
38
 
39
  def classify_tamil(text):
40
+ if not text.strip():
41
+ return {"error": "கருணையுடன் உரையை உள்ளிடவும்"}
42
+ X = ta_vec.transform([text])
43
+ pred = ta_clf.predict(X)[0]
44
+ emotion = ta_le.inverse_transform([pred])[0]
45
+ return {"emotion": emotion, "confidence": float(ta_clf.predict_proba(X).max())}
46
+
47
+ # ── UI with tabs
48
+ with gr.Blocks(title="Multilingual Emotion Recognition") as demo:
49
+ gr.Markdown("""
50
+ # Multilingual Emotion Detector
51
+ ඉංග්‍රීසි / සිංහල / தமிழ் භාෂාවලින් හැඟීම් හඳුනාගැනීම
52
+ (TF-IDF + Logistic Regression models)
53
+ """)
54
 
55
+ with gr.TabbedInterface(
 
56
  [
57
+ gr.Interface(
58
+ fn=classify_english,
59
+ inputs=gr.Textbox(label="English Text", placeholder="I'm feeling really happy today!"),
60
+ outputs=gr.JSON(label="Result"),
61
+ examples=["I hate this weather", "This is the best day ever", "Why is everything so boring?"]
62
+ ),
63
+ gr.Interface(
64
+ fn=classify_sinhala,
65
+ inputs=gr.Textbox(label="සිංහල ටෙක්ස්ට්", placeholder="මම අද ගොඩක් සතුටින් ඉන්නවා"),
66
+ outputs=gr.JSON(label="ප්‍රතිඵලය"),
67
+ examples=["මට බයයි", "ජීවිතේ ලස්සනයි", "කොහොමද මේක?"]
68
+ ),
69
+ gr.Interface(
70
+ fn=classify_tamil,
71
+ inputs=gr.Textbox(label="தமிழ் உரை", placeholder="இன்று மிகவும் சந்தோஷமாக இருக்கிறேன்"),
72
+ outputs=gr.JSON(label="முடிவு"),
73
+ examples=["எனக்கு கோபமாக இருக்கு", "இது சூப்பர்!", "எல்லாம் சோர்வாக உள்ளது"]
74
+ ),
75
  ],
76
+ tab_names=["English", "සිංහල", "தமிழ்"]
77
  )
78
 
79
  demo.launch()