Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load pre-trained model and tokenizer
|
6 |
+
model_name = "borisn70/bert-43-multilabel-emotion-detection"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Labels corresponding to different emotions
|
11 |
+
labels = [
|
12 |
+
'admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion',
|
13 |
+
'curiosity', 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment',
|
14 |
+
'excitement', 'fear', 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'optimism',
|
15 |
+
'pride', 'realization', 'relief', 'remorse', 'sadness', 'surprise', 'neutral'
|
16 |
+
]
|
17 |
+
|
18 |
+
# Function to predict emotions based on input text
|
19 |
+
def predict_emotions(text):
|
20 |
+
inputs = tokenizer(text, return_tensors="pt") # Tokenize the input text
|
21 |
+
with torch.no_grad():
|
22 |
+
logits = model(**inputs).logits # Get the model's output logits
|
23 |
+
probs = torch.sigmoid(logits)[0] # Apply sigmoid to get probabilities
|
24 |
+
|
25 |
+
# Filter results with probability > 0.5
|
26 |
+
results = {label: float(prob) for label, prob in zip(labels, probs) if prob > 0.5}
|
27 |
+
return results
|
28 |
+
|
29 |
+
# Set up Gradio interface
|
30 |
+
iface = gr.Interface(fn=predict_emotions, inputs="text", outputs="label")
|
31 |
+
|
32 |
+
# Launch the app
|
33 |
+
iface.launch()
|