Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load emotion detection model (RoBERTa) | |
| classifier = pipeline( | |
| "text-classification", | |
| model="j-hartmann/emotion-english-distilroberta-base", | |
| top_k=None | |
| ) | |
| # Prediction function | |
| def detect_emotion(text): | |
| results = classifier(text)[0] | |
| # Get highest score emotion | |
| best = max(results, key=lambda x: x['score']) | |
| label = best['label'] | |
| score = round(best['score'], 3) | |
| # Emoji mapping | |
| emoji_map = { | |
| "joy": "π", | |
| "anger": "π‘", | |
| "sadness": "π’", | |
| "fear": "π¨", | |
| "love": "β€οΈ", | |
| "surprise": "π²" | |
| } | |
| emoji = emoji_map.get(label.lower(), "") | |
| return f"{emoji} Emotion: {label} (Confidence: {score})" | |
| # Gradio UI | |
| app = gr.Interface( | |
| fn=detect_emotion, | |
| inputs=gr.Textbox(lines=3, placeholder="Enter text..."), | |
| outputs="text", | |
| title="RoBERTa Emotion Detection", | |
| description="Detect emotions like joy, anger, sadness, fear, love, surprise" | |
| ) | |
| app.launch() |