cassandrasestier commited on
Commit
add6350
Β·
verified Β·
1 Parent(s): 7dd3dd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -1,17 +1,30 @@
 
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- MODEL_ID = "j-hartmann/emotion-english-distilroberta-base"
 
5
  analyzer = pipeline("text-classification", model=MODEL_ID, return_all_scores=False)
6
 
 
7
  SUGGESTIONS = {
8
- "joy": "Keep up the good vibes! πŸ₯³ Share your happiness with someone.",
9
- "sadness": "Take a short break, drink some water, or go for a walk. 🌿",
10
- "anger": "Take a deep breath and try to relax. πŸ’ͺ",
11
- "fear": "Talk to someone you trust. πŸ’¬",
12
- "love": "Send a kind message to someone you care about. πŸ’–",
13
- "surprise": "Write down what surprised you β€” curiosity is powerful! πŸ€”",
14
- "neutral": "Take a moment to stretch and breathe. 🌸",
 
 
 
 
 
 
 
 
15
  }
16
 
17
  def detect_emotion(text):
@@ -19,20 +32,16 @@ def detect_emotion(text):
19
  return {"Emotion": "β€”", "Confidence": "β€”", "Suggestion": "Please write something πŸ™‚"}
20
  result = analyzer(text)[0]
21
  label = result["label"]
22
- score = result["score"]
23
- suggestion = SUGGESTIONS.get(label.lower(), SUGGESTIONS["neutral"])
24
- return {
25
- "Emotion": label,
26
- "Confidence": f"{score*100:.1f}%",
27
- "Suggestion": suggestion
28
- }
29
 
30
  demo = gr.Interface(
31
  fn=detect_emotion,
32
- inputs=gr.Textbox(lines=2, placeholder="Type how you feel... (English preferred)"),
33
  outputs="json",
34
- title="πŸͺž MoodMirror",
35
- description="Detects the main emotion in your text and gives a self-care suggestion πŸ’¬"
36
  )
37
 
38
  if __name__ == "__main__":
 
1
+ # =========================
2
+ # πŸͺž MoodMirror+ β€” Emotional Self-Care Assistant
3
+ # =========================
4
  import gradio as gr
5
  from transformers import pipeline
6
 
7
+ # Smaller, faster model for emotions
8
+ MODEL_ID = "bhadresh-savani/distilbert-base-uncased-emotion"
9
  analyzer = pipeline("text-classification", model=MODEL_ID, return_all_scores=False)
10
 
11
+ # Expanded, psychology-based suggestions
12
  SUGGESTIONS = {
13
+ "joy": "Celebrate your happiness! Write down three things you’re grateful for today. Positive reflection reinforces joy. πŸ₯³",
14
+ "sadness": "Allow yourself to feel it, but don’t isolate. Reach out to someone or do a small act of self-kindness, like journaling or a short walk. 🌿",
15
+ "anger": "Pause and breathe slowly through your nose, exhale longer than you inhale. When calm, write down what triggered you β€” it helps regain control. πŸ’ͺ",
16
+ "fear": "Ground yourself in the present: notice 5 things you see, 4 you feel, 3 you hear. This calms the nervous system. πŸ’¬",
17
+ "love": "Express it! Send a message or do something kind for someone you care about. Connection strengthens emotional well-being. πŸ’–",
18
+ "surprise": "Note what surprised you β€” was it positive or challenging? Curiosity helps your brain adapt to change. πŸ€”",
19
+ "neutral": "Check in with your body: are you tense or relaxed? Take a short stretch or deep breath to stay balanced. 🌸",
20
+ "disgust": "It’s okay to feel repelled; ask yourself what boundary was crossed. Channel that energy into assertive communication. 🚫",
21
+ "guilt": "Reflect: what part of this can you learn from, and what can you let go? Apologize if needed, but forgive yourself too. β€οΈβ€πŸ©Ή",
22
+ "shame": "Remember that everyone makes mistakes. You deserve compassion. Talk to someone safe β€” shame shrinks in empathy. 🀝",
23
+ "anxiety": "Try the 4-7-8 breathing method (inhale 4s, hold 7s, exhale 8s). Gentle movement or calming music can help. 🌬️",
24
+ "gratitude": "Write down one thing that went well today, no matter how small. Gratitude rewires the brain toward positivity. πŸ™",
25
+ "boredom": "Switch tasks or move your body β€” even 2 minutes helps reset focus. Try learning one new fact or word. ⚑",
26
+ "confusion": "Break your thoughts down: what exactly feels unclear? Write your questions, and find one small next step. 🧩",
27
+ "hope": "Nurture it! Visualize a small goal and take one concrete step toward it today. Progress feeds optimism. πŸŒ…"
28
  }
29
 
30
  def detect_emotion(text):
 
32
  return {"Emotion": "β€”", "Confidence": "β€”", "Suggestion": "Please write something πŸ™‚"}
33
  result = analyzer(text)[0]
34
  label = result["label"]
35
+ score = float(result["score"])
36
+ suggestion = SUGGESTIONS.get(label.lower(), "Take a mindful pause β€” you’re doing great for checking in. πŸ’«")
37
+ return {"Emotion": label, "Confidence": f"{score*100:.1f}%", "Suggestion": suggestion}
 
 
 
 
38
 
39
  demo = gr.Interface(
40
  fn=detect_emotion,
41
+ inputs=gr.Textbox(lines=3, placeholder="Type how you feel... (English preferred)"),
42
  outputs="json",
43
+ title="πŸͺž MoodMirror+",
44
+ description="Detects your emotion and gives psychology-based self-care advice πŸ’¬"
45
  )
46
 
47
  if __name__ == "__main__":