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