saherPervaiz commited on
Commit
e768b29
·
verified ·
1 Parent(s): 5d8ef8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -27
app.py CHANGED
@@ -1,40 +1,29 @@
1
  import os
2
- import logging
3
- import pandas as pd
4
  import streamlit as st
 
5
  import requests
6
- from groq import groq
7
-
8
- # Set up logging
9
- logging.basicConfig(level=logging.INFO)
10
- logger = logging.getLogger(__name__)
11
 
12
- # Load environment variables from .env file
13
- load_dotenv()
14
-
15
- # Get the Groq API key from environment variables
16
  groq_api_key = os.getenv("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")
 
 
17
  if not groq_api_key:
18
- raise ValueError("GROQ_API_KEY is not set. Please provide a valid API key.")
 
19
 
20
- # Initialize Groq client
21
  try:
22
  groq_client = Groq(api_key=groq_api_key)
23
- logger.info("Groq client initialized successfully.")
24
  except Exception as e:
25
- logger.error(f"Error initializing Groq client: {e}")
26
- raise # Exit the program if client initialization fails
27
 
28
  # Function to load and preprocess data
29
  @st.cache_data
30
  def load_data(file):
31
- try:
32
- df = pd.read_csv(file)
33
- return df
34
- except Exception as e:
35
- logger.error(f"Error loading CSV file: {e}")
36
- st.error("There was an issue loading the file. Please try again.")
37
- return pd.DataFrame() # Return an empty DataFrame in case of error
38
 
39
  # Function to provide detailed health advice based on user data
40
  def provide_observed_advice(data):
@@ -77,11 +66,10 @@ def get_health_articles(query):
77
  articles = []
78
  return articles
79
  except requests.exceptions.HTTPError as http_err:
80
- logger.error(f"HTTP error occurred: {http_err}")
81
  st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
 
82
  return []
83
  except requests.exceptions.RequestException as err:
84
- logger.error(f"Error fetching articles: {err}")
85
  st.error(f"Error fetching articles: {err}. Please check your internet connection.")
86
  return []
87
 
@@ -118,8 +106,6 @@ def main():
118
  uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
119
  if uploaded_file:
120
  df = load_data(uploaded_file)
121
- if df.empty:
122
- return
123
  st.write("### Dataset Preview:")
124
  st.dataframe(df.head())
125
 
 
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, if not show an error and stop
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
  try:
17
  groq_client = Groq(api_key=groq_api_key)
 
18
  except Exception as e:
19
+ st.error(f"Error initializing GROQ client: {e}")
20
+ st.stop()
21
 
22
  # Function to load and preprocess data
23
  @st.cache_data
24
  def load_data(file):
25
+ df = pd.read_csv(file)
26
+ return df
 
 
 
 
 
27
 
28
  # Function to provide detailed health advice based on user data
29
  def provide_observed_advice(data):
 
66
  articles = []
67
  return articles
68
  except requests.exceptions.HTTPError as http_err:
 
69
  st.error(f"HTTP error occurred: {http_err}. Please check your API key and the endpoint.")
70
+ st.error(f"Response content: {response.text}")
71
  return []
72
  except requests.exceptions.RequestException as err:
 
73
  st.error(f"Error fetching articles: {err}. Please check your internet connection.")
74
  return []
75
 
 
106
  uploaded_file = st.file_uploader("Upload your dataset (CSV)", type=["csv"])
107
  if uploaded_file:
108
  df = load_data(uploaded_file)
 
 
109
  st.write("### Dataset Preview:")
110
  st.dataframe(df.head())
111