Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,23 +6,32 @@ import seaborn as sns
|
|
6 |
import requests
|
7 |
from googleapiclient.discovery import build
|
8 |
from google.oauth2.credentials import Credentials
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
@st.cache_data
|
14 |
def load_data(file):
|
15 |
return pd.read_csv(file)
|
16 |
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
def stress_level_to_string(stress_level):
|
28 |
"""Convert numerical stress level (0, 1, 2) to a string representation."""
|
@@ -33,28 +42,6 @@ def stress_level_to_string(stress_level):
|
|
33 |
else:
|
34 |
return "High"
|
35 |
|
36 |
-
def provide_advice_from_articles(data):
|
37 |
-
advice = []
|
38 |
-
stress_level = stress_level_to_string(data['stress_level'])
|
39 |
-
|
40 |
-
if stress_level == "High":
|
41 |
-
advice.append("Searching for articles related to high stress...")
|
42 |
-
articles = fetch_health_articles("high stress")
|
43 |
-
for article in articles:
|
44 |
-
advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
|
45 |
-
elif stress_level == "Moderate":
|
46 |
-
advice.append("Searching for articles related to moderate stress...")
|
47 |
-
articles = fetch_health_articles("moderate stress")
|
48 |
-
for article in articles:
|
49 |
-
advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
|
50 |
-
else:
|
51 |
-
advice.append("Searching for articles related to low stress...")
|
52 |
-
articles = fetch_health_articles("low stress")
|
53 |
-
for article in articles:
|
54 |
-
advice.append(f"**{article['title']}**\n{article['description']}\n[Read more]({article['url']})")
|
55 |
-
|
56 |
-
return advice
|
57 |
-
|
58 |
def plot_graphs(data):
|
59 |
"""Create subplots for visualization."""
|
60 |
st.markdown("### π Data Visualizations")
|
@@ -132,11 +119,10 @@ def main():
|
|
132 |
st.write("### Selected User Details:")
|
133 |
st.json(row_data)
|
134 |
|
135 |
-
st.subheader("π Health Advice Based on
|
136 |
-
advice =
|
137 |
if advice:
|
138 |
-
|
139 |
-
st.write(f"π **{i}.** {tip}")
|
140 |
else:
|
141 |
st.warning("No specific advice available based on this user's data.")
|
142 |
|
|
|
6 |
import requests
|
7 |
from googleapiclient.discovery import build
|
8 |
from google.oauth2.credentials import Credentials
|
9 |
+
from groq import Groq
|
10 |
|
11 |
+
# Groq API Key (replace with your actual API key)
|
12 |
+
groq_api_key = "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"
|
13 |
+
|
14 |
+
# Initialize Groq client
|
15 |
+
client = Groq(api_key=groq_api_key)
|
16 |
|
17 |
@st.cache_data
|
18 |
def load_data(file):
|
19 |
return pd.read_csv(file)
|
20 |
|
21 |
+
def get_personalized_advice(user_data):
|
22 |
+
"""Fetch personalized advice using the Groq API based on user data"""
|
23 |
+
# Construct the query for the Groq API
|
24 |
+
query = f"Based on the user's data: anxiety level: {user_data['anxiety_level']}, self esteem: {user_data['self_esteem']}, mental health history: {user_data['mental_health_history']}, stress level: {user_data['stress_level']}, provide personalized mental health advice."
|
25 |
+
|
26 |
+
try:
|
27 |
+
response = client.chat.completions.create(
|
28 |
+
messages=[{"role": "user", "content": query}],
|
29 |
+
model="llama-3.3-70b-versatile",
|
30 |
+
)
|
31 |
+
return response.choices[0].message.content
|
32 |
+
except Exception as e:
|
33 |
+
st.error(f"Error fetching advice from Groq: {str(e)}")
|
34 |
+
return None
|
35 |
|
36 |
def stress_level_to_string(stress_level):
|
37 |
"""Convert numerical stress level (0, 1, 2) to a string representation."""
|
|
|
42 |
else:
|
43 |
return "High"
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
def plot_graphs(data):
|
46 |
"""Create subplots for visualization."""
|
47 |
st.markdown("### π Data Visualizations")
|
|
|
119 |
st.write("### Selected User Details:")
|
120 |
st.json(row_data)
|
121 |
|
122 |
+
st.subheader("π Health Advice Based on Groq")
|
123 |
+
advice = get_personalized_advice(row_data)
|
124 |
if advice:
|
125 |
+
st.write(advice)
|
|
|
126 |
else:
|
127 |
st.warning("No specific advice available based on this user's data.")
|
128 |
|