Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import pandas as pd
|
| 6 |
import streamlit as st
|
| 7 |
from dotenv import load_dotenv
|
|
|
|
| 8 |
|
| 9 |
def main():
|
| 10 |
# Load environment variables from .env file (if you're using one)
|
|
@@ -18,7 +19,7 @@ def main():
|
|
| 18 |
st.error("OpenAI API Key is not set in the environment. Please check your environment variables.")
|
| 19 |
return
|
| 20 |
else:
|
| 21 |
-
st.success("")
|
| 22 |
|
| 23 |
# Upload the CSV file
|
| 24 |
csv_file = st.file_uploader("Upload a CSV file", type="csv")
|
|
@@ -26,7 +27,7 @@ def main():
|
|
| 26 |
# If a file is uploaded
|
| 27 |
if csv_file is not None:
|
| 28 |
try:
|
| 29 |
-
# Read the CSV
|
| 30 |
df = pd.read_csv(csv_file, encoding='ISO-8859-1')
|
| 31 |
st.write("Preview of the CSV file:")
|
| 32 |
st.dataframe(df.head()) # Display the first few rows of the DataFrame
|
|
@@ -34,10 +35,15 @@ def main():
|
|
| 34 |
st.error(f"Error reading the CSV file: {str(e)}")
|
| 35 |
return
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Create the agent
|
| 38 |
agent = create_csv_agent(
|
| 39 |
ChatOpenAI(temperature=0, model="gpt-4-turbo"),
|
| 40 |
-
|
| 41 |
verbose=True,
|
| 42 |
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 43 |
)
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import streamlit as st
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
+
import tempfile
|
| 9 |
|
| 10 |
def main():
|
| 11 |
# Load environment variables from .env file (if you're using one)
|
|
|
|
| 19 |
st.error("OpenAI API Key is not set in the environment. Please check your environment variables.")
|
| 20 |
return
|
| 21 |
else:
|
| 22 |
+
st.success("OpenAI API Key loaded successfully!")
|
| 23 |
|
| 24 |
# Upload the CSV file
|
| 25 |
csv_file = st.file_uploader("Upload a CSV file", type="csv")
|
|
|
|
| 27 |
# If a file is uploaded
|
| 28 |
if csv_file is not None:
|
| 29 |
try:
|
| 30 |
+
# Read the CSV file to show a preview
|
| 31 |
df = pd.read_csv(csv_file, encoding='ISO-8859-1')
|
| 32 |
st.write("Preview of the CSV file:")
|
| 33 |
st.dataframe(df.head()) # Display the first few rows of the DataFrame
|
|
|
|
| 35 |
st.error(f"Error reading the CSV file: {str(e)}")
|
| 36 |
return
|
| 37 |
|
| 38 |
+
# Save the uploaded file to a temporary location
|
| 39 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as tmp_file:
|
| 40 |
+
tmp_file.write(csv_file.getbuffer())
|
| 41 |
+
tmp_file_path = tmp_file.name
|
| 42 |
+
|
| 43 |
# Create the agent
|
| 44 |
agent = create_csv_agent(
|
| 45 |
ChatOpenAI(temperature=0, model="gpt-4-turbo"),
|
| 46 |
+
tmp_file_path, # Pass the path of the saved CSV file
|
| 47 |
verbose=True,
|
| 48 |
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 49 |
)
|