cececerece commited on
Commit
30af771
1 Parent(s): a3ad05b

csv loader app commit

Browse files
Files changed (3) hide show
  1. app.py +41 -0
  2. comm.py +16 -0
  3. table_load.py +35 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import table_load as tl
3
+ import comm
4
+
5
+
6
+ css="""
7
+ #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
8
+ """
9
+
10
+ title = """
11
+ <div style="text-align: center;max-width: 700px;">
12
+ <h1>Analyze your CSV</h1>
13
+ <p style="text-align: center;">Please specify OpenAI Key before use</p>
14
+ </div>
15
+ """
16
+
17
+
18
+ with gr.Blocks(css=css) as demo:
19
+ with gr.Column(elem_id="col-container"):
20
+ gr.HTML(title)
21
+ with gr.Column():
22
+ openai_key = gr.Textbox(label="You OpenAI API key", type="password")
23
+ raw_table = gr.File(label="Load a table file (xls or csv)", file_types=['.csv, xlsx, xls'], type="file")
24
+ with gr.Row():
25
+ langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
26
+ load_table = gr.Button("Load table to langchain")
27
+
28
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
29
+ with gr.Column(scale=0.85):
30
+ question = gr.Textbox(show_label=False, placeholder="Type your question and hit Enter").style(container=False)
31
+ with gr.Column(scale=0.15, min_width=0):
32
+ clr_btn = gr.Button("Clear History")
33
+
34
+
35
+ load_table.click(tl.load_file, None, langchain_status, queue=False)
36
+ load_table.click(tl.table_loader, inputs=[raw_table, openai_key], outputs=[langchain_status], queue=False)
37
+
38
+ question.submit(comm.respond, [question, chatbot], [question, chatbot])
39
+ clr_btn.click(lambda: None, None, chatbot, queue=False)
40
+
41
+ demo.launch()
comm.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def run(query):
2
+ from langchain.callbacks import get_openai_callback
3
+
4
+ with get_openai_callback() as cb:
5
+ response = (agent.run(query))
6
+ costs = (f"Total Cost (USD): ${cb.total_cost}")
7
+ output = f'{response} \n {costs}'
8
+ return output
9
+
10
+ def respond(message, chat_history):
11
+ import time
12
+
13
+ bot_message = run(message)
14
+ chat_history.append((message, bot_message))
15
+ time.sleep(0.5)
16
+ return "", chat_history
table_load.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """functions related to tables loading"""
2
+
3
+ def load_file():
4
+ return "Loading..."
5
+
6
+ def load_xlsx(name):
7
+ import pandas as pd
8
+
9
+ xls_file = rf'{name}'
10
+ data = pd.read_excel(xls_file)
11
+ return data
12
+
13
+ def table_loader(table_file, open_ai_key):
14
+ import os
15
+ from langchain.llms import OpenAI
16
+ from langchain.agents import create_pandas_dataframe_agent
17
+ from pandas import read_csv
18
+
19
+ global agent
20
+ if open_ai_key is not None:
21
+ os.environ['OPENAI_API_KEY'] = open_ai_key
22
+ else:
23
+ return "Enter API"
24
+
25
+ if table_file.name.endswith('.xlsx') or table_file.name.endswith('.xls'):
26
+ data = load_xlsx(table_file.name)
27
+ agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
28
+ return "Ready!"
29
+ elif table_file.name.endswith('.csv'):
30
+ data = read_csv(table_file.name)
31
+ agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
32
+ return "Ready!"
33
+ else:
34
+ return "Wrong file format! Upload excel file or csv!"
35
+