saherPervaiz commited on
Commit
43471b3
·
verified ·
1 Parent(s): dac5058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
 
4
 
5
  # Set the GROC API Key directly in the code
6
- GROC_API_KEY = "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" # Replace with your actual GROC API key
7
 
8
  # Function to load and preprocess data
9
  @st.cache_data
@@ -35,8 +36,7 @@ def provide_observed_advice(data):
35
  if data['stress_relief_activities'] < 5:
36
  advice.append("Your engagement in stress-relief activities is quite low. It's essential to engage in activities that reduce stress and promote mental wellness, such as hobbies, physical exercise, and relaxation techniques like deep breathing or yoga.")
37
 
38
- # Ensure we return a list, even if it's empty
39
- return advice if advice else ["No advice based on your inputs. Please consider consulting a professional."]
40
 
41
  # Function to fetch health articles from the GROC API based on the query
42
  def get_health_articles(query):
@@ -45,12 +45,18 @@ def get_health_articles(query):
45
 
46
  try:
47
  response = requests.get(url, headers=headers)
48
- response.raise_for_status() # Check for any HTTP errors
49
  data = response.json() # Assuming the API returns JSON
50
- articles = [{"title": item["title"], "url": item["url"]} for item in data.get("results", [])]
 
 
 
51
  return articles
52
- except requests.exceptions.RequestException as e:
53
- st.error(f"Error fetching articles: {e}")
 
 
 
54
  return []
55
 
56
  # Streamlit app layout
 
1
  import streamlit as st
2
  import pandas as pd
3
  import requests
4
+ import os
5
 
6
  # Set the GROC API Key directly in the code
7
+ GROC_API_KEY = os.getenv("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") # Make sure your API key is stored in the environment variable
8
 
9
  # Function to load and preprocess data
10
  @st.cache_data
 
36
  if data['stress_relief_activities'] < 5:
37
  advice.append("Your engagement in stress-relief activities is quite low. It's essential to engage in activities that reduce stress and promote mental wellness, such as hobbies, physical exercise, and relaxation techniques like deep breathing or yoga.")
38
 
39
+ return advice
 
40
 
41
  # Function to fetch health articles from the GROC API based on the query
42
  def get_health_articles(query):
 
45
 
46
  try:
47
  response = requests.get(url, headers=headers)
48
+ response.raise_for_status() # This will raise an HTTPError for bad responses
49
  data = response.json() # Assuming the API returns JSON
50
+ if 'results' in data:
51
+ articles = [{"title": item["title"], "url": item["url"]} for item in data['results']]
52
+ else:
53
+ articles = []
54
  return articles
55
+ except requests.exceptions.HTTPError as http_err:
56
+ st.error(f"HTTP error occurred: {http_err}")
57
+ return []
58
+ except requests.exceptions.RequestException as err:
59
+ st.error(f"Error fetching articles: {err}")
60
  return []
61
 
62
  # Streamlit app layout