Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,83 @@
|
|
1 |
-
import random
|
2 |
-
import streamlit as st
|
3 |
import requests
|
4 |
-
|
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 = [
|
16 |
"How are you feeling today in one word?",
|
17 |
"What's currently on your mind?",
|
18 |
"Do you feel calm or overwhelmed right now?",
|
19 |
]
|
20 |
|
21 |
-
# Function to
|
22 |
-
def
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
def main():
|
44 |
st.title("Mood Analysis and Suggestions")
|
|
|
45 |
st.write("Answer the following 3 questions to help us understand your mood:")
|
46 |
|
47 |
-
# Collect responses
|
48 |
responses = []
|
49 |
for i, question in enumerate(questions):
|
50 |
response = st.text_input(f"{i+1}. {question}")
|
51 |
if response:
|
52 |
responses.append(response)
|
53 |
|
54 |
-
#
|
55 |
if len(responses) == len(questions):
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
66 |
st.write(f"Detected Mood: {mood}")
|
67 |
|
68 |
-
# Display
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
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 |
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Define the 3 questions for mood analysis
|
5 |
questions = [
|
6 |
"How are you feeling today in one word?",
|
7 |
"What's currently on your mind?",
|
8 |
"Do you feel calm or overwhelmed right now?",
|
9 |
]
|
10 |
|
11 |
+
# Function to query the Gemini API
|
12 |
+
def query_gemini_api(user_answers):
|
13 |
+
url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=AIzaSyBDo08q1vihxhI4nIHv0R6kDOPNT02NYG4' # Replace with your actual API key
|
14 |
+
headers = {'Content-Type': 'application/json'}
|
15 |
+
|
16 |
+
# Prepare the payload with user answers
|
17 |
+
input_text = " ".join(user_answers) # Combining all answers into one input text
|
18 |
+
|
19 |
+
payload = {
|
20 |
+
'contents': [
|
21 |
+
{
|
22 |
+
'parts': [
|
23 |
+
{'text': input_text}
|
24 |
+
]
|
25 |
+
}
|
26 |
+
]
|
27 |
}
|
28 |
|
29 |
+
try:
|
30 |
+
# Send the request to the Gemini API
|
31 |
+
response = requests.post(url, headers=headers, json=payload)
|
32 |
+
|
33 |
+
if response.status_code == 200:
|
34 |
+
result = response.json()
|
35 |
+
# Return the result from Gemini API (mood and recommendations)
|
36 |
+
return result
|
37 |
+
else:
|
38 |
+
print(f"Error: {response.status_code} - {response.text}")
|
39 |
+
return None
|
40 |
+
except requests.exceptions.RequestException as e:
|
41 |
+
print(f"An error occurred: {e}")
|
42 |
+
return None
|
43 |
+
|
44 |
+
# Streamlit app for collecting answers
|
45 |
def main():
|
46 |
st.title("Mood Analysis and Suggestions")
|
47 |
+
|
48 |
st.write("Answer the following 3 questions to help us understand your mood:")
|
49 |
|
50 |
+
# Collect responses from the user
|
51 |
responses = []
|
52 |
for i, question in enumerate(questions):
|
53 |
response = st.text_input(f"{i+1}. {question}")
|
54 |
if response:
|
55 |
responses.append(response)
|
56 |
|
57 |
+
# If all 3 responses are collected, send them to Gemini for analysis
|
58 |
if len(responses) == len(questions):
|
59 |
+
st.write("Processing your answers...")
|
60 |
+
|
61 |
+
# Get mood and recommendations from Gemini API
|
62 |
+
result = query_gemini_api(responses)
|
63 |
+
|
64 |
+
if result:
|
65 |
+
# Assuming the response contains mood and recommendations
|
66 |
+
mood = result.get("mood", "Unknown")
|
67 |
+
recommendations = result.get("recommendations", [])
|
68 |
+
|
69 |
+
# Display the detected mood
|
70 |
st.write(f"Detected Mood: {mood}")
|
71 |
|
72 |
+
# Display the recommendations
|
73 |
+
if recommendations:
|
74 |
+
st.write("### Recommendations to Improve Your Mood:")
|
75 |
+
for recommendation in recommendations:
|
76 |
+
st.write(f"- {recommendation}")
|
77 |
+
else:
|
78 |
+
st.write("No recommendations available.")
|
79 |
+
else:
|
80 |
+
st.write("Sorry, we couldn't process your answers. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
81 |
else:
|
82 |
st.write("Please answer all 3 questions to receive suggestions.")
|
83 |
|