tarrasyed19472007 commited on
Commit
ac877e3
·
verified ·
1 Parent(s): e09485f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -58
app.py CHANGED
@@ -1,84 +1,83 @@
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 = [
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 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")
 
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
- # 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
 
 
 
 
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