Spaces:
Sleeping
Sleeping
import os | |
import streamlit as st | |
import pandas as pd | |
import requests | |
# 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 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 are experiencing high levels of both depression and anxiety. Seek professional support and try calming activities like mindfulness or yoga." | |
) | |
if data['depression'] > 5 or data['anxiety'] > 5: | |
advice.append( | |
"Moderate levels of depression or anxiety detected. Regular exercise, proper sleep, and connecting with loved ones may help." | |
) | |
if data['isolation'] > 7 and data['stress_relief_activities'] < 5: | |
advice.append( | |
"You may feel isolated and have low engagement in stress-relief activities. Connecting with community groups or engaging in a hobby might help." | |
) | |
if data['future_insecurity'] > 7: | |
advice.append( | |
"High insecurity about the future detected. Consider breaking down your goals into smaller, manageable tasks and seeking guidance from a mentor." | |
) | |
if data['stress_relief_activities'] < 5: | |
advice.append( | |
"Low stress-relief activity levels. Try physical activities or relaxation techniques like deep breathing or journaling." | |
) | |
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 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( | |
""" | |
**Features:** | |
- Analyze your mental well-being through uploaded datasets. | |
- Get personalized advice based on your inputs. | |
- Explore the latest health-related articles for guidance. | |
""" | |
) | |
# 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()) | |
st.markdown("### Select a Row for Analysis") | |
selected_row = st.selectbox( | |
"Select a row (based on index) to analyze:", | |
options=df.index, | |
format_func=lambda x: f"Row {x} - Age: {df.loc[x, 'age']}, Gender: {df.loc[x, 'gender']}", | |
) | |
# Extract data for the selected row | |
row_data = df.loc[selected_row].to_dict() | |
# Show extracted details | |
st.write("### Selected User Details:") | |
st.json(row_data) | |
# Generate advice | |
st.subheader("π Health Advice Based on Observations") | |
advice = provide_observed_advice(row_data) | |
if advice: | |
for i, tip in enumerate(advice, 1): | |
st.write(f"π **{i}.** {tip}") | |
else: | |
st.warning("No specific advice available based on this user's data.") | |
# Health Articles | |
elif option == "Health Articles": | |
st.title("π° Browse Health Articles") | |
st.markdown("Get the latest updates and tips on mental health and well-being.") | |
query = st.text_input("Search for health topics (e.g., anxiety, stress relief)", value="mental health") | |
if st.button("Search Articles"): | |
articles = get_health_articles(query) | |
if articles: | |
st.write("### Found Articles:") | |
for article in articles: | |
st.markdown(f"π [{article['title']}]({article['url']})") | |
else: | |
st.warning("No articles found for the given topic. Try a different query.") | |
if __name__ == "__main__": | |
main() | |