QuentinGuironnet commited on
Commit
1f3e8b1
·
verified ·
1 Parent(s): bd5a627

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ APP_NAME = "MoodMapper"
5
+ MODEL_ID = "j-hartmann/emotion-english-distilroberta-base" # garde celui-ci pour l’instant
6
+
7
+ _clf = None
8
+ def get_clf():
9
+ global _clf
10
+ if _clf is None:
11
+ _clf = pipeline(
12
+ "text-classification",
13
+ model=MODEL_ID,
14
+ return_all_scores=True,
15
+ truncation=True
16
+ )
17
+ return _clf
18
+
19
+ EXAMPLES = [
20
+ "I just got the internship — I'm so happy!",
21
+ "Je suis déçue et un peu en colère par ce mail.",
22
+ "This is fine, nothing special today.",
23
+ "I'm worried about the deadline...",
24
+ "Quelle surprise ! Je ne m’y attendais pas."
25
+ ]
26
+
27
+ def predict_emotion(text):
28
+ try:
29
+ if not text or not text.strip():
30
+ return {"": 0.0}, "Veuillez entrer un texte."
31
+ scores = get_clf()(text)[0] # [{'label': 'joy', 'score': 0.97}, ...]
32
+ score_dict = {s["label"]: float(s["score"]) for s in scores}
33
+ top = max(scores, key=lambda d: d["score"])
34
+ notes = (
35
+ f"**Émotion prédite :** {top['label']} ({top['score']*100:.1f}%)\n\n"
36
+ "- Fonctionne mieux sur des phrases courtes et explicites.\n"
37
+ "- Modèle surtout entraîné en anglais ; le français simple passe quand même.\n"
38
+ "- L'ironie et le sarcasme peuvent tromper le modèle."
39
+ )
40
+ return score_dict, notes
41
+ except Exception as e:
42
+ # Renvoie un message clair dans l’UI plutôt qu’un crash
43
+ return {"error": 1.0}, f"Erreur interne : {e}"
44
+
45
+ with gr.Blocks(title=APP_NAME) as demo:
46
+ gr.Markdown(f"# {APP_NAME} — Détecteur d'émotions (texte)")
47
+ txt = gr.Textbox(label="Votre texte", placeholder="Ex: Je suis déçue et un peu en colère par ce mail.", lines=3)
48
+ btn = gr.Button("Analyser")
49
+ out_scores = gr.Label(label="Scores (confiances)")
50
+ out_notes = gr.Markdown()
51
+ gr.Examples(inputs=txt, examples=EXAMPLES, label="Exemples à tester")
52
+ btn.click(predict_emotion, txt, [out_scores, out_notes])
53
+ txt.submit(predict_emotion, txt, [out_scores, out_notes])
54
+
55
+ # IMPORTANT sur Hugging Face Spaces: ne pas appeler demo.launch()
56
+ app = demo