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