saherPervaiz commited on
Commit
37c12fe
Β·
verified Β·
1 Parent(s): 48ad81c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -35
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
- # Load environment variables from .env file (optional)
8
- load_dotenv()
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 use the GROQ API for health queries (e.g., for symptoms or wellness queries)
60
- def get_health_advice_from_groq(query):
 
 
 
61
  try:
62
- # Sending request to GROQ API
63
- chat_completion = groq_client.chat.completions.create(
64
- messages=[{
65
- "role": "user",
66
- "content": query
67
- }],
68
- model="llama-3.3-70b-versatile",
69
- )
70
-
71
- # Extracting and returning the response from GROQ API
72
- response_content = chat_completion.choices[0].message.content
73
- return response_content
74
- except Exception as e:
75
- st.error(f"Error with GROQ API: {str(e)}")
76
- return "Could not fetch advice from GROQ API."
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 additional health advice from GROQ API
146
- st.subheader("πŸ’¬ **Additional Health Advice from GROQ** πŸ’¬")
147
- groq_advice = get_health_advice_from_groq("Provide wellness tips for managing anxiety and stress.")
148
- st.write(f"πŸ’‘ {groq_advice}")
 
 
 
 
 
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()