Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,20 @@
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
|
|
4 |
from groq import Groq
|
5 |
-
from dotenv import load_dotenv
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
# Get the API key from environment variable or set directly
|
11 |
-
groq_api_key = os.getenv("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") # First try from environment variable
|
12 |
-
if not groq_api_key:
|
13 |
-
# Optionally, set the API key directly for testing
|
14 |
-
groq_api_key = "your_actual_groq_api_key_here"
|
15 |
|
|
|
16 |
if not groq_api_key:
|
17 |
st.error("GROQ_API_KEY is not set. Please provide a valid API key.")
|
18 |
st.stop()
|
19 |
|
20 |
-
# Initialize the GROQ client
|
21 |
groq_client = Groq(api_key=groq_api_key)
|
22 |
|
23 |
-
if not groq_client:
|
24 |
-
st.error("Failed to initialize the GROQ client. Please check your API key.")
|
25 |
-
st.stop()
|
26 |
-
|
27 |
# Function to load and preprocess data
|
28 |
@st.cache_data
|
29 |
def load_data(file):
|
@@ -56,24 +47,27 @@ def provide_observed_advice(data):
|
|
56 |
|
57 |
return advice
|
58 |
|
59 |
-
# Function to
|
60 |
-
def
|
|
|
|
|
|
|
61 |
try:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
return
|
74 |
-
except
|
75 |
-
st.error(f"Error
|
76 |
-
return
|
77 |
|
78 |
# Streamlit app layout
|
79 |
def main():
|
@@ -142,10 +136,15 @@ def main():
|
|
142 |
else:
|
143 |
st.warning("No advice available based on your inputs.")
|
144 |
|
145 |
-
# Fetch
|
146 |
-
st.subheader("
|
147 |
-
|
148 |
-
|
|
|
|
|
|
|
|
|
|
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
main()
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
+
import requests
|
5 |
from groq import Groq
|
|
|
6 |
|
7 |
+
# Fetch the GROQ API key from the environment variable
|
8 |
+
groq_api_key = os.getenv("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")
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Check if the API key is available
|
11 |
if not groq_api_key:
|
12 |
st.error("GROQ_API_KEY is not set. Please provide a valid API key.")
|
13 |
st.stop()
|
14 |
|
15 |
+
# Initialize the GROQ client with the fetched API key
|
16 |
groq_client = Groq(api_key=groq_api_key)
|
17 |
|
|
|
|
|
|
|
|
|
18 |
# Function to load and preprocess data
|
19 |
@st.cache_data
|
20 |
def load_data(file):
|
|
|
47 |
|
48 |
return advice
|
49 |
|
50 |
+
# Function to fetch health articles from the GROC API based on the query
|
51 |
+
def get_health_articles(query):
|
52 |
+
url = f"https://api.groc.com/search?q={query}"
|
53 |
+
headers = {"Authorization": f"Bearer {groq_api_key}"} # Use the demo API key in the header
|
54 |
+
|
55 |
try:
|
56 |
+
response = requests.get(url, headers=headers)
|
57 |
+
response.raise_for_status() # This will raise an HTTPError for bad responses
|
58 |
+
data = response.json() # Assuming the API returns JSON
|
59 |
+
if 'results' in data:
|
60 |
+
articles = [{"title": item["title"], "url": item["url"]} for item in data['results']]
|
61 |
+
else:
|
62 |
+
articles = []
|
63 |
+
return articles
|
64 |
+
except requests.exceptions.HTTPError as http_err:
|
65 |
+
st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
|
66 |
+
st.error(f"Response content: {response.text}")
|
67 |
+
return []
|
68 |
+
except requests.exceptions.RequestException as err:
|
69 |
+
st.error(f"Error fetching articles: {err}. Please check your internet connection.")
|
70 |
+
return []
|
71 |
|
72 |
# Streamlit app layout
|
73 |
def main():
|
|
|
136 |
else:
|
137 |
st.warning("No advice available based on your inputs.")
|
138 |
|
139 |
+
# Fetch related health articles based on user input
|
140 |
+
st.subheader("π° **Related Health Articles** π°")
|
141 |
+
query = "mental health anxiety depression isolation stress relief"
|
142 |
+
articles = get_health_articles(query)
|
143 |
+
if articles:
|
144 |
+
for article in articles:
|
145 |
+
st.write(f"π [{article['title']}]({article['url']})")
|
146 |
+
else:
|
147 |
+
st.write("No articles found. Please check your API key or internet connection.")
|
148 |
|
149 |
if __name__ == "__main__":
|
150 |
main()
|