Spaces:
Sleeping
Sleeping
import streamlit as st | |
import pandas as pd | |
import requests | |
# Function to load and preprocess data | |
def load_data(file): | |
df = pd.read_csv(file) | |
return df | |
# Function to provide detailed health advice | |
def provide_detailed_advice(data): | |
advice = [] | |
if data['depression'] > 7: | |
advice.append("You have high levels of depression. Consider consulting a mental health professional. Practice self-care activities like mindfulness, journaling, and physical exercise.") | |
elif data['depression'] > 5: | |
advice.append("Your depression levels are moderate. Engage in uplifting activities, maintain a healthy routine, and seek social support.") | |
if data['anxiety'] > 7: | |
advice.append("High levels of anxiety detected. Use breathing exercises, yoga, and reduce caffeine intake. Consulting a therapist is highly recommended.") | |
elif data['anxiety'] > 5: | |
advice.append("Moderate anxiety levels observed. Focus on managing stress, maintaining proper sleep, and practicing relaxation techniques.") | |
if data['isolation'] > 7: | |
advice.append("You might be feeling isolated. Try joining community groups, reaching out to friends, or participating in social activities.") | |
elif data['isolation'] > 5: | |
advice.append("Moderate isolation detected. Schedule regular social interactions to maintain mental wellness.") | |
if data['future_insecurity'] > 7: | |
advice.append("You have significant concerns about the future. Break large goals into smaller tasks, and consider career counseling or mentorship.") | |
elif data['future_insecurity'] > 5: | |
advice.append("Moderate future insecurity observed. Building a concrete plan and seeking guidance from trusted professionals can help.") | |
if data['stress_relief_activities'] < 5: | |
advice.append("Low engagement in stress-relief activities. Incorporate activities like walking, meditation, or pursuing hobbies into your routine.") | |
return advice | |
# Function to fetch health articles using the GROC API | |
def get_health_articles(query, api_key): | |
api_key = "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" | |
url = f"https://api.groc.com/search?q={query}" | |
headers = {"Authorization": f"Bearer {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 advice") | |
# GROC API key input | |
api_key = st.text_input("Enter your GROC API Key", type="password") | |
if not api_key: | |
st.warning("Please enter your GROC API Key to fetch related articles.") | |
return | |
# 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 | |
if st.button("Get Detailed Advice"): | |
st.subheader("Health Advice") | |
advice = provide_detailed_advice(user_data) | |
for i, tip in enumerate(advice, 1): | |
st.write(f"{i}. {tip}") | |
# Fetch articles from GROC API | |
st.subheader("Related Health Articles") | |
query = "mental health, stress relief, social well-being" | |
articles = get_health_articles(query, api_key) | |
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() | |