Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
import pandas as pd | |
import requests | |
import urllib.parse | |
# Fetch the News API key from the environment variable | |
news_api_key = "fe1e6bcbbf384b3e9220a7a1138805e0" # Replace with your News API key | |
# Check if the API key is available, if not show an error and stop | |
if not news_api_key: | |
st.error("NEWS_API_KEY is not set. Please provide a valid API key.") | |
st.stop() | |
# Function to load and preprocess data | |
def load_data(file): | |
df = pd.read_csv(file) | |
return df | |
# Function to provide detailed health advice based on user data | |
def provide_observed_advice(data): | |
advice = [] | |
if data['depression'] > 7 and data['anxiety'] > 7: | |
advice.append( | |
"You seem to be experiencing high levels of both depression and anxiety. Consider professional mental health support and calming activities like deep breathing, mindfulness, or yoga." | |
) | |
if data['depression'] > 5 or data['anxiety'] > 5: | |
advice.append( | |
"Moderate levels of depression or anxiety detected. Maintain a regular sleep schedule, engage in physical activity, and reach out for support." | |
) | |
if data['isolation'] > 7 and data['stress_relief_activities'] < 5: | |
advice.append( | |
"Feeling isolated with low stress-relief activities. Connect with friends or community groups and try journaling or meditation." | |
) | |
if data['future_insecurity'] > 7: | |
advice.append( | |
"Significant insecurity about the future detected. Break down goals into smaller tasks, and consider career counseling or mentorship." | |
) | |
if data['stress_relief_activities'] < 5: | |
advice.append( | |
"Low engagement in stress-relief activities. Try hobbies, physical exercise, or relaxation techniques like yoga." | |
) | |
return advice | |
# Function to fetch health articles from News API based on the query | |
def get_health_articles(query): | |
url = f"https://newsapi.org/v2/everything?q={query}&apiKey={news_api_key}" | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
data = response.json() | |
articles = [{"title": item["title"], "url": item["url"]} for item in data.get("articles", [])] | |
return articles | |
except requests.exceptions.RequestException as err: | |
st.error(f"Error fetching articles: {err}. Please check your internet connection.") | |
return [] | |
# Streamlit app layout | |
def main(): | |
st.set_page_config(page_title="Student Health Advisory Assistant", layout="wide") | |
# Sidebar for navigation | |
with st.sidebar: | |
st.header("Navigation") | |
option = st.radio("Go to", ["Home", "Analyze Your Well-being", "Health Articles"]) | |
# Home page | |
if option == "Home": | |
st.title("π Welcome to the Student Health Advisory Assistant π") | |
st.image("https://via.placeholder.com/800x300?text=Student+Health+Advisory", use_column_width=True) | |
st.markdown("### Helping you analyze your well-being and provide personalized advice for a healthier mind.") | |
st.markdown( | |
""" | |
- Upload your dataset for in-depth analysis. | |
- Input your details to receive personalized advice. | |
- Browse the latest health-related articles. | |
""" | |
) | |
# Well-being analysis | |
elif option == "Analyze Your Well-being": | |
st.title("π Analyze Your Well-being") | |
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()) | |
with st.expander("Enter 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) | |
user_data = { | |
"gender": gender, | |
"age": age, | |
"depression": depression, | |
"anxiety": anxiety, | |
"isolation": isolation, | |
"future_insecurity": future_insecurity, | |
"stress_relief_activities": stress_relief_activities, | |
} | |
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}") | |
# Health articles | |
elif option == "Health Articles": | |
st.title("π° Browse Health Articles") | |
query = st.text_input("Search for health topics (e.g., anxiety, stress relief)") | |
if query and st.button("Search Articles"): | |
articles = get_health_articles(query) | |
for article in articles: | |
st.write(f"π [{article['title']}]({article['url']})") | |
if __name__ == "__main__": | |
main() | |