Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
|
| 7 |
-
# analyze user's mood
|
| 8 |
def analyze_mood(user_input):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
suggestion = "Keep enjoying your day π"
|
| 15 |
-
elif
|
| 16 |
-
|
| 17 |
-
suggestion = "Try playing a game you like or practice some deep breathing exercises it might help!π"
|
| 18 |
else:
|
| 19 |
-
mood = "Neutral"
|
| 20 |
suggestion = "You're doing well! Stay calm πΈ"
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
return "Your mood
|
| 24 |
|
| 25 |
inputs = gr.Textbox(label="How are you feeling today?", placeholder="Type your thoughts here...")
|
| 26 |
outputs = gr.Textbox(label="Mood and Suggestion")
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load sentiment analysis pipeline
|
| 5 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
| 6 |
|
| 7 |
+
# Function to analyze user's mood based on input
|
| 8 |
def analyze_mood(user_input):
|
| 9 |
+
# Analyze the mood from input text
|
| 10 |
+
results = sentiment_analysis(user_input)
|
| 11 |
+
mood_summary = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
| 12 |
+
suggestions = []
|
| 13 |
+
|
| 14 |
+
# Loop through all results and summarize sentiments
|
| 15 |
+
for result in results:
|
| 16 |
+
label = result["label"]
|
| 17 |
+
score = result["score"]
|
| 18 |
+
mood_summary[label] += score
|
| 19 |
+
|
| 20 |
+
# Determine the dominant mood
|
| 21 |
+
dominant_mood = max(mood_summary, key=mood_summary.get)
|
| 22 |
+
|
| 23 |
+
# Provide suggestions based on mood
|
| 24 |
+
if dominant_mood == "POSITIVE":
|
| 25 |
suggestion = "Keep enjoying your day π"
|
| 26 |
+
elif dominant_mood == "NEGATIVE":
|
| 27 |
+
suggestion = "Try playing a game you like or practice some deep breathing exercises. It might help! π"
|
|
|
|
| 28 |
else:
|
|
|
|
| 29 |
suggestion = "You're doing well! Stay calm πΈ"
|
| 30 |
+
|
| 31 |
+
# Return mood and suggestion
|
| 32 |
+
return f"Your mood seems mostly {dominant_mood.lower()}.", suggestion
|
| 33 |
|
| 34 |
inputs = gr.Textbox(label="How are you feeling today?", placeholder="Type your thoughts here...")
|
| 35 |
outputs = gr.Textbox(label="Mood and Suggestion")
|
| 36 |
+
interface = gr.Interface(fn=analyze_mood, inputs=inputs, outputs=outputs, title="Mood Analyzer with Suggestions")
|
| 37 |
|
| 38 |
+
interface.launch()
|