Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
analyzer = pipeline("text-classification", model=MODEL_ID, return_all_scores=False)
|
| 6 |
|
|
|
|
| 7 |
SUGGESTIONS = {
|
| 8 |
-
"joy": "
|
| 9 |
-
"sadness": "
|
| 10 |
-
"anger": "
|
| 11 |
-
"fear": "
|
| 12 |
-
"love": "Send a kind
|
| 13 |
-
"surprise": "
|
| 14 |
-
"neutral": "Take a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(),
|
| 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=
|
| 33 |
outputs="json",
|
| 34 |
-
title="πͺ MoodMirror",
|
| 35 |
-
description="Detects
|
| 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__":
|