Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,62 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
from langchain.agents import create_csv_agent
|
| 4 |
from langchain.chat_models import ChatOpenAI
|
| 5 |
from langchain.agents.agent_types import AgentType
|
| 6 |
-
#from dotenv import load_dotenv
|
| 7 |
import os
|
| 8 |
import pandas as pd
|
| 9 |
import streamlit as st
|
| 10 |
-
|
| 11 |
|
| 12 |
def main():
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
# text input to ask for openai api key
|
| 16 |
-
# then hide the input
|
| 17 |
-
openai_api_key = st.text_input("Enter your key", type="password")
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
os.
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "":
|
| 27 |
-
print("OPENAI_API_KEY is not set")
|
| 28 |
-
exit(1)
|
| 29 |
else:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# inform user that the model being used is the turbo model
|
| 33 |
-
st.write("Using the key")
|
| 34 |
-
|
| 35 |
|
| 36 |
# Upload the CSV file
|
| 37 |
csv_file = st.file_uploader("Upload a CSV file", type="csv")
|
| 38 |
|
| 39 |
-
#
|
| 40 |
if csv_file is not None:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
# create the agent
|
| 52 |
agent = create_csv_agent(
|
| 53 |
ChatOpenAI(temperature=0, model="gpt-4-turbo"),
|
| 54 |
-
|
| 55 |
verbose=True,
|
| 56 |
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 57 |
)
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
user_question = st.text_input("Ask a question:
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
if user_question
|
| 64 |
with st.spinner(text="In progress..."):
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain.agents import create_csv_agent
|
| 2 |
from langchain.chat_models import ChatOpenAI
|
| 3 |
from langchain.agents.agent_types import AgentType
|
|
|
|
| 4 |
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)
|
| 11 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Load the OpenAI API key from the environment
|
| 14 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 15 |
|
| 16 |
+
# Check if the OpenAI API key is set
|
| 17 |
+
if openai_api_key is None or openai_api_key == "":
|
| 18 |
+
st.error("OpenAI API Key is not set in the environment. Please check your environment variables.")
|
| 19 |
+
return
|
|
|
|
|
|
|
|
|
|
| 20 |
else:
|
| 21 |
+
st.success("OpenAI API Key is set from the backend.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Upload the CSV file
|
| 24 |
csv_file = st.file_uploader("Upload a CSV file", type="csv")
|
| 25 |
|
| 26 |
+
# If a file is uploaded
|
| 27 |
if csv_file is not None:
|
| 28 |
+
try:
|
| 29 |
+
# Read the CSV without saving to disk
|
| 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
|
| 33 |
+
except Exception as e:
|
| 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 |
+
csv_file,
|
| 41 |
verbose=True,
|
| 42 |
agent_type=AgentType.OPENAI_FUNCTIONS,
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Ask the user for a question
|
| 46 |
+
user_question = st.text_input("Ask a question:")
|
| 47 |
|
| 48 |
+
# If a question is entered, process it with the agent
|
| 49 |
+
if user_question and user_question.strip():
|
| 50 |
with st.spinner(text="In progress..."):
|
| 51 |
+
try:
|
| 52 |
+
response = agent.run(user_question)
|
| 53 |
+
st.write(response)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(f"Error while processing your request: {str(e)}")
|
| 56 |
+
else:
|
| 57 |
+
st.warning("Please enter a valid question.")
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
main()
|
| 61 |
+
|
| 62 |
+
|