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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" # Replace with your Groq API key | |
# Initialize Groq client | |
client = Groq(api_key=groq_api_key) | |
# Function to send a request to the Groq API and fetch health advice | |
def get_health_advice_from_groq(anxiety_level, self_esteem, stress_level): | |
"""Fetch health advice from Groq API based on user input.""" | |
query = f"Provide personalized health advice based on the following data: anxiety level: {anxiety_level}, self-esteem: {self_esteem}, stress level: {stress_level}." | |
try: | |
# Request to Groq API | |
response = client.chat.completions.create( | |
messages=[{"role": "user", "content": query}], | |
model="llama-3.3-70b-versatile", # You can use the model that suits your needs | |
) | |
# Extract and return the health advice from the response | |
advice = response.choices[0].message.content | |
return advice | |
except Exception as e: | |
st.error(f"Error fetching advice from Groq: {e}") | |
return None | |
# Function to create the chatbot interface | |
def health_advice_chatbot(): | |
st.title("Health Advice Chatbot") | |
st.write("Welcome! I'm here to help you with some basic health advice based on your well-being.") | |
# User input | |
st.write("Please answer the following questions to receive personalized health advice.") | |
# Anxiety Level (Slider) | |
anxiety_level = st.slider("On a scale of 1 to 10, how would you rate your anxiety level?", 1, 10, 5) | |
# Self-esteem Level (Slider) | |
self_esteem = st.slider("On a scale of 1 to 10, how would you rate your self-esteem?", 1, 10, 5) | |
# Stress Level (Radio Buttons with "Low", "Moderate", "High") | |
stress_level = st.radio( | |
"How would you rate your stress level?", | |
["Low", "Moderate", "High"] | |
) | |
# Store user input in session state for persistent state across reruns | |
if 'user_data' not in st.session_state: | |
st.session_state.user_data = {} | |
st.session_state.user_data['anxiety_level'] = anxiety_level | |
st.session_state.user_data['self_esteem'] = self_esteem | |
st.session_state.user_data['stress_level'] = stress_level | |
# Submit button to get health advice | |
if st.button("Get Health Advice"): | |
# Fetch health advice from Groq API | |
advice = get_health_advice_from_groq( | |
st.session_state.user_data['anxiety_level'], | |
st.session_state.user_data['self_esteem'], | |
st.session_state.user_data['stress_level'] | |
) | |
if advice: | |
st.subheader("Here is your personalized health advice:") | |
st.write(advice) | |
# Option to reset form and restart | |
if st.button("Start Over"): | |
st.session_state.user_data = {} | |
st.rerun() | |
# Main function to run the chatbot | |
def main(): | |
health_advice_chatbot() | |
if __name__ == "__main__": | |
main() | |