ShanukaB commited on
Commit
46b54ff
·
verified ·
1 Parent(s): 7a79852

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -35
app.py CHANGED
@@ -1,38 +1,85 @@
1
- from flask import Flask, request, render_template_string
2
  import joblib
3
  from huggingface_hub import hf_hub_download
 
4
 
5
- app = Flask(__name__)
6
-
7
- REPO = "E-motionAssistant/Englsih_Trained_Model_LR"
8
-
9
- vectorizer = joblib.load(hf_hub_download(REPO, "tfidf_vectorizer.joblib"))
10
- model = joblib.load(hf_hub_download(REPO, "logreg_model.joblib"))
11
- le = joblib.load(hf_hub_download(REPO, "label_encoder.joblib"))
12
-
13
- HTML = """
14
- <!doctype html>
15
- <title>Emotion Detector</title>
16
- <h1>Emotion Detector</h1>
17
- <form method=post>
18
- <textarea name=text rows=5 cols=60 placeholder="Write your tweet here..."></textarea><br>
19
- <input type=submit value=Predict>
20
- </form>
21
- {% if emotion %}
22
- <h2 style="color: green;">Detected: {{ emotion }}</h2>
23
- {% endif %}
24
- """
25
-
26
- @app.route("/", methods=["GET", "POST"])
27
- def home():
28
- emotion = None
29
- if request.method == "POST":
30
- text = request.form.get("text", "").strip()
31
- if text:
32
- X = vectorizer.transform([text])
33
- pred = model.predict(X)[0]
34
- emotion = le.inverse_transform([pred])[0]
35
- return render_template_string(HTML, emotion=emotion)
36
-
37
- if __name__ == "__main__":
38
- app.run(host="0.0.0.0", port=7860, debug=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)