Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import pipeline | |
| # 1) Load the HF pipeline with all scores so we can show probabilities | |
| classifier = pipeline( | |
| "text-classification", | |
| model="j-hartmann/emotion-english-roberta-large", | |
| return_all_scores=True | |
| ) | |
| # 2) Wrap it in a function that returns a label→score dict | |
| def classify_emotion(text: str): | |
| scores = classifier(text)[0] # returns list of {label, score} | |
| return {item["label"]: float(item["score"]) for item in scores} | |
| # 3) Build the Gradio interface | |
| iface = gr.Interface( | |
| fn=classify_emotion, | |
| inputs=gr.Textbox( | |
| lines=2, | |
| placeholder="Type any English sentence here…", | |
| label="Input Text" | |
| ), | |
| outputs=gr.Label( | |
| num_top_classes=6, | |
| label="Emotion Probabilities" | |
| ), | |
| examples=[ | |
| ["I love you!"], | |
| ["The movie was heart breaking!"] | |
| ], | |
| title="English Emotion Classifier", | |
| description=( | |
| "Predicts one of Ekman's 6 basic emotions plus neutral " | |
| "(anger 🤬, disgust 🤢, fear 😨, joy 😀, neutral 😐, " | |
| "sadness 😭, surprise 😲)." | |
| ) | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |