saherPervaiz commited on
Commit
c4587cb
Β·
verified Β·
1 Parent(s): fe3261d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -37
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
- # News API Key
11
- news_api_key = "fe1e6bcbbf384b3e9220a7a1138805e0" # Replace with your News API key
 
 
 
12
 
13
  @st.cache_data
14
  def load_data(file):
15
  return pd.read_csv(file)
16
 
17
- def fetch_health_articles(query):
18
- url = f"https://newsapi.org/v2/everything?q={query}&apiKey={news_api_key}"
19
- response = requests.get(url)
20
- if response.status_code == 200:
21
- articles = response.json().get('articles', [])
22
- return articles[:5]
23
- else:
24
- st.error("Failed to fetch news articles. Please check your API key or try again later.")
25
- return []
 
 
 
 
 
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 Articles")
136
- advice = provide_advice_from_articles(row_data)
137
  if advice:
138
- for i, tip in enumerate(advice, 1):
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