Spaces:
Sleeping
Sleeping
razanalsulami
commited on
Commit
β’
61c54ed
1
Parent(s):
903f7b1
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,38 @@
|
|
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
|
8 |
def analyze_mood(user_input):
|
9 |
-
#
|
10 |
results = sentiment_analysis(user_input)
|
11 |
mood_summary = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
12 |
suggestions = []
|
13 |
|
14 |
-
#
|
15 |
for result in results:
|
16 |
label = result["label"]
|
17 |
score = result["score"]
|
18 |
mood_summary[label] += score
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
|
23 |
-
#
|
24 |
-
if
|
25 |
-
suggestion = "Keep enjoying your day
|
26 |
-
elif
|
27 |
-
suggestion = "
|
28 |
else:
|
29 |
-
suggestion = "
|
30 |
|
31 |
-
#
|
32 |
-
return
|
33 |
|
34 |
-
inputs = gr.Textbox(label="How are you
|
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()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
|
4 |
+
# Load the sentiment analysis pipeline
|
5 |
sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
6 |
|
7 |
+
# Function to analyze mood
|
8 |
def analyze_mood(user_input):
|
9 |
+
# analyze mood from text
|
10 |
results = sentiment_analysis(user_input)
|
11 |
mood_summary = {"POSITIVE": 0, "NEGATIVE": 0, "NEUTRAL": 0}
|
12 |
suggestions = []
|
13 |
|
14 |
+
# sum up scores
|
15 |
for result in results:
|
16 |
label = result["label"]
|
17 |
score = result["score"]
|
18 |
mood_summary[label] += score
|
19 |
|
20 |
+
# find most mood
|
21 |
+
main_mood = max(mood_summary, key=mood_summary.get)
|
22 |
|
23 |
+
# suggest based on mood
|
24 |
+
if main_mood == "POSITIVE":
|
25 |
+
suggestion = "Keep enjoying your day :)"
|
26 |
+
elif main_mood == "NEGATIVE":
|
27 |
+
suggestion = "Maybe play a game or breathe deeply could help!"
|
28 |
else:
|
29 |
+
suggestion = "Doing well! stay calm"
|
30 |
|
31 |
+
# return mood and suggestion
|
32 |
+
return "Your mood seems mostly " + main_mood.lower() + ". " + suggestion
|
33 |
|
34 |
+
inputs = gr.Textbox(label="How are you today?", placeholder="Type your feelings 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()
|