Raemih commited on
Commit
c55fa26
ยท
verified ยท
1 Parent(s): 2c78f35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -89
app.py CHANGED
@@ -1,111 +1,91 @@
1
- # app.py - very simple Flask emotion detector
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
- # English model (already joblib)
13
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
14
- print("Loading English model...")
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
- # Sinhala model โ€“ now using joblib (LR + TF-IDF)
30
- # Replace REPO_SINHALA with your actual Sinhala LR model repository
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
- si_vectorizer = joblib.load(hf_hub_download(
36
- "E-motionAssistant/Sinhala_Text_Emotion_Model_LR",
37
- "tfidf_vectorizer.joblib"
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
- # Tamil model (still transformers)
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
- # Routes
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
- # English โ€“ joblib
72
- if action == "predict_english":
73
- text = request.form.get("english_text", "").strip()
74
- if text:
75
- X = en_vectorizer.transform([text])
76
- pred = en_classifier.predict(X)[0]
77
- emotion = en_label_encoder.inverse_transform([pred])[0]
78
- english_result = f"Emotion: {emotion}"
79
- else:
80
- english_result = "Please write something"
 
 
 
 
 
 
 
 
 
 
 
81
 
82
- # Sinhala โ€“ now joblib (same pattern as English)
83
- elif action == "predict_sinhala":
84
- text = request.form.get("sinhala_text", "").strip()
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
- # Tamil โ€“ still transformers / pipeline
94
- elif action == "predict_tamil":
95
- text = request.form.get("tamil_text", "").strip()
96
- if text:
97
- res = tamil_pipe(text)[0]
98
- tamil_result = f"Emotion: {res['label']} ({res['score']:.3f})"
99
- else:
100
- tamil_result = "Please write something"
 
 
 
 
 
 
 
 
101
 
102
- return render_template(
103
- "index.html",
104
- english_result=english_result,
105
- sinhala_result=sinhala_result,
106
- tamil_result=tamil_result
 
107
  )
108
 
 
 
 
 
 
 
 
 
109
 
110
  if __name__ == "__main__":
111
- 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
  # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
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()