Spaces:
Running
Running
File size: 1,502 Bytes
9c2c8d1 33763cd 9c2c8d1 33763cd 9c2c8d1 49c46e0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | import gradio as gr
from transformers import pipeline
MODEL_ID = "dk409/emotion-roberta"
classifier = pipeline("text-classification", model=MODEL_ID, top_k=None)
# Prediction function
def classify_emotion(text):
"""
Run the classifier and return a dict of {label: score} for Gradio's Label component.
The Label component automatically sorts and displays as a bar chart.
"""
if not text or not text.strip():
return {}
results = classifier(text)[0] # list of {"label": ..., "score": ...}
return {r["label"]: r["score"] for r in results}
# Gradio interface
examples = [
"I'm so happy to see you after all these years!",
"This is absolutely terrifying, I can't watch.",
"I can't believe they cancelled the show. So angry right now.",
"She looked at him with so much love in her eyes.",
"I feel so alone and empty inside.",
"Wait, you got promoted? I had no idea! That's amazing!",
]
demo = gr.Interface(
fn=classify_emotion,
inputs=gr.Textbox(
label="Enter text",
placeholder="Type a sentence and I'll detect the emotion...",
lines=3,
),
outputs=gr.Label(label="Emotion Probabilities", num_top_classes=6),
title="Emotion Text Classifier",
description=(
"Detects 6 emotions in text: **sadness, joy, love, anger, fear, surprise**. "
"Fine-tuned RoBERTa model trained on the dair-ai/emotion dataset."
),
examples=examples,
)
if __name__ == "__main__":
demo.launch() |