saherPervaiz commited on
Commit
b382f24
Β·
verified Β·
1 Parent(s): 0b3c5da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -32
app.py CHANGED
@@ -2,10 +2,12 @@ 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
  # Function to load and preprocess data
11
  @st.cache_data
@@ -39,27 +41,24 @@ def provide_observed_advice(data):
39
 
40
  return advice
41
 
42
- # Function to fetch health articles from the GROC API based on the query
43
- def get_health_articles(query):
44
- url = f"https://api.groc.com/search?q={query}"
45
- headers = {"Authorization": f"Bearer {api_key}"} # Use the demo API key in the header
46
-
47
  try:
48
- response = requests.get(url, headers=headers)
49
- response.raise_for_status() # This will raise an HTTPError for bad responses
50
- data = response.json() # Assuming the API returns JSON
51
- if 'results' in data:
52
- articles = [{"title": item["title"], "url": item["url"]} for item in data['results']]
53
- else:
54
- articles = []
55
- return articles
56
- except requests.exceptions.HTTPError as http_err:
57
- st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
58
- st.error(f"Response content: {response.text}")
59
- return []
60
- except requests.exceptions.RequestException as err:
61
- st.error(f"Error fetching articles: {err}. Please check your internet connection.")
62
- return []
63
 
64
  # Streamlit app layout
65
  def main():
@@ -128,15 +127,10 @@ def main():
128
  else:
129
  st.warning("No advice available based on your inputs.")
130
 
131
- # Fetch related health articles based on user input
132
- st.subheader("πŸ“° **Related Health Articles** πŸ“°")
133
- query = "mental health anxiety depression isolation stress relief"
134
- articles = get_health_articles(query)
135
- if articles:
136
- for article in articles:
137
- st.write(f"🌐 [{article['title']}]({article['url']})")
138
- else:
139
- st.write("No articles found. Please check your API key or internet connection.")
140
 
141
  if __name__ == "__main__":
142
  main()
 
2
  import pandas as pd
3
  import os
4
  from groq import Groq
 
5
 
6
+ # Set the GROQ API key from environment variable
7
+ groq_api_key = os.getenv("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") # Make sure the GROQ API key is set in your environment
8
+
9
+ # Initialize the GROQ client
10
+ groq_client = Groq(api_key=groq_api_key)
11
 
12
  # Function to load and preprocess data
13
  @st.cache_data
 
41
 
42
  return advice
43
 
44
+ # Function to use the GROQ API for health queries (e.g., for symptoms or wellness queries)
45
+ def get_health_advice_from_groq(query):
 
 
 
46
  try:
47
+ # Sending request to GROQ API
48
+ chat_completion = groq_client.chat.completions.create(
49
+ messages=[{
50
+ "role": "user",
51
+ "content": query
52
+ }],
53
+ model="llama-3.3-70b-versatile",
54
+ )
55
+
56
+ # Extracting and returning the response from GROQ API
57
+ response_content = chat_completion.choices[0].message.content
58
+ return response_content
59
+ except Exception as e:
60
+ st.error(f"Error with GROQ API: {str(e)}")
61
+ return "Could not fetch advice from GROQ API."
62
 
63
  # Streamlit app layout
64
  def main():
 
127
  else:
128
  st.warning("No advice available based on your inputs.")
129
 
130
+ # Fetch additional health advice from GROQ API
131
+ st.subheader("πŸ’¬ **Additional Health Advice from GROQ** πŸ’¬")
132
+ groq_advice = get_health_advice_from_groq("Provide wellness tips for managing anxiety and stress.")
133
+ st.write(f"πŸ’‘ {groq_advice}")
 
 
 
 
 
134
 
135
  if __name__ == "__main__":
136
  main()