Spaces:
Sleeping
Sleeping
File size: 1,931 Bytes
9b310da | 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# app.py - HuggingFace Space para clasificación de emociones
# Requiere: fastai, gradio en requirements.txt
import gradio as gr
from fastai.text.all import *
# Cargamos el modelo exportado
learn = load_learner("emotion_classifier.pkl")
# Mapeo de etiquetas a nombres y emojis
EMOTION_INFO = {
"0": ("Sadness 😢", "#3498db"),
"1": ("Joy 😄", "#2ecc71"),
"2": ("Love ❤️", "#e74c3c"),
"3": ("Anger 😠", "#e67e22"),
"4": ("Fear 😨", "#9b59b6"),
"5": ("Surprise 😲","#1abc9c"),
}
def predict_emotion(text):
"""Predice la emoción de un texto."""
if not text.strip():
return {}
pred_class, pred_idx, pred_probs = learn.predict(text)
label_key = str(int(pred_class))
emotion_name, _ = EMOTION_INFO.get(label_key, (str(pred_class), "gray"))
# Devolvemos un dict con las probabilidades para Gradio
results = {}
for i, prob in enumerate(pred_probs):
name, _ = EMOTION_INFO[str(i)]
results[name] = float(prob)
return results
# Interfaz Gradio
examples = [
"I feel absolutely wonderful today!",
"I am so angry about what happened.",
"I am terrified of what might happen next.",
"I feel so sad and lonely.",
"I love spending time with my family!",
"I cannot believe how this turned out, it's shocking!"
]
iface = gr.Interface(
fn=predict_emotion,
inputs=gr.Textbox(
placeholder="Enter a sentence expressing an emotion...",
label="Input Text",
lines=3
),
outputs=gr.Label(num_top_classes=6, label="Emotion Probabilities"),
title="🎭 Emotion Classifier with ULMFit",
description="""Classify emotions in text using a ULMFit model trained on the
[Emotion dataset](https://huggingface.co/datasets/dair-ai/emotion).
Detects: **Sadness, Joy, Love, Anger, Fear, Surprise**.""",
examples=examples,
theme=gr.themes.Soft()
)
iface.launch()
|