Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ client = Groq(api_key=groq_api_key)
|
|
10 |
# Function to fetch personalized advice using Groq API
|
11 |
def get_personalized_advice(user_data):
|
12 |
"""Fetch personalized advice using the Groq API based on user data."""
|
13 |
-
query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self
|
14 |
|
15 |
try:
|
16 |
response = client.chat.completions.create(
|
@@ -22,7 +22,6 @@ def get_personalized_advice(user_data):
|
|
22 |
st.error(f"Error fetching advice from Groq: {str(e)}")
|
23 |
return None
|
24 |
|
25 |
-
# Function to convert numerical stress level to string form
|
26 |
def stress_level_to_string(stress_level):
|
27 |
"""Convert numerical stress level (0, 1, 2) to a string representation."""
|
28 |
if stress_level == 0:
|
@@ -32,16 +31,11 @@ def stress_level_to_string(stress_level):
|
|
32 |
else:
|
33 |
return "High"
|
34 |
|
35 |
-
#
|
36 |
def chatbot_interface():
|
37 |
-
st.set_page_config(page_title="π Student Well-being Chatbot", layout="centered")
|
38 |
st.title("π Student Well-being Chatbot")
|
39 |
-
st.write("
|
40 |
-
|
41 |
-
# Styling the sidebar for improved UX
|
42 |
-
st.sidebar.title("Student Well-being")
|
43 |
-
st.sidebar.write("The well-being questionnaire will guide you through assessing your mental health and providing personalized advice.")
|
44 |
-
|
45 |
# Initialize session state
|
46 |
if 'step' not in st.session_state:
|
47 |
st.session_state.step = 0
|
@@ -56,62 +50,61 @@ def chatbot_interface():
|
|
56 |
|
57 |
# Step 0: Welcome message and initial question
|
58 |
if st.session_state.step == 0:
|
59 |
-
st.write("Bot: Hi there! I'm here to
|
60 |
-
st.
|
61 |
-
next_step_button = st.button("Start Chat"
|
62 |
if next_step_button:
|
63 |
next_step()
|
64 |
|
65 |
-
# Step 1: Ask for anxiety level
|
66 |
elif st.session_state.step == 1:
|
67 |
st.write("Bot: On a scale of 1 to 10, how would you rate your anxiety level?")
|
68 |
-
anxiety_level = st.slider("
|
69 |
-
|
70 |
-
|
|
|
71 |
|
72 |
-
# Step 2: Ask for self-esteem
|
73 |
elif st.session_state.step == 2:
|
74 |
st.write("Bot: On a scale of 1 to 10, how would you rate your self-esteem?")
|
75 |
-
self_esteem = st.slider("
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
# Step 3: Ask for mental health history
|
80 |
elif st.session_state.step == 3:
|
81 |
st.write("Bot: How would you describe your mental health history?")
|
82 |
mental_health_history = st.selectbox(
|
83 |
-
"Select
|
84 |
-
["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
|
85 |
-
help="Choose an option that best describes your mental health background."
|
86 |
)
|
87 |
st.session_state.user_data['mental_health_history'] = mental_health_history
|
88 |
next_step()
|
89 |
|
90 |
-
# Step 4: Ask for stress level
|
91 |
elif st.session_state.step == 4:
|
92 |
st.write("Bot: On a scale of 0 to 2, how would you rate your stress level?")
|
93 |
-
stress_level = st.
|
94 |
-
|
95 |
-
st.session_state.user_data['stress_level'] = stress_level_string
|
96 |
next_step()
|
97 |
|
98 |
# Step 5: Show advice based on user input
|
99 |
elif st.session_state.step == 5:
|
100 |
-
st.write("Bot: Thank you for providing the information!
|
101 |
user_data = st.session_state.user_data
|
102 |
-
st.write(f"
|
103 |
|
104 |
-
#
|
105 |
st.subheader("π Here's Your Personalized Well-being Advice:")
|
106 |
advice = get_personalized_advice(user_data)
|
107 |
if advice:
|
108 |
-
st.write(advice)
|
109 |
else:
|
110 |
-
st.write("I couldn't retrieve specific advice right now, but please check back later.")
|
111 |
|
112 |
# Option to restart the chat
|
113 |
-
st.
|
114 |
-
restart_button = st.button("Start Over", key="restart_button", help="Click to restart the questionnaire.")
|
115 |
if restart_button:
|
116 |
st.session_state.step = 0
|
117 |
st.session_state.user_data = {}
|
|
|
10 |
# Function to fetch personalized advice using Groq API
|
11 |
def get_personalized_advice(user_data):
|
12 |
"""Fetch personalized advice using the Groq API based on user data."""
|
13 |
+
query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self-esteem: {user_data['self_esteem']}, mental health history: {user_data['mental_health_history']}, stress level: {user_data['stress_level']}, provide personalized mental health advice."
|
14 |
|
15 |
try:
|
16 |
response = client.chat.completions.create(
|
|
|
22 |
st.error(f"Error fetching advice from Groq: {str(e)}")
|
23 |
return None
|
24 |
|
|
|
25 |
def stress_level_to_string(stress_level):
|
26 |
"""Convert numerical stress level (0, 1, 2) to a string representation."""
|
27 |
if stress_level == 0:
|
|
|
31 |
else:
|
32 |
return "High"
|
33 |
|
34 |
+
# Chatbot interface for conversation-like experience
|
35 |
def chatbot_interface():
|
|
|
36 |
st.title("π Student Well-being Chatbot")
|
37 |
+
st.write("Hello! Iβm here to assist you with well-being advice based on your responses.")
|
38 |
+
|
|
|
|
|
|
|
|
|
39 |
# Initialize session state
|
40 |
if 'step' not in st.session_state:
|
41 |
st.session_state.step = 0
|
|
|
50 |
|
51 |
# Step 0: Welcome message and initial question
|
52 |
if st.session_state.step == 0:
|
53 |
+
st.write("Bot: Hi there! I'm here to help with your well-being. Let's start! π")
|
54 |
+
st.write("Bot: I'll ask you a few questions about your well-being to give you personalized advice.")
|
55 |
+
next_step_button = st.button("Start Chat")
|
56 |
if next_step_button:
|
57 |
next_step()
|
58 |
|
59 |
+
# Step 1: Ask for anxiety level
|
60 |
elif st.session_state.step == 1:
|
61 |
st.write("Bot: On a scale of 1 to 10, how would you rate your anxiety level?")
|
62 |
+
anxiety_level = st.slider("Anxiety level (1-10)", 1, 10, 5)
|
63 |
+
if anxiety_level:
|
64 |
+
st.session_state.user_data['anxiety_level'] = anxiety_level
|
65 |
+
next_step()
|
66 |
|
67 |
+
# Step 2: Ask for self-esteem
|
68 |
elif st.session_state.step == 2:
|
69 |
st.write("Bot: On a scale of 1 to 10, how would you rate your self-esteem?")
|
70 |
+
self_esteem = st.slider("Self-esteem level (1-10)", 1, 10, 5)
|
71 |
+
if self_esteem:
|
72 |
+
st.session_state.user_data['self_esteem'] = self_esteem
|
73 |
+
next_step()
|
74 |
|
75 |
# Step 3: Ask for mental health history
|
76 |
elif st.session_state.step == 3:
|
77 |
st.write("Bot: How would you describe your mental health history?")
|
78 |
mental_health_history = st.selectbox(
|
79 |
+
"Select an option:",
|
80 |
+
["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
|
|
|
81 |
)
|
82 |
st.session_state.user_data['mental_health_history'] = mental_health_history
|
83 |
next_step()
|
84 |
|
85 |
+
# Step 4: Ask for stress level
|
86 |
elif st.session_state.step == 4:
|
87 |
st.write("Bot: On a scale of 0 to 2, how would you rate your stress level?")
|
88 |
+
stress_level = st.radio("Stress level (0 = Low, 1 = Moderate, 2 = High)", [0, 1, 2])
|
89 |
+
st.session_state.user_data['stress_level'] = stress_level
|
|
|
90 |
next_step()
|
91 |
|
92 |
# Step 5: Show advice based on user input
|
93 |
elif st.session_state.step == 5:
|
94 |
+
st.write("Bot: Thank you for providing the information!")
|
95 |
user_data = st.session_state.user_data
|
96 |
+
st.write(f"Bot: Here's a summary of your responses: {user_data}")
|
97 |
|
98 |
+
# Show a conversational summary of the user's well-being status
|
99 |
st.subheader("π Here's Your Personalized Well-being Advice:")
|
100 |
advice = get_personalized_advice(user_data)
|
101 |
if advice:
|
102 |
+
st.write(f"Bot: {advice}")
|
103 |
else:
|
104 |
+
st.write("Bot: I couldn't retrieve specific advice right now, but please check back later.")
|
105 |
|
106 |
# Option to restart the chat
|
107 |
+
restart_button = st.button("Start Over")
|
|
|
108 |
if restart_button:
|
109 |
st.session_state.step = 0
|
110 |
st.session_state.user_data = {}
|