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 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 'messages' not in st.session_state: | |
st.session_state.messages = [ | |
{"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! π"} | |
] | |
def next_step(user_input=None): | |
# Save the user's message to the chat history | |
if user_input: | |
st.session_state.messages.append({"role": "user", "content": user_input}) | |
# Proceed to next step in the conversation | |
if len(st.session_state.messages) == 1: | |
st.session_state.messages.append({"role": "bot", "content": "On a scale of 1 to 10, how would you rate your anxiety level?"}) | |
elif len(st.session_state.messages) == 2: | |
anxiety_level = int(user_input) | |
st.session_state.user_data = {'anxiety_level': anxiety_level} | |
st.session_state.messages.append({"role": "bot", "content": f"Got it! Your anxiety level is {anxiety_level}. Now, on a scale of 1 to 10, how would you rate your self-esteem?"}) | |
elif len(st.session_state.messages) == 3: | |
self_esteem = int(user_input) | |
st.session_state.user_data['self_esteem'] = self_esteem | |
st.session_state.messages.append({"role": "bot", "content": f"Got it! Your self-esteem level is {self_esteem}. How would you describe your mental health history?"}) | |
elif len(st.session_state.messages) == 4: | |
st.session_state.user_data['mental_health_history'] = user_input | |
st.session_state.messages.append({"role": "bot", "content": "Thanks! Now, on a scale of 0 to 2, how would you rate your stress level?"}) | |
elif len(st.session_state.messages) == 5: | |
stress_level = int(user_input) | |
st.session_state.user_data['stress_level'] = stress_level | |
st.session_state.messages.append({"role": "bot", "content": "Thank you for providing the information! Let me get your personalized advice..."}) | |
# Get the advice from Groq API | |
advice = get_personalized_advice(st.session_state.user_data) | |
st.session_state.messages.append({"role": "bot", "content": advice or "I couldn't retrieve specific advice right now, but please check back later."}) | |
elif len(st.session_state.messages) == 6: | |
st.session_state.messages.append({"role": "bot", "content": "Would you like to start over?"}) | |
# Display conversation history | |
for message in st.session_state.messages: | |
if message['role'] == 'bot': | |
st.markdown(f"**Bot**: {message['content']}") | |
else: | |
st.markdown(f"**You**: {message['content']}") | |
# User input field for chat interaction | |
user_input = st.text_input("Your message:", "") | |
if user_input: | |
next_step(user_input) | |
# Option to restart the conversation | |
if len(st.session_state.messages) >= 6: | |
restart_button = st.button("Start Over") | |
if restart_button: | |
st.session_state.messages = [{"role": "bot", "content": "Hi there! I'm here to help with your well-being. Let's start! π"}] | |
# Main function to run the chatbot | |
def main(): | |
chatbot_interface() | |
if __name__ == "__main__": | |
main() | |