tarrasyed19472007 commited on
Commit
a48d950
·
verified ·
1 Parent(s): 5861d1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -85
app.py CHANGED
@@ -1,9 +1,15 @@
1
  import random
2
  import streamlit as st
3
- from transformers import pipeline
 
 
4
 
5
- # Load the emotion analyzer model
6
- emotion_analyzer = pipeline("text-classification", model="distilbert-base-uncased")
 
 
 
 
7
 
8
  # Define the questions for mood analysis
9
  questions = [
@@ -12,57 +18,27 @@ questions = [
12
  "Do you feel calm or overwhelmed right now?",
13
  ]
14
 
15
- # Define a suggestion database for different moods
16
- suggestion_database = {
17
- "NEGATIVE": {
18
- "suggestions": ["Try guided meditation", "Take a walk in nature", "Connect with a loved one"],
19
- "articles": [
20
- {"title": "Emotional Wellness Toolkit", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
21
- {"title": "Understanding Anxiety", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
22
- ],
23
- "videos": [
24
- {"title": "Mindfulness for Calm", "url": "https://youtu.be/m1vaUGtyo-A"},
25
- {"title": "Relaxation Techniques", "url": "https://www.youtube.com/shorts/Tq49ajl7c8Q?feature=share"},
26
- ],
27
- },
28
- "POSITIVE": {
29
- "suggestions": ["Practice gratitude", "Engage in a hobby", "Celebrate your wins"],
30
- "articles": [
31
- {"title": "Benefits of Joy", "url": "https://www.health.harvard.edu/health-a-to-z"},
32
- {"title": "Gratitude Practices", "url": "https://www.helpguide.org/mental-health/meditation/mindful-breathing-meditation"},
33
- ],
34
- "videos": [
35
- {"title": "Boosting Happiness", "url": "https://youtu.be/MIc299Flibs"},
36
- {"title": "Celebrating Wins", "url": "https://www.youtube.com/shorts/fwH8Ygb0K60?feature=share"},
37
- ],
38
- },
39
- "NEUTRAL": {
40
- "suggestions": ["Take a short break", "Engage in a relaxing activity", "Spend time outdoors"],
41
- "articles": [
42
- {"title": "Self-Care Practices", "url": "https://www.nih.gov/health-information/emotional-wellness-toolkit"},
43
- {"title": "Stress Management", "url": "https://www.helpguide.org/mental-health/anxiety/tips-for-dealing-with-anxiety"},
44
- ],
45
- "videos": [
46
- {"title": "Relaxation Exercises", "url": "https://youtu.be/Y8HIFRPU6pM"},
47
- {"title": "Mindfulness Tips", "url": "https://youtu.be/-e-4Kx5px_I"},
48
- ],
49
- },
50
- }
51
-
52
- # Function to fetch relevant resources based on detected mood
53
- def get_relevant_resources(mood):
54
- resources = suggestion_database.get(mood, {})
55
- return resources.get("suggestions", []), resources.get("articles", []), resources.get("videos", [])
56
-
57
- # Function to suggest activities based on the mood
58
- def suggest_activity(mood):
59
- suggestions, articles, videos = get_relevant_resources(mood)
60
- return {
61
- "suggestions": suggestions,
62
- "articles": articles,
63
- "videos": videos,
64
  }
65
 
 
 
 
 
 
 
 
 
 
66
  # Streamlit app
67
  def main():
68
  st.title("Mood Analysis and Suggestions")
@@ -77,39 +53,32 @@ def main():
77
 
78
  # Analyze responses if all questions are answered
79
  if len(responses) == len(questions):
80
- combined_text = " ".join(responses)
81
- analysis_result = emotion_analyzer(combined_text)
82
- detected_emotion = analysis_result[0]['label']
83
-
84
- # Map detected emotion to a mood state
85
- mood_mapping = {
86
- "LABEL_0": "NEGATIVE", # for negative emotions
87
- "LABEL_1": "POSITIVE", # for positive emotions
88
- "LABEL_2": "NEUTRAL", # for neutral emotions
89
- }
90
-
91
- # Map the detected emotion into a proper mood
92
- mood = mood_mapping.get(detected_emotion, "NEUTRAL")
93
-
94
- st.write(f"Detected Mood: {mood}")
95
-
96
- # Fetch suggestions based on mood
97
- resources = suggest_activity(mood)
98
-
99
- # Display suggestions
100
- st.write("### Suggestions")
101
- for suggestion in resources["suggestions"]:
102
- st.write(f"- {suggestion}")
103
-
104
- # Display articles
105
- st.write("### Articles")
106
- for article in resources["articles"]:
107
- st.write(f"- [{article['title']}]({article['url']})")
108
-
109
- # Display videos
110
- st.write("### Videos")
111
- for video in resources["videos"]:
112
- st.write(f"- [{video['title']}]({video['url']})")
113
  else:
114
  st.write("Please answer all 3 questions to receive suggestions.")
115
 
 
1
  import random
2
  import streamlit as st
3
+ import requests
4
+ from dotenv import load_dotenv
5
+ import os
6
 
7
+ # Load environment variables from .env file
8
+ load_dotenv()
9
+
10
+ # Fetch the API key from the environment
11
+ API_KEY = os.getenv("GEMINI_API_KEY")
12
+ GEMINI_API_URL = "https://gemini-api-url.com" # Replace with the actual URL for Gemini API
13
 
14
  # Define the questions for mood analysis
15
  questions = [
 
18
  "Do you feel calm or overwhelmed right now?",
19
  ]
20
 
21
+ # Function to call Gemini API and get recommendations
22
+ def get_recommendations(user_responses):
23
+ # Create the payload with user responses
24
+ data = {
25
+ "responses": user_responses,
26
+ }
27
+
28
+ headers = {
29
+ "Authorization": f"Bearer {API_KEY}",
30
+ "Content-Type": "application/json"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
32
 
33
+ # Call the Gemini API
34
+ response = requests.post(GEMINI_API_URL, json=data, headers=headers)
35
+
36
+ if response.status_code == 200:
37
+ return response.json() # Return the JSON response containing suggestions and mood analysis
38
+ else:
39
+ st.error(f"Error in calling Gemini API: {response.status_code}")
40
+ return {}
41
+
42
  # Streamlit app
43
  def main():
44
  st.title("Mood Analysis and Suggestions")
 
53
 
54
  # Analyze responses if all questions are answered
55
  if len(responses) == len(questions):
56
+ # Send the responses to the Gemini API for analysis and suggestions
57
+ analysis_result = get_recommendations(responses)
58
+
59
+ if analysis_result:
60
+ # Extract mood and recommendations from the response
61
+ mood = analysis_result.get("mood", "NEUTRAL")
62
+ suggestions = analysis_result.get("suggestions", [])
63
+ articles = analysis_result.get("articles", [])
64
+ videos = analysis_result.get("videos", [])
65
+
66
+ st.write(f"Detected Mood: {mood}")
67
+
68
+ # Display suggestions
69
+ st.write("### Suggestions")
70
+ for suggestion in suggestions:
71
+ st.write(f"- {suggestion}")
72
+
73
+ # Display articles
74
+ st.write("### Articles")
75
+ for article in articles:
76
+ st.write(f"- [{article['title']}]({article['url']})")
77
+
78
+ # Display videos
79
+ st.write("### Videos")
80
+ for video in videos:
81
+ st.write(f"- [{video['title']}]({video['url']})")
 
 
 
 
 
 
 
82
  else:
83
  st.write("Please answer all 3 questions to receive suggestions.")
84