Spaces:
Runtime error
Runtime error
File size: 2,168 Bytes
1f3e8b1 | 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 | import gradio as gr
from transformers import pipeline
APP_NAME = "MoodMapper"
MODEL_ID = "j-hartmann/emotion-english-distilroberta-base" # garde celui-ci pour l’instant
_clf = None
def get_clf():
global _clf
if _clf is None:
_clf = pipeline(
"text-classification",
model=MODEL_ID,
return_all_scores=True,
truncation=True
)
return _clf
EXAMPLES = [
"I just got the internship — I'm so happy!",
"Je suis déçue et un peu en colère par ce mail.",
"This is fine, nothing special today.",
"I'm worried about the deadline...",
"Quelle surprise ! Je ne m’y attendais pas."
]
def predict_emotion(text):
try:
if not text or not text.strip():
return {"": 0.0}, "Veuillez entrer un texte."
scores = get_clf()(text)[0] # [{'label': 'joy', 'score': 0.97}, ...]
score_dict = {s["label"]: float(s["score"]) for s in scores}
top = max(scores, key=lambda d: d["score"])
notes = (
f"**Émotion prédite :** {top['label']} ({top['score']*100:.1f}%)\n\n"
"- Fonctionne mieux sur des phrases courtes et explicites.\n"
"- Modèle surtout entraîné en anglais ; le français simple passe quand même.\n"
"- L'ironie et le sarcasme peuvent tromper le modèle."
)
return score_dict, notes
except Exception as e:
# Renvoie un message clair dans l’UI plutôt qu’un crash
return {"error": 1.0}, f"Erreur interne : {e}"
with gr.Blocks(title=APP_NAME) as demo:
gr.Markdown(f"# {APP_NAME} — Détecteur d'émotions (texte)")
txt = gr.Textbox(label="Votre texte", placeholder="Ex: Je suis déçue et un peu en colère par ce mail.", lines=3)
btn = gr.Button("Analyser")
out_scores = gr.Label(label="Scores (confiances)")
out_notes = gr.Markdown()
gr.Examples(inputs=txt, examples=EXAMPLES, label="Exemples à tester")
btn.click(predict_emotion, txt, [out_scores, out_notes])
txt.submit(predict_emotion, txt, [out_scores, out_notes])
# IMPORTANT sur Hugging Face Spaces: ne pas appeler demo.launch()
app = demo
|