Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,42 +23,20 @@ label_encoder = joblib.load(hf_hub_download(
|
|
| 23 |
))
|
| 24 |
|
| 25 |
print("Loading Sinhala model...")
|
| 26 |
-
sinhala_pipe = pipeline(
|
| 27 |
-
|
| 28 |
-
model="E-motionAssistant/Sinhala_Text_Emotion_Model_Retrained"
|
| 29 |
-
)
|
| 30 |
|
| 31 |
print("Loading Tamil model...")
|
| 32 |
-
tamil_pipe = pipeline(
|
| 33 |
-
|
| 34 |
-
model="E-motionAssistant/Tamil_Emotion_Recognition_Model"
|
| 35 |
-
)
|
| 36 |
|
| 37 |
print("All models loaded successfully!")
|
| 38 |
|
| 39 |
-
# Sinhala label mapping (replace these names with your actual classes)
|
| 40 |
-
# You need to fill this list with the correct Sinhala emotion names in order
|
| 41 |
-
SINHALA_LABELS = [
|
| 42 |
-
"neutral", # LABEL_0
|
| 43 |
-
"සතුට", # LABEL_1
|
| 44 |
-
"දුක", # LABEL_2
|
| 45 |
-
"කෝපය", # LABEL_3
|
| 46 |
-
"බය", # LABEL_4
|
| 47 |
-
"ආදරය", # LABEL_5
|
| 48 |
-
"අප්රසාදය", # LABEL_6
|
| 49 |
-
"අපහාසය", # LABEL_7
|
| 50 |
-
"කලකිරීම", # LABEL_8
|
| 51 |
-
"අනුකම්පාව", # LABEL_9
|
| 52 |
-
"අපේක්ෂාව", # LABEL_10
|
| 53 |
-
"සැහැල්ලුව", # LABEL_11
|
| 54 |
-
"වෙනත්" # LABEL_12 ← change this if LABEL_12 is something else
|
| 55 |
-
]
|
| 56 |
-
|
| 57 |
@app.route("/", methods=["GET", "POST"])
|
| 58 |
def home():
|
| 59 |
english_result = ""
|
| 60 |
sinhala_result = ""
|
| 61 |
-
tamil_result
|
| 62 |
|
| 63 |
if request.method == "POST":
|
| 64 |
action = request.form.get("action")
|
|
@@ -77,32 +55,17 @@ def home():
|
|
| 77 |
text = request.form.get("sinhala_text", "").strip()
|
| 78 |
if text:
|
| 79 |
res = sinhala_pipe(text)[0]
|
| 80 |
-
|
| 81 |
-
score = res['score']
|
| 82 |
-
|
| 83 |
-
# Convert LABEL_XX → real Sinhala emotion name
|
| 84 |
-
if label.startswith("LABEL_"):
|
| 85 |
-
try:
|
| 86 |
-
num = int(label.replace("LABEL_", ""))
|
| 87 |
-
if 0 <= num < len(SINHALA_LABELS):
|
| 88 |
-
label = SINHALA_LABELS[num]
|
| 89 |
-
else:
|
| 90 |
-
label = f"නොදන්නා ({num})"
|
| 91 |
-
except:
|
| 92 |
-
pass # keep original if conversion fails
|
| 93 |
-
|
| 94 |
-
sinhala_result = f"හැඟීම: {label} ({score:.3f})"
|
| 95 |
else:
|
| 96 |
-
sinhala_result = "
|
| 97 |
|
| 98 |
elif action == "predict_tamil":
|
| 99 |
text = request.form.get("tamil_text", "").strip()
|
| 100 |
if text:
|
| 101 |
res = tamil_pipe(text)[0]
|
| 102 |
-
|
| 103 |
-
tamil_result = f"உணர்வு: {res['label']} ({res['score']:.3f})"
|
| 104 |
else:
|
| 105 |
-
tamil_result = "
|
| 106 |
|
| 107 |
return render_template(
|
| 108 |
"index.html",
|
|
@@ -111,6 +74,5 @@ def home():
|
|
| 111 |
tamil_result=tamil_result
|
| 112 |
)
|
| 113 |
|
| 114 |
-
|
| 115 |
if __name__ == "__main__":
|
| 116 |
app.run(host="0.0.0.0", port=7860, debug=False)
|
|
|
|
| 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")
|
|
|
|
| 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 |
tamil_result=tamil_result
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
if __name__ == "__main__":
|
| 78 |
app.run(host="0.0.0.0", port=7860, debug=False)
|