Spaces:
Runtime error

File size: 1,053 Bytes
5e0dcc9
 
 
9f12c8a
1b3f7b0
 
9f12c8a
1b3f7b0
9f12c8a
1b3f7b0
 
 
 
 
9f12c8a
1b3f7b0
9f12c8a
5e0dcc9
 
9f12c8a
5e0dcc9
 
9f12c8a
 
 
5e0dcc9
 
9f12c8a
 
5e0dcc9
9f12c8a
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
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 😲"
    )
)