Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,114 +1,63 @@
|
|
1 |
import streamlit as st
|
|
|
2 |
from groq import Groq
|
3 |
|
4 |
-
#
|
5 |
-
groq_api_key = "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"
|
6 |
|
7 |
# Initialize Groq client
|
8 |
-
|
9 |
|
10 |
-
# Function to
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
Self-Esteem Level: {user_data['self_esteem']}
|
16 |
-
Mental Health History: {user_data['mental_health_history']}
|
17 |
-
Stress Level: {user_data['stress_level']}
|
18 |
-
|
19 |
-
Please provide personalized advice for the user's well-being.
|
20 |
-
"""
|
21 |
try:
|
22 |
-
|
|
|
23 |
messages=[{"role": "user", "content": query}],
|
24 |
-
model="llama-3.3-70b-versatile" #
|
25 |
)
|
26 |
-
return response
|
|
|
|
|
27 |
except Exception as e:
|
28 |
-
st.error(f"Error: {
|
29 |
-
return
|
30 |
-
|
31 |
-
# Function to
|
32 |
-
def
|
33 |
-
st.
|
34 |
-
st.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
def restart_chat():
|
54 |
-
st.session_state.step = 0
|
55 |
-
st.session_state.user_data = {}
|
56 |
-
st.session_state.chat_history = []
|
57 |
-
|
58 |
-
# Step 1: Greet the user
|
59 |
-
if st.session_state.step == 0:
|
60 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Hello! How are you feeling today? π Let's start your well-being assessment!"})
|
61 |
-
user_input = st.text_input("You:", "")
|
62 |
-
if user_input:
|
63 |
-
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
64 |
-
next_step()
|
65 |
-
|
66 |
-
# Step 2: Form to collect data (Anxiety Level, Self-Esteem, Mental Health, Stress)
|
67 |
-
elif st.session_state.step == 1:
|
68 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Please provide the following details to help me understand your well-being:"})
|
69 |
-
|
70 |
-
with st.form("Well-being Form"):
|
71 |
-
anxiety_level = st.slider("Anxiety Level (1 to 10)", 1, 10, 5)
|
72 |
-
self_esteem = st.slider("Self-esteem Level (1 to 10)", 1, 10, 5)
|
73 |
-
mental_health_history = st.selectbox("Mental Health History", ["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
|
74 |
-
stress_level = st.radio("Stress Level (0 to 2)", [0, 1, 2])
|
75 |
-
|
76 |
-
submit_button = st.form_submit_button(label="Submit")
|
77 |
-
|
78 |
-
if submit_button:
|
79 |
-
# Collecting user input in session state
|
80 |
-
st.session_state.user_data = {
|
81 |
-
"anxiety_level": anxiety_level,
|
82 |
-
"self_esteem": self_esteem,
|
83 |
-
"mental_health_history": mental_health_history,
|
84 |
-
"stress_level": stress_level
|
85 |
-
}
|
86 |
-
st.session_state.chat_history.append({"role": "user", "content": f"Form Submitted:\nAnxiety Level: {anxiety_level}\nSelf-esteem Level: {self_esteem}\nMental Health History: {mental_health_history}\nStress Level: {stress_level}"})
|
87 |
-
next_step()
|
88 |
-
|
89 |
-
# Step 3: Provide Personalized Advice
|
90 |
-
elif st.session_state.step == 2:
|
91 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "Thank you for submitting your information! Here's a summary of your well-being:"})
|
92 |
-
user_data = st.session_state.user_data
|
93 |
-
st.write(f"Anxiety Level: {user_data['anxiety_level']}")
|
94 |
-
st.write(f"Self-Esteem Level: {user_data['self_esteem']}")
|
95 |
-
st.write(f"Mental Health History: {user_data['mental_health_history']}")
|
96 |
-
st.write(f"Stress Level: {user_data['stress_level']}")
|
97 |
-
|
98 |
-
# Provide personalized advice
|
99 |
-
st.subheader("π Personalized Well-being Advice:")
|
100 |
-
advice = get_personalized_advice(user_data)
|
101 |
if advice:
|
102 |
-
st.
|
103 |
-
|
104 |
-
st.write("Bot: I couldn't retrieve personalized advice at the moment.")
|
105 |
|
106 |
-
|
107 |
-
|
|
|
108 |
|
109 |
# Main function to run the chatbot
|
110 |
def main():
|
111 |
-
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
import requests
|
3 |
from groq import Groq
|
4 |
|
5 |
+
# Groq API Key (replace with your actual API key)
|
6 |
+
groq_api_key = "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" # Replace with your Groq API key
|
7 |
|
8 |
# Initialize Groq client
|
9 |
+
client = Groq(api_key=groq_api_key)
|
10 |
|
11 |
+
# Function to send a request to the Groq API and fetch health advice
|
12 |
+
def get_health_advice_from_groq(anxiety_level, self_esteem, stress_level):
|
13 |
+
"""Fetch health advice from Groq API based on user input."""
|
14 |
+
query = f"Provide personalized health advice based on the following data: anxiety level: {anxiety_level}, self-esteem: {self_esteem}, stress level: {stress_level}."
|
15 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
try:
|
17 |
+
# Request to Groq API
|
18 |
+
response = client.chat.completions.create(
|
19 |
messages=[{"role": "user", "content": query}],
|
20 |
+
model="llama-3.3-70b-versatile", # You can use the model that suits your needs
|
21 |
)
|
22 |
+
# Extract and return the health advice from the response
|
23 |
+
advice = response.choices[0].message.content
|
24 |
+
return advice
|
25 |
except Exception as e:
|
26 |
+
st.error(f"Error fetching advice from Groq: {e}")
|
27 |
+
return None
|
28 |
+
|
29 |
+
# Function to create the chatbot interface
|
30 |
+
def health_advice_chatbot():
|
31 |
+
st.title("Health Advice Chatbot")
|
32 |
+
st.write("Welcome! I'm here to help you with some basic health advice based on your well-being.")
|
33 |
+
|
34 |
+
# User input
|
35 |
+
st.write("Please answer the following questions to receive personalized health advice.")
|
36 |
+
|
37 |
+
# Anxiety Level (Slider)
|
38 |
+
anxiety_level = st.slider("On a scale of 1 to 10, how would you rate your anxiety level?", 1, 10, 5)
|
39 |
+
|
40 |
+
# Self-esteem Level (Slider)
|
41 |
+
self_esteem = st.slider("On a scale of 1 to 10, how would you rate your self-esteem?", 1, 10, 5)
|
42 |
+
|
43 |
+
# Stress Level (Radio Buttons)
|
44 |
+
stress_level = st.radio("On a scale of 0 to 2, how would you rate your stress level?", [0, 1, 2])
|
45 |
+
|
46 |
+
# Submit button to get health advice
|
47 |
+
if st.button("Get Health Advice"):
|
48 |
+
# Fetch health advice from Groq API
|
49 |
+
advice = get_health_advice_from_groq(anxiety_level, self_esteem, stress_level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
if advice:
|
51 |
+
st.subheader("Here is your personalized health advice:")
|
52 |
+
st.write(advice)
|
|
|
53 |
|
54 |
+
# Option to restart the conversation
|
55 |
+
if st.button("Start Over"):
|
56 |
+
st.experimental_rerun()
|
57 |
|
58 |
# Main function to run the chatbot
|
59 |
def main():
|
60 |
+
health_advice_chatbot()
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
main()
|