shuvo100 commited on
Commit
f66df03
·
verified ·
1 Parent(s): 60993c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -7,28 +7,24 @@ emotion_classifier = pipeline(
7
  model="Toshifumi/bert-base-multilingual-cased-finetuned-emotion"
8
  )
9
 
10
- # Fallback function for Bengali
11
- def fallback_bangla_emotion(text):
12
- if any(word in text for word in ["দুঃখ", "কষ্ট", "বিরক্ত", "অসন্তুষ্ট"]):
13
- return "Sad"
14
- elif any(word in text for word in ["ভালো", "খুশি", "আনন্দ"]):
15
- return "Happy"
16
- elif any(word in text for word in ["রাগ", "গরম", "ক্ষোভ"]):
17
- return "Angry"
18
- else:
19
- return "Neutral"
20
 
21
  def detect_emotion(text):
22
  try:
23
  result = emotion_classifier(text)[0]
24
  label = result["label"]
25
-
26
- if label.lower() == "neutral":
27
- return fallback_bangla_emotion(text)
28
- else:
29
- return label
30
- except:
31
- return fallback_bangla_emotion(text)
32
 
33
  interface = gr.Interface(
34
  fn=detect_emotion,
 
7
  model="Toshifumi/bert-base-multilingual-cased-finetuned-emotion"
8
  )
9
 
10
+ # Mapping label_x to actual emotion name
11
+ label_map = {
12
+ "label_0": "Anger",
13
+ "label_1": "Sadness",
14
+ "label_2": "Joy",
15
+ "label_3": "Love",
16
+ "label_4": "Fear",
17
+ "label_5": "Surprise"
18
+ }
 
19
 
20
  def detect_emotion(text):
21
  try:
22
  result = emotion_classifier(text)[0]
23
  label = result["label"]
24
+ emotion = label_map.get(label, "Unknown")
25
+ return emotion
26
+ except Exception as e:
27
+ return "Unknown"
 
 
 
28
 
29
  interface = gr.Interface(
30
  fn=detect_emotion,