Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| # Load your model (CHANGE THIS to your model path!) | |
| MODEL_NAME = "Somya26/deberta-emotion-classifier" | |
| print("Loading model...") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| model.eval() | |
| print("Model loaded!") | |
| # Emotion labels | |
| emotions = ['anger', 'fear', 'joy', 'sadness', 'surprise'] | |
| emojis = ['π ', 'π¨', 'π', 'π’', 'π²'] | |
| def predict_emotion(text): | |
| if not text.strip(): | |
| return {}, "Please enter some text!" | |
| # Tokenize | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256) | |
| # Predict | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = torch.sigmoid(outputs.logits).squeeze().numpy() | |
| # Create results | |
| results = {f"{emojis[i]} {emotions[i].capitalize()}": float(probs[i]) | |
| for i in range(len(emotions))} | |
| # Get predicted emotions (threshold > 0.5) | |
| predicted = [f"{emojis[i]} {emotions[i].capitalize()}" | |
| for i in range(len(emotions)) if probs[i] > 0.5] | |
| prediction_text = "**Detected Emotions:** " + ", ".join(predicted) if predicted else "**No strong emotions detected**" | |
| return results, prediction_text | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict_emotion, | |
| inputs=gr.Textbox( | |
| lines=5, | |
| placeholder="Enter your text here...", | |
| label="Input Text" | |
| ), | |
| outputs=[ | |
| gr.Label(num_top_classes=5, label="Emotion Probabilities"), | |
| gr.Markdown(label="Prediction") | |
| ], | |
| title="π Emotion Classifier", | |
| description="Detect multiple emotions in text: anger, fear, joy, sadness, and surprise. Powered by DeBERTa.", | |
| examples=[ | |
| ["I can't believe they did this to me! This is so unfair!"], | |
| ["I'm so excited about the party tomorrow! Can't wait!"], | |
| ["Walking alone at night in that neighborhood makes me nervous."], | |
| ["This is the best day of my life! Everything is perfect!"], | |
| ["I miss you so much. Life isn't the same without you."], | |
| ["Wow! I didn't expect that at all!"] | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |