Update app.py
Browse files
app.py
CHANGED
@@ -123,9 +123,28 @@ def upload_pdf(pdf_file):
|
|
123 |
|
124 |
|
125 |
def respond(message, history):
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
def clear_chatbot():
|
131 |
pdf_chatbot.memory.clear()
|
@@ -153,7 +172,18 @@ with gr.Blocks() as demo:
|
|
153 |
msg = gr.Textbox()
|
154 |
clear = gr.Button("Clear")
|
155 |
|
156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
clear.click(clear_chatbot, outputs=[chatbot_interface])
|
158 |
path_button.click(get_pdf_path, outputs=[pdf_path_display])
|
159 |
|
|
|
123 |
|
124 |
|
125 |
def respond(message, history):
|
126 |
+
if not pdf_chatbot.qa_chain:
|
127 |
+
return "", history, "", "", "", "", "", ""
|
128 |
+
|
129 |
+
# Generate response using QA chain
|
130 |
+
response = pdf_chatbot.chat(message)
|
131 |
+
response_answer = response["answer"]
|
132 |
+
if response_answer.find("Helpful Answer:") != -1:
|
133 |
+
response_answer = response_answer.split("Helpful Answer:")[-1]
|
134 |
+
response_sources = response["source_documents"]
|
135 |
+
|
136 |
+
# Extract source documents and page numbers
|
137 |
+
response_source1 = response_sources[0].page_content.strip() if len(response_sources) > 0 else ""
|
138 |
+
response_source2 = response_sources[1].page_content.strip() if len(response_sources) > 1 else ""
|
139 |
+
response_source3 = response_sources[2].page_content.strip() if len(response_sources) > 2 else ""
|
140 |
+
response_source1_page = response_sources[0].metadata["page"] + 1 if len(response_sources) > 0 else ""
|
141 |
+
response_source2_page = response_sources[1].metadata["page"] + 1 if len(response_sources) > 1 else ""
|
142 |
+
response_source3_page = response_sources[2].metadata["page"] + 1 if len(response_sources) > 2 else ""
|
143 |
+
|
144 |
+
# Append user message and response to chat history
|
145 |
+
history.append((message, response_answer))
|
146 |
+
|
147 |
+
return "", history, response_source1, response_source1_page, response_source2, response_source2_page, response_source3, response_source3_page
|
148 |
|
149 |
def clear_chatbot():
|
150 |
pdf_chatbot.memory.clear()
|
|
|
172 |
msg = gr.Textbox()
|
173 |
clear = gr.Button("Clear")
|
174 |
|
175 |
+
with gr.Accordion("Advanced - Document references", open=False):
|
176 |
+
with gr.Row():
|
177 |
+
doc_source1 = gr.Textbox(label="Reference 1", lines=2, container=True, scale=20)
|
178 |
+
source1_page = gr.Number(label="Page", scale=1)
|
179 |
+
with gr.Row():
|
180 |
+
doc_source2 = gr.Textbox(label="Reference 2", lines=2, container=True, scale=20)
|
181 |
+
source2_page = gr.Number(label="Page", scale=1)
|
182 |
+
with gr.Row():
|
183 |
+
doc_source3 = gr.Textbox(label="Reference 3", lines=2, container=True, scale=20)
|
184 |
+
source3_page = gr.Number(label="Page", scale=1)
|
185 |
+
|
186 |
+
msg.submit(respond, inputs=[msg, chatbot_interface], outputs=[msg, chatbot_interface, doc_source1, source1_page, doc_source2, source2_page, doc_source3, source3_page])
|
187 |
clear.click(clear_chatbot, outputs=[chatbot_interface])
|
188 |
path_button.click(get_pdf_path, outputs=[pdf_path_display])
|
189 |
|