nanohit2 commited on
Commit
c485e99
·
verified ·
1 Parent(s): f2d4f8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -20
app.py CHANGED
@@ -3,7 +3,6 @@ from transformers import pipeline
3
  import torch
4
 
5
  MODEL = "AnasAlokla/multilingual_go_emotions"
6
-
7
  classifier = pipeline(
8
  "text-classification",
9
  model=MODEL,
@@ -12,33 +11,28 @@ classifier = pipeline(
12
  device=0 if torch.cuda.is_available() else -1
13
  )
14
 
15
- def detect_gratitude(text):
16
  if not text or not text.strip():
17
- return {"gratitude_detected": False, "gratitude_score": 0.0, "all_emotions": {}}
18
-
19
  out = classifier(text)[0] # list of dicts: each with label & score
20
-
21
- # Find 'Gratitude'
22
- grat = next((e for e in out if e['label'].lower() == "gratitude"), None)
23
- grat_score = grat['score'] if grat else 0.0
24
-
25
- # Decide
26
- THRESH = 0.6 # you adjust
27
- detected = grat_score >= THRESH
28
-
29
  return {
30
- "gratitude_detected": detected,
31
- "gratitude_score": round(grat_score, 3),
32
- "all_emotions": {e['label']: round(e['score'],3) for e in out}
33
  }
34
 
35
  demo = gr.Interface(
36
- fn=detect_gratitude,
37
  inputs=gr.Textbox(lines=2, placeholder="Введите English или Russian..."),
38
  outputs="json",
39
- title="Gratitude Detector",
40
- description="Detects gratitude using multilingual_go_emotions"
41
  )
42
 
43
  if __name__ == "__main__":
44
- demo.launch()
 
3
  import torch
4
 
5
  MODEL = "AnasAlokla/multilingual_go_emotions"
 
6
  classifier = pipeline(
7
  "text-classification",
8
  model=MODEL,
 
11
  device=0 if torch.cuda.is_available() else -1
12
  )
13
 
14
+ def detect_emotions(text):
15
  if not text or not text.strip():
16
+ return {"top_emotion": "none", "top_score": 0.0, "all_emotions": {}}
17
+
18
  out = classifier(text)[0] # list of dicts: each with label & score
19
+
20
+ # Find the emotion with highest score
21
+ top_emotion = max(out, key=lambda x: x['score'])
22
+
 
 
 
 
 
23
  return {
24
+ "top_emotion": top_emotion['label'],
25
+ "top_score": round(top_emotion['score'], 3),
26
+ "all_emotions": {e['label']: round(e['score'], 3) for e in out}
27
  }
28
 
29
  demo = gr.Interface(
30
+ fn=detect_emotions,
31
  inputs=gr.Textbox(lines=2, placeholder="Введите English или Russian..."),
32
  outputs="json",
33
+ title="Emotion Detector",
34
+ description="Detects emotions using multilingual_go_emotions - shows top emotion"
35
  )
36
 
37
  if __name__ == "__main__":
38
+ demo.launch()