Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,73 @@
|
|
| 1 |
-
|
| 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 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
tamil_classifier = pipeline("text-classification", model=TAMIL_MODEL)
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
score = result["score"]
|
| 48 |
-
|
| 49 |
-
return f"සිංහල → **{label}** ({score:.2f})"
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
score = result["score"]
|
| 59 |
-
|
| 60 |
-
return f"தமிழ் → **{label}** ({score:.2f})"
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 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 |
-
|
| 108 |
-
|
|
|
|
| 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)
|