-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.py
More file actions
97 lines (77 loc) · 3.42 KB
/
Copy pathconverter.py
File metadata and controls
97 lines (77 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from typing import Generator, Tuple
import pypdfium2 as pdfium
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter as _DocumentConverter, PdfFormatOption
from docling_core.types.doc.document import DoclingDocument
class Converter:
"""Converts documents to DoclingDocument. Page images are rendered on demand for visual grounding."""
def __init__(self):
self._converter = _DocumentConverter(
format_options={
InputFormat.PDF: PdfFormatOption(
pipeline_options=PdfPipelineOptions(
generate_page_images=False,
generate_picture_images=False,
generate_table_images=False,
do_ocr=False,
images_scale=1.0,
),
)
}
)
@staticmethod
def page_count(source: str) -> int:
"""Return the total page count of a PDF without running the ML pipeline.
Uses pypdfium2 for a fast read of the PDF cross-reference table.
Args:
source: Local path to a PDF file.
Returns:
Total number of pages.
"""
doc = pdfium.PdfDocument(source)
try:
return len(doc)
finally:
doc.close()
def convert(self, source: str) -> DoclingDocument:
"""Convert a PDF to a DoclingDocument.
Args:
source: Local file path or URL to a PDF.
Returns:
DoclingDocument containing layout, text, and tables.
"""
return self._converter.convert(source=source).document
def convert_page_range(self, source: str, start_page: int, end_page: int) -> DoclingDocument:
"""Convert a contiguous page range of a PDF to a DoclingDocument.
This avoids loading the entire document into memory at once, which is
useful for very large PDFs that cause memory spikes on specific pages.
Args:
source: Local file path to a PDF.
start_page: First page to convert (1-indexed, inclusive).
end_page: Last page to convert (1-indexed, inclusive).
Returns:
DoclingDocument for the requested page range.
"""
return self._converter.convert(
source=source,
page_range=(start_page, end_page),
).document
def convert_in_page_chunks(
self, source: str, chunk_size: int
) -> Generator[Tuple[int, int, DoclingDocument], None, None]:
"""Convert a PDF in sequential page-range chunks to limit peak memory use.
Converts ``chunk_size`` pages at a time (e.g. 1–50, 51–100, …) so that
the ML pipeline never has to hold the full document in memory at once.
Use this for documents with memory-spike pages.
Args:
source: Local file path to a PDF.
chunk_size: Number of pages per chunk (e.g. 50).
Yields:
``(start_page, end_page, DoclingDocument)`` tuples where
*start_page* and *end_page* are 1-indexed and inclusive.
"""
total_pages = self.page_count(source)
for start in range(1, total_pages + 1, chunk_size):
end = min(start + chunk_size - 1, total_pages)
yield start, end, self.convert_page_range(source, start, end)