cassandrasestier commited on
Commit
7ead88a
Β·
verified Β·
1 Parent(s): 073634d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
18
+ if not text or not text.strip():
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__":
39
+ demo.launch()