saherPervaiz commited on
Commit
446e8da
·
verified ·
1 Parent(s): 76f6db0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -66
app.py CHANGED
@@ -2,83 +2,70 @@ import streamlit as st
2
  import pandas as pd
3
  import requests
4
 
5
- # Function to analyze stress level based on various factors
6
- def analyze_stress_level(df, inputs):
7
- filtered_df = df
8
- for column, value in inputs.items():
9
- if column in df.columns:
10
- filtered_df = filtered_df[filtered_df[column] == value]
11
 
12
- if not filtered_df.empty:
13
- return filtered_df.iloc[0]['stress_level']
14
- return "No matching data found."
15
-
16
- # Function to provide advice based on stress level
17
- def provide_advice(stress_level):
18
- if stress_level == "Low":
19
- return "Keep up the good work! Continue maintaining a balanced lifestyle."
20
- elif stress_level == "Moderate":
21
- return "Consider practicing relaxation techniques like yoga or meditation. Maintain healthy sleep and exercise routines."
22
- elif stress_level == "High":
23
- return "Seek support from counselors or health professionals. Share your concerns with trusted friends or family."
24
  else:
25
- return "No specific advice available."
 
26
 
27
- # Function to fetch related health articles from GROC API
28
- def get_health_documents_from_groc(query):
29
- api_key = "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" # Replace with your actual GROC API key
30
- url = "https://api.groc.com/v1/search"
31
- params = {
32
- "query": query,
33
- "api_key": api_key,
34
- "type": "article"
35
- }
36
- response = requests.get(url, params=params)
37
- if response.status_code == 200:
38
- return response.json().get("results", [])
39
- else:
40
- st.error(f"Error {response.status_code}: {response.text}")
41
  return []
42
 
43
- # Main Streamlit app
44
  def main():
45
- st.title("Student Stress Analysis and Health Advisory")
46
 
47
- # Upload dataset
48
- uploaded_file = st.file_uploader("Upload your dataset (CSV)", type="csv")
49
- if uploaded_file is not None:
50
- df = pd.read_csv(uploaded_file)
51
- st.write("Dataset Preview:")
52
  st.dataframe(df.head())
53
 
54
- # Dynamic input fields based on dataset columns
55
- inputs = {}
56
- for column in [
57
- 'anxiety_level', 'self_esteem', 'mental_health_history', 'depression',
58
- 'headache', 'blood_pressure', 'sleep_quality', 'breathing_problem',
59
- 'noise_level', 'living_conditions', 'safety', 'basic_needs',
60
- 'academic_performance', 'study_load', 'teacher_student_relationship',
61
- 'future_career_concerns', 'social_support', 'peer_pressure',
62
- 'extracurricular_activities', 'bullying'
63
- ]:
64
- if column in df.columns:
65
- inputs[column] = st.selectbox(f"Select {column.replace('_', ' ').title()}", df[column].unique())
66
-
67
- # Analyze stress level
68
- if st.button("Analyze Stress Level"):
69
- stress_level = analyze_stress_level(df, inputs)
70
- st.write(f"Stress Level: {stress_level}")
71
 
72
- # Provide advice
73
- advice = provide_advice(stress_level)
74
- st.write(f"Advice: {advice}")
 
 
 
 
75
 
76
- # Fetch related health articles
77
- query = f"Health advice for stress level: {stress_level}"
78
- articles = get_health_documents_from_groc(query)
79
- st.write("Related Health Articles:")
80
- for article in articles:
81
- st.markdown(f"- [{article['title']}]({article['url']})")
82
 
83
  if __name__ == "__main__":
84
  main()
 
2
  import pandas as pd
3
  import requests
4
 
5
+ # Function to load and preprocess data
6
+ @st.cache_data
7
+ def load_data(file):
8
+ df = pd.read_csv(file)
9
+ return df
 
10
 
11
+ # Function to provide advice based on input
12
+ def provide_advice(gender, age, depression, anxiety, isolation, future_insecurity, stress_relief_activities):
13
+ # Analyze mental health and stress levels
14
+ if depression > 7 or anxiety > 7 or isolation > 7:
15
+ advice = "Your mental health indicators are high. Consider consulting a mental health professional for personalized support."
16
+ elif future_insecurity > 5:
17
+ advice = "Concerns about future career prospects might be contributing to your stress. Try exploring career counseling or workshops."
18
+ elif stress_relief_activities < 5:
19
+ advice = "Engage in more stress-relief activities like physical exercise, hobbies, or mindfulness practices."
 
 
 
20
  else:
21
+ advice = "You're managing well, but continue healthy habits and seek support if needed."
22
+ return advice
23
 
24
+ # Mock GROC API function for fetching related articles
25
+ def get_health_articles(query):
26
+ try:
27
+ # Mock response (replace with actual GROC API call if available)
28
+ return [
29
+ {"title": "How to Manage Anxiety Effectively", "url": "https://example.com/anxiety-management"},
30
+ {"title": "Top Stress Relief Activities", "url": "https://example.com/stress-relief"}
31
+ ]
32
+ except Exception as e:
33
+ st.error(f"Error fetching articles: {e}")
 
 
 
 
34
  return []
35
 
36
+ # Streamlit app layout
37
  def main():
38
+ st.title("Student Health Advisory Assistant")
39
 
40
+ # File upload
41
+ uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
42
+ if uploaded_file:
43
+ df = load_data(uploaded_file)
 
44
  st.dataframe(df.head())
45
 
46
+ # User input section
47
+ st.header("Input Your Information")
48
+ gender = st.selectbox("Gender", df["gender"].unique())
49
+ age = st.slider("Age", 18, 35, step=1)
50
+ depression = st.slider("Depression Level (1-10)", 1, 10)
51
+ anxiety = st.slider("Anxiety Level (1-10)", 1, 10)
52
+ isolation = st.slider("Isolation Level (1-10)", 1, 10)
53
+ future_insecurity = st.slider("Future Insecurity Level (1-10)", 1, 10)
54
+ stress_relief_activities = st.slider("Stress Relief Activities Level (1-10)", 1, 10)
 
 
 
 
 
 
 
 
55
 
56
+ # Provide advice and fetch articles
57
+ if st.button("Get Advice"):
58
+ st.subheader("Health Advice")
59
+ advice = provide_advice(
60
+ gender, age, depression, anxiety, isolation, future_insecurity, stress_relief_activities
61
+ )
62
+ st.write(advice)
63
 
64
+ st.subheader("Related Health Articles")
65
+ articles = get_health_articles("mental health support")
66
+ if articles:
67
+ for article in articles:
68
+ st.write(f"- [{article['title']}]({article['url']})")
 
69
 
70
  if __name__ == "__main__":
71
  main()