Hasnan Ramadhan commited on
Commit
9899a61
·
1 Parent(s): 4b496f2

summarize immediately

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -236,10 +236,16 @@ def process_pdf_and_chat(pdf_file, message, history, system_message, max_tokens,
236
  return history + [(message, "Please upload a PDF file first.")]
237
 
238
  try:
239
- # Create a temporary file path for the uploaded PDF
240
- with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
241
- tmp_file.write(pdf_file.read())
242
- tmp_pdf_path = tmp_file.name
 
 
 
 
 
 
243
 
244
  # Check if user wants to search for additional information
245
  search_keywords = ['search', 'find more', 'additional info', 'more information', 'research']
@@ -294,8 +300,9 @@ def process_pdf_and_chat(pdf_file, message, history, system_message, max_tokens,
294
  else:
295
  response = "Unable to process the PDF. Please check the file format."
296
 
297
- # Clean up temporary file
298
- os.unlink(tmp_pdf_path)
 
299
 
300
  return history + [(message, response)]
301
 
@@ -336,10 +343,16 @@ def process_pdf_and_chat_messages(pdf_file, message, history, system_message, ma
336
  return "Please upload a PDF file first."
337
 
338
  try:
339
- # Create a temporary file path for the uploaded PDF
340
- with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
341
- tmp_file.write(pdf_file.read())
342
- tmp_pdf_path = tmp_file.name
 
 
 
 
 
 
343
 
344
  # Check if user wants to search for additional information
345
  search_keywords = ['search', 'find more', 'additional info', 'more information', 'research']
@@ -394,8 +407,9 @@ def process_pdf_and_chat_messages(pdf_file, message, history, system_message, ma
394
  else:
395
  response = "Unable to process the PDF. Please check the file format."
396
 
397
- # Clean up temporary file
398
- os.unlink(tmp_pdf_path)
 
399
 
400
  return response
401
 
@@ -463,10 +477,22 @@ with gr.Blocks() as demo:
463
  response = respond_messages(message, history[:-1], system_message, max_tokens, temperature, top_p, enable_search)
464
  return history[:-1] + [{"role": "user", "content": message}, {"role": "assistant", "content": response}]
465
 
 
 
 
 
 
 
 
 
 
466
  msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
467
  bot_response, [chatbot, pdf_upload, enable_search, system_message, max_tokens, temperature, top_p], chatbot
468
  )
469
  clear.click(lambda: None, None, chatbot, queue=False)
 
 
 
470
 
471
  if __name__ == "__main__":
472
  demo.launch(share=True)
 
236
  return history + [(message, "Please upload a PDF file first.")]
237
 
238
  try:
239
+ # Handle file path - in newer Gradio versions, pdf_file is already a path
240
+ if isinstance(pdf_file, str):
241
+ tmp_pdf_path = pdf_file
242
+ cleanup_needed = False
243
+ else:
244
+ # For older versions where pdf_file is a file object
245
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
246
+ tmp_file.write(pdf_file.read())
247
+ tmp_pdf_path = tmp_file.name
248
+ cleanup_needed = True
249
 
250
  # Check if user wants to search for additional information
251
  search_keywords = ['search', 'find more', 'additional info', 'more information', 'research']
 
300
  else:
301
  response = "Unable to process the PDF. Please check the file format."
302
 
303
+ # Clean up temporary file only if we created it
304
+ if cleanup_needed:
305
+ os.unlink(tmp_pdf_path)
306
 
307
  return history + [(message, response)]
308
 
 
343
  return "Please upload a PDF file first."
344
 
345
  try:
346
+ # Handle file path - in newer Gradio versions, pdf_file is already a path
347
+ if isinstance(pdf_file, str):
348
+ tmp_pdf_path = pdf_file
349
+ cleanup_needed = False
350
+ else:
351
+ # For older versions where pdf_file is a file object
352
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
353
+ tmp_file.write(pdf_file.read())
354
+ tmp_pdf_path = tmp_file.name
355
+ cleanup_needed = True
356
 
357
  # Check if user wants to search for additional information
358
  search_keywords = ['search', 'find more', 'additional info', 'more information', 'research']
 
407
  else:
408
  response = "Unable to process the PDF. Please check the file format."
409
 
410
+ # Clean up temporary file only if we created it
411
+ if cleanup_needed:
412
+ os.unlink(tmp_pdf_path)
413
 
414
  return response
415
 
 
477
  response = respond_messages(message, history[:-1], system_message, max_tokens, temperature, top_p, enable_search)
478
  return history[:-1] + [{"role": "user", "content": message}, {"role": "assistant", "content": response}]
479
 
480
+ def auto_summarize_pdf(pdf_file):
481
+ """Automatically summarize PDF when uploaded"""
482
+ if pdf_file is None:
483
+ return []
484
+
485
+ # Trigger automatic summarization
486
+ response = process_pdf_and_chat_messages(pdf_file, "Please provide a summary of this document", [], "You are a helpful assistant for summarizing documents.", 512, 0.7, 0.95, False)
487
+ return [{"role": "assistant", "content": response}]
488
+
489
  msg.submit(user_input, [msg, chatbot], [msg, chatbot], queue=False).then(
490
  bot_response, [chatbot, pdf_upload, enable_search, system_message, max_tokens, temperature, top_p], chatbot
491
  )
492
  clear.click(lambda: None, None, chatbot, queue=False)
493
+
494
+ # Auto-summarize when PDF is uploaded
495
+ pdf_upload.upload(auto_summarize_pdf, [pdf_upload], [chatbot])
496
 
497
  if __name__ == "__main__":
498
  demo.launch(share=True)