docling 2.23.1__py3-none-any.whl → 2.25.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- docling/backend/html_backend.py +42 -3
- docling/cli/models.py +28 -4
- docling/datamodel/base_models.py +5 -0
- docling/datamodel/pipeline_options.py +62 -1
- docling/models/hf_vlm_model.py +180 -0
- docling/models/page_assemble_model.py +8 -0
- docling/models/picture_description_vlm_model.py +2 -2
- docling/models/readingorder_model.py +389 -0
- docling/pipeline/standard_pdf_pipeline.py +2 -2
- docling/pipeline/vlm_pipeline.py +534 -0
- docling/utils/model_downloader.py +15 -2
- docling/utils/visualization.py +5 -0
- {docling-2.23.1.dist-info → docling-2.25.0.dist-info}/METADATA +3 -3
- {docling-2.23.1.dist-info → docling-2.25.0.dist-info}/RECORD +17 -15
- docling/models/ds_glm_model.py +0 -386
- {docling-2.23.1.dist-info → docling-2.25.0.dist-info}/LICENSE +0 -0
- {docling-2.23.1.dist-info → docling-2.25.0.dist-info}/WHEEL +0 -0
- {docling-2.23.1.dist-info → docling-2.25.0.dist-info}/entry_points.txt +0 -0
@@ -0,0 +1,534 @@
|
|
1
|
+
import itertools
|
2
|
+
import logging
|
3
|
+
import re
|
4
|
+
import warnings
|
5
|
+
from io import BytesIO
|
6
|
+
|
7
|
+
# from io import BytesIO
|
8
|
+
from pathlib import Path
|
9
|
+
from typing import Optional
|
10
|
+
|
11
|
+
from docling_core.types import DoclingDocument
|
12
|
+
from docling_core.types.doc import (
|
13
|
+
BoundingBox,
|
14
|
+
DocItem,
|
15
|
+
DocItemLabel,
|
16
|
+
DoclingDocument,
|
17
|
+
GroupLabel,
|
18
|
+
ImageRef,
|
19
|
+
ImageRefMode,
|
20
|
+
PictureItem,
|
21
|
+
ProvenanceItem,
|
22
|
+
Size,
|
23
|
+
TableCell,
|
24
|
+
TableData,
|
25
|
+
TableItem,
|
26
|
+
)
|
27
|
+
from docling_core.types.doc.tokens import DocumentToken, TableToken
|
28
|
+
|
29
|
+
from docling.backend.abstract_backend import AbstractDocumentBackend
|
30
|
+
from docling.backend.md_backend import MarkdownDocumentBackend
|
31
|
+
from docling.backend.pdf_backend import PdfDocumentBackend
|
32
|
+
from docling.datamodel.base_models import InputFormat, Page
|
33
|
+
from docling.datamodel.document import ConversionResult, InputDocument
|
34
|
+
from docling.datamodel.pipeline_options import (
|
35
|
+
PdfPipelineOptions,
|
36
|
+
ResponseFormat,
|
37
|
+
VlmPipelineOptions,
|
38
|
+
)
|
39
|
+
from docling.datamodel.settings import settings
|
40
|
+
from docling.models.hf_vlm_model import HuggingFaceVlmModel
|
41
|
+
from docling.pipeline.base_pipeline import PaginatedPipeline
|
42
|
+
from docling.utils.profiling import ProfilingScope, TimeRecorder
|
43
|
+
|
44
|
+
_log = logging.getLogger(__name__)
|
45
|
+
|
46
|
+
|
47
|
+
class VlmPipeline(PaginatedPipeline):
|
48
|
+
|
49
|
+
def __init__(self, pipeline_options: VlmPipelineOptions):
|
50
|
+
super().__init__(pipeline_options)
|
51
|
+
self.keep_backend = True
|
52
|
+
|
53
|
+
warnings.warn(
|
54
|
+
"The VlmPipeline is currently experimental and may change in upcoming versions without notice.",
|
55
|
+
category=UserWarning,
|
56
|
+
stacklevel=2,
|
57
|
+
)
|
58
|
+
|
59
|
+
self.pipeline_options: VlmPipelineOptions
|
60
|
+
|
61
|
+
artifacts_path: Optional[Path] = None
|
62
|
+
if pipeline_options.artifacts_path is not None:
|
63
|
+
artifacts_path = Path(pipeline_options.artifacts_path).expanduser()
|
64
|
+
elif settings.artifacts_path is not None:
|
65
|
+
artifacts_path = Path(settings.artifacts_path).expanduser()
|
66
|
+
|
67
|
+
if artifacts_path is not None and not artifacts_path.is_dir():
|
68
|
+
raise RuntimeError(
|
69
|
+
f"The value of {artifacts_path=} is not valid. "
|
70
|
+
"When defined, it must point to a folder containing all models required by the pipeline."
|
71
|
+
)
|
72
|
+
|
73
|
+
# force_backend_text = False - use text that is coming from VLM response
|
74
|
+
# force_backend_text = True - get text from backend using bounding boxes predicted by SmolDocling doctags
|
75
|
+
self.force_backend_text = (
|
76
|
+
pipeline_options.force_backend_text
|
77
|
+
and pipeline_options.vlm_options.response_format == ResponseFormat.DOCTAGS
|
78
|
+
)
|
79
|
+
|
80
|
+
self.keep_images = self.pipeline_options.generate_page_images
|
81
|
+
|
82
|
+
self.build_pipe = [
|
83
|
+
HuggingFaceVlmModel(
|
84
|
+
enabled=True, # must be always enabled for this pipeline to make sense.
|
85
|
+
artifacts_path=artifacts_path,
|
86
|
+
accelerator_options=pipeline_options.accelerator_options,
|
87
|
+
vlm_options=self.pipeline_options.vlm_options,
|
88
|
+
),
|
89
|
+
]
|
90
|
+
|
91
|
+
self.enrichment_pipe = [
|
92
|
+
# Other models working on `NodeItem` elements in the DoclingDocument
|
93
|
+
]
|
94
|
+
|
95
|
+
def initialize_page(self, conv_res: ConversionResult, page: Page) -> Page:
|
96
|
+
with TimeRecorder(conv_res, "page_init"):
|
97
|
+
page._backend = conv_res.input._backend.load_page(page.page_no) # type: ignore
|
98
|
+
if page._backend is not None and page._backend.is_valid():
|
99
|
+
page.size = page._backend.get_size()
|
100
|
+
|
101
|
+
return page
|
102
|
+
|
103
|
+
def _assemble_document(self, conv_res: ConversionResult) -> ConversionResult:
|
104
|
+
with TimeRecorder(conv_res, "doc_assemble", scope=ProfilingScope.DOCUMENT):
|
105
|
+
|
106
|
+
if (
|
107
|
+
self.pipeline_options.vlm_options.response_format
|
108
|
+
== ResponseFormat.DOCTAGS
|
109
|
+
):
|
110
|
+
conv_res.document = self._turn_tags_into_doc(conv_res.pages)
|
111
|
+
elif (
|
112
|
+
self.pipeline_options.vlm_options.response_format
|
113
|
+
== ResponseFormat.MARKDOWN
|
114
|
+
):
|
115
|
+
conv_res.document = self._turn_md_into_doc(conv_res)
|
116
|
+
|
117
|
+
else:
|
118
|
+
raise RuntimeError(
|
119
|
+
f"Unsupported VLM response format {self.pipeline_options.vlm_options.response_format}"
|
120
|
+
)
|
121
|
+
|
122
|
+
# Generate images of the requested element types
|
123
|
+
if self.pipeline_options.generate_picture_images:
|
124
|
+
scale = self.pipeline_options.images_scale
|
125
|
+
for element, _level in conv_res.document.iterate_items():
|
126
|
+
if not isinstance(element, DocItem) or len(element.prov) == 0:
|
127
|
+
continue
|
128
|
+
if (
|
129
|
+
isinstance(element, PictureItem)
|
130
|
+
and self.pipeline_options.generate_picture_images
|
131
|
+
):
|
132
|
+
page_ix = element.prov[0].page_no - 1
|
133
|
+
page = conv_res.pages[page_ix]
|
134
|
+
assert page.size is not None
|
135
|
+
assert page.image is not None
|
136
|
+
|
137
|
+
crop_bbox = (
|
138
|
+
element.prov[0]
|
139
|
+
.bbox.scaled(scale=scale)
|
140
|
+
.to_top_left_origin(page_height=page.size.height * scale)
|
141
|
+
)
|
142
|
+
|
143
|
+
cropped_im = page.image.crop(crop_bbox.as_tuple())
|
144
|
+
element.image = ImageRef.from_pil(
|
145
|
+
cropped_im, dpi=int(72 * scale)
|
146
|
+
)
|
147
|
+
|
148
|
+
return conv_res
|
149
|
+
|
150
|
+
def _turn_md_into_doc(self, conv_res):
|
151
|
+
predicted_text = ""
|
152
|
+
for pg_idx, page in enumerate(conv_res.pages):
|
153
|
+
if page.predictions.vlm_response:
|
154
|
+
predicted_text += page.predictions.vlm_response.text + "\n\n"
|
155
|
+
response_bytes = BytesIO(predicted_text.encode("utf8"))
|
156
|
+
out_doc = InputDocument(
|
157
|
+
path_or_stream=response_bytes,
|
158
|
+
filename=conv_res.input.file.name,
|
159
|
+
format=InputFormat.MD,
|
160
|
+
backend=MarkdownDocumentBackend,
|
161
|
+
)
|
162
|
+
backend = MarkdownDocumentBackend(
|
163
|
+
in_doc=out_doc,
|
164
|
+
path_or_stream=response_bytes,
|
165
|
+
)
|
166
|
+
return backend.convert()
|
167
|
+
|
168
|
+
def _turn_tags_into_doc(self, pages: list[Page]) -> DoclingDocument:
|
169
|
+
###############################################
|
170
|
+
# Tag definitions and color mappings
|
171
|
+
###############################################
|
172
|
+
|
173
|
+
# Maps the recognized tag to a Docling label.
|
174
|
+
# Code items will be given DocItemLabel.CODE
|
175
|
+
tag_to_doclabel = {
|
176
|
+
"title": DocItemLabel.TITLE,
|
177
|
+
"document_index": DocItemLabel.DOCUMENT_INDEX,
|
178
|
+
"otsl": DocItemLabel.TABLE,
|
179
|
+
"section_header_level_1": DocItemLabel.SECTION_HEADER,
|
180
|
+
"checkbox_selected": DocItemLabel.CHECKBOX_SELECTED,
|
181
|
+
"checkbox_unselected": DocItemLabel.CHECKBOX_UNSELECTED,
|
182
|
+
"text": DocItemLabel.TEXT,
|
183
|
+
"page_header": DocItemLabel.PAGE_HEADER,
|
184
|
+
"page_footer": DocItemLabel.PAGE_FOOTER,
|
185
|
+
"formula": DocItemLabel.FORMULA,
|
186
|
+
"caption": DocItemLabel.CAPTION,
|
187
|
+
"picture": DocItemLabel.PICTURE,
|
188
|
+
"list_item": DocItemLabel.LIST_ITEM,
|
189
|
+
"footnote": DocItemLabel.FOOTNOTE,
|
190
|
+
"code": DocItemLabel.CODE,
|
191
|
+
}
|
192
|
+
|
193
|
+
# Maps each tag to an associated bounding box color.
|
194
|
+
tag_to_color = {
|
195
|
+
"title": "blue",
|
196
|
+
"document_index": "darkblue",
|
197
|
+
"otsl": "green",
|
198
|
+
"section_header_level_1": "purple",
|
199
|
+
"checkbox_selected": "black",
|
200
|
+
"checkbox_unselected": "gray",
|
201
|
+
"text": "red",
|
202
|
+
"page_header": "orange",
|
203
|
+
"page_footer": "cyan",
|
204
|
+
"formula": "pink",
|
205
|
+
"caption": "magenta",
|
206
|
+
"picture": "yellow",
|
207
|
+
"list_item": "brown",
|
208
|
+
"footnote": "darkred",
|
209
|
+
"code": "lightblue",
|
210
|
+
}
|
211
|
+
|
212
|
+
def extract_bounding_box(text_chunk: str) -> Optional[BoundingBox]:
|
213
|
+
"""Extracts <loc_...> bounding box coords from the chunk, normalized by / 500."""
|
214
|
+
coords = re.findall(r"<loc_(\d+)>", text_chunk)
|
215
|
+
if len(coords) == 4:
|
216
|
+
l, t, r, b = map(float, coords)
|
217
|
+
return BoundingBox(l=l / 500, t=t / 500, r=r / 500, b=b / 500)
|
218
|
+
return None
|
219
|
+
|
220
|
+
def extract_inner_text(text_chunk: str) -> str:
|
221
|
+
"""Strips all <...> tags inside the chunk to get the raw text content."""
|
222
|
+
return re.sub(r"<.*?>", "", text_chunk, flags=re.DOTALL).strip()
|
223
|
+
|
224
|
+
def extract_text_from_backend(page: Page, bbox: BoundingBox | None) -> str:
|
225
|
+
# Convert bounding box normalized to 0-100 into page coordinates for cropping
|
226
|
+
text = ""
|
227
|
+
if bbox:
|
228
|
+
if page.size:
|
229
|
+
bbox.l = bbox.l * page.size.width
|
230
|
+
bbox.t = bbox.t * page.size.height
|
231
|
+
bbox.r = bbox.r * page.size.width
|
232
|
+
bbox.b = bbox.b * page.size.height
|
233
|
+
if page._backend:
|
234
|
+
text = page._backend.get_text_in_rect(bbox)
|
235
|
+
return text
|
236
|
+
|
237
|
+
def otsl_parse_texts(texts, tokens):
|
238
|
+
split_word = TableToken.OTSL_NL.value
|
239
|
+
split_row_tokens = [
|
240
|
+
list(y)
|
241
|
+
for x, y in itertools.groupby(tokens, lambda z: z == split_word)
|
242
|
+
if not x
|
243
|
+
]
|
244
|
+
table_cells = []
|
245
|
+
r_idx = 0
|
246
|
+
c_idx = 0
|
247
|
+
|
248
|
+
def count_right(tokens, c_idx, r_idx, which_tokens):
|
249
|
+
span = 0
|
250
|
+
c_idx_iter = c_idx
|
251
|
+
while tokens[r_idx][c_idx_iter] in which_tokens:
|
252
|
+
c_idx_iter += 1
|
253
|
+
span += 1
|
254
|
+
if c_idx_iter >= len(tokens[r_idx]):
|
255
|
+
return span
|
256
|
+
return span
|
257
|
+
|
258
|
+
def count_down(tokens, c_idx, r_idx, which_tokens):
|
259
|
+
span = 0
|
260
|
+
r_idx_iter = r_idx
|
261
|
+
while tokens[r_idx_iter][c_idx] in which_tokens:
|
262
|
+
r_idx_iter += 1
|
263
|
+
span += 1
|
264
|
+
if r_idx_iter >= len(tokens):
|
265
|
+
return span
|
266
|
+
return span
|
267
|
+
|
268
|
+
for i, text in enumerate(texts):
|
269
|
+
cell_text = ""
|
270
|
+
if text in [
|
271
|
+
TableToken.OTSL_FCEL.value,
|
272
|
+
TableToken.OTSL_ECEL.value,
|
273
|
+
TableToken.OTSL_CHED.value,
|
274
|
+
TableToken.OTSL_RHED.value,
|
275
|
+
TableToken.OTSL_SROW.value,
|
276
|
+
]:
|
277
|
+
row_span = 1
|
278
|
+
col_span = 1
|
279
|
+
right_offset = 1
|
280
|
+
if text != TableToken.OTSL_ECEL.value:
|
281
|
+
cell_text = texts[i + 1]
|
282
|
+
right_offset = 2
|
283
|
+
|
284
|
+
# Check next element(s) for lcel / ucel / xcel, set properly row_span, col_span
|
285
|
+
next_right_cell = ""
|
286
|
+
if i + right_offset < len(texts):
|
287
|
+
next_right_cell = texts[i + right_offset]
|
288
|
+
|
289
|
+
next_bottom_cell = ""
|
290
|
+
if r_idx + 1 < len(split_row_tokens):
|
291
|
+
if c_idx < len(split_row_tokens[r_idx + 1]):
|
292
|
+
next_bottom_cell = split_row_tokens[r_idx + 1][c_idx]
|
293
|
+
|
294
|
+
if next_right_cell in [
|
295
|
+
TableToken.OTSL_LCEL.value,
|
296
|
+
TableToken.OTSL_XCEL.value,
|
297
|
+
]:
|
298
|
+
# we have horisontal spanning cell or 2d spanning cell
|
299
|
+
col_span += count_right(
|
300
|
+
split_row_tokens,
|
301
|
+
c_idx + 1,
|
302
|
+
r_idx,
|
303
|
+
[TableToken.OTSL_LCEL.value, TableToken.OTSL_XCEL.value],
|
304
|
+
)
|
305
|
+
if next_bottom_cell in [
|
306
|
+
TableToken.OTSL_UCEL.value,
|
307
|
+
TableToken.OTSL_XCEL.value,
|
308
|
+
]:
|
309
|
+
# we have a vertical spanning cell or 2d spanning cell
|
310
|
+
row_span += count_down(
|
311
|
+
split_row_tokens,
|
312
|
+
c_idx,
|
313
|
+
r_idx + 1,
|
314
|
+
[TableToken.OTSL_UCEL.value, TableToken.OTSL_XCEL.value],
|
315
|
+
)
|
316
|
+
|
317
|
+
table_cells.append(
|
318
|
+
TableCell(
|
319
|
+
text=cell_text.strip(),
|
320
|
+
row_span=row_span,
|
321
|
+
col_span=col_span,
|
322
|
+
start_row_offset_idx=r_idx,
|
323
|
+
end_row_offset_idx=r_idx + row_span,
|
324
|
+
start_col_offset_idx=c_idx,
|
325
|
+
end_col_offset_idx=c_idx + col_span,
|
326
|
+
)
|
327
|
+
)
|
328
|
+
if text in [
|
329
|
+
TableToken.OTSL_FCEL.value,
|
330
|
+
TableToken.OTSL_ECEL.value,
|
331
|
+
TableToken.OTSL_CHED.value,
|
332
|
+
TableToken.OTSL_RHED.value,
|
333
|
+
TableToken.OTSL_SROW.value,
|
334
|
+
TableToken.OTSL_LCEL.value,
|
335
|
+
TableToken.OTSL_UCEL.value,
|
336
|
+
TableToken.OTSL_XCEL.value,
|
337
|
+
]:
|
338
|
+
c_idx += 1
|
339
|
+
if text == TableToken.OTSL_NL.value:
|
340
|
+
r_idx += 1
|
341
|
+
c_idx = 0
|
342
|
+
return table_cells, split_row_tokens
|
343
|
+
|
344
|
+
def otsl_extract_tokens_and_text(s: str):
|
345
|
+
# Pattern to match anything enclosed by < > (including the angle brackets themselves)
|
346
|
+
pattern = r"(<[^>]+>)"
|
347
|
+
# Find all tokens (e.g. "<otsl>", "<loc_140>", etc.)
|
348
|
+
tokens = re.findall(pattern, s)
|
349
|
+
# Remove any tokens that start with "<loc_"
|
350
|
+
tokens = [
|
351
|
+
token
|
352
|
+
for token in tokens
|
353
|
+
if not (
|
354
|
+
token.startswith(rf"<{DocumentToken.LOC.value}")
|
355
|
+
or token
|
356
|
+
in [
|
357
|
+
rf"<{DocumentToken.OTSL.value}>",
|
358
|
+
rf"</{DocumentToken.OTSL.value}>",
|
359
|
+
]
|
360
|
+
)
|
361
|
+
]
|
362
|
+
# Split the string by those tokens to get the in-between text
|
363
|
+
text_parts = re.split(pattern, s)
|
364
|
+
text_parts = [
|
365
|
+
token
|
366
|
+
for token in text_parts
|
367
|
+
if not (
|
368
|
+
token.startswith(rf"<{DocumentToken.LOC.value}")
|
369
|
+
or token
|
370
|
+
in [
|
371
|
+
rf"<{DocumentToken.OTSL.value}>",
|
372
|
+
rf"</{DocumentToken.OTSL.value}>",
|
373
|
+
]
|
374
|
+
)
|
375
|
+
]
|
376
|
+
# Remove any empty or purely whitespace strings from text_parts
|
377
|
+
text_parts = [part for part in text_parts if part.strip()]
|
378
|
+
|
379
|
+
return tokens, text_parts
|
380
|
+
|
381
|
+
def parse_table_content(otsl_content: str) -> TableData:
|
382
|
+
tokens, mixed_texts = otsl_extract_tokens_and_text(otsl_content)
|
383
|
+
table_cells, split_row_tokens = otsl_parse_texts(mixed_texts, tokens)
|
384
|
+
|
385
|
+
return TableData(
|
386
|
+
num_rows=len(split_row_tokens),
|
387
|
+
num_cols=(
|
388
|
+
max(len(row) for row in split_row_tokens) if split_row_tokens else 0
|
389
|
+
),
|
390
|
+
table_cells=table_cells,
|
391
|
+
)
|
392
|
+
|
393
|
+
doc = DoclingDocument(name="Document")
|
394
|
+
for pg_idx, page in enumerate(pages):
|
395
|
+
xml_content = ""
|
396
|
+
predicted_text = ""
|
397
|
+
if page.predictions.vlm_response:
|
398
|
+
predicted_text = page.predictions.vlm_response.text
|
399
|
+
image = page.image
|
400
|
+
|
401
|
+
page_no = pg_idx + 1
|
402
|
+
bounding_boxes = []
|
403
|
+
|
404
|
+
if page.size:
|
405
|
+
pg_width = page.size.width
|
406
|
+
pg_height = page.size.height
|
407
|
+
size = Size(width=pg_width, height=pg_height)
|
408
|
+
parent_page = doc.add_page(page_no=page_no, size=size)
|
409
|
+
|
410
|
+
"""
|
411
|
+
1. Finds all <tag>...</tag> blocks in the entire string (multi-line friendly) in the order they appear.
|
412
|
+
2. For each chunk, extracts bounding box (if any) and inner text.
|
413
|
+
3. Adds the item to a DoclingDocument structure with the right label.
|
414
|
+
4. Tracks bounding boxes + color in a separate list for later visualization.
|
415
|
+
"""
|
416
|
+
|
417
|
+
# Regex for all recognized tags
|
418
|
+
tag_pattern = (
|
419
|
+
rf"<(?P<tag>{DocItemLabel.TITLE}|{DocItemLabel.DOCUMENT_INDEX}|"
|
420
|
+
rf"{DocItemLabel.CHECKBOX_UNSELECTED}|{DocItemLabel.CHECKBOX_SELECTED}|"
|
421
|
+
rf"{DocItemLabel.TEXT}|{DocItemLabel.PAGE_HEADER}|"
|
422
|
+
rf"{DocItemLabel.PAGE_FOOTER}|{DocItemLabel.FORMULA}|"
|
423
|
+
rf"{DocItemLabel.CAPTION}|{DocItemLabel.PICTURE}|"
|
424
|
+
rf"{DocItemLabel.LIST_ITEM}|{DocItemLabel.FOOTNOTE}|{DocItemLabel.CODE}|"
|
425
|
+
rf"{DocItemLabel.SECTION_HEADER}_level_1|{DocumentToken.OTSL.value})>.*?</(?P=tag)>"
|
426
|
+
)
|
427
|
+
|
428
|
+
# DocumentToken.OTSL
|
429
|
+
pattern = re.compile(tag_pattern, re.DOTALL)
|
430
|
+
|
431
|
+
# Go through each match in order
|
432
|
+
for match in pattern.finditer(predicted_text):
|
433
|
+
full_chunk = match.group(0)
|
434
|
+
tag_name = match.group("tag")
|
435
|
+
|
436
|
+
bbox = extract_bounding_box(full_chunk)
|
437
|
+
doc_label = tag_to_doclabel.get(tag_name, DocItemLabel.PARAGRAPH)
|
438
|
+
color = tag_to_color.get(tag_name, "white")
|
439
|
+
|
440
|
+
# Store bounding box + color
|
441
|
+
if bbox:
|
442
|
+
bounding_boxes.append((bbox, color))
|
443
|
+
|
444
|
+
if tag_name == DocumentToken.OTSL.value:
|
445
|
+
table_data = parse_table_content(full_chunk)
|
446
|
+
bbox = extract_bounding_box(full_chunk)
|
447
|
+
|
448
|
+
if bbox:
|
449
|
+
prov = ProvenanceItem(
|
450
|
+
bbox=bbox.resize_by_scale(pg_width, pg_height),
|
451
|
+
charspan=(0, 0),
|
452
|
+
page_no=page_no,
|
453
|
+
)
|
454
|
+
doc.add_table(data=table_data, prov=prov)
|
455
|
+
else:
|
456
|
+
doc.add_table(data=table_data)
|
457
|
+
|
458
|
+
elif tag_name == DocItemLabel.PICTURE:
|
459
|
+
text_caption_content = extract_inner_text(full_chunk)
|
460
|
+
if image:
|
461
|
+
if bbox:
|
462
|
+
im_width, im_height = image.size
|
463
|
+
|
464
|
+
crop_box = (
|
465
|
+
int(bbox.l * im_width),
|
466
|
+
int(bbox.t * im_height),
|
467
|
+
int(bbox.r * im_width),
|
468
|
+
int(bbox.b * im_height),
|
469
|
+
)
|
470
|
+
cropped_image = image.crop(crop_box)
|
471
|
+
pic = doc.add_picture(
|
472
|
+
parent=None,
|
473
|
+
image=ImageRef.from_pil(image=cropped_image, dpi=72),
|
474
|
+
prov=(
|
475
|
+
ProvenanceItem(
|
476
|
+
bbox=bbox.resize_by_scale(pg_width, pg_height),
|
477
|
+
charspan=(0, 0),
|
478
|
+
page_no=page_no,
|
479
|
+
)
|
480
|
+
),
|
481
|
+
)
|
482
|
+
# If there is a caption to an image, add it as well
|
483
|
+
if len(text_caption_content) > 0:
|
484
|
+
caption_item = doc.add_text(
|
485
|
+
label=DocItemLabel.CAPTION,
|
486
|
+
text=text_caption_content,
|
487
|
+
parent=None,
|
488
|
+
)
|
489
|
+
pic.captions.append(caption_item.get_ref())
|
490
|
+
else:
|
491
|
+
if bbox:
|
492
|
+
# In case we don't have access to an binary of an image
|
493
|
+
doc.add_picture(
|
494
|
+
parent=None,
|
495
|
+
prov=ProvenanceItem(
|
496
|
+
bbox=bbox, charspan=(0, 0), page_no=page_no
|
497
|
+
),
|
498
|
+
)
|
499
|
+
# If there is a caption to an image, add it as well
|
500
|
+
if len(text_caption_content) > 0:
|
501
|
+
caption_item = doc.add_text(
|
502
|
+
label=DocItemLabel.CAPTION,
|
503
|
+
text=text_caption_content,
|
504
|
+
parent=None,
|
505
|
+
)
|
506
|
+
pic.captions.append(caption_item.get_ref())
|
507
|
+
else:
|
508
|
+
# For everything else, treat as text
|
509
|
+
if self.force_backend_text:
|
510
|
+
text_content = extract_text_from_backend(page, bbox)
|
511
|
+
else:
|
512
|
+
text_content = extract_inner_text(full_chunk)
|
513
|
+
doc.add_text(
|
514
|
+
label=doc_label,
|
515
|
+
text=text_content,
|
516
|
+
prov=(
|
517
|
+
ProvenanceItem(
|
518
|
+
bbox=bbox.resize_by_scale(pg_width, pg_height),
|
519
|
+
charspan=(0, len(text_content)),
|
520
|
+
page_no=page_no,
|
521
|
+
)
|
522
|
+
if bbox
|
523
|
+
else None
|
524
|
+
),
|
525
|
+
)
|
526
|
+
return doc
|
527
|
+
|
528
|
+
@classmethod
|
529
|
+
def get_default_options(cls) -> VlmPipelineOptions:
|
530
|
+
return VlmPipelineOptions()
|
531
|
+
|
532
|
+
@classmethod
|
533
|
+
def is_backend_supported(cls, backend: AbstractDocumentBackend):
|
534
|
+
return isinstance(backend, PdfDocumentBackend)
|
@@ -2,7 +2,10 @@ import logging
|
|
2
2
|
from pathlib import Path
|
3
3
|
from typing import Optional
|
4
4
|
|
5
|
-
from docling.datamodel.pipeline_options import
|
5
|
+
from docling.datamodel.pipeline_options import (
|
6
|
+
granite_picture_description,
|
7
|
+
smolvlm_picture_description,
|
8
|
+
)
|
6
9
|
from docling.datamodel.settings import settings
|
7
10
|
from docling.models.code_formula_model import CodeFormulaModel
|
8
11
|
from docling.models.document_picture_classifier import DocumentPictureClassifier
|
@@ -23,7 +26,8 @@ def download_models(
|
|
23
26
|
with_tableformer: bool = True,
|
24
27
|
with_code_formula: bool = True,
|
25
28
|
with_picture_classifier: bool = True,
|
26
|
-
with_smolvlm: bool =
|
29
|
+
with_smolvlm: bool = False,
|
30
|
+
with_granite_vision: bool = False,
|
27
31
|
with_easyocr: bool = True,
|
28
32
|
):
|
29
33
|
if output_dir is None:
|
@@ -73,6 +77,15 @@ def download_models(
|
|
73
77
|
progress=progress,
|
74
78
|
)
|
75
79
|
|
80
|
+
if with_granite_vision:
|
81
|
+
_log.info(f"Downloading Granite Vision model...")
|
82
|
+
PictureDescriptionVlmModel.download_models(
|
83
|
+
repo_id=granite_picture_description.repo_id,
|
84
|
+
local_dir=output_dir / granite_picture_description.repo_cache_folder,
|
85
|
+
force=force,
|
86
|
+
progress=progress,
|
87
|
+
)
|
88
|
+
|
76
89
|
if with_easyocr:
|
77
90
|
_log.info(f"Downloading easyocr models...")
|
78
91
|
EasyOcrModel.download_models(
|
docling/utils/visualization.py
CHANGED
@@ -43,6 +43,11 @@ def draw_clusters(
|
|
43
43
|
y0 *= scale_x
|
44
44
|
y1 *= scale_y
|
45
45
|
|
46
|
+
if y1 <= y0:
|
47
|
+
y1, y0 = y0, y1
|
48
|
+
if x1 <= x0:
|
49
|
+
x1, x0 = x0, x1
|
50
|
+
|
46
51
|
cluster_fill_color = (*list(DocItemLabel.get_color(c.label)), 70)
|
47
52
|
cluster_outline_color = (
|
48
53
|
*list(DocItemLabel.get_color(c.label)),
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: docling
|
3
|
-
Version: 2.
|
3
|
+
Version: 2.25.0
|
4
4
|
Summary: SDK and CLI for parsing PDF, DOCX, HTML, and more, to a unified document representation for powering downstream workflows such as gen AI applications.
|
5
5
|
Home-page: https://github.com/DS4SD/docling
|
6
6
|
License: MIT
|
@@ -25,11 +25,11 @@ Provides-Extra: ocrmac
|
|
25
25
|
Provides-Extra: rapidocr
|
26
26
|
Provides-Extra: tesserocr
|
27
27
|
Provides-Extra: vlm
|
28
|
+
Requires-Dist: accelerate (>=1.2.1,<2.0.0) ; (sys_platform != "darwin" or platform_machine != "x86_64") and (extra == "vlm")
|
28
29
|
Requires-Dist: beautifulsoup4 (>=4.12.3,<5.0.0)
|
29
30
|
Requires-Dist: certifi (>=2024.7.4)
|
30
|
-
Requires-Dist: deepsearch-glm (>=1.0.0,<2.0.0)
|
31
31
|
Requires-Dist: docling-core[chunking] (>=2.19.0,<3.0.0)
|
32
|
-
Requires-Dist: docling-ibm-models (>=3.
|
32
|
+
Requires-Dist: docling-ibm-models (>=3.4.0,<4.0.0)
|
33
33
|
Requires-Dist: docling-parse (>=3.3.0,<4.0.0)
|
34
34
|
Requires-Dist: easyocr (>=1.7,<2.0)
|
35
35
|
Requires-Dist: filetype (>=1.2.0,<2.0.0)
|
@@ -5,7 +5,7 @@ docling/backend/asciidoc_backend.py,sha256=zyHxlG_BvlLwvpdNca3P6aopxOJZw8wbDFkJQ
|
|
5
5
|
docling/backend/csv_backend.py,sha256=xuId4JGEXjoyPgO9Fy9hQ5C-ezXvJwv0TGB8fyFHgWM,4533
|
6
6
|
docling/backend/docling_parse_backend.py,sha256=hEEJibI1oJS0LAnFoIs6gMshS3bCqGtVxHnDNvBGZuA,7649
|
7
7
|
docling/backend/docling_parse_v2_backend.py,sha256=IpwrBrtLGwNRl5AYO-o3NjEfNRsAkuMhzvDt2HXb9Ko,8655
|
8
|
-
docling/backend/html_backend.py,sha256=
|
8
|
+
docling/backend/html_backend.py,sha256=j5ivNBDMM0bs24GxTHGGcsA7Z0pnb3iEZ2QKS0Xxdrc,17286
|
9
9
|
docling/backend/json/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
10
|
docling/backend/json/docling_json_backend.py,sha256=LlFMVoZrrCfVwbDuRbNN4Xg96Lujh4xxrTBt9jGhY9I,1984
|
11
11
|
docling/backend/md_backend.py,sha256=NaVfcnEH-5bwVovjn76EobF6B6Wm8AhaTZ4E8k0TUPo,16826
|
@@ -20,12 +20,12 @@ docling/backend/xml/uspto_backend.py,sha256=IGUNeF2xpLeaVrX6nKb-jXgtSYD2ozULsrDP
|
|
20
20
|
docling/chunking/__init__.py,sha256=h83TDs0AuOV6oEPLAPrn9dpGKiU-2Vg6IRNo4cv6GDA,346
|
21
21
|
docling/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
22
22
|
docling/cli/main.py,sha256=pCJ_GFgxsgZ0soz32OhMl-CWi7YXIrvax_m9Qw4UhMs,16839
|
23
|
-
docling/cli/models.py,sha256=
|
23
|
+
docling/cli/models.py,sha256=DDnz-boX2MexPxC8OnOMPgSPG0iwseT3xkkCfgPrZis,3969
|
24
24
|
docling/cli/tools.py,sha256=QhtRxQG0TVrfsMqdv5i7J0_qQy1ZZyWYnHPwJl7b5oY,322
|
25
25
|
docling/datamodel/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
26
|
-
docling/datamodel/base_models.py,sha256=
|
26
|
+
docling/datamodel/base_models.py,sha256=kMDT-rFhtJUFOOOry4wd2PzCMTLFixFklgSgmRDMS64,7201
|
27
27
|
docling/datamodel/document.py,sha256=DbJifyMgBEkAk80BMYXTuSgqH2vijDENDkU7Fmr6j_g,14567
|
28
|
-
docling/datamodel/pipeline_options.py,sha256=
|
28
|
+
docling/datamodel/pipeline_options.py,sha256=YpWqCqkA44YUFPhiBg_LYcfOAXxNhv10vZKrkfLtJ_I,11987
|
29
29
|
docling/datamodel/settings.py,sha256=bNMdowIKv7RUchabQTo4rFNEsxfB6pGg2LoZSY634zo,1869
|
30
30
|
docling/document_converter.py,sha256=AeiSmKzWcnOkZm8O-KIBG72g3l4W2CAsq3yEbfC1tiE,13184
|
31
31
|
docling/exceptions.py,sha256=K1WnCS1leK2JtMB5ewZWKkb0EaijFgl-tRzrO9ntgPM,134
|
@@ -34,36 +34,38 @@ docling/models/base_model.py,sha256=q_lKeQ0FT70idXlZ3JgyAv8dA8J3bZWBSDBkqTzy0lo,
|
|
34
34
|
docling/models/base_ocr_model.py,sha256=YiUMvdjnHw9SHjnfJKT5INrPMoIGEf_Z2OApfl_VRTE,6919
|
35
35
|
docling/models/code_formula_model.py,sha256=6grbRPWaLljadheT5s4omdT6hmXfin4gJU17csWvhjY,8611
|
36
36
|
docling/models/document_picture_classifier.py,sha256=6I_j6fG5fnhIV6rqN31LYikNTZyg5isXrVs0GIqHDaY,6235
|
37
|
-
docling/models/ds_glm_model.py,sha256=1jLEM-B_oHFevKq23zDQpdifE3eJL7qiLr5YLpEf1kQ,15217
|
38
37
|
docling/models/easyocr_model.py,sha256=ePg1exAXeOzkBRBT-6PBSmqKFmnNFkCEd4HNDsGVgLM,6860
|
38
|
+
docling/models/hf_vlm_model.py,sha256=NUtLEuG-kNGJeDHWmQKAAOZG4WF0a5hn-KXUUM1mHBQ,6820
|
39
39
|
docling/models/layout_model.py,sha256=7fQWipGV1HDrvbP4uOKa9QAicQl89jp7lailQmbFL3w,7804
|
40
40
|
docling/models/ocr_mac_model.py,sha256=bLP14UUmZcSzjDe-HLj-mtksTuBmsCTg2C1wCxUpan0,4502
|
41
|
-
docling/models/page_assemble_model.py,sha256=
|
41
|
+
docling/models/page_assemble_model.py,sha256=ivkCdbZJpFcGl7CazLegcP1tLK8ZixDfVhQXqsdW_UA,6359
|
42
42
|
docling/models/page_preprocessing_model.py,sha256=1gVrZjObKxAvXkkKvXnIFApPOggzgiTFPtt1CGbMbSs,2763
|
43
43
|
docling/models/picture_description_api_model.py,sha256=SKNoHpqzbfM8iO-DJJ4ccyNVqO0B2d9neLBnXqt50FY,3186
|
44
44
|
docling/models/picture_description_base_model.py,sha256=rZLIW1_CaRAw_EP3zuI8ktC0ZxwO7yubhh2RkaC_8e8,1910
|
45
|
-
docling/models/picture_description_vlm_model.py,sha256=
|
45
|
+
docling/models/picture_description_vlm_model.py,sha256=EvKn4zWgTsQnbMFEoDhU3Ox4Pu5DkPqd2QewsGoXULU,3641
|
46
46
|
docling/models/rapid_ocr_model.py,sha256=2HXmurNRPP6qyqn7U5h9NQIs8zi0TMHf56CpcKQk0fU,5038
|
47
|
+
docling/models/readingorder_model.py,sha256=hNWbBX3uZv1FxMwKNKn2JFQuQqTspBLsJBVEidXr6Wk,14869
|
47
48
|
docling/models/table_structure_model.py,sha256=UIqWlw_9JNfGsO86c00rPb4GCg-yNliKEwyhCqlsZbM,11225
|
48
49
|
docling/models/tesseract_ocr_cli_model.py,sha256=F5EhS4NDEmLkPq-a0P7o2LrzjmJgACzlYXTDvtD3NtY,9343
|
49
50
|
docling/models/tesseract_ocr_model.py,sha256=ikGu6QNknLG64c9yYIb0Ix6MGhBzOoa1ODbNc8MT5r8,8508
|
50
51
|
docling/pipeline/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
51
52
|
docling/pipeline/base_pipeline.py,sha256=9ABK-Cr235bxE5vweoIA5rgBZV_EF8qFxAqLI27H_Pg,8749
|
52
53
|
docling/pipeline/simple_pipeline.py,sha256=mZqANqUtAOFAyqQEShErQnAUz6tJFOl6zVoazEDJ_wE,2254
|
53
|
-
docling/pipeline/standard_pdf_pipeline.py,sha256=
|
54
|
+
docling/pipeline/standard_pdf_pipeline.py,sha256=IQHktVYvueTrYnIgLonaMvfYKKsU3L-hC9dqrR-Lw8g,12904
|
55
|
+
docling/pipeline/vlm_pipeline.py,sha256=glPwNH1QEuHj35L3tdPyuCX0CGlJn81ZDFrj3WwLa7o,22265
|
54
56
|
docling/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
55
57
|
docling/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
56
58
|
docling/utils/accelerator_utils.py,sha256=ONNRrC8fH-8E93WUCNhfOq1t7WrQ1T7-YsmExTOY5f0,2292
|
57
59
|
docling/utils/export.py,sha256=KyGF1BVDHPFfHVXZc8vegsWlFfOgGPP2YckWpTadyI8,4694
|
58
60
|
docling/utils/glm_utils.py,sha256=W4JRoP0xQ6SJmhhIoAfcKxm5dr1CFvLHp8pqI1kdhxs,12250
|
59
61
|
docling/utils/layout_postprocessor.py,sha256=urRzeF9PrKiMBvA6DdHHwyLxG06CMhelgJeV5B1l6l0,24258
|
60
|
-
docling/utils/model_downloader.py,sha256=
|
62
|
+
docling/utils/model_downloader.py,sha256=sxAQvjiIu9m2Ur5Ot5C5SATmgWJAHi0xSjzxj8QXYJk,3213
|
61
63
|
docling/utils/ocr_utils.py,sha256=F7iOOjqolUcImUzir4qjDQd4QWSO3s6JC4WRn3U7uY4,263
|
62
64
|
docling/utils/profiling.py,sha256=YaMGoB9MMZpagF9mb5ndoHj8Lpb9aIdb7El-Pl7IcFs,1753
|
63
65
|
docling/utils/utils.py,sha256=0ozCk7zUkYzxRVmYoIB2zA1lqjQOuaQzxfGuf1wmKW4,1866
|
64
|
-
docling/utils/visualization.py,sha256=
|
65
|
-
docling-2.
|
66
|
-
docling-2.
|
67
|
-
docling-2.
|
68
|
-
docling-2.
|
69
|
-
docling-2.
|
66
|
+
docling/utils/visualization.py,sha256=cmbIroPQXPmJdFrNIfpC26WpijBwx05qmpu3QhiG1EI,2850
|
67
|
+
docling-2.25.0.dist-info/LICENSE,sha256=mBb7ErEcM8VS9OhiGHnQ2kk75HwPhr54W1Oiz3965MY,1088
|
68
|
+
docling-2.25.0.dist-info/METADATA,sha256=9k71yJWmZHMXgiGxqsmh6KhItKh5kvIDG5TpX2-1vgI,8797
|
69
|
+
docling-2.25.0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
70
|
+
docling-2.25.0.dist-info/entry_points.txt,sha256=cFrINXsORijdm2EWJzf1m9_rDxH9G9W1fP385-9atY4,84
|
71
|
+
docling-2.25.0.dist-info/RECORD,,
|