Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,10 +6,10 @@ from dotenv import load_dotenv
|
|
| 6 |
# Load environment variables from the .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
-
# Get the
|
| 10 |
-
|
| 11 |
|
| 12 |
-
if
|
| 13 |
st.error("API key not found! Please set the GEMINI_API_KEY in your .env file.")
|
| 14 |
st.stop()
|
| 15 |
|
|
@@ -22,67 +22,65 @@ questions = [
|
|
| 22 |
|
| 23 |
# Function to query the Gemini API
|
| 24 |
def query_gemini_api(user_answers):
|
| 25 |
-
# Correct
|
| 26 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/
|
| 27 |
|
| 28 |
headers = {'Content-Type': 'application/json'}
|
| 29 |
|
| 30 |
-
# Combine
|
| 31 |
input_text = " ".join(user_answers)
|
| 32 |
|
| 33 |
-
# Payload for the API
|
| 34 |
payload = {
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
}
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
# Send
|
| 46 |
response = requests.post(url, headers=headers, json=payload)
|
| 47 |
-
|
| 48 |
-
# Check if the response is successful
|
| 49 |
if response.status_code == 200:
|
| 50 |
result = response.json()
|
| 51 |
|
| 52 |
-
# Extract
|
| 53 |
-
|
| 54 |
-
return
|
| 55 |
else:
|
|
|
|
| 56 |
st.error(f"API Error {response.status_code}: {response.text}")
|
| 57 |
return None
|
| 58 |
except requests.exceptions.RequestException as e:
|
| 59 |
st.error(f"An error occurred: {e}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
-
# Streamlit app
|
| 63 |
def main():
|
| 64 |
st.title("Mood Analysis and Suggestions")
|
| 65 |
-
|
| 66 |
st.write("Answer the following 3 questions to help us understand your mood:")
|
| 67 |
|
| 68 |
-
# Collect responses
|
| 69 |
responses = []
|
| 70 |
for i, question in enumerate(questions):
|
| 71 |
response = st.text_input(f"{i+1}. {question}")
|
| 72 |
if response:
|
| 73 |
responses.append(response)
|
| 74 |
|
| 75 |
-
#
|
| 76 |
if len(responses) == len(questions):
|
| 77 |
st.write("Processing your answers...")
|
| 78 |
|
| 79 |
# Query the Gemini API
|
| 80 |
-
|
| 81 |
|
| 82 |
-
if
|
| 83 |
-
|
| 84 |
-
st.write(
|
| 85 |
-
st.write(generated_text)
|
| 86 |
else:
|
| 87 |
st.warning("Could not generate mood analysis. Please try again later.")
|
| 88 |
else:
|
|
|
|
| 6 |
# Load environment variables from the .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
# Get the API key from the .env file
|
| 10 |
+
API_KEY = os.getenv("GEMINI_API_KEY")
|
| 11 |
|
| 12 |
+
if API_KEY is None:
|
| 13 |
st.error("API key not found! Please set the GEMINI_API_KEY in your .env file.")
|
| 14 |
st.stop()
|
| 15 |
|
|
|
|
| 22 |
|
| 23 |
# Function to query the Gemini API
|
| 24 |
def query_gemini_api(user_answers):
|
| 25 |
+
# Correct API URL
|
| 26 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={API_KEY}"
|
| 27 |
|
| 28 |
headers = {'Content-Type': 'application/json'}
|
| 29 |
|
| 30 |
+
# Combine user responses into a single input
|
| 31 |
input_text = " ".join(user_answers)
|
| 32 |
|
| 33 |
+
# Payload for the API request
|
| 34 |
payload = {
|
| 35 |
+
"contents": [
|
| 36 |
+
{
|
| 37 |
+
"parts": [
|
| 38 |
+
{"text": f"Analyze the mood based on these inputs: {input_text}. Provide recommendations."}
|
| 39 |
+
]
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
}
|
| 43 |
|
| 44 |
try:
|
| 45 |
+
# Send POST request
|
| 46 |
response = requests.post(url, headers=headers, json=payload)
|
| 47 |
+
|
|
|
|
| 48 |
if response.status_code == 200:
|
| 49 |
result = response.json()
|
| 50 |
|
| 51 |
+
# Extract recommendations from the response
|
| 52 |
+
recommendations = result.get("contents", [{}])[0].get("parts", [{}])[0].get("text", "")
|
| 53 |
+
return recommendations
|
| 54 |
else:
|
| 55 |
+
# Handle API error
|
| 56 |
st.error(f"API Error {response.status_code}: {response.text}")
|
| 57 |
return None
|
| 58 |
except requests.exceptions.RequestException as e:
|
| 59 |
st.error(f"An error occurred: {e}")
|
| 60 |
return None
|
| 61 |
|
| 62 |
+
# Streamlit app
|
| 63 |
def main():
|
| 64 |
st.title("Mood Analysis and Suggestions")
|
|
|
|
| 65 |
st.write("Answer the following 3 questions to help us understand your mood:")
|
| 66 |
|
| 67 |
+
# Collect user responses
|
| 68 |
responses = []
|
| 69 |
for i, question in enumerate(questions):
|
| 70 |
response = st.text_input(f"{i+1}. {question}")
|
| 71 |
if response:
|
| 72 |
responses.append(response)
|
| 73 |
|
| 74 |
+
# Query the API if all questions are answered
|
| 75 |
if len(responses) == len(questions):
|
| 76 |
st.write("Processing your answers...")
|
| 77 |
|
| 78 |
# Query the Gemini API
|
| 79 |
+
recommendations = query_gemini_api(responses)
|
| 80 |
|
| 81 |
+
if recommendations:
|
| 82 |
+
st.write("### Recommendations to Improve Your Mood:")
|
| 83 |
+
st.write(recommendations)
|
|
|
|
| 84 |
else:
|
| 85 |
st.warning("Could not generate mood analysis. Please try again later.")
|
| 86 |
else:
|