Update app.py
Browse files
app.py
CHANGED
@@ -40,16 +40,15 @@ emotion_icons = {
|
|
40 |
"neutral": "😐"
|
41 |
}
|
42 |
|
43 |
-
# Prediction function
|
44 |
-
def get_emotions(text):
|
45 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
46 |
with torch.no_grad():
|
47 |
logits = model(**inputs).logits
|
48 |
probs = sigmoid(logits)[0]
|
49 |
|
50 |
-
threshold = 0.3 # You can adjust this threshold
|
51 |
labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]
|
52 |
-
icons = [emotion_icons.get(label, '') + ' ' + label.capitalize() for label in labels]
|
53 |
|
54 |
return ", ".join(icons) if icons else "No strong emotion detected."
|
55 |
|
@@ -86,10 +85,13 @@ body {
|
|
86 |
|
87 |
demo = gr.Interface(
|
88 |
fn=get_emotions,
|
89 |
-
inputs=
|
|
|
|
|
|
|
90 |
outputs=gr.Textbox(label="Detected Emotions", elem_classes=["output-textbox"]),
|
91 |
title="🥰 Multi-Label Emotion Detector",
|
92 |
-
description="Enter a sentence or paragraph to detect multiple emotions present in the text.",
|
93 |
theme="default",
|
94 |
css=custom_css
|
95 |
)
|
|
|
40 |
"neutral": "😐"
|
41 |
}
|
42 |
|
43 |
+
# Prediction function with adjustable threshold
|
44 |
+
def get_emotions(text, threshold):
|
45 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
46 |
with torch.no_grad():
|
47 |
logits = model(**inputs).logits
|
48 |
probs = sigmoid(logits)[0]
|
49 |
|
|
|
50 |
labels = [model.config.id2label[i] for i, p in enumerate(probs) if p > threshold]
|
51 |
+
icons = [emotion_icons.get(label, '') + ' ' + label.capitalize() + f" ({probs[i]:.2f})" for i, label in enumerate(labels)]
|
52 |
|
53 |
return ", ".join(icons) if icons else "No strong emotion detected."
|
54 |
|
|
|
85 |
|
86 |
demo = gr.Interface(
|
87 |
fn=get_emotions,
|
88 |
+
inputs=[
|
89 |
+
gr.Textbox(lines=5, placeholder="Write a sentence or a full paragraph...", label="Your Text"),
|
90 |
+
gr.Slider(minimum=0.1, maximum=0.9, value=0.3, step=0.05, label="Threshold")
|
91 |
+
],
|
92 |
outputs=gr.Textbox(label="Detected Emotions", elem_classes=["output-textbox"]),
|
93 |
title="🥰 Multi-Label Emotion Detector",
|
94 |
+
description="Enter a sentence or paragraph to detect multiple emotions present in the text. Adjust the threshold to be more or less sensitive.",
|
95 |
theme="default",
|
96 |
css=custom_css
|
97 |
)
|