saherPervaiz commited on
Commit
982b8ff
Β·
verified Β·
1 Parent(s): a4b12c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -41
app.py CHANGED
@@ -5,7 +5,8 @@ import matplotlib.pyplot as plt
5
  import seaborn as sns
6
  import requests
7
 
8
- news_api_key = "fe1e6bcbbf384b3e9220a7a1138805e0" # Replace with your News API key
 
9
 
10
  @st.cache_data
11
  def load_data(file):
@@ -21,50 +22,42 @@ def fetch_health_articles(query):
21
  st.error("Failed to fetch news articles. Please check your API key or try again later.")
22
  return []
23
 
24
- def provide_advice_from_selected_row(data):
 
 
 
 
 
 
 
 
 
25
  advice = []
 
26
 
27
- stress_level = data['stress_level']
28
- anxiety_level = data['anxiety_level']
29
- depression_level = data['depression']
30
-
31
- # Generate advice based on the stress level of the selected row
32
- if stress_level > 7:
33
- advice.append("πŸ”΄ **High Stress Levels Detected**: It's critical to manage stress effectively. Please explore the following articles to help you manage stress:")
34
- articles = fetch_health_articles("high stress management")
35
  for article in articles:
36
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
37
- elif stress_level > 4:
38
- advice.append("🟑 **Moderate Stress Levels**: It's important to manage moderate stress. Here are some helpful articles and tips:")
39
- articles = fetch_health_articles("moderate stress management")
40
  for article in articles:
41
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
42
  else:
43
- advice.append("🟒 **Low Stress Levels**: Great! You're in a good place, but it's still important to maintain healthy stress management. Here are some resources to help:")
44
- articles = fetch_health_articles("stress relief techniques")
45
- for article in articles:
46
- advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
47
-
48
- # Additional advice based on anxiety and depression levels
49
- if anxiety_level > 7:
50
- advice.append("\nπŸ”΄ **High Anxiety Levels**: Managing anxiety is crucial. Here are some resources to help manage anxiety:")
51
- articles = fetch_health_articles("high anxiety management")
52
  for article in articles:
53
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
54
 
55
- if depression_level > 7:
56
- advice.append("\nπŸ”΄ **High Depression Levels**: It's important to seek professional support for depression. Here are some helpful resources:")
57
- articles = fetch_health_articles("high depression management")
58
- for article in articles:
59
- advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
60
-
61
  return advice
62
 
63
  def plot_graphs(data):
64
- # Create subplots for visualization
65
  st.markdown("### πŸ“Š Data Visualizations")
66
  st.write("Explore key insights through visualizations.")
67
-
68
  # Correlation heatmap
69
  st.markdown("#### Correlation Heatmap")
70
  fig, ax = plt.subplots(figsize=(10, 8))
@@ -74,8 +67,8 @@ def plot_graphs(data):
74
 
75
  def main():
76
  st.set_page_config(
77
- page_title="Stress Management Advisor",
78
- page_icon="🧠",
79
  layout="wide",
80
  initial_sidebar_state="expanded",
81
  )
@@ -87,10 +80,10 @@ def main():
87
  st.sidebar.markdown("### πŸ“Š Analysis & Advice")
88
  st.sidebar.write("Get detailed insights and personalized advice.")
89
 
90
- st.title("πŸŽ“ Stress Management Advisor")
91
- st.subheader("Analyze stress levels and receive professional stress management recommendations.")
92
  st.write("""
93
- This app helps identify areas of concern related to stress in individuals and provides personalized advice based on their responses.
94
  """)
95
 
96
  st.markdown("## πŸ“‚ Upload Your Dataset")
@@ -121,7 +114,7 @@ def main():
121
  tab1, tab2, tab3 = st.tabs(["🏠 Home", "πŸ“Š Analysis", "πŸ“° Resources"])
122
 
123
  with tab1:
124
- st.write("### Welcome to the Stress Management Advisor!")
125
  st.write("""
126
  Use the tabs to explore data, generate advice, and access mental health resources.
127
  """)
@@ -131,26 +124,26 @@ def main():
131
  selected_row = st.selectbox(
132
  "Select a row (based on index) to analyze:",
133
  options=df.index,
134
- format_func=lambda x: f"Row {x} - Stress Level: {df.loc[x, 'stress_level']}, Anxiety: {df.loc[x, 'anxiety_level']} (Depression: {df.loc[x, 'depression']})",
135
  )
136
  row_data = df.loc[selected_row].to_dict()
137
  st.write("### Selected User Details:")
138
  st.json(row_data)
139
 
140
- st.subheader("πŸ”” Health Advice Based on Selected Row")
141
- advice = provide_advice_from_selected_row(row_data)
142
  if advice:
143
  for i, tip in enumerate(advice, 1):
144
  st.write(f"πŸ“Œ **{i}.** {tip}")
145
  else:
146
- st.warning("No specific advice available based on this user's stress level.")
147
 
148
  # Include graphs in analysis tab
149
  plot_graphs(df)
150
 
151
  with tab3:
152
  st.subheader("πŸ“° Mental Health Resources")
153
- articles = fetch_health_articles("stress relief")
154
  if articles:
155
  for article in articles:
156
  st.write(f"**{article['title']}**")
 
5
  import seaborn as sns
6
  import requests
7
 
8
+ # News API Key
9
+ news_api_key = "your_news_api_key" # Replace with your News API key
10
 
11
  @st.cache_data
12
  def load_data(file):
 
22
  st.error("Failed to fetch news articles. Please check your API key or try again later.")
23
  return []
24
 
25
+ def stress_level_to_string(stress_level):
26
+ """Convert numerical stress level (0, 1, 2) to a string representation."""
27
+ if stress_level == 0:
28
+ return "Low"
29
+ elif stress_level == 1:
30
+ return "Moderate"
31
+ else:
32
+ return "High"
33
+
34
+ def provide_advice_from_articles(data):
35
  advice = []
36
+ stress_level = stress_level_to_string(data['stress_level'])
37
 
38
+ if stress_level == "High":
39
+ advice.append("Searching for articles related to high stress...")
40
+ articles = fetch_health_articles("high stress")
 
 
 
 
 
41
  for article in articles:
42
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
43
+ elif stress_level == "Moderate":
44
+ advice.append("Searching for articles related to moderate stress...")
45
+ articles = fetch_health_articles("moderate stress")
46
  for article in articles:
47
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
48
  else:
49
+ advice.append("Searching for articles related to low stress...")
50
+ articles = fetch_health_articles("low stress")
 
 
 
 
 
 
 
51
  for article in articles:
52
  advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
53
 
 
 
 
 
 
 
54
  return advice
55
 
56
  def plot_graphs(data):
57
+ """Create subplots for visualization."""
58
  st.markdown("### πŸ“Š Data Visualizations")
59
  st.write("Explore key insights through visualizations.")
60
+
61
  # Correlation heatmap
62
  st.markdown("#### Correlation Heatmap")
63
  fig, ax = plt.subplots(figsize=(10, 8))
 
67
 
68
  def main():
69
  st.set_page_config(
70
+ page_title="Student Well-being Advisor",
71
+ page_icon="πŸ“Š",
72
  layout="wide",
73
  initial_sidebar_state="expanded",
74
  )
 
80
  st.sidebar.markdown("### πŸ“Š Analysis & Advice")
81
  st.sidebar.write("Get detailed insights and personalized advice.")
82
 
83
+ st.title("πŸŽ“ Student Well-being Advisor")
84
+ st.subheader("Analyze data and provide professional mental health recommendations.")
85
  st.write("""
86
+ This app helps identify areas of concern in students' well-being and provides personalized advice based on their responses.
87
  """)
88
 
89
  st.markdown("## πŸ“‚ Upload Your Dataset")
 
114
  tab1, tab2, tab3 = st.tabs(["🏠 Home", "πŸ“Š Analysis", "πŸ“° Resources"])
115
 
116
  with tab1:
117
+ st.write("### Welcome to the Well-being Advisor!")
118
  st.write("""
119
  Use the tabs to explore data, generate advice, and access mental health resources.
120
  """)
 
124
  selected_row = st.selectbox(
125
  "Select a row (based on index) to analyze:",
126
  options=df.index,
127
+ format_func=lambda x: f"Row {x} - Stress Level: {stress_level_to_string(df.loc[x, 'stress_level'])}, Anxiety: {df.loc[x, 'anxiety_level']} (Depression: {df.loc[x, 'depression']})",
128
  )
129
  row_data = df.loc[selected_row].to_dict()
130
  st.write("### Selected User Details:")
131
  st.json(row_data)
132
 
133
+ st.subheader("πŸ”” Health Advice Based on Articles")
134
+ advice = provide_advice_from_articles(row_data)
135
  if advice:
136
  for i, tip in enumerate(advice, 1):
137
  st.write(f"πŸ“Œ **{i}.** {tip}")
138
  else:
139
+ st.warning("No specific advice available based on this user's data.")
140
 
141
  # Include graphs in analysis tab
142
  plot_graphs(df)
143
 
144
  with tab3:
145
  st.subheader("πŸ“° Mental Health Resources")
146
+ articles = fetch_health_articles("mental health")
147
  if articles:
148
  for article in articles:
149
  st.write(f"**{article['title']}**")