fedor-ch commited on
Commit
ad079c2
1 Parent(s): f2097d9

New interface & xlsx analyzer (#3)

Browse files

- On branch pr/3 (c0fd588b06bba9fdbf04e7c1a360122b8a23caf4)

Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +159 -16
  3. requirements.txt +3 -1
README.md CHANGED
@@ -5,9 +5,9 @@ colorFrom: purple
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.27.0
 
8
  app_file: app.py
9
  pinned: false
10
  duplicated_from: fffiloni/langchain-chat-with-pdf-openai
11
  ---
12
-
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
5
  colorTo: pink
6
  sdk: gradio
7
  sdk_version: 3.27.0
8
+ python_version: 3.10.9
9
  app_file: app.py
10
  pinned: false
11
  duplicated_from: fffiloni/langchain-chat-with-pdf-openai
12
  ---
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -33,7 +33,7 @@ def loading_pdf():
33
 
34
 
35
  def pdf_changes(pdf_doc, open_ai_key):
36
- if openai_key is not None:
37
  os.environ['OPENAI_API_KEY'] = open_ai_key
38
  loader = OnlinePDFLoader(pdf_doc.name)
39
  documents = loader.load()
@@ -83,34 +83,177 @@ css="""
83
  """
84
 
85
  title = """
86
- <div style="text-align: center;max-width: 700px;">
87
  <h1>YnP LangChain Test </h1>
88
  <p style="text-align: center;">Please specify OpenAI Key before use</p>
89
  </div>
90
  """
91
 
92
 
93
- with gr.Blocks(css=css) as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  with gr.Column(elem_id="col-container"):
95
  gr.HTML(title)
 
 
 
 
 
 
 
 
96
 
97
- with gr.Column():
98
- openai_key = gr.Textbox(label="You OpenAI API key", type="password")
99
- pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
100
- with gr.Row():
101
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
102
  load_pdf = gr.Button("Load pdf to langchain")
103
-
104
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
105
- question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
106
- submit_btn = gr.Button("Send Message")
107
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  load_pdf.click(loading_pdf, None, langchain_status, queue=False)
109
- load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
110
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
111
  bot, chatbot, chatbot
112
  )
113
- submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
114
- bot, chatbot, chatbot)
115
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  demo.launch()
 
33
 
34
 
35
  def pdf_changes(pdf_doc, open_ai_key):
36
+ if open_ai_key is not None:
37
  os.environ['OPENAI_API_KEY'] = open_ai_key
38
  loader = OnlinePDFLoader(pdf_doc.name)
39
  documents = loader.load()
 
83
  """
84
 
85
  title = """
86
+ <div style="text-align: center;">
87
  <h1>YnP LangChain Test </h1>
88
  <p style="text-align: center;">Please specify OpenAI Key before use</p>
89
  </div>
90
  """
91
 
92
 
93
+ # with gr.Blocks(css=css) as demo:
94
+ # with gr.Column(elem_id="col-container"):
95
+ # gr.HTML(title)
96
+
97
+ # with gr.Column():
98
+ # openai_key = gr.Textbox(label="You OpenAI API key", type="password")
99
+ # pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
100
+ # with gr.Row():
101
+ # langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
102
+ # load_pdf = gr.Button("Load pdf to langchain")
103
+
104
+ # chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
105
+ # question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
106
+ # submit_btn = gr.Button("Send Message")
107
+
108
+ # load_pdf.click(loading_pdf, None, langchain_status, queue=False)
109
+ # load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
110
+ # question.submit(add_text, [chatbot, question], [chatbot, question]).then(
111
+ # bot, chatbot, chatbot
112
+ # )
113
+ # submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
114
+ # bot, chatbot, chatbot)
115
+
116
+ # demo.launch()
117
+
118
+
119
+ """functions"""
120
+
121
+ def load_file():
122
+ return "Loading..."
123
+
124
+ def load_xlsx(name):
125
+ import pandas as pd
126
+
127
+ xls_file = rf'{name}'
128
+ data = pd.read_excel(xls_file)
129
+ return data
130
+
131
+ def table_loader(table_file, open_ai_key):
132
+ import os
133
+ from langchain.llms import OpenAI
134
+ from langchain.agents import create_pandas_dataframe_agent
135
+ from pandas import read_csv
136
+
137
+ global agent
138
+ if open_ai_key is not None:
139
+ os.environ['OPENAI_API_KEY'] = open_ai_key
140
+ else:
141
+ return "Enter API"
142
+
143
+ if table_file.name.endswith('.xlsx') or table_file.name.endswith('.xls'):
144
+ data = load_xlsx(table_file.name)
145
+ agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
146
+ return "Ready!"
147
+ elif table_file.name.endswith('.csv'):
148
+ data = read_csv(table_file.name)
149
+ agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
150
+ return "Ready!"
151
+ else:
152
+ return "Wrong file format! Upload excel file or csv!"
153
+
154
+ def run(query):
155
+ from langchain.callbacks import get_openai_callback
156
+
157
+ with get_openai_callback() as cb:
158
+ response = (agent.run(query))
159
+ costs = (f"Total Cost (USD): ${cb.total_cost}")
160
+ output = f'{response} \n {costs}'
161
+ return output
162
+
163
+ def respond(message, chat_history):
164
+ import time
165
+
166
+ bot_message = run(message)
167
+ chat_history.append((message, bot_message))
168
+ time.sleep(0.5)
169
+ return "", chat_history
170
+
171
+
172
+
173
+ with gr.Blocks() as demo:
174
  with gr.Column(elem_id="col-container"):
175
  gr.HTML(title)
176
+ openai_key = gr.Textbox(
177
+ show_label=False,
178
+ placeholder="Your OpenAI key",
179
+ type = 'password',
180
+ ).style(container=False)
181
+
182
+ # PDF processing tab
183
+ with gr.Tab("PDFs"):
184
 
185
+ with gr.Row():
186
+
187
+ with gr.Column(scale=0.5):
 
188
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
189
  load_pdf = gr.Button("Load pdf to langchain")
190
+
191
+ with gr.Column(scale=0.5):
192
+ pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
193
+
194
+
195
+ with gr.Row():
196
+
197
+ with gr.Column(scale=1):
198
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
199
+
200
+ with gr.Row():
201
+
202
+ with gr.Column(scale=0.85):
203
+ question = gr.Textbox(
204
+ show_label=False,
205
+ placeholder="Enter text and press enter, or upload an image",
206
+ ).style(container=False)
207
+
208
+ with gr.Column(scale=0.15, min_width=0):
209
+ clr_btn = gr.Button("Clear!")
210
+
211
  load_pdf.click(loading_pdf, None, langchain_status, queue=False)
212
+ load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=True)
213
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
214
  bot, chatbot, chatbot
215
  )
216
+
217
+ # XLSX and CSV processing tab
218
+ with gr.Tab("Spreadsheets"):
219
+ with gr.Row():
220
+
221
+ with gr.Column(scale=0.5):
222
+ status_sh = gr.Textbox(label="Status", placeholder="", interactive=False)
223
+ load_table = gr.Button("Load csv|xlsx to langchain")
224
+
225
+ with gr.Column(scale=0.5):
226
+ raw_table = gr.File(label="Load a table file (xls or csv)", file_types=['.csv, xlsx, xls'], type="file")
227
+
228
+
229
+ with gr.Row():
230
+
231
+ with gr.Column(scale=1):
232
+ chatbot_sh = gr.Chatbot([], elem_id="chatbot").style(height=350)
233
+
234
+
235
+ with gr.Row():
236
+
237
+ with gr.Column(scale=0.85):
238
+ question_sh = gr.Textbox(
239
+ show_label=False,
240
+ placeholder="Enter text and press enter, or upload an image",
241
+ ).style(container=False)
242
+
243
+ with gr.Column(scale=0.15, min_width=0):
244
+ clr_btn = gr.Button("Clear!")
245
+
246
+ load_table.click(load_file, None, status_sh, queue=False)
247
+ load_table.click(table_loader, inputs=[raw_table, openai_key], outputs=[status_sh], queue=False)
248
+
249
+ question_sh.submit(respond, [question_sh, chatbot_sh], [question_sh, chatbot_sh])
250
+ clr_btn.click(lambda: None, None, chatbot_sh, queue=False)
251
+
252
+
253
+ with gr.Tab("Charts"):
254
+ gr.Text('Soon!')
255
+
256
+
257
+
258
+ demo.queue(concurrency_count=3)
259
  demo.launch()
requirements.txt CHANGED
@@ -3,4 +3,6 @@ tiktoken
3
  chromadb
4
  langchain
5
  unstructured
6
- unstructured[local-inference]
 
 
 
3
  chromadb
4
  langchain
5
  unstructured
6
+ unstructured[local-inference]
7
+ pandas
8
+ tabulate