ShanukaB commited on
Commit
d90884e
·
verified ·
1 Parent(s): 9119378

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -97
app.py CHANGED
@@ -1,108 +1,73 @@
1
- # app.py
2
- # Very simple 3-language emotion detector
3
- # Beginner version - no cache, no complicated things
4
-
5
- import gradio as gr
6
  import joblib
7
  from huggingface_hub import hf_hub_download
8
  from transformers import pipeline
9
 
10
- # === Your model links ===
11
- ENGLISH_MODEL = "E-motionAssistant/Englsih_Trained_Model_LR"
12
- SINHALA_MODEL = "E-motionAssistant/SInhala_Text_Emotion_Recognition_Model"
13
- TAMIL_MODEL = "E-motionAssistant/Tamil_Emotion_Recognition_Model"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # === Load English model (TF-IDF + Logistic Regression) ===
16
- vectorizer = joblib.load(hf_hub_download(ENGLISH_MODEL, "tfidf_vectorizer.joblib"))
17
- classifier = joblib.load(hf_hub_download(ENGLISH_MODEL, "logreg_model.joblib"))
18
- label_encoder = joblib.load(hf_hub_download(ENGLISH_MODEL, "label_encoder.joblib"))
 
19
 
20
- # === Load Sinhala and Tamil models (transformers) ===
21
- sinhala_classifier = pipeline("text-classification", model=SINHALA_MODEL)
22
- tamil_classifier = pipeline("text-classification", model=TAMIL_MODEL)
23
 
24
- # === English prediction function ===
25
- def predict_english(text):
26
- if text.strip() == "":
27
- return "Please write something"
28
-
29
- # transform text to numbers
30
- numbers = vectorizer.transform([text])
31
-
32
- # make prediction
33
- prediction_number = classifier.predict(numbers)[0]
34
-
35
- # convert number back to emotion name
36
- emotion = label_encoder.inverse_transform([prediction_number])[0]
37
-
38
- return f"English → **{emotion}**"
39
 
40
- # === Sinhala prediction function ===
41
- def predict_sinhala(text):
42
- if text.strip() == "":
43
- return "කරුණාකර යමක් ලියන්න"
44
-
45
- result = sinhala_classifier(text)[0]
46
- label = result["label"]
47
- score = result["score"]
48
-
49
- return f"සිංහල → **{label}** ({score:.2f})"
50
 
51
- # === Tamil prediction function ===
52
- def predict_tamil(text):
53
- if text.strip() == "":
54
- return "கருணையுடன் உரையை உள்ளிடவும்"
55
-
56
- result = tamil_classifier(text)[0]
57
- label = result["label"]
58
- score = result["score"]
59
-
60
- return f"தமிழ் → **{label}** ({score:.2f})"
61
 
62
- # === Create the interface ===
63
- demo = gr.TabbedInterface(
64
- [
65
- # Tab 1 - English
66
- gr.Interface(
67
- fn=predict_english,
68
- inputs=gr.Textbox(lines=3, placeholder="Type English text here..."),
69
- outputs="text",
70
- title="English Emotion",
71
- examples=[
72
- "I am very happy today",
73
- "This is so boring",
74
- "My head hurts"
75
- ]
76
- ),
77
-
78
- # Tab 2 - Sinhala
79
- gr.Interface(
80
- fn=predict_sinhala,
81
- inputs=gr.Textbox(lines=3, placeholder="සිංහලෙන් ලියන්න..."),
82
- outputs="text",
83
- title="සිංහල හැඟීම්",
84
- examples=[
85
- "මම ගොඩක් සතුටින්",
86
- "මට බයයි",
87
- "හිසරදයි බොහොමයි"
88
- ]
89
- ),
90
-
91
- # Tab 3 - Tamil
92
- gr.Interface(
93
- fn=predict_tamil,
94
- inputs=gr.Textbox(lines=3, placeholder="தமிழில் எழுதுங்கள்..."),
95
- outputs="text",
96
- title="தமிழ் உணர்வு",
97
- examples=[
98
- "இன்று மிகவும் சந்தோஷமாக இருக்கிறேன்",
99
- "எனக்கு ரொம்ப கோபமா இருக்கு",
100
- "தலைவலி கொடுமை"
101
- ]
102
- )
103
- ],
104
- tab_names=["English", "සිංහල", "தமிழ்"]
105
- )
106
 
107
- # Start the app
108
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
+ from flask import Flask, render_template, request
 
 
 
 
2
  import joblib
3
  from huggingface_hub import hf_hub_download
4
  from transformers import pipeline
5
 
6
+ app = Flask(__name__)
7
+
8
+ # ─── Load models once when the app starts ────────────────────────────────
9
+
10
+ # English (joblib model)
11
+ vectorizer = joblib.load(hf_hub_download(
12
+ "E-motionAssistant/Englsih_Trained_Model_LR",
13
+ "tfidf_vectorizer.joblib"
14
+ ))
15
+ classifier = joblib.load(hf_hub_download(
16
+ "E-motionAssistant/Englsih_Trained_Model_LR",
17
+ "logreg_model.joblib"
18
+ ))
19
+ label_encoder = joblib.load(hf_hub_download(
20
+ "E-motionAssistant/Englsih_Trained_Model_LR",
21
+ "label_encoder.joblib"
22
+ ))
23
+
24
+ # Sinhala & Tamil (transformers)
25
+ sinhala_pipe = pipeline("text-classification", model="E-motionAssistant/SInhala_Text_Emotion_Recognition_Model")
26
+ tamil_pipe = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model")
27
+
28
+ # ─── Routes ───────────────────────────────────────────────────────────────
29
 
30
+ @app.route("/", methods=["GET", "POST"])
31
+ def home():
32
+ english_result = ""
33
+ sinhala_result = ""
34
+ tamil_result = ""
35
 
36
+ if request.method == "POST":
37
+ action = request.form.get("action")
 
38
 
39
+ if action == "predict_english":
40
+ text = request.form.get("english_text", "").strip()
41
+ if text:
42
+ X = vectorizer.transform([text])
43
+ pred = classifier.predict(X)[0]
44
+ emotion = label_encoder.inverse_transform([pred])[0]
45
+ english_result = f"Emotion: {emotion}"
46
+ else:
47
+ english_result = "Please write something"
 
 
 
 
 
 
48
 
49
+ elif action == "predict_sinhala":
50
+ text = request.form.get("sinhala_text", "").strip()
51
+ if text:
52
+ res = sinhala_pipe(text)[0]
53
+ sinhala_result = f"හැඟීම: {res['label']} ({res['score']:.3f})"
54
+ else:
55
+ sinhala_result = "කරුණාකර යමක් ලියන්න"
 
 
 
56
 
57
+ elif action == "predict_tamil":
58
+ text = request.form.get("tamil_text", "").strip()
59
+ if text:
60
+ res = tamil_pipe(text)[0]
61
+ tamil_result = f"உணர்வு: {res['label']} ({res['score']:.3f})"
62
+ else:
63
+ tamil_result = "கருணையுடன் உரையை உள்ளிடவும்"
 
 
 
64
 
65
+ return render_template(
66
+ "index.html",
67
+ english_result=english_result,
68
+ sinhala_result=sinhala_result,
69
+ tamil_result=tamil_result
70
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
+ if __name__ == "__main__":
73
+ app.run(host="0.0.0.0", port=7860, debug=False)