Introduce page-level error checks

Signed-off-by: Christoph Auer <cau@zurich.ibm.com>
This commit is contained in:
Christoph Auer 2024-08-23 11:41:18 +02:00
parent 1930f08d4e
commit 591a3c7b6b
4 changed files with 43 additions and 38 deletions

View File

@ -7,8 +7,6 @@ from PIL import Image
class PdfPageBackend(ABC): class PdfPageBackend(ABC):
def __init__(self, page_obj: Any) -> object:
pass
@abstractmethod @abstractmethod
def get_text_in_rect(self, bbox: "BoundingBox") -> str: def get_text_in_rect(self, bbox: "BoundingBox") -> str:

View File

@ -19,7 +19,6 @@ class DoclingParsePageBackend(PdfPageBackend):
def __init__( def __init__(
self, parser: pdf_parser, document_hash: str, page_no: int, page_obj: PdfPage self, parser: pdf_parser, document_hash: str, page_no: int, page_obj: PdfPage
): ):
super().__init__(page_obj)
self._ppage = page_obj self._ppage = page_obj
parsed_page = parser.parse_pdf_from_key_on_page(document_hash, page_no) parsed_page = parser.parse_pdf_from_key_on_page(document_hash, page_no)

View File

@ -13,9 +13,8 @@ from docling.datamodel.base_models import BoundingBox, Cell, CoordOrigin, PageSi
class PyPdfiumPageBackend(PdfPageBackend): class PyPdfiumPageBackend(PdfPageBackend):
def __init__(self, page_obj: PdfPage): def __init__(self, pdfium_doc: pdfium.PdfDocument, page_no: int):
super().__init__(page_obj) self._ppage: pdfium.PdfPage = pdfium_doc[page_no]
self._ppage = page_obj
self.text_page = None self.text_page = None
def get_bitmap_rects(self, scale: int = 1) -> Iterable[BoundingBox]: def get_bitmap_rects(self, scale: int = 1) -> Iterable[BoundingBox]:
@ -223,7 +222,7 @@ class PyPdfiumDocumentBackend(PdfDocumentBackend):
return len(self._pdoc) return len(self._pdoc)
def load_page(self, page_no: int) -> PyPdfiumPageBackend: def load_page(self, page_no: int) -> PyPdfiumPageBackend:
return PyPdfiumPageBackend(self._pdoc[page_no]) return PyPdfiumPageBackend(self._pdoc, page_no)
def is_valid(self) -> bool: def is_valid(self) -> bool:
return self.page_count() > 0 return self.page_count() > 0

View File

@ -157,6 +157,7 @@ class DocumentConverter:
for page_batch in chunkify( for page_batch in chunkify(
converted_doc.pages, settings.perf.page_batch_size converted_doc.pages, settings.perf.page_batch_size
): ):
try:
start_pb_time = time.time() start_pb_time = time.time()
# Pipeline # Pipeline
@ -199,6 +200,12 @@ class DocumentConverter:
end_pb_time = time.time() - start_pb_time end_pb_time = time.time() - start_pb_time
_log.info(f"Finished converting page batch time={end_pb_time:.3f}") _log.info(f"Finished converting page batch time={end_pb_time:.3f}")
except Exception as e:
trace = "\n".join(traceback.format_exception(e))
_log.info(
f"Encountered an error during processing of page batch: {trace}"
)
# Free up mem resources of PDF backend # Free up mem resources of PDF backend
in_doc._backend.unload() in_doc._backend.unload()
@ -230,7 +237,9 @@ class DocumentConverter:
# Generate the page image and store it in the page object # Generate the page image and store it in the page object
def populate_page_images(self, doc: InputDocument, page: Page) -> Page: def populate_page_images(self, doc: InputDocument, page: Page) -> Page:
# default scale # default scale
page.get_image(scale=1.0) page.get_image(
scale=1.0
) # puts the page image on the image cache at default scale
# user requested scales # user requested scales
if self.assemble_options.images_scale is not None: if self.assemble_options.images_scale is not None: