Spaces:
Sleeping
Sleeping
examples added
Browse files- app.py +33 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load model from the Hub
|
| 6 |
+
model_name = "enansari/emotion_roberta_weighted"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
emotion_labels = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
| 11 |
+
|
| 12 |
+
def classify_emotion(text):
|
| 13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
|
| 14 |
+
with torch.no_grad():
|
| 15 |
+
outputs = model(**inputs)
|
| 16 |
+
logits = outputs.logits
|
| 17 |
+
predicted_id = torch.argmax(logits, dim=-1).item()
|
| 18 |
+
return emotion_labels[predicted_id]
|
| 19 |
+
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=classify_emotion,
|
| 22 |
+
inputs=gr.Textbox(lines=2, placeholder="Type a sentence...", label="Input Text"),
|
| 23 |
+
outputs=gr.Textbox(label="Predicted Emotion"),
|
| 24 |
+
title="Sentiment Analysis with RoBERTa",
|
| 25 |
+
description="Detect emotions (joy, sadness, anger, etc.) in text.",
|
| 26 |
+
examples=[
|
| 27 |
+
["I am extremely happy today!"],
|
| 28 |
+
["This creates a lot of frustration."],
|
| 29 |
+
["I feel so lonely and cold."]
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|