RKP64 commited on
Commit
ec7d9d8
·
verified ·
1 Parent(s): 4c31d24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -41
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
- # set the page title
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
- # set this key as an environment variable
20
- os.environ["OPENAI_API_KEY"] = openai_api_key
21
 
22
- # load the api key from the .env file
23
- #load_dotenv()
24
-
25
- # inform the user that the api key is loaded
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
- print("OPENAI_API_KEY is set")
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
- # if the user has uploaded a csv file then save it to the current directory
40
  if csv_file is not None:
41
- with open(os.path.join(os.getcwd(), csv_file.name), "wb") as f:
42
- f.write(csv_file.getbuffer())
43
- st.write("CSV file uploaded to: ", os.path.join(os.getcwd(), csv_file.name))
44
-
45
- # see a preview of the csv file
46
- st.write("Preview of the CSV file:")
47
- # see a preview of the csv file
48
- df = pd.read_csv(os.path.join(os.getcwd(), csv_file.name))
49
- st.dataframe(df.head()) # Display the first few rows of the DataFrame
50
-
51
- # create the agent
52
  agent = create_csv_agent(
53
  ChatOpenAI(temperature=0, model="gpt-4-turbo"),
54
- os.path.join(os.getcwd(), csv_file.name),
55
  verbose=True,
56
  agent_type=AgentType.OPENAI_FUNCTIONS,
57
  )
58
 
59
- # ask the user for a question
60
- user_question = st.text_input("Ask a question: ")
61
 
62
- # if the user has asked a question then run the agent
63
- if user_question is not None and user_question != "":
64
  with st.spinner(text="In progress..."):
65
- st.write(agent.run(user_question))
 
 
 
 
 
 
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
+