Spaces:
Sleeping
Sleeping
Create app.py
Browse files
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()
|