razanalsulami commited on
Commit
61c54ed
β€’
1 Parent(s): 903f7b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
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 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()
 
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()