Suzana's picture
Update app.py
9f12c8a verified
Raw
History Blame Contribute Delete
1.05 kB
import gradio as gr
from transformers import pipeline
# Global variable for the classifier
classifier = None
def classify_emotion(text: str):
global classifier
# Load model on first request
if classifier is None:
classifier = pipeline(
"text-classification",
model="j-hartmann/emotion-english-roberta-large",
return_all_scores=True,
device_map="auto" # automatically uses GPU if available
)
scores = classifier(text)[0]
return {item["label"]: float(item["score"]) for item in scores}
# Gradio interface
iface = gr.Interface(
fn=classify_emotion,
inputs=gr.Textbox(lines=2, placeholder="Type any English sentence…"),
outputs=gr.Label(num_top_classes=6),
examples=[["I love you!"], ["The movie was heart breaking!"]],
title="English Emotion Classifier",
description=(
"Predicts one of Ekman's 6 basic emotions plus neutral:\n"
"anger 🤬, disgust 🤢, fear 😨, joy 😀, neutral 😐, sadness 😭, surprise 😲"
)
)