ilia_khristoforov commited on
Commit
d775f5c
1 Parent(s): 1e5e466

изменено: app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -267
app.py CHANGED
@@ -1,287 +1,83 @@
1
  import gradio as gr
2
- import os
3
  import time
 
 
4
 
5
- from langchain.document_loaders import OnlinePDFLoader
6
- from langchain.text_splitter import CharacterTextSplitter
7
- from langchain.llms import OpenAI
8
- from langchain.embeddings import OpenAIEmbeddings
9
- from langchain.vectorstores import Chroma
10
- from langchain.chains import ConversationalRetrievalChain
11
- from langchain import PromptTemplate
12
- from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
13
- import requests
14
- from PIL import Image
15
- import torch
16
-
17
-
18
-
19
- # _template = """Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
20
- # Chat History:
21
- # {chat_history}
22
- # Follow Up Input: {question}
23
- # Standalone question:"""
24
-
25
- # CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(_template)
26
-
27
- # template = """
28
- # You are given the following extracted parts of a long document and a question. Provide a short structured answer.
29
- # If you don't know the answer, look on the web. Don't try to make up an answer.
30
- # Question: {question}
31
- # =========
32
- # {context}
33
- # =========
34
- # Answer in Markdown:"""
35
-
36
- torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/val/png/20294671002019.png', 'chart_example.png')
37
- torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/multi_col_1081.png', 'chart_example_2.png')
38
- torch.hub.download_url_to_file('https://raw.githubusercontent.com/vis-nlp/ChartQA/main/ChartQA%20Dataset/test/png/18143564004789.png', 'chart_example_3.png')
39
- torch.hub.download_url_to_file('https://sharkcoder.com/files/article/matplotlib-bar-plot.png', 'chart_example_4.png')
40
-
41
-
42
- model_name = "google/matcha-chartqa"
43
- model = Pix2StructForConditionalGeneration.from_pretrained(model_name)
44
- processor = Pix2StructProcessor.from_pretrained(model_name)
45
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
46
- model.to(device)
47
-
48
- def filter_output(output):
49
- return output.replace("<0x0A>", "")
50
-
51
- def chart_qa(image, question):
52
- inputs = processor(images=image, text=question, return_tensors="pt").to(device)
53
- predictions = model.generate(**inputs, max_new_tokens=512)
54
- return filter_output(processor.decode(predictions[0], skip_special_tokens=True))
55
-
56
- def loading_pdf():
57
- return "Loading..."
58
-
59
-
60
- def pdf_changes(pdf_doc, open_ai_key):
61
- if open_ai_key is not None:
62
- os.environ['OPENAI_API_KEY'] = open_ai_key
63
- loader = OnlinePDFLoader(pdf_doc.name)
64
- documents = loader.load()
65
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
66
- texts = text_splitter.split_documents(documents)
67
- embeddings = OpenAIEmbeddings()
68
- db = Chroma.from_documents(texts, embeddings)
69
- retriever = db.as_retriever()
70
- global qa
71
- qa = ConversationalRetrievalChain.from_llm(
72
- llm=OpenAI(temperature=0.5),
73
- retriever=retriever,
74
- return_source_documents=True)
75
- return "Ready"
76
- else:
77
- return "You forgot OpenAI API key"
78
-
79
- def add_text(history, text):
80
- history = history + [(text, None)]
81
- return history, ""
82
-
83
- def bot(history):
84
- response = infer(history[-1][0], history)
85
- history[-1][1] = ""
86
-
87
- for character in response:
88
- history[-1][1] += character
89
- time.sleep(0.05)
90
- yield history
91
 
92
-
93
- def infer(question, history):
94
- res = []
95
- for human, ai in history[:-1]:
96
- pair = (human, ai)
97
- res.append(pair)
98
 
99
- chat_history = res
100
- #print(chat_history)
101
- query = question
102
- result = qa({"question": query, "chat_history": chat_history})
103
- #print(result)
104
- return result["answer"]
105
-
106
- css="""
107
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
108
- """
109
-
110
- title = """
111
- <div style="text-align: center;">
112
- <h1>YnP LangChain Test </h1>
113
- <p style="text-align: center;">Please specify OpenAI Key before use</p>
114
- </div>
115
- """
116
-
117
-
118
- # with gr.Blocks(css=css) as demo:
119
- # with gr.Column(elem_id="col-container"):
120
- # gr.HTML(title)
121
-
122
- # with gr.Column():
123
- # openai_key = gr.Textbox(label="You OpenAI API key", type="password")
124
- # pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
125
- # with gr.Row():
126
- # langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
127
- # load_pdf = gr.Button("Load pdf to langchain")
128
-
129
- # chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
130
- # question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
131
- # submit_btn = gr.Button("Send Message")
132
-
133
- # load_pdf.click(loading_pdf, None, langchain_status, queue=False)
134
- # load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
135
- # question.submit(add_text, [chatbot, question], [chatbot, question]).then(
136
- # bot, chatbot, chatbot
137
- # )
138
- # submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
139
- # bot, chatbot, chatbot)
140
-
141
- # demo.launch()
142
-
143
-
144
- """functions"""
145
-
146
- def load_file():
147
- return "Loading..."
148
-
149
- def load_xlsx(name):
150
- import pandas as pd
151
 
152
- xls_file = rf'{name}'
153
- data = pd.read_excel(xls_file)
154
- return data
155
-
156
- def table_loader(table_file, open_ai_key):
157
- import os
158
- from langchain.llms import OpenAI
159
- from langchain.agents import create_pandas_dataframe_agent
160
- from pandas import read_csv
161
 
162
- global agent
163
- if open_ai_key is not None:
164
- os.environ['OPENAI_API_KEY'] = open_ai_key
165
- else:
166
- return "Enter API"
167
 
168
- if table_file.name.endswith('.xlsx') or table_file.name.endswith('.xls'):
169
- data = load_xlsx(table_file.name)
170
- agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
171
- return "Ready!"
172
- elif table_file.name.endswith('.csv'):
173
- data = read_csv(table_file.name)
174
- agent = create_pandas_dataframe_agent(OpenAI(temperature=0), data)
175
- return "Ready!"
176
- else:
177
- return "Wrong file format! Upload excel file or csv!"
178
 
179
- def run(query):
180
- from langchain.callbacks import get_openai_callback
181
 
182
- with get_openai_callback() as cb:
183
- response = (agent.run(query))
184
- costs = (f"Total Cost (USD): ${cb.total_cost}")
185
- output = f'{response} \n {costs}'
186
- return output
 
187
 
188
- def respond(message, chat_history):
189
- import time
 
 
190
 
191
- bot_message = run(message)
192
- chat_history.append((message, bot_message))
193
- time.sleep(0.5)
194
- return "", chat_history
 
 
195
 
 
 
196
 
197
  with gr.Blocks() as demo:
198
- with gr.Column(elem_id="col-container"):
199
- gr.HTML(title)
200
- key = gr.Textbox(
201
- show_label=False,
202
- placeholder="Your OpenAI key",
203
- type = 'password',
204
- ).style(container=False)
205
-
206
- # PDF processing tab
207
- with gr.Tab("PDFs"):
208
-
209
- with gr.Row():
210
-
211
- with gr.Column(scale=0.5):
212
- langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
213
- load_pdf = gr.Button("Load pdf to langchain")
214
-
215
- with gr.Column(scale=0.5):
216
- pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
217
-
218
-
219
- with gr.Row():
220
-
221
- with gr.Column(scale=1):
222
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
223
-
224
- with gr.Row():
225
-
226
- with gr.Column(scale=0.85):
227
- question = gr.Textbox(
228
- show_label=False,
229
- placeholder="Enter text and press enter, or upload an image",
230
- ).style(container=False)
231
-
232
- with gr.Column(scale=0.15, min_width=0):
233
- clr_btn = gr.Button("Clear!")
234
-
235
- load_pdf.click(loading_pdf, None, langchain_status, queue=False)
236
- load_pdf.click(pdf_changes, inputs=[pdf_doc, key], outputs=[langchain_status], queue=True)
237
- question.submit(add_text, [chatbot, question], [chatbot, question]).then(
238
- bot, chatbot, chatbot
239
- )
240
-
241
- # XLSX and CSV processing tab
242
- with gr.Tab("Spreadsheets"):
243
- with gr.Row():
244
-
245
- with gr.Column(scale=0.5):
246
- status_sh = gr.Textbox(label="Status", placeholder="", interactive=False)
247
- load_table = gr.Button("Load csv|xlsx to langchain")
248
-
249
- with gr.Column(scale=0.5):
250
- raw_table = gr.File(label="Load a table file (xls or csv)", file_types=['.csv, xlsx, xls'], type="file")
251
-
252
-
253
- with gr.Row():
254
 
255
- with gr.Column(scale=1):
256
- chatbot_sh = gr.Chatbot([], elem_id="chatbot").style(height=350)
257
-
258
-
259
- with gr.Row():
 
 
 
260
 
261
- with gr.Column(scale=0.85):
262
- question_sh = gr.Textbox(
263
- show_label=False,
264
- placeholder="Enter text and press enter, or upload an image",
265
- ).style(container=False)
266
-
267
- with gr.Column(scale=0.15, min_width=0):
268
- clr_btn = gr.Button("Clear!")
269
 
270
- load_table.click(load_file, None, status_sh, queue=False)
271
- load_table.click(table_loader, inputs=[raw_table, key], outputs=[status_sh], queue=False)
272
-
273
- question_sh.submit(respond, [question_sh, chatbot_sh], [question_sh, chatbot_sh])
274
- clr_btn.click(lambda: None, None, chatbot_sh, queue=False)
275
-
 
 
 
 
 
276
 
277
- with gr.Tab("Charts"):
278
- image = gr.Image(type="pil", label="Chart")
279
- question = gr.Textbox(label="Question")
280
- load_chart = gr.Button("Load chart and question!")
281
- answer = gr.Textbox(label="Model Output")
282
-
283
- load_chart.click(chart_qa, [image, question], answer)
284
 
285
-
286
- demo.queue(concurrency_count=3)
287
- demo.launch()
 
1
  import gradio as gr
 
2
  import time
3
+ from utils import Bot
4
+ from utils.functions import make_documents, make_descriptions
5
 
6
+ def init_bot(file=None,title=None,pdf=None,key=None):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ if key is None:
9
+ return 'You must submit OpenAI key'
 
 
 
 
10
 
11
+ if pdf is None:
12
+ return 'You must submit pdf file'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ if file is None:
15
+ return 'You must submit media file'
 
 
 
 
 
 
 
16
 
17
+ if title is None:
18
+ return 'You must submit the description of the media'
 
 
 
19
 
20
+ file = file.name
21
+ print(file)
22
+ pdf = pdf.name
23
+ file_description = make_descriptions(file, title)
24
+ # print(file_description)
25
+ documents = make_documents(pdf)
 
 
 
 
26
 
27
+ # print(documents[0])
28
+ global bot
29
 
30
+ bot = Bot(
31
+ openai_api_key=key,
32
+ file_descriptions=file_description,
33
+ text_documents=documents,
34
+ verbose=False
35
+ )
36
 
37
+ return 'Chat bot successfully initialized'
38
+
39
+ def msg_bot(history):
40
+ message = history[-1][0]
41
 
42
+ bot_message = bot(message)['output']
43
+ history[-1][1] = ""
44
+ for character in bot_message:
45
+ history[-1][1] += character
46
+ time.sleep(0.05)
47
+ yield history
48
 
49
+ def user(user_message, history):
50
+ return "", history + [[user_message, None]]
51
 
52
  with gr.Blocks() as demo:
53
+
54
+ key = gr.Textbox(label='OpenAI key')
55
+ with gr.Tab("Chat bot initialization"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
+ with gr.Row(variant='panel'):
58
+ with gr.Column():
59
+ with gr.Row():
60
+ title = gr.Textbox(label='File short description')
61
+ with gr.Row():
62
+ file = gr.File(label='CSV or image', file_types=['.csv', 'image'])
63
+
64
+ pdf = gr.File(label='pdf')
65
 
66
+ with gr.Row(variant='panel'):
67
+ init_button = gr.Button('submit')
68
+ init_output = gr.Textbox(label="Initialization status")
69
+ init_button.click(fn=init_bot,inputs=[file,title,pdf,key],outputs=init_output,api_name='init')
 
 
 
 
70
 
71
+ chatbot = gr.Chatbot()
72
+ msg = gr.Textbox(label='Ask the bot')
73
+ clear = gr.Button('Clear')
74
+ msg.submit(user,[msg,chatbot],[msg,chatbot],queue=False).then(
75
+ msg_bot, chatbot, chatbot
76
+ )
77
+ clear.click(lambda: None, None, chatbot, queue=False)
78
+
79
+ demo.queue()
80
+ demo.launch()
81
+
82
 
 
 
 
 
 
 
 
83