Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -167,9 +167,7 @@ def format_chat_history(message, chat_history):
|
|
167 |
|
168 |
def conversation(qa_chain, message, history):
|
169 |
formatted_chat_history = format_chat_history(message, history)
|
170 |
-
|
171 |
-
|
172 |
-
# Generate response using QA chain
|
173 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
174 |
response_answer = response["answer"]
|
175 |
if response_answer.find("Helpful Answer:") != -1:
|
@@ -182,12 +180,11 @@ def conversation(qa_chain, message, history):
|
|
182 |
response_source1_page = response_sources[0].metadata["page"] + 1
|
183 |
response_source2_page = response_sources[1].metadata["page"] + 1
|
184 |
response_source3_page = response_sources[2].metadata["page"] + 1
|
185 |
-
# print ('chat response: ', response_answer)
|
186 |
-
# print('DB source', response_sources)
|
187 |
|
188 |
-
|
|
|
189 |
new_history = history + [(message, response_answer)]
|
190 |
-
|
191 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
192 |
|
193 |
|
@@ -196,8 +193,7 @@ def upload_file(file_obj):
|
|
196 |
for idx, file in enumerate(file_obj):
|
197 |
file_path = file_obj.name
|
198 |
list_file_path.append(file_path)
|
199 |
-
|
200 |
-
# initialize_database(file_path, progress)
|
201 |
return list_file_path
|
202 |
|
203 |
|
@@ -207,20 +203,10 @@ def demo():
|
|
207 |
qa_chain = gr.State()
|
208 |
collection_name = gr.State()
|
209 |
|
210 |
-
gr.Markdown(
|
211 |
-
"""<center><h2>PDF-based chatbot</center></h2>
|
212 |
-
<h3>Ask any questions about your PDF documents</h3>""")
|
213 |
-
gr.Markdown(
|
214 |
-
"""<b>Note:</b> This AI assistant, using Langchain and open-source LLMs, performs retrieval-augmented generation (RAG) from your PDF documents. \
|
215 |
-
The user interface explicitely shows multiple steps to help understand the RAG workflow.
|
216 |
-
This chatbot takes past questions into account when generating answers (via conversational memory), and includes document references for clarity purposes.<br>
|
217 |
-
<br><b>Warning:</b> This space uses the free CPU Basic hardware from Hugging Face. Some steps and LLM models used below (free inference endpoints) can take some time to generate a reply.
|
218 |
-
""")
|
219 |
-
|
220 |
with gr.Tab("Step 1 - Upload PDF"):
|
221 |
with gr.Row():
|
222 |
document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
|
223 |
-
|
224 |
|
225 |
with gr.Tab("Step 2 - Process document"):
|
226 |
with gr.Row():
|
@@ -268,9 +254,7 @@ def demo():
|
|
268 |
with gr.Row():
|
269 |
submit_btn = gr.Button("Submit message")
|
270 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear conversation")
|
271 |
-
|
272 |
-
# Preprocessing events
|
273 |
-
#upload_btn.upload(upload_file, inputs=[upload_btn], outputs=[document])
|
274 |
db_btn.click(initialize_database, \
|
275 |
inputs=[document, slider_chunk_size, slider_chunk_overlap], \
|
276 |
outputs=[vector_db, collection_name, db_progress])
|
@@ -281,7 +265,7 @@ def demo():
|
|
281 |
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
282 |
queue=False)
|
283 |
|
284 |
-
|
285 |
msg.submit(conversation, \
|
286 |
inputs=[qa_chain, msg, chatbot], \
|
287 |
outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
|
|
167 |
|
168 |
def conversation(qa_chain, message, history):
|
169 |
formatted_chat_history = format_chat_history(message, history)
|
170 |
+
|
|
|
|
|
171 |
response = qa_chain({"question": message, "chat_history": formatted_chat_history})
|
172 |
response_answer = response["answer"]
|
173 |
if response_answer.find("Helpful Answer:") != -1:
|
|
|
180 |
response_source1_page = response_sources[0].metadata["page"] + 1
|
181 |
response_source2_page = response_sources[1].metadata["page"] + 1
|
182 |
response_source3_page = response_sources[2].metadata["page"] + 1
|
|
|
|
|
183 |
|
184 |
+
|
185 |
+
|
186 |
new_history = history + [(message, response_answer)]
|
187 |
+
|
188 |
return qa_chain, gr.update(value=""), new_history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
189 |
|
190 |
|
|
|
193 |
for idx, file in enumerate(file_obj):
|
194 |
file_path = file_obj.name
|
195 |
list_file_path.append(file_path)
|
196 |
+
|
|
|
197 |
return list_file_path
|
198 |
|
199 |
|
|
|
203 |
qa_chain = gr.State()
|
204 |
collection_name = gr.State()
|
205 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
with gr.Tab("Step 1 - Upload PDF"):
|
207 |
with gr.Row():
|
208 |
document = gr.Files(height=100, file_count="multiple", file_types=["pdf"], interactive=True, label="Upload your PDF documents (single or multiple)")
|
209 |
+
|
210 |
|
211 |
with gr.Tab("Step 2 - Process document"):
|
212 |
with gr.Row():
|
|
|
254 |
with gr.Row():
|
255 |
submit_btn = gr.Button("Submit message")
|
256 |
clear_btn = gr.ClearButton([msg, chatbot], value="Clear conversation")
|
257 |
+
|
|
|
|
|
258 |
db_btn.click(initialize_database, \
|
259 |
inputs=[document, slider_chunk_size, slider_chunk_overlap], \
|
260 |
outputs=[vector_db, collection_name, db_progress])
|
|
|
265 |
outputs=[chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|
266 |
queue=False)
|
267 |
|
268 |
+
|
269 |
msg.submit(conversation, \
|
270 |
inputs=[qa_chain, msg, chatbot], \
|
271 |
outputs=[qa_chain, msg, chatbot, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page], \
|