Spaces:
Runtime error
Runtime error
Iterate page-by-page: Instead of failing on the whole document, it now processes each page individually.
Browse filesGraceful 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.
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|