saherPervaiz commited on
Commit
3da247b
·
verified ·
1 Parent(s): aafbea1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -1,10 +1,14 @@
1
  import streamlit as st
2
  import pandas as pd
3
- import requests
4
  from groq import Groq
 
5
 
6
  # Set the API key directly or from environment variable
7
- api_key = "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" # Replace with your actual demo API key
 
 
 
8
 
9
  # Function to load and preprocess data
10
  @st.cache_data
@@ -44,13 +48,14 @@ def get_health_articles(query):
44
  headers = {"Authorization": f"Bearer {api_key}"} # Use the demo API key in the header
45
 
46
  try:
47
- response = requests.get(url, headers=headers, timeout=10) # Timeout after 10 seconds
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
- return [{"title": item["title"], "url": item["url"]} for item in data['results']]
52
  else:
53
- return []
 
54
  except requests.exceptions.HTTPError as http_err:
55
  st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
56
  st.error(f"Response content: {response.text}")
 
1
  import streamlit as st
2
  import pandas as pd
3
+ import os
4
  from groq import Groq
5
+ import requests
6
 
7
  # Set the API key directly or from environment variable
8
+ api_key = os.getenv("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") # Ensure your API key is stored in the environment variable.
9
+
10
+ # Initialize the Groq client with the API key
11
+ client = Groq(api_key=api_key)
12
 
13
  # Function to load and preprocess data
14
  @st.cache_data
 
48
  headers = {"Authorization": f"Bearer {api_key}"} # Use the demo API key in the header
49
 
50
  try:
51
+ response = requests.get(url, headers=headers)
52
  response.raise_for_status() # This will raise an HTTPError for bad responses
53
  data = response.json() # Assuming the API returns JSON
54
  if 'results' in data:
55
+ articles = [{"title": item["title"], "url": item["url"]} for item in data['results']]
56
  else:
57
+ articles = []
58
+ return articles
59
  except requests.exceptions.HTTPError as http_err:
60
  st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
61
  st.error(f"Response content: {response.text}")