Spaces:
Sleeping
Sleeping
import streamlit as st | |
from groq import Groq | |
# Groq API Key (replace with your actual API key) | |
groq_api_key = "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" | |
# Initialize Groq client | |
client = Groq(api_key=groq_api_key) | |
# Function to fetch personalized advice using Groq API | |
def get_personalized_advice(user_data): | |
"""Fetch personalized advice based on user data.""" | |
query = f""" | |
Based on the following student's responses: | |
- 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 professional, personalized mental health advice. | |
""" | |
try: | |
response = client.chat.completions.create( | |
messages=[{"role": "user", "content": query}], | |
model="llama-3.3-70b-versatile", | |
) | |
return response.choices[0].message.content | |
except Exception as e: | |
st.error(f"Error fetching advice: {str(e)}") | |
return None | |
def stress_level_to_string(stress_level): | |
"""Convert stress level to string representation.""" | |
if stress_level == 0: | |
return "Low" | |
elif stress_level == 1: | |
return "Moderate" | |
else: | |
return "High" | |
# Chatbot interface | |
def chatbot_interface(): | |
st.title("π Student Health and Stress Chatbot") | |
st.write("Hello! Iβm here to assist you in assessing your well-being and provide helpful advice. Let's get started! π") | |
# Initialize session state | |
if 'step' not in st.session_state: | |
st.session_state.step = 0 | |
st.session_state.user_data = {} | |
def next_step(): | |
st.session_state.step += 1 | |
def show_input_error(message="Please provide a valid input."): | |
st.error(message) | |
return False | |
def clear_data(): | |
"""Clear session data and reset to initial step.""" | |
st.session_state.step = 0 | |
st.session_state.user_data = {} | |
# Step 0: Welcome message and initial question | |
if st.session_state.step == 0: | |
st.write("Bot: Welcome to the Student Health and Stress Assessment. Let's start with some simple questions.") | |
start_button = st.button("Start Assessment") | |
clear_button = st.button("Clear All") | |
if start_button: | |
next_step() | |
if clear_button: | |
clear_data() | |
# Step 1: Ask for anxiety level | |
elif st.session_state.step == 1: | |
st.write("Bot: On a scale of 1 to 10, how would you rate your current anxiety level?") | |
anxiety_level = st.slider("Anxiety Level", 1, 10, 5) | |
st.session_state.user_data['anxiety_level'] = anxiety_level | |
next_step_button = st.button("Next") | |
if next_step_button: | |
next_step() | |
# Step 2: Ask for self-esteem | |
elif st.session_state.step == 2: | |
st.write("Bot: On a scale from 1 to 10, how would you rate your current self-esteem?") | |
self_esteem = st.slider("Self-esteem Level", 1, 10, 5) | |
st.session_state.user_data['self_esteem'] = self_esteem | |
next_step_button = st.button("Next") | |
if next_step_button: | |
next_step() | |
# Step 3: Ask for mental health history | |
elif st.session_state.step == 3: | |
st.write("Bot: How would you describe your mental health history?") | |
mental_health_history = st.selectbox( | |
"Please select the option that best represents your mental health history:", | |
["None", "Minor Issues", "Moderate Issues", "Severe Issues"] | |
) | |
st.session_state.user_data['mental_health_history'] = mental_health_history | |
next_step_button = st.button("Next") | |
if next_step_button: | |
next_step() | |
# Step 4: Ask for stress level | |
elif st.session_state.step == 4: | |
st.write("Bot: On a scale from 0 to 2, how would you rate your current stress level?") | |
stress_level = st.radio("Stress Level", [0, 1, 2]) | |
st.session_state.user_data['stress_level'] = stress_level | |
next_step_button = st.button("Next") | |
if next_step_button: | |
next_step() | |
# Step 5: Provide personalized advice based on responses | |
elif st.session_state.step == 5: | |
st.write("Bot: Thank you for providing the information! Here's a summary of your responses:") | |
user_data = st.session_state.user_data | |
st.write(f"Bot: Your Anxiety Level: {user_data['anxiety_level']}") | |
st.write(f"Bot: Your Self-Esteem Level: {user_data['self_esteem']}") | |
st.write(f"Bot: Mental Health History: {user_data['mental_health_history']}") | |
st.write(f"Bot: Your Stress Level: {stress_level_to_string(user_data['stress_level'])}") | |
st.subheader("π Personalized Mental Health Advice:") | |
advice = get_personalized_advice(user_data) | |
if advice: | |
st.write(f"Bot: {advice}") | |
else: | |
st.write("Bot: I'm unable to retrieve personalized advice at the moment, but feel free to try again later.") | |
# Option to restart or clear data | |
restart_button = st.button("Start Over") | |
clear_button = st.button("Clear All") | |
if restart_button: | |
clear_data() | |
if clear_button: | |
clear_data() | |
# Main function to run the chatbot | |
def main(): | |
chatbot_interface() | |
if __name__ == "__main__": | |
main() | |