ShanukaB commited on
Commit
55bb220
ยท
verified ยท
1 Parent(s): 83dd819

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -18
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # app.py - very simple Flask emotion detector
2
- # English (joblib) + Sinhala & Tamil (transformers)
3
 
4
  from flask import Flask, render_template, request
5
  import joblib
@@ -8,64 +8,96 @@ from transformers import pipeline
8
 
9
  app = Flask(__name__)
10
 
 
 
 
11
  print("Loading English model...")
12
- vectorizer = joblib.load(hf_hub_download(
13
  "E-motionAssistant/Englsih_Trained_Model_LR",
14
  "tfidf_vectorizer.joblib"
15
  ))
16
- classifier = joblib.load(hf_hub_download(
17
  "E-motionAssistant/Englsih_Trained_Model_LR",
18
  "logreg_model.joblib"
19
  ))
20
- label_encoder = joblib.load(hf_hub_download(
21
  "E-motionAssistant/Englsih_Trained_Model_LR",
22
  "label_encoder.joblib"
23
  ))
24
 
25
- print("Loading Sinhala model...")
26
- sinhala_pipe = pipeline("text-classification",
27
- model="E-motionAssistant/Sinhala_Text_Emotion_Model_Retrained")
 
 
 
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  print("Loading Tamil model...")
30
- tamil_pipe = pipeline("text-classification",
31
- model="E-motionAssistant/Tamil_Emotion_Recognition_Model")
 
 
32
 
33
  print("All models loaded successfully!")
34
 
 
 
 
35
  @app.route("/", methods=["GET", "POST"])
36
  def home():
37
  english_result = ""
38
  sinhala_result = ""
39
- tamil_result = ""
40
 
41
  if request.method == "POST":
42
  action = request.form.get("action")
43
 
 
44
  if action == "predict_english":
45
  text = request.form.get("english_text", "").strip()
46
  if text:
47
- X = vectorizer.transform([text])
48
- pred = classifier.predict(X)[0]
49
- emotion = label_encoder.inverse_transform([pred])[0]
50
  english_result = f"Emotion: {emotion}"
51
  else:
52
  english_result = "Please write something"
53
 
 
54
  elif action == "predict_sinhala":
55
  text = request.form.get("sinhala_text", "").strip()
56
  if text:
57
- res = sinhala_pipe(text)[0]
58
- sinhala_result = f"Emotion: {res['label']})"
 
 
59
  else:
60
- sinhala_result = "Please write something"
61
 
 
62
  elif action == "predict_tamil":
63
  text = request.form.get("tamil_text", "").strip()
64
  if text:
65
  res = tamil_pipe(text)[0]
66
- tamil_result = f"Emotion: {res['label']})"
67
  else:
68
- tamil_result = "Please write something"
69
 
70
  return render_template(
71
  "index.html",
@@ -74,5 +106,6 @@ def home():
74
  tamil_result=tamil_result
75
  )
76
 
 
77
  if __name__ == "__main__":
78
  app.run(host="0.0.0.0", port=7860, debug=False)
 
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
 
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}"
90
  else:
91
+ sinhala_result = "เถšเถปเท”เถซเทเถšเถป เถบเถธเถšเทŠ เถฝเท’เถบเถฑเทŠเถฑ"
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"เฎ‰เฎฃเฎฐเฏเฎตเฏ: {res['label']} ({res['score']:.3f})"
99
  else:
100
+ tamil_result = "เฎ•เฎฐเฏเฎฃเฏˆเฎฏเฏเฎŸเฎฉเฏ เฎ‰เฎฐเฏˆเฎฏเฏˆ เฎ‰เฎณเฏเฎณเฎฟเฎŸเฎตเฏเฎฎเฏ"
101
 
102
  return render_template(
103
  "index.html",
 
106
  tamil_result=tamil_result
107
  )
108
 
109
+
110
  if __name__ == "__main__":
111
  app.run(host="0.0.0.0", port=7860, debug=False)