Health_advisor / app.py
saherPervaiz's picture
Update app.py
e3e8ff9 verified
raw
history blame
5.18 kB
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 using the Groq API based on user data."""
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."
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 from Groq: {str(e)}")
return None
def stress_level_to_string(stress_level):
"""Convert numerical stress level (0, 1, 2) to a string representation."""
if stress_level == 0:
return "Low"
elif stress_level == 1:
return "Moderate"
else:
return "High"
# Chatbot interface for conversation-like experience
def chatbot_interface():
st.title("πŸŽ“ Student Well-being Chatbot")
st.write("Hello! I’m here to assist you with well-being advice based on your responses.")
# 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
# Step 0: Welcome message and initial question
if st.session_state.step == 0:
st.write("Hi! I'm here to assist you with well-being advice based on your responses. Let's get started!")
next_step_button = st.button("Start Chat")
if next_step_button:
next_step()
# Step 1: Ask for anxiety level
elif st.session_state.step == 1:
anxiety_level = st.text_input("On a scale of 1 to 10, how would you rate your anxiety level?")
if anxiety_level:
try:
anxiety_level = int(anxiety_level)
if anxiety_level < 1 or anxiety_level > 10:
raise ValueError
st.session_state.user_data['anxiety_level'] = anxiety_level
st.write(f"Got it! Your anxiety level is {anxiety_level}.")
next_step()
except ValueError:
show_input_error("Please enter a number between 1 and 10 for anxiety level.")
# Step 2: Ask for self-esteem
elif st.session_state.step == 2:
self_esteem = st.text_input("On a scale of 1 to 10, how would you rate your self-esteem?")
if self_esteem:
try:
self_esteem = int(self_esteem)
if self_esteem < 1 or self_esteem > 10:
raise ValueError
st.session_state.user_data['self_esteem'] = self_esteem
st.write(f"Got it! Your self-esteem level is {self_esteem}.")
next_step()
except ValueError:
show_input_error("Please enter a number between 1 and 10 for self-esteem.")
# Step 3: Ask for mental health history
elif st.session_state.step == 3:
mental_health_history = st.selectbox("How would you describe your mental health history?",
["None", "Minor Issues", "Moderate Issues", "Severe Issues"])
st.session_state.user_data['mental_health_history'] = mental_health_history
st.write(f"Got it! Your mental health history is: {mental_health_history}.")
next_step()
# Step 4: Ask for stress level
elif st.session_state.step == 4:
stress_level = st.radio("On a scale of 0 to 2, how would you rate your stress level?", [0, 1, 2])
st.session_state.user_data['stress_level'] = stress_level
st.write(f"Got it! Your stress level is: {stress_level_to_string(stress_level)}.")
next_step()
# Step 5: Show advice based on user input
elif st.session_state.step == 5:
st.write("Thank you for providing the information!")
user_data = st.session_state.user_data
st.write(f"Your data: {user_data}")
# Show a conversational summary of the user's well-being status
st.subheader("πŸ”” Here's Your Personalized Well-being Advice:")
advice = get_personalized_advice(user_data)
if advice:
st.write(advice)
else:
st.write("I couldn't retrieve specific advice right now, but please check back later.")
# Option to restart the chat
restart_button = st.button("Start Over")
if restart_button:
st.session_state.step = 0
st.session_state.user_data = {}
# Main function to run the chatbot
def main():
chatbot_interface()
if __name__ == "__main__":
main()