import streamlit as st import pandas as pd import os import requests # Retrieve the GROC API Key from environment variable GROC_API_KEY = os.getenv("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") # Check if the API key is missing if not GROC_API_KEY: st.error("API key is missing. Please set the GROC_API_KEY environment variable.") else: # Function to load and preprocess data @st.cache_data def load_data(file): df = pd.read_csv(file) return df # Function to provide detailed health advice based on model observation def provide_observed_advice(data): advice = [] # High depression, anxiety, and low stress-relief activities may indicate a need for professional help if data['depression'] > 7 and data['anxiety'] > 7: advice.append("You seem to be experiencing high levels of both depression and anxiety. It is important to consider professional mental health support, such as therapy or counseling. Engage in calming activities like deep breathing, mindfulness, and yoga.") # Moderate depression and anxiety levels can indicate stress but still manageable elif data['depression'] > 5 or data['anxiety'] > 5: advice.append("You are showing moderate levels of depression and/or anxiety. Focus on developing healthy coping strategies such as maintaining a regular sleep schedule, engaging in physical activity, and reaching out to friends or family for support.") # High isolation combined with low engagement in stress-relief activities could suggest loneliness if data['isolation'] > 7 and data['stress_relief_activities'] < 5: advice.append("It seems that you're feeling isolated, and your engagement in stress-relief activities is low. Try connecting with friends or a community group, and incorporate activities that help alleviate stress, such as walking, meditation, or journaling.") # If future insecurity is high, career counseling might be helpful if data['future_insecurity'] > 7: advice.append("You are feeling a significant amount of insecurity about the future. It might be helpful to break down your larger goals into smaller, manageable tasks. Seeking career counseling or mentorship could provide valuable guidance.") # Overall, low levels of stress-relief activities are a concern if data['stress_relief_activities'] < 5: advice.append("Your engagement in stress-relief activities is quite low. It is crucial to engage in activities that reduce stress and promote mental wellness, such as hobbies, physical exercise, or relaxation techniques.") return advice # Function to fetch health articles from the GROC API based on the query def get_health_articles(query): url = f"https://api.groc.com/search?q={query}" headers = {"Authorization": f"Bearer {GROC_API_KEY}"} try: response = requests.get(url, headers=headers) response.raise_for_status() data = response.json() # Assuming the API returns JSON articles = [{"title": item["title"], "url": item["url"]} for item in data.get("results", [])] return articles except requests.exceptions.RequestException as e: st.error(f"Error fetching articles: {e}") return [] # Streamlit app layout def main(): st.title("Student Health Advisory Assistant") st.subheader("Analyze your well-being and get personalized advice") # File upload uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"]) if uploaded_file: df = load_data(uploaded_file) st.write("Dataset preview:") st.dataframe(df.head()) # User input for analysis st.header("Input Your Details") gender = st.selectbox("Gender", ["Male", "Female"]) age = st.slider("Age", 18, 35, step=1) depression = st.slider("Depression Level (1-10)", 1, 10) anxiety = st.slider("Anxiety Level (1-10)", 1, 10) isolation = st.slider("Isolation Level (1-10)", 1, 10) future_insecurity = st.slider("Future Insecurity Level (1-10)", 1, 10) stress_relief_activities = st.slider("Stress Relief Activities Level (1-10)", 1, 10) # Data dictionary for advice user_data = { "gender": gender, "age": age, "depression": depression, "anxiety": anxiety, "isolation": isolation, "future_insecurity": future_insecurity, "stress_relief_activities": stress_relief_activities, } # Provide advice based on model observations if st.button("Get Observed Advice"): st.subheader("Health Advice Based on Observations") advice = provide_observed_advice(user_data) for i, tip in enumerate(advice, 1): st.write(f"{i}. {tip}") # Fetch related health articles based on user input st.subheader("Related Health Articles") query = f"mental health anxiety depression isolation stress relief" articles = get_health_articles(query) if articles: for article in articles: st.write(f"- [{article['title']}]({article['url']})") else: st.write("No articles found. Please check your API key or internet connection.") if __name__ == "__main__": main()