Skier8402 commited on
Commit
0b0825a
·
1 Parent(s): fc7a778

Create app.py

Browse files

Add application code.

Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
12
+ from langchain.agents import create_csv_agent
13
+ from langchain.chat_models import ChatOpenAI
14
+ from langchain.agents.agent_types import AgentType
15
+ from dotenv import load_dotenv
16
+ import os
17
+ import pandas as pd
18
+ import streamlit as st
19
+
20
+
21
+ def main():
22
+ # set the page title
23
+ st.set_page_config("Data Analysis Helper \U0001F4CA")
24
+ st.markdown("# Data Analysis Helper")
25
+ 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.")
26
+ st.markdown("## How to use this tool")
27
+ st.markdown("1. Upload your CSV file")
28
+ st.markdown("2. Ask a question about your CSV file")
29
+ st.markdown("3. Wait for the answer to appear")
30
+ st.markdown("## Example questions")
31
+ st.markdown("1. What is the average age?")
32
+ st.markdown("2. What is the average income?")
33
+ st.markdown("3. What is the average age of people who live in London?")
34
+ st.markdown("Go to [this page](https://openai.com/pricing) to get an OpenAI API key.")
35
+ # text input to ask for openai api key
36
+ # then hide the input
37
+ openai_api_key = st.text_input("Enter your OpenAI API key", type="password")
38
+
39
+ # load the api key from the .env file
40
+ load_dotenv()
41
+
42
+ # inform the user that the api key is loaded
43
+ if openai_api_key is not None and openai_api_key != "":
44
+ st.write("OpenAI API key loaded")
45
+
46
+ # inform user that the model being used is the turbo model
47
+ st.write("Using the gpt-3.5-turbo-0613 model from OpenAI")
48
+
49
+
50
+ # Upload the CSV file
51
+ csv_file = st.file_uploader("Upload a CSV file", type="csv")
52
+
53
+ # if the user has uploaded a csv file then save it to the current directory
54
+ if csv_file is not None:
55
+ with open(os.path.join(os.getcwd(), csv_file.name), "wb") as f:
56
+ f.write(csv_file.getbuffer())
57
+ st.write("CSV file uploaded to: ", os.path.join(os.getcwd(), csv_file.name))
58
+
59
+ # see a preview of the csv file
60
+ st.write("Preview of the CSV file:")
61
+ # see a preview of the csv file
62
+ df = pd.read_csv(os.path.join(os.getcwd(), csv_file.name))
63
+ st.dataframe(df.head()) # Display the first few rows of the DataFrame
64
+
65
+ # create the agent
66
+ agent = create_csv_agent(
67
+ ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613"),
68
+ os.path.join(os.getcwd(), csv_file.name),
69
+ verbose=True,
70
+ agent_type=AgentType.OPENAI_FUNCTIONS,
71
+ )
72
+
73
+ # ask the user for a question
74
+ user_question = st.text_input("Ask a question \U0001F914 about your CSV: ")
75
+
76
+ # if the user has asked a question then run the agent
77
+ if user_question is not None and user_question != "":
78
+ with st.spinner(text="In progress..."):
79
+ st.write(agent.run(user_question))
80
+
81
+ if __name__ == "__main__":
82
+ main()