Update app.py
Browse files
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
|
| 16 |
if not text or not text.strip():
|
| 17 |
-
return {"
|
| 18 |
-
|
| 19 |
out = classifier(text)[0] # list of dicts: each with label & score
|
| 20 |
-
|
| 21 |
-
# Find
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Decide
|
| 26 |
-
THRESH = 0.6 # you adjust
|
| 27 |
-
detected = grat_score >= THRESH
|
| 28 |
-
|
| 29 |
return {
|
| 30 |
-
"
|
| 31 |
-
"
|
| 32 |
-
"all_emotions": {e['label']: round(e['score'],3) for e in out}
|
| 33 |
}
|
| 34 |
|
| 35 |
demo = gr.Interface(
|
| 36 |
-
fn=
|
| 37 |
inputs=gr.Textbox(lines=2, placeholder="Введите English или Russian..."),
|
| 38 |
outputs="json",
|
| 39 |
-
title="
|
| 40 |
-
description="Detects
|
| 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()
|