Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,91 @@
|
|
| 1 |
-
|
| 2 |
-
# English (joblib) + Sinhala (joblib) + Tamil (transformers)
|
| 3 |
-
|
| 4 |
-
from flask import Flask, render_template, request
|
| 5 |
import joblib
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
|
| 11 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 12 |
-
#
|
| 13 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 14 |
-
print("Loading
|
| 15 |
-
en_vectorizer = joblib.load(hf_hub_download(
|
| 16 |
-
"E-motionAssistant/Englsih_Trained_Model_LR",
|
| 17 |
-
"tfidf_vectorizer.joblib"
|
| 18 |
-
))
|
| 19 |
-
en_classifier = joblib.load(hf_hub_download(
|
| 20 |
-
"E-motionAssistant/Englsih_Trained_Model_LR",
|
| 21 |
-
"logreg_model.joblib"
|
| 22 |
-
))
|
| 23 |
-
en_label_encoder = joblib.load(hf_hub_download(
|
| 24 |
-
"E-motionAssistant/Englsih_Trained_Model_LR",
|
| 25 |
-
"label_encoder.joblib"
|
| 26 |
-
))
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
print("Loading Sinhala LR model...")
|
| 33 |
-
REPO_SINHALA = "E-motionAssistant/Sinhala_Text_Emotion_Model_LR" # โ CHANGE THIS to your real repo name
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
))
|
| 39 |
-
si_classifier = joblib.load(hf_hub_download(
|
| 40 |
-
"E-motionAssistant/Sinhala_Text_Emotion_Model_LR",
|
| 41 |
-
"logreg_model.joblib"
|
| 42 |
-
))
|
| 43 |
-
si_label_encoder = joblib.load(hf_hub_download(
|
| 44 |
-
"E-motionAssistant/Sinhala_Text_Emotion_Model_LR",
|
| 45 |
-
"label_encoder.joblib"
|
| 46 |
-
))
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 51 |
-
print("Loading Tamil model...")
|
| 52 |
-
tamil_pipe = pipeline(
|
| 53 |
-
"text-classification",
|
| 54 |
-
model="E-motionAssistant/Tamil_Emotion_Recognition_Model"
|
| 55 |
-
)
|
| 56 |
|
| 57 |
print("All models loaded successfully!")
|
| 58 |
|
| 59 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 60 |
-
#
|
| 61 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 62 |
-
@app.route("/", methods=["GET", "POST"])
|
| 63 |
-
def home():
|
| 64 |
-
english_result = ""
|
| 65 |
-
sinhala_result = ""
|
| 66 |
-
tamil_result = ""
|
| 67 |
-
|
| 68 |
-
if request.method == "POST":
|
| 69 |
-
action = request.form.get("action")
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
if text:
|
| 86 |
-
X = si_vectorizer.transform([text])
|
| 87 |
-
pred = si_classifier.predict(X)[0]
|
| 88 |
-
emotion = si_label_encoder.inverse_transform([pred])[0]
|
| 89 |
-
sinhala_result = f"Emotion: {emotion}"
|
| 90 |
-
else:
|
| 91 |
-
sinhala_result = "Please write something"
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import joblib
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from transformers import pipeline
|
| 5 |
|
|
|
|
|
|
|
| 6 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 7 |
+
# 1. Load Models & Components
|
| 8 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 9 |
+
print("Loading Models...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# English (LR)
|
| 12 |
+
en_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "tfidf_vectorizer.joblib"))
|
| 13 |
+
en_classifier = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "logreg_model.joblib"))
|
| 14 |
+
en_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Englsih_Trained_Model_LR", "label_encoder.joblib"))
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Sinhala (LR)
|
| 17 |
+
si_vectorizer = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "tfidf_vectorizer.joblib"))
|
| 18 |
+
si_classifier = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "logreg_model.joblib"))
|
| 19 |
+
si_label_encoder = joblib.load(hf_hub_download("E-motionAssistant/Sinhala_Text_Emotion_Model_LR", "label_encoder.joblib"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# Tamil (Transformers)
|
| 22 |
+
tamil_pipe = pipeline("text-classification", model="E-motionAssistant/Tamil_Emotion_Recognition_Model")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
print("All models loaded successfully!")
|
| 25 |
|
| 26 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 27 |
+
# 2. Prediction Functions
|
| 28 |
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
def predict_emotion(text, language):
|
| 31 |
+
if not text.strip():
|
| 32 |
+
return "Please enter some text."
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
if language == "English":
|
| 36 |
+
X = en_vectorizer.transform([text])
|
| 37 |
+
pred = en_classifier.predict(X)[0]
|
| 38 |
+
return en_label_encoder.inverse_transform([pred])[0]
|
| 39 |
+
|
| 40 |
+
elif language == "Sinhala":
|
| 41 |
+
X = si_vectorizer.transform([text])
|
| 42 |
+
pred = si_classifier.predict(X)[0]
|
| 43 |
+
return si_label_encoder.inverse_transform([pred])[0]
|
| 44 |
+
|
| 45 |
+
elif language == "Tamil":
|
| 46 |
+
res = tamil_pipe(text)[0]
|
| 47 |
+
return f"{res['label']} (Confidence: {res['score']:.3f})"
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error: {str(e)}"
|
| 51 |
|
| 52 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 53 |
+
# 3. Gradio Interface
|
| 54 |
+
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 57 |
+
gr.Markdown("# ๐ Multi-Language Emotion Detector")
|
| 58 |
+
gr.Markdown("Select a language and enter your text to analyze the emotion.")
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
with gr.Column():
|
| 62 |
+
input_text = gr.Textbox(label="Input Text", placeholder="Type here...", lines=3)
|
| 63 |
+
lang_dropdown = gr.Dropdown(
|
| 64 |
+
choices=["English", "Sinhala", "Tamil"],
|
| 65 |
+
value="English",
|
| 66 |
+
label="Select Language"
|
| 67 |
+
)
|
| 68 |
+
submit_btn = gr.Button("Analyze Emotion", variant="primary")
|
| 69 |
+
|
| 70 |
+
with gr.Column():
|
| 71 |
+
output_label = gr.Label(label="Predicted Emotion")
|
| 72 |
|
| 73 |
+
# This defines the API logic automatically
|
| 74 |
+
submit_btn.click(
|
| 75 |
+
fn=predict_emotion,
|
| 76 |
+
inputs=[input_text, lang_dropdown],
|
| 77 |
+
outputs=output_label,
|
| 78 |
+
api_name="predict" # This creates the /predict API endpoint
|
| 79 |
)
|
| 80 |
|
| 81 |
+
gr.Examples(
|
| 82 |
+
examples=[
|
| 83 |
+
["I am very happy today!", "English"],
|
| 84 |
+
["เถธเถธ เถ
เถฏ เถเทเถฉเถเท เทเถญเทเถงเทเถฑเท", "Sinhala"],
|
| 85 |
+
["เฎจเฎพเฎฉเฏ เฎเฎฉเฏเฎฑเฏ เฎฎเฎฟเฎเฎตเฏเฎฎเฏ เฎฎเฎเฎฟเฎดเฏเฎเฏเฎเฎฟเฎฏเฎพเฎ เฎเฎฐเฏเฎเฏเฎเฎฟเฎฑเฏเฎฉเฏ", "Tamil"]
|
| 86 |
+
],
|
| 87 |
+
inputs=[input_text, lang_dropdown]
|
| 88 |
+
)
|
| 89 |
|
| 90 |
if __name__ == "__main__":
|
| 91 |
+
demo.launch()
|