saherPervaiz commited on
Commit
5117e78
·
verified ·
1 Parent(s): c4360f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -109
app.py CHANGED
@@ -1,112 +1,114 @@
1
  import streamlit as st
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 detailed health advice
12
- def provide_detailed_advice(data):
13
- advice = []
14
-
15
- if data['depression'] > 7:
16
- advice.append("You have high levels of depression. Consider consulting a mental health professional. Practice self-care activities like mindfulness, journaling, and physical exercise.")
17
- elif data['depression'] > 5:
18
- advice.append("Your depression levels are moderate. Engage in uplifting activities, maintain a healthy routine, and seek social support.")
19
-
20
- if data['anxiety'] > 7:
21
- advice.append("High levels of anxiety detected. Use breathing exercises, yoga, and reduce caffeine intake. Consulting a therapist is highly recommended.")
22
- elif data['anxiety'] > 5:
23
- advice.append("Moderate anxiety levels observed. Focus on managing stress, maintaining proper sleep, and practicing relaxation techniques.")
24
-
25
- if data['isolation'] > 7:
26
- advice.append("You might be feeling isolated. Try joining community groups, reaching out to friends, or participating in social activities.")
27
- elif data['isolation'] > 5:
28
- advice.append("Moderate isolation detected. Schedule regular social interactions to maintain mental wellness.")
29
-
30
- if data['future_insecurity'] > 7:
31
- advice.append("You have significant concerns about the future. Break large goals into smaller tasks, and consider career counseling or mentorship.")
32
- elif data['future_insecurity'] > 5:
33
- advice.append("Moderate future insecurity observed. Building a concrete plan and seeking guidance from trusted professionals can help.")
34
-
35
- if data['stress_relief_activities'] < 5:
36
- advice.append("Low engagement in stress-relief activities. Incorporate activities like walking, meditation, or pursuing hobbies into your routine.")
37
-
38
- return advice
39
-
40
- # Function to fetch health articles using the GROC API
41
- def get_health_articles(query, api_key):
42
- url = f"https://api.groc.com/search?q={query}"
43
- headers = {"Authorization": f"Bearer {api_key}"}
44
-
45
- try:
46
- response = requests.get(url, headers=headers)
47
- response.raise_for_status()
48
- data = response.json() # Assuming the API returns JSON
49
- articles = [{"title": item["title"], "url": item["url"]} for item in data.get("results", [])]
50
- return articles
51
- except requests.exceptions.RequestException as e:
52
- st.error(f"Error fetching articles: {e}")
53
- return []
54
-
55
- # Streamlit app layout
56
- def main():
57
- st.title("Student Health Advisory Assistant")
58
- st.subheader("Analyze your well-being and get advice")
59
-
60
- # GROC API key input
61
- api_key = st.text_input("Enter your GROC API Key", type="password")
62
- if not api_key:
63
- st.warning("Please enter your GROC API Key to fetch related articles.")
64
- return
65
-
66
- # File upload
67
- uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
68
- if uploaded_file:
69
- df = load_data(uploaded_file)
70
- st.write("Dataset preview:")
71
- st.dataframe(df.head())
72
-
73
- # User input for analysis
74
- st.header("Input Your Details")
75
- gender = st.selectbox("Gender", ["Male", "Female"])
76
- age = st.slider("Age", 18, 35, step=1)
77
- depression = st.slider("Depression Level (1-10)", 1, 10)
78
- anxiety = st.slider("Anxiety Level (1-10)", 1, 10)
79
- isolation = st.slider("Isolation Level (1-10)", 1, 10)
80
- future_insecurity = st.slider("Future Insecurity Level (1-10)", 1, 10)
81
- stress_relief_activities = st.slider("Stress Relief Activities Level (1-10)", 1, 10)
82
-
83
- # Data dictionary for advice
84
- user_data = {
85
- "gender": gender,
86
- "age": age,
87
- "depression": depression,
88
- "anxiety": anxiety,
89
- "isolation": isolation,
90
- "future_insecurity": future_insecurity,
91
- "stress_relief_activities": stress_relief_activities,
92
- }
93
-
94
- # Provide advice
95
- if st.button("Get Detailed Advice"):
96
- st.subheader("Health Advice")
97
- advice = provide_detailed_advice(user_data)
98
- for i, tip in enumerate(advice, 1):
99
- st.write(f"{i}. {tip}")
100
-
101
- # Fetch articles from GROC API
102
- st.subheader("Related Health Articles")
103
- query = "mental health, stress relief, social well-being"
104
- articles = get_health_articles(query, api_key)
105
- if articles:
106
- for article in articles:
107
- st.write(f"- [{article['title']}]({article['url']})")
108
- else:
109
- st.write("No articles found. Please check your API key or internet connection.")
110
-
111
- if __name__ == "__main__":
112
- main()
 
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
+ import os
5
+
6
+ # Retrieve the GROC API Key from environment variable
7
+ GROC_API_KEY = "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"
8
+
9
+ # Check if the API key is missing
10
+ if not GROC_API_KEY:
11
+ st.error("API key is missing. Please set the GROC_API_KEY environment variable.")
12
+ else:
13
+ # Function to load and preprocess data
14
+ @st.cache_data
15
+ def load_data(file):
16
+ df = pd.read_csv(file)
17
+ return df
18
+
19
+ # Function to provide detailed health advice
20
+ def provide_detailed_advice(data):
21
+ advice = []
22
+
23
+ if data['depression'] > 7:
24
+ advice.append("You have high levels of depression. Consider consulting a mental health professional. Practice self-care activities like mindfulness, journaling, and physical exercise.")
25
+ elif data['depression'] > 5:
26
+ advice.append("Your depression levels are moderate. Engage in uplifting activities, maintain a healthy routine, and seek social support.")
27
+
28
+ if data['anxiety'] > 7:
29
+ advice.append("High levels of anxiety detected. Use breathing exercises, yoga, and reduce caffeine intake. Consulting a therapist is highly recommended.")
30
+ elif data['anxiety'] > 5:
31
+ advice.append("Moderate anxiety levels observed. Focus on managing stress, maintaining proper sleep, and practicing relaxation techniques.")
32
+
33
+ if data['isolation'] > 7:
34
+ advice.append("You might be feeling isolated. Try joining community groups, reaching out to friends, or participating in social activities.")
35
+ elif data['isolation'] > 5:
36
+ advice.append("Moderate isolation detected. Schedule regular social interactions to maintain mental wellness.")
37
+
38
+ if data['future_insecurity'] > 7:
39
+ advice.append("You have significant concerns about the future. Break large goals into smaller tasks, and consider career counseling or mentorship.")
40
+ elif data['future_insecurity'] > 5:
41
+ advice.append("Moderate future insecurity observed. Building a concrete plan and seeking guidance from trusted professionals can help.")
42
+
43
+ if data['stress_relief_activities'] < 5:
44
+ advice.append("Low engagement in stress-relief activities. Incorporate activities like walking, meditation, or pursuing hobbies into your routine.")
45
+
46
+ return advice
47
+
48
+ # Function to fetch health articles using the GROC API
49
+ def get_health_articles(query):
50
+ url = f"https://api.groc.com/search?q={query}"
51
+ headers = {"Authorization": f"Bearer {GROC_API_KEY}"}
52
+
53
+ try:
54
+ response = requests.get(url, headers=headers)
55
+ response.raise_for_status()
56
+ data = response.json() # Assuming the API returns JSON
57
+ articles = [{"title": item["title"], "url": item["url"]} for item in data.get("results", [])]
58
+ return articles
59
+ except requests.exceptions.RequestException as e:
60
+ st.error(f"Error fetching articles: {e}")
61
+ return []
62
+
63
+ # Streamlit app layout
64
+ def main():
65
+ st.title("Student Health Advisory Assistant")
66
+ st.subheader("Analyze your well-being and get advice")
67
+
68
+ # File upload
69
+ uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
70
+ if uploaded_file:
71
+ df = load_data(uploaded_file)
72
+ st.write("Dataset preview:")
73
+ st.dataframe(df.head())
74
+
75
+ # User input for analysis
76
+ st.header("Input Your Details")
77
+ gender = st.selectbox("Gender", ["Male", "Female"])
78
+ age = st.slider("Age", 18, 35, step=1)
79
+ depression = st.slider("Depression Level (1-10)", 1, 10)
80
+ anxiety = st.slider("Anxiety Level (1-10)", 1, 10)
81
+ isolation = st.slider("Isolation Level (1-10)", 1, 10)
82
+ future_insecurity = st.slider("Future Insecurity Level (1-10)", 1, 10)
83
+ stress_relief_activities = st.slider("Stress Relief Activities Level (1-10)", 1, 10)
84
+
85
+ # Data dictionary for advice
86
+ user_data = {
87
+ "gender": gender,
88
+ "age": age,
89
+ "depression": depression,
90
+ "anxiety": anxiety,
91
+ "isolation": isolation,
92
+ "future_insecurity": future_insecurity,
93
+ "stress_relief_activities": stress_relief_activities,
94
+ }
95
+
96
+ # Provide advice
97
+ if st.button("Get Detailed Advice"):
98
+ st.subheader("Health Advice")
99
+ advice = provide_detailed_advice(user_data)
100
+ for i, tip in enumerate(advice, 1):
101
+ st.write(f"{i}. {tip}")
102
+
103
+ # Fetch articles from GROC API
104
+ st.subheader("Related Health Articles")
105
+ query = "mental health, stress relief, social well-being"
106
+ articles = get_health_articles(query)
107
+ if articles:
108
+ for article in articles:
109
+ st.write(f"- [{article['title']}]({article['url']})")
110
+ else:
111
+ st.write("No articles found. Please check your API key or internet connection.")
112
+
113
+ if __name__ == "__main__":
114
+ main()