# 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()