Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -1,11 +1,14 @@
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(
@@ -16,18 +19,21 @@ def get_clf():
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"])
@@ -39,18 +45,29 @@ def predict_emotion(text):
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
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # --- Configuration ---
5
  APP_NAME = "MoodMapper"
6
+ MODEL_ID = "j-hartmann/emotion-english-distilroberta-base"
7
 
8
+ # --- Initialisation du modèle ---
9
  _clf = None
10
  def get_clf():
11
+ """Charge le modèle de classification d’émotions une seule fois."""
12
  global _clf
13
  if _clf is None:
14
  _clf = pipeline(
 
19
  )
20
  return _clf
21
 
22
+ # --- Exemples à afficher ---
23
  EXAMPLES = [
24
+ ["I just got the internship — I'm so happy!"],
25
+ ["Je suis déçue et un peu en colère par ce mail."],
26
+ ["This is fine, nothing special today."],
27
+ ["I'm worried about the deadline..."],
28
+ ["Quelle surprise ! Je ne m’y attendais pas."]
29
  ]
30
 
31
+ # --- Fonction principale ---
32
  def predict_emotion(text):
33
+ if not text or not text.strip():
34
+ return {"": 0.0}, "Veuillez entrer un texte."
35
+
36
  try:
 
 
37
  scores = get_clf()(text)[0] # [{'label': 'joy', 'score': 0.97}, ...]
38
  score_dict = {s["label"]: float(s["score"]) for s in scores}
39
  top = max(scores, key=lambda d: d["score"])
 
45
  )
46
  return score_dict, notes
47
  except Exception as e:
 
48
  return {"error": 1.0}, f"Erreur interne : {e}"
49
 
50
+ # --- Interface Gradio ---
51
  with gr.Blocks(title=APP_NAME) as demo:
52
+ gr.Markdown(f"# 🧠 {APP_NAME} — Détecteur d'émotions dans le texte")
53
+ gr.Markdown(
54
+ "Entrez un texte en anglais ou en français simple pour découvrir l’émotion principale qu’il exprime."
55
+ )
56
+
57
+ txt = gr.Textbox(
58
+ label="Votre texte",
59
+ placeholder="Ex : Je suis déçue et un peu en colère par ce mail.",
60
+ lines=3
61
+ )
62
+ btn = gr.Button("Analyser le texte")
63
+ out_scores = gr.Label(label="Scores (confiance du modèle)")
64
  out_notes = gr.Markdown()
65
+
66
  gr.Examples(inputs=txt, examples=EXAMPLES, label="Exemples à tester")
 
 
67
 
68
+ btn.click(fn=predict_emotion, inputs=txt, outputs=[out_scores, out_notes])
69
+ txt.submit(fn=predict_emotion, inputs=txt, outputs=[out_scores, out_notes])
70
+
71
+ # --- IMPORTANT ---
72
+ # Hugging Face Spaces détecte automatiquement "app" comme interface principale
73
  app = demo