Moodmapper / app.py
zuch95's picture
Update app.py
4fac129 verified
raw
history blame
2.04 kB
import gradio as gr
from transformers import pipeline
APP_NAME = "MoodMapper"
MODEL_ID = "j-hartmann/emotion-english-distilroberta-base"
_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]
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:
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])
# Ne pas lancer manuellement sur Hugging Face
app = demo