Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Initialize docling PDF parser on module level #80

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docling/backend/docling_parse_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

_log = logging.getLogger(__name__)

pdf_parser = pdf_parser()


class DoclingParsePageBackend(PdfPageBackend):
def __init__(
Expand Down Expand Up @@ -190,7 +192,7 @@ def __init__(self, path_or_stream: Union[BytesIO, Path], document_hash: str):
super().__init__(path_or_stream, document_hash)

self._pdoc = pdfium.PdfDocument(path_or_stream)
self.parser = pdf_parser()
self.parser = pdf_parser

success = False
if isinstance(path_or_stream, BytesIO):
Expand Down
11 changes: 7 additions & 4 deletions tests/test_backend_docling_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_doc_path():
def test_text_cell_counts():
pdf_doc = Path("./tests/data/redp5695.pdf")

doc_backend = DoclingParseDocumentBackend(pdf_doc, "123456xyz")
doc_backend = DoclingParseDocumentBackend(pdf_doc, "123456xyz5")

for page_index in range(0, doc_backend.page_count()):
last_cell_count = None
Expand All @@ -36,7 +36,7 @@ def test_text_cell_counts():


def test_get_text_from_rect(test_doc_path):
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz")
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz4")
page_backend: DoclingParsePageBackend = doc_backend.load_page(0)

# Get the title text of the DocLayNet paper
Expand All @@ -46,19 +46,22 @@ def test_get_text_from_rect(test_doc_path):
ref = "DocLayNet: A Large Human-Annotated Dataset for Document-Layout Analysis"

assert textpiece.strip() == ref
doc_backend.unload()


def test_crop_page_image(test_doc_path):
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz")
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz3")
page_backend: DoclingParsePageBackend = doc_backend.load_page(0)

# Crop out "Figure 1" from the DocLayNet paper
im = page_backend.get_page_image(
scale=2, cropbox=BoundingBox(l=317, t=246, r=574, b=527)
)
# im.show()
doc_backend.unload()


def test_num_pages(test_doc_path):
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz")
doc_backend = DoclingParseDocumentBackend(test_doc_path, "123456xyz2")
doc_backend.page_count() == 9
doc_backend.unload()
Loading