Spaces:
Sleeping
Sleeping
| import os | |
| import requests | |
| import streamlit as st | |
| from dotenv import load_dotenv | |
| # Load environment variables from the .env file | |
| load_dotenv() | |
| # Get the Gemini API key from the .env file | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
| if GEMINI_API_KEY is None: | |
| st.error("API key not found! Please set the GEMINI_API_KEY in your .env file.") | |
| st.stop() | |
| # Define the 3 questions for mood analysis | |
| questions = [ | |
| "How are you feeling today in one word?", | |
| "What's currently on your mind?", | |
| "Do you feel calm or overwhelmed right now?", | |
| ] | |
| # Function to query the Gemini API | |
| def query_gemini_api(user_answers): | |
| url = f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={GEMINI_API_KEY}' | |
| headers = {'Content-Type': 'application/json'} | |
| # Prepare the payload with user answers | |
| input_text = " ".join(user_answers) # Combining all answers into one input text | |
| payload = { | |
| "contents": [ | |
| { | |
| "parts": [ | |
| {"text": input_text} | |
| ] | |
| } | |
| ] | |
| } | |
| try: | |
| # Send the request to the Gemini API | |
| response = requests.post(url, headers=headers, json=payload) | |
| # Log the response for debugging | |
| print(f"Status Code: {response.status_code}") # Log the status code | |
| print(f"Response Text: {response.text}") # Log the response text | |
| # Check if the API call is successful | |
| if response.status_code == 200: | |
| result = response.json() | |
| # Check if the response contains valid mood and recommendations | |
| mood = result.get("mood", None) | |
| recommendations = result.get("recommendations", None) | |
| if mood and recommendations: | |
| return mood, recommendations | |
| else: | |
| st.error("No mood or recommendations found in the response.") | |
| return None, None | |
| else: | |
| st.error(f"API Error {response.status_code}: {response.text}") | |
| return None, None | |
| except requests.exceptions.RequestException as e: | |
| st.error(f"An error occurred: {e}") | |
| return None, None | |
| # Streamlit app for collecting answers | |
| def main(): | |
| st.title("Mood Analysis and Suggestions") | |
| st.write("Answer the following 3 questions to help us understand your mood:") | |
| # Collect responses from the user | |
| responses = [] | |
| for i, question in enumerate(questions): | |
| response = st.text_input(f"{i+1}. {question}") | |
| if response: | |
| responses.append(response) | |
| # If all 3 responses are collected, send them to Gemini for analysis | |
| if len(responses) == len(questions): | |
| st.write("Processing your answers...") | |
| # Get mood and recommendations from Gemini API | |
| mood, recommendations = query_gemini_api(responses) | |
| if mood and recommendations: | |
| # Display the detected mood | |
| st.write(f"Detected Mood: {mood}") | |
| # Display the recommendations | |
| st.write("### Recommendations to Improve Your Mood:") | |
| for recommendation in recommendations: | |
| st.write(f"- {recommendation}") | |
| else: | |
| # If no valid mood or recommendations are found, show a message | |
| st.warning("Could not generate mood analysis. Please try again later.") | |
| else: | |
| st.write("Please answer all 3 questions to receive suggestions.") | |
| if __name__ == "__main__": | |
| main() | |