Crackershoot commited on
Commit
320b22a
Β·
verified Β·
1 Parent(s): b02bf26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -23
app.py CHANGED
@@ -174,29 +174,65 @@ def download_pdf_from_url(url):
174
  # -----------------------------
175
  # Display PDF
176
  # -----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  def display_pdf(pdf_url):
178
  if not pdf_url:
179
- return gr.update(value=None, visible=False)
180
-
 
 
 
181
  try:
182
- logger.info(f"Displaying PDF from: {pdf_url}")
 
 
 
 
 
183
  pdf_bytes = download_pdf_from_url(pdf_url)
184
  doc = fitz.open(stream=pdf_bytes, filetype="pdf")
185
  page = doc[0]
186
-
187
- zoom = 5
188
- mat = fitz.Matrix(zoom, zoom)
189
  pix = page.get_pixmap(matrix=mat)
190
-
191
- img_data = pix.tobytes("png")
192
- img = Image.open(io.BytesIO(img_data))
193
  doc.close()
194
-
195
- logger.info("PDF displayed successfully")
196
- return gr.update(value=img, visible=True)
 
 
 
197
  except Exception as e:
198
- logger.error(f"Error displaying PDF: {str(e)}")
199
- return gr.update(value=None, visible=False)
 
 
 
200
 
201
  # -----------------------------
202
  # UI Wrapper for Blocks
@@ -322,6 +358,7 @@ with gr.Blocks(
322
  with gr.Column(scale=3):
323
 
324
  chatbot = gr.Chatbot(label="πŸ’¬ Conversation", elem_classes="chatbot")
 
325
 
326
  question = gr.Textbox(
327
  label="Ask Dox a question:",
@@ -348,7 +385,7 @@ with gr.Blocks(
348
  gr.Markdown("### πŸ“„ Referenced PDF Document")
349
 
350
  link_state = gr.State()
351
-
352
  output_image = gr.Image(
353
  label="Preview (Page 1)",
354
  visible=False
@@ -361,6 +398,15 @@ with gr.Blocks(
361
  if chat_history is None:
362
  chat_history = []
363
 
 
 
 
 
 
 
 
 
 
364
  response_text, link = ask_agent(user_message)
365
 
366
  chat_history.append({
@@ -373,31 +419,32 @@ with gr.Blocks(
373
  "content": response_text
374
  })
375
 
376
- return (
377
  chat_history,
378
  link,
379
- gr.update(value=None, visible=False)
 
 
380
  )
381
 
382
  ask_btn.click(
383
  chat_ui,
384
  inputs=[question, chatbot],
385
- outputs=[chatbot, link_state, output_image]
386
  ).then(
387
  display_pdf,
388
  inputs=link_state,
389
- outputs=output_image
390
  )
391
-
392
- # Enter key submit
393
  question.submit(
394
  chat_ui,
395
  inputs=[question, chatbot],
396
- outputs=[chatbot, link_state, output_image]
397
  ).then(
398
  display_pdf,
399
  inputs=link_state,
400
- outputs=output_image
401
  )
402
 
403
  if __name__ == "__main__":
 
174
  # -----------------------------
175
  # Display PDF
176
  # -----------------------------
177
+ # def display_pdf(pdf_url):
178
+ # if not pdf_url:
179
+ # return gr.update(value=None, visible=False)
180
+
181
+ # try:
182
+ # logger.info(f"Displaying PDF from: {pdf_url}")
183
+ # pdf_bytes = download_pdf_from_url(pdf_url)
184
+ # doc = fitz.open(stream=pdf_bytes, filetype="pdf")
185
+ # page = doc[0]
186
+
187
+ # zoom = 5
188
+ # mat = fitz.Matrix(zoom, zoom)
189
+ # pix = page.get_pixmap(matrix=mat)
190
+
191
+ # img_data = pix.tobytes("png")
192
+ # img = Image.open(io.BytesIO(img_data))
193
+ # doc.close()
194
+
195
+ # logger.info("PDF displayed successfully")
196
+ # return gr.update(value=img, visible=True)
197
+ # except Exception as e:
198
+ # logger.error(f"Error displaying PDF: {str(e)}")
199
+ # return gr.update(value=None, visible=False)
200
+
201
  def display_pdf(pdf_url):
202
  if not pdf_url:
203
+ return (
204
+ gr.update(value=None, visible=False),
205
+ gr.update(value="", visible=False)
206
+ )
207
+
208
  try:
209
+ # Show loading text
210
+ yield (
211
+ gr.update(value=None, visible=False),
212
+ gr.update(value="πŸ“„ Loading PDF preview...", visible=True)
213
+ )
214
+
215
  pdf_bytes = download_pdf_from_url(pdf_url)
216
  doc = fitz.open(stream=pdf_bytes, filetype="pdf")
217
  page = doc[0]
218
+
219
+ mat = fitz.Matrix(5, 5)
 
220
  pix = page.get_pixmap(matrix=mat)
221
+
222
+ img = Image.open(io.BytesIO(pix.tobytes("png")))
 
223
  doc.close()
224
+
225
+ yield (
226
+ gr.update(value=img, visible=True),
227
+ gr.update(value="", visible=False)
228
+ )
229
+
230
  except Exception as e:
231
+ logger.error(f"PDF error: {e}")
232
+ yield (
233
+ gr.update(value=None, visible=False),
234
+ gr.update(value="❌ Failed to load PDF", visible=True)
235
+ )
236
 
237
  # -----------------------------
238
  # UI Wrapper for Blocks
 
358
  with gr.Column(scale=3):
359
 
360
  chatbot = gr.Chatbot(label="πŸ’¬ Conversation", elem_classes="chatbot")
361
+ status_text = gr.Markdown("")
362
 
363
  question = gr.Textbox(
364
  label="Ask Dox a question:",
 
385
  gr.Markdown("### πŸ“„ Referenced PDF Document")
386
 
387
  link_state = gr.State()
388
+ pdf_status = gr.Markdown(visible=False)
389
  output_image = gr.Image(
390
  label="Preview (Page 1)",
391
  visible=False
 
398
  if chat_history is None:
399
  chat_history = []
400
 
401
+ # Show thinking immediately
402
+ yield (
403
+ chat_history,
404
+ None,
405
+ gr.update(value=None, visible=False),
406
+ "πŸ€” Thinking...",
407
+ ""
408
+ )
409
+
410
  response_text, link = ask_agent(user_message)
411
 
412
  chat_history.append({
 
419
  "content": response_text
420
  })
421
 
422
+ yield (
423
  chat_history,
424
  link,
425
+ gr.update(value=None, visible=False),
426
+ "", # clear thinking
427
+ ""
428
  )
429
 
430
  ask_btn.click(
431
  chat_ui,
432
  inputs=[question, chatbot],
433
+ outputs=[chatbot, link_state, output_image, status_text, question]
434
  ).then(
435
  display_pdf,
436
  inputs=link_state,
437
+ outputs=[output_image, pdf_status]
438
  )
439
+
 
440
  question.submit(
441
  chat_ui,
442
  inputs=[question, chatbot],
443
+ outputs=[chatbot, link_state, output_image, status_text, question]
444
  ).then(
445
  display_pdf,
446
  inputs=link_state,
447
+ outputs=[output_image, pdf_status]
448
  )
449
 
450
  if __name__ == "__main__":