RKP64 commited on
Commit
34f1fba
·
1 Parent(s): 157aa72

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -0
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This is a streamlit application that allows user to query multiple csv
3
+ files and return the answer based on the text in the csv files.
4
+
5
+ It uses streamlit to create a web application to load the csv files and
6
+ query them.
7
+
8
+ Langchain and OpenAI API are used to generate the answer based on the
9
+ text in the csv files.
10
+
11
+ Credit: https://www.youtube.com/@alejandro_ao 👊🏿
12
+ """
13
+
14
+ from langchain.agents import create_csv_agent
15
+ from langchain.chat_models import ChatOpenAI
16
+ from langchain.agents.agent_types import AgentType
17
+ #from dotenv import load_dotenv
18
+ import os
19
+ import pandas as pd
20
+ import streamlit as st
21
+
22
+
23
+ def main():
24
+ # set the page title
25
+ st.set_page_config("Data Analysis Helper \U0001F4CA")
26
+ st.markdown("# Data Analysis Helper")
27
+ st.markdown("This tool helps you analyze your CSV files. Please remember to **remove personal information** first, such as names, addresses, phone numbers, emails, etc.")
28
+ st.markdown("## How to use this tool")
29
+ st.markdown("1. Upload your CSV file")
30
+ st.markdown("2. Ask a question about your CSV file")
31
+ st.markdown("3. Wait for the answer to appear")
32
+ st.markdown("## Example questions")
33
+ st.markdown("1. What is the average age?")
34
+ st.markdown("2. What is the average income?")
35
+ st.markdown("3. What is the average age of people who live in London?")
36
+ st.markdown("Go to [this page](https://openai.com/pricing) to get an OpenAI API key.")
37
+ st.markdown("The API key takes the following form: sk****hx. NB: This is not a viable key.")
38
+
39
+ # text input to ask for openai api key
40
+ # then hide the input
41
+ openai_api_key = st.text_input("Enter your OpenAI API key", type="password")
42
+
43
+ # set this key as an environment variable
44
+ os.environ["OPENAI_API_KEY"] = openai_api_key
45
+
46
+ # load the api key from the .env file
47
+ #load_dotenv()
48
+
49
+ # inform the user that the api key is loaded
50
+ if os.getenv("OPENAI_API_KEY") is None or os.getenv("OPENAI_API_KEY") == "":
51
+ print("OPENAI_API_KEY is not set")
52
+ exit(1)
53
+ else:
54
+ print("OPENAI_API_KEY is set")
55
+
56
+ # inform user that the model being used is the turbo model
57
+ st.write("Using the gpt-3.5-turbo-0613 model from OpenAI")
58
+
59
+
60
+ # Upload the CSV file
61
+ csv_file = st.file_uploader("Upload a CSV file", type="csv")
62
+
63
+ # if the user has uploaded a csv file then save it to the current directory
64
+ if csv_file is not None:
65
+ with open(os.path.join(os.getcwd(), csv_file.name), "wb") as f:
66
+ f.write(csv_file.getbuffer())
67
+ st.write("CSV file uploaded to: ", os.path.join(os.getcwd(), csv_file.name))
68
+
69
+ # see a preview of the csv file
70
+ st.write("Preview of the CSV file:")
71
+ # see a preview of the csv file
72
+ df = pd.read_csv(os.path.join(os.getcwd(), csv_file.name))
73
+ st.dataframe(df.head()) # Display the first few rows of the DataFrame
74
+
75
+ # create the agent
76
+ agent = create_csv_agent(
77
+ ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),
78
+ os.path.join(os.getcwd(), csv_file.name),
79
+ verbose=True,
80
+ agent_type=AgentType.OPENAI_FUNCTIONS,
81
+ )
82
+
83
+ # ask the user for a question
84
+ user_question = st.text_input("Ask a question \U0001F914 about your CSV: ")
85
+
86
+ # if the user has asked a question then run the agent
87
+ if user_question is not None and user_question != "":
88
+ with st.spinner(text="In progress..."):
89
+ st.write(agent.run(user_question))
90
+
91
+ if __name__ == "__main__":
92
+ main()