razanalsulami commited on
Commit
903f7b1
β€’
1 Parent(s): 93b7ce0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,28 +1,38 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load pre-trained sentiment analysis model
5
  sentiment_analysis = pipeline("sentiment-analysis", model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
6
 
7
- # analyze user's mood from text
8
  def analyze_mood(user_input):
9
- result = sentiment_analysis(user_input)[0]
10
-
11
- # assign mood based on sentiment
12
- if result["label"] == "POSITIVE":
13
- mood = "Happy"
 
 
 
 
 
 
 
 
 
 
 
14
  suggestion = "Keep enjoying your day 😊"
15
- elif result["label"] == "NEGATIVE":
16
- mood = "Sad"
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
- # Output mood and suggestion
23
- return "Your mood is " + mood + ". " + suggestion
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
- gr.Interface(fn=analyze_mood, inputs=inputs, outputs=outputs, title="Mood Analyzer with Suggestions").launch()
 
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()