Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,137 +1,106 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
groq_api_key = "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"
|
|
|
|
|
|
|
| 6 |
groq_client = Groq(api_key=groq_api_key)
|
| 7 |
|
| 8 |
-
# Function to
|
| 9 |
def get_personalized_advice(user_data):
|
| 10 |
-
"""Fetch personalized advice from Groq API."""
|
| 11 |
query = f"""
|
| 12 |
-
Based on the following user
|
| 13 |
-
Anxiety
|
| 14 |
-
Self-
|
| 15 |
-
Mental
|
| 16 |
-
Stress
|
| 17 |
-
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
| 20 |
try:
|
| 21 |
response = groq_client.chat.completions.create(
|
| 22 |
messages=[{"role": "user", "content": query}],
|
| 23 |
-
model="llama-3.3-70b-versatile"
|
| 24 |
)
|
| 25 |
-
|
| 26 |
-
return advice
|
| 27 |
except Exception as e:
|
| 28 |
-
st.error(f"Error
|
| 29 |
-
return
|
| 30 |
|
| 31 |
-
#
|
| 32 |
def chatbot_interface():
|
| 33 |
-
"
|
| 34 |
-
st.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
# Initialize session state
|
| 38 |
if 'step' not in st.session_state:
|
| 39 |
st.session_state.step = 0
|
| 40 |
st.session_state.user_data = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def next_step():
|
| 43 |
st.session_state.step += 1
|
| 44 |
|
| 45 |
-
def
|
| 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 |
-
#
|
| 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": "
|
| 61 |
-
|
| 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
|
| 68 |
elif st.session_state.step == 1:
|
| 69 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
st.
|
| 74 |
-
st.
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
elif st.session_state.step == 2:
|
| 79 |
-
st.session_state.chat_history.append({"role": "assistant", "content": "
|
| 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 |
-
#
|
| 121 |
-
st.subheader("π Personalized
|
| 122 |
advice = get_personalized_advice(user_data)
|
| 123 |
if advice:
|
| 124 |
st.write(f"Bot: {advice}")
|
| 125 |
else:
|
| 126 |
-
st.write("Bot: I'
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 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():
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
+
# Replace with your Groq API key
|
| 5 |
+
groq_api_key = "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"
|
| 6 |
+
|
| 7 |
+
# Initialize Groq client
|
| 8 |
groq_client = Groq(api_key=groq_api_key)
|
| 9 |
|
| 10 |
+
# Function to generate personalized advice based on user input
|
| 11 |
def get_personalized_advice(user_data):
|
|
|
|
| 12 |
query = f"""
|
| 13 |
+
Based on the following information about the user:
|
| 14 |
+
Anxiety Level: {user_data['anxiety_level']}
|
| 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 |
response = groq_client.chat.completions.create(
|
| 23 |
messages=[{"role": "user", "content": query}],
|
| 24 |
+
model="llama-3.3-70b-versatile" # Use the appropriate model
|
| 25 |
)
|
| 26 |
+
return response.choices[0].message.content
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
+
st.error(f"Error: {str(e)}")
|
| 29 |
+
return "Sorry, I couldn't retrieve personalized advice at the moment."
|
| 30 |
|
| 31 |
+
# Function to display the chatbot interface
|
| 32 |
def chatbot_interface():
|
| 33 |
+
st.title("π Professional Student Well-being Chatbot")
|
| 34 |
+
st.markdown("#### Let's talk about how you're feeling today. I'm here to assist you with your mental well-being.")
|
| 35 |
+
|
|
|
|
| 36 |
# Initialize session state
|
| 37 |
if 'step' not in st.session_state:
|
| 38 |
st.session_state.step = 0
|
| 39 |
st.session_state.user_data = {}
|
| 40 |
+
st.session_state.chat_history = []
|
| 41 |
+
|
| 42 |
+
# Display chat history (simulating chatbot-style interaction)
|
| 43 |
+
for message in st.session_state.chat_history:
|
| 44 |
+
st.chat_message(message["role"]).markdown(message["content"])
|
| 45 |
|
| 46 |
def next_step():
|
| 47 |
st.session_state.step += 1
|
| 48 |
|
| 49 |
+
def restart_chat():
|
| 50 |
st.session_state.step = 0
|
| 51 |
st.session_state.user_data = {}
|
|
|
|
|
|
|
|
|
|
| 52 |
st.session_state.chat_history = []
|
| 53 |
|
| 54 |
+
# Step 1: Greet the user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
if st.session_state.step == 0:
|
| 56 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Hello! How are you feeling today? π I'm here to assist you with your mental well-being."})
|
| 57 |
+
user_input = st.text_input("You:", "")
|
|
|
|
| 58 |
if user_input:
|
| 59 |
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
| 60 |
next_step()
|
| 61 |
|
| 62 |
+
# Step 2: Form to collect data (Anxiety Level, Self-Esteem, Mental Health, Stress)
|
| 63 |
elif st.session_state.step == 1:
|
| 64 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Please fill out the form below to help me understand your current well-being."})
|
| 65 |
+
|
| 66 |
+
with st.form("Well-being Form"):
|
| 67 |
+
anxiety_level = st.slider("Anxiety Level (1 to 10)", 1, 10, 5)
|
| 68 |
+
self_esteem = st.slider("Self-esteem Level (1 to 10)", 1, 10, 5)
|
| 69 |
+
mental_health_history = st.selectbox("Mental Health History", ["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
|
| 70 |
+
stress_level = st.radio("Stress Level (0 to 2)", [0, 1, 2])
|
| 71 |
+
|
| 72 |
+
submit_button = st.form_submit_button(label="Submit")
|
| 73 |
+
|
| 74 |
+
if submit_button:
|
| 75 |
+
# Collecting user input in session state
|
| 76 |
+
st.session_state.user_data = {
|
| 77 |
+
"anxiety_level": anxiety_level,
|
| 78 |
+
"self_esteem": self_esteem,
|
| 79 |
+
"mental_health_history": mental_health_history,
|
| 80 |
+
"stress_level": stress_level
|
| 81 |
+
}
|
| 82 |
+
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}"})
|
| 83 |
+
next_step()
|
| 84 |
+
|
| 85 |
+
# Step 3: Provide Personalized Advice
|
| 86 |
elif st.session_state.step == 2:
|
| 87 |
+
st.session_state.chat_history.append({"role": "assistant", "content": "Thank you for providing the information! Here's a summary of your well-being:"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
user_data = st.session_state.user_data
|
| 89 |
st.write(f"Anxiety Level: {user_data['anxiety_level']}")
|
| 90 |
st.write(f"Self-Esteem Level: {user_data['self_esteem']}")
|
| 91 |
st.write(f"Mental Health History: {user_data['mental_health_history']}")
|
| 92 |
st.write(f"Stress Level: {user_data['stress_level']}")
|
| 93 |
|
| 94 |
+
# Provide personalized advice
|
| 95 |
+
st.subheader("π Personalized Well-being Advice:")
|
| 96 |
advice = get_personalized_advice(user_data)
|
| 97 |
if advice:
|
| 98 |
st.write(f"Bot: {advice}")
|
| 99 |
else:
|
| 100 |
+
st.write("Bot: I couldn't retrieve personalized advice at the moment.")
|
| 101 |
+
|
| 102 |
+
if st.button("Start Over"):
|
| 103 |
+
restart_chat()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# Main function to run the chatbot
|
| 106 |
def main():
|