Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,39 @@
|
|
1 |
import streamlit as st
|
2 |
from groq import Groq
|
3 |
|
4 |
-
# Groq API
|
5 |
-
groq_api_key = "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"
|
|
|
6 |
|
7 |
-
#
|
8 |
-
client = Groq(api_key=groq_api_key)
|
9 |
-
|
10 |
-
# Function to fetch personalized advice using Groq API
|
11 |
def get_personalized_advice(user_data):
|
12 |
-
"""Fetch personalized advice
|
13 |
query = f"""
|
14 |
-
Based on the following
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
Provide
|
20 |
"""
|
21 |
-
|
22 |
try:
|
23 |
-
response =
|
24 |
messages=[{"role": "user", "content": query}],
|
25 |
-
model="llama-3.3-70b-versatile",
|
26 |
)
|
27 |
-
|
|
|
28 |
except Exception as e:
|
29 |
-
st.error(f"Error fetching advice: {str(e)}")
|
30 |
return None
|
31 |
|
32 |
-
|
33 |
-
"""Convert stress level to string representation."""
|
34 |
-
if stress_level == 0:
|
35 |
-
return "Low"
|
36 |
-
elif stress_level == 1:
|
37 |
-
return "Moderate"
|
38 |
-
else:
|
39 |
-
return "High"
|
40 |
-
|
41 |
-
# Chatbot interface
|
42 |
def chatbot_interface():
|
|
|
43 |
st.title("π Student Health and Stress Chatbot")
|
44 |
-
st.write("Hello! I
|
45 |
-
|
46 |
# Initialize session state
|
47 |
if 'step' not in st.session_state:
|
48 |
st.session_state.step = 0
|
@@ -51,87 +42,96 @@ def chatbot_interface():
|
|
51 |
def next_step():
|
52 |
st.session_state.step += 1
|
53 |
|
54 |
-
def
|
55 |
-
st.error(message)
|
56 |
-
return False
|
57 |
-
|
58 |
-
def clear_data():
|
59 |
-
"""Clear session data and reset to initial step."""
|
60 |
st.session_state.step = 0
|
61 |
st.session_state.user_data = {}
|
62 |
|
63 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
if st.session_state.step == 0:
|
65 |
-
st.
|
66 |
-
|
67 |
-
|
68 |
-
if
|
|
|
69 |
next_step()
|
70 |
-
if clear_button:
|
71 |
-
clear_data()
|
72 |
|
73 |
# Step 1: Ask for anxiety level
|
74 |
elif st.session_state.step == 1:
|
75 |
-
st.
|
|
|
76 |
anxiety_level = st.slider("Anxiety Level", 1, 10, 5)
|
77 |
-
st.
|
78 |
-
|
79 |
-
|
80 |
next_step()
|
81 |
|
82 |
-
# Step 2: Ask for self-esteem
|
83 |
elif st.session_state.step == 2:
|
84 |
-
st.
|
|
|
85 |
self_esteem = st.slider("Self-esteem Level", 1, 10, 5)
|
86 |
-
st.
|
87 |
-
|
88 |
-
|
89 |
next_step()
|
90 |
|
91 |
# Step 3: Ask for mental health history
|
92 |
elif st.session_state.step == 3:
|
93 |
-
st.
|
|
|
94 |
mental_health_history = st.selectbox(
|
95 |
"Please select the option that best represents your mental health history:",
|
96 |
["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
|
97 |
)
|
98 |
-
st.
|
99 |
-
|
100 |
-
|
101 |
next_step()
|
102 |
|
103 |
# Step 4: Ask for stress level
|
104 |
elif st.session_state.step == 4:
|
105 |
-
st.
|
|
|
106 |
stress_level = st.radio("Stress Level", [0, 1, 2])
|
107 |
-
st.
|
108 |
-
|
109 |
-
|
110 |
next_step()
|
111 |
|
112 |
-
# Step 5:
|
113 |
elif st.session_state.step == 5:
|
114 |
-
st.
|
|
|
115 |
user_data = st.session_state.user_data
|
116 |
-
st.write(f"
|
117 |
-
st.write(f"
|
118 |
-
st.write(f"
|
119 |
-
st.write(f"
|
120 |
|
|
|
121 |
st.subheader("π Personalized Mental Health Advice:")
|
122 |
advice = get_personalized_advice(user_data)
|
123 |
if advice:
|
124 |
st.write(f"Bot: {advice}")
|
125 |
else:
|
126 |
-
st.write("Bot: I'm unable to
|
127 |
-
|
128 |
-
# Option to restart or clear data
|
129 |
restart_button = st.button("Start Over")
|
130 |
-
clear_button = st.button("Clear All")
|
131 |
if restart_button:
|
132 |
-
|
133 |
if clear_button:
|
134 |
-
|
135 |
|
136 |
# Main function to run the chatbot
|
137 |
def main():
|
|
|
1 |
import streamlit as st
|
2 |
from groq import Groq
|
3 |
|
4 |
+
# Initialize Groq API Client
|
5 |
+
groq_api_key = "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" # Replace with your actual Groq API key
|
6 |
+
groq_client = Groq(api_key=groq_api_key)
|
7 |
|
8 |
+
# Function to get personalized advice from Groq API based on user data
|
|
|
|
|
|
|
9 |
def get_personalized_advice(user_data):
|
10 |
+
"""Fetch personalized advice from Groq API."""
|
11 |
query = f"""
|
12 |
+
Based on the following user data:
|
13 |
+
Anxiety level: {user_data['anxiety_level']},
|
14 |
+
Self-esteem: {user_data['self_esteem']},
|
15 |
+
Mental health history: {user_data['mental_health_history']},
|
16 |
+
Stress level: {user_data['stress_level']},
|
17 |
+
Provide tailored advice to the user.
|
18 |
"""
|
19 |
+
|
20 |
try:
|
21 |
+
response = groq_client.chat.completions.create(
|
22 |
messages=[{"role": "user", "content": query}],
|
23 |
+
model="llama-3.3-70b-versatile", # You can use another model if needed
|
24 |
)
|
25 |
+
advice = response.choices[0].message.content
|
26 |
+
return advice
|
27 |
except Exception as e:
|
28 |
+
st.error(f"Error fetching advice from Groq: {str(e)}")
|
29 |
return None
|
30 |
|
31 |
+
# Chatbot interface with Streamlit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def chatbot_interface():
|
33 |
+
"""Create the chatbot interface."""
|
34 |
st.title("π Student Health and Stress Chatbot")
|
35 |
+
st.write("Hello! I'm here to assist you with personalized well-being advice based on your responses.")
|
36 |
+
|
37 |
# Initialize session state
|
38 |
if 'step' not in st.session_state:
|
39 |
st.session_state.step = 0
|
|
|
42 |
def next_step():
|
43 |
st.session_state.step += 1
|
44 |
|
45 |
+
def restart_conversation():
|
|
|
|
|
|
|
|
|
|
|
46 |
st.session_state.step = 0
|
47 |
st.session_state.user_data = {}
|
48 |
|
49 |
+
# Chat history
|
50 |
+
if 'chat_history' not in st.session_state:
|
51 |
+
st.session_state.chat_history = []
|
52 |
+
|
53 |
+
# Function to display chat messages
|
54 |
+
def display_chat(messages):
|
55 |
+
for message in messages:
|
56 |
+
st.chat_message(message['role']).markdown(message['content'])
|
57 |
+
|
58 |
+
# Step 0: Welcome message and initial prompt
|
59 |
if st.session_state.step == 0:
|
60 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Hi! I'm here to help you with your well-being. Let's start by discussing how you're feeling."})
|
61 |
+
display_chat(st.session_state.chat_history)
|
62 |
+
user_input = st.text_input("You:", key="input_1")
|
63 |
+
if user_input:
|
64 |
+
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
65 |
next_step()
|
|
|
|
|
66 |
|
67 |
# Step 1: Ask for anxiety level
|
68 |
elif st.session_state.step == 1:
|
69 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "On a scale of 1 to 10, how would you rate your anxiety level?"})
|
70 |
+
display_chat(st.session_state.chat_history)
|
71 |
anxiety_level = st.slider("Anxiety Level", 1, 10, 5)
|
72 |
+
if st.button("Submit Anxiety Level"):
|
73 |
+
st.session_state.user_data['anxiety_level'] = anxiety_level
|
74 |
+
st.session_state.chat_history.append({"role": "user", "content": f"Anxiety Level: {anxiety_level}"})
|
75 |
next_step()
|
76 |
|
77 |
+
# Step 2: Ask for self-esteem level
|
78 |
elif st.session_state.step == 2:
|
79 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "On a scale of 1 to 10, how would you rate your self-esteem?"})
|
80 |
+
display_chat(st.session_state.chat_history)
|
81 |
self_esteem = st.slider("Self-esteem Level", 1, 10, 5)
|
82 |
+
if st.button("Submit Self-Esteem Level"):
|
83 |
+
st.session_state.user_data['self_esteem'] = self_esteem
|
84 |
+
st.session_state.chat_history.append({"role": "user", "content": f"Self-Esteem Level: {self_esteem}"})
|
85 |
next_step()
|
86 |
|
87 |
# Step 3: Ask for mental health history
|
88 |
elif st.session_state.step == 3:
|
89 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "How would you describe your mental health history?"})
|
90 |
+
display_chat(st.session_state.chat_history)
|
91 |
mental_health_history = st.selectbox(
|
92 |
"Please select the option that best represents your mental health history:",
|
93 |
["None", "Minor Issues", "Moderate Issues", "Severe Issues"]
|
94 |
)
|
95 |
+
if st.button("Submit Mental Health History"):
|
96 |
+
st.session_state.user_data['mental_health_history'] = mental_health_history
|
97 |
+
st.session_state.chat_history.append({"role": "user", "content": f"Mental Health History: {mental_health_history}"})
|
98 |
next_step()
|
99 |
|
100 |
# Step 4: Ask for stress level
|
101 |
elif st.session_state.step == 4:
|
102 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "On a scale from 0 to 2, how would you rate your current stress level?"})
|
103 |
+
display_chat(st.session_state.chat_history)
|
104 |
stress_level = st.radio("Stress Level", [0, 1, 2])
|
105 |
+
if st.button("Submit Stress Level"):
|
106 |
+
st.session_state.user_data['stress_level'] = stress_level
|
107 |
+
st.session_state.chat_history.append({"role": "user", "content": f"Stress Level: {stress_level}"})
|
108 |
next_step()
|
109 |
|
110 |
+
# Step 5: Display personalized advice based on user input
|
111 |
elif st.session_state.step == 5:
|
112 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Thank you for providing your responses! Here's a summary of your well-being:"})
|
113 |
+
display_chat(st.session_state.chat_history)
|
114 |
user_data = st.session_state.user_data
|
115 |
+
st.write(f"Anxiety Level: {user_data['anxiety_level']}")
|
116 |
+
st.write(f"Self-Esteem Level: {user_data['self_esteem']}")
|
117 |
+
st.write(f"Mental Health History: {user_data['mental_health_history']}")
|
118 |
+
st.write(f"Stress Level: {user_data['stress_level']}")
|
119 |
|
120 |
+
# Generate personalized advice
|
121 |
st.subheader("π Personalized Mental Health Advice:")
|
122 |
advice = get_personalized_advice(user_data)
|
123 |
if advice:
|
124 |
st.write(f"Bot: {advice}")
|
125 |
else:
|
126 |
+
st.write("Bot: I'm unable to provide personalized advice at the moment.")
|
127 |
+
|
128 |
+
# Option to restart the chat or clear all data
|
129 |
restart_button = st.button("Start Over")
|
130 |
+
clear_button = st.button("Clear All Data")
|
131 |
if restart_button:
|
132 |
+
restart_conversation()
|
133 |
if clear_button:
|
134 |
+
restart_conversation()
|
135 |
|
136 |
# Main function to run the chatbot
|
137 |
def main():
|