Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
MODEL = "AnasAlokla/multilingual_go_emotions"
|
| 6 |
+
|
| 7 |
+
classifier = pipeline(
|
| 8 |
+
"text-classification",
|
| 9 |
+
model=MODEL,
|
| 10 |
+
tokenizer=MODEL,
|
| 11 |
+
return_all_scores=True,
|
| 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()
|