chrisjcc commited on
Commit
ad92b4b
·
1 Parent(s): 0882126

Iterate page-by-page: Instead of failing on the whole document, it now processes each page individually.

Browse files

Graceful Skip: If a specific page fails (like the one with the 'bbox' error), it logs a warning, skips that page, inserts a placeholder [Error extracting page X], and continues to the next page.

Files changed (1) hide show
  1. app.py +13 -3
app.py CHANGED
@@ -446,11 +446,21 @@ def query_agent(question: str, files: Optional[List[FilePayload]] = None) -> str
446
  if fmt == 'pdf':
447
  try:
448
  pdf_reader = pypdf.PdfReader(io.BytesIO(file_bytes))
449
- for page in pdf_reader.pages:
450
- extracted_text += page.extract_text() + "\n"
 
 
 
 
 
 
 
 
 
 
451
  except Exception as pdf_err:
452
  logger.error(f"PDF extraction failed for {file_obj.name}: {pdf_err}")
453
- extracted_text = f"[Error extracting PDF text for {file_obj.name}]"
454
  else:
455
  # Try decoding as plain text (csv, txt, md, html, etc)
456
  try:
 
446
  if fmt == 'pdf':
447
  try:
448
  pdf_reader = pypdf.PdfReader(io.BytesIO(file_bytes))
449
+ for i, page in enumerate(pdf_reader.pages):
450
+ try:
451
+ text = page.extract_text()
452
+ if text:
453
+ extracted_text += text + "\n"
454
+ except Exception as page_err:
455
+ logger.warning(f"Failed to extract text from page {i} of {file_obj.name}: {page_err}")
456
+ extracted_text += f"[Error extracting page {i+1}]\n"
457
+
458
+ if not extracted_text.strip():
459
+ extracted_text = "[No text could be extracted from this PDF. It might be an image-only PDF.]"
460
+
461
  except Exception as pdf_err:
462
  logger.error(f"PDF extraction failed for {file_obj.name}: {pdf_err}")
463
+ extracted_text = f"[Error extracting PDF text for {file_obj.name}: {str(pdf_err)}]"
464
  else:
465
  # Try decoding as plain text (csv, txt, md, html, etc)
466
  try: