docling 2.41.0__py3-none-any.whl → 2.42.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.
@@ -260,7 +260,15 @@ class oMath2Latex(Tag2Method):
260
260
  the fraction object
261
261
  """
262
262
  c_dict = self.process_children_dict(elm)
263
- pr = c_dict["fPr"]
263
+ pr = c_dict.get("fPr")
264
+ if pr is None:
265
+ # Handle missing fPr element gracefully
266
+ _log.debug("Missing fPr element in fraction, using default formatting")
267
+ latex_s = F_DEFAULT
268
+ return latex_s.format(
269
+ num=c_dict.get("num"),
270
+ den=c_dict.get("den"),
271
+ )
264
272
  latex_s = get_val(pr.type, default=F_DEFAULT, store=F)
265
273
  return pr.text + latex_s.format(num=c_dict.get("num"), den=c_dict.get("den"))
266
274
 
@@ -379,6 +379,25 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
379
379
  else:
380
380
  _log.debug(f"list-item has no text: {element}")
381
381
 
382
+ @staticmethod
383
+ def _get_cell_spans(cell: Tag) -> tuple[int, int]:
384
+ """Extract colspan and rowspan values from a table cell tag.
385
+
386
+ This function retrieves the 'colspan' and 'rowspan' attributes from a given
387
+ table cell tag.
388
+ If the attribute does not exist or it is not numeric, it defaults to 1.
389
+ """
390
+ raw_spans: tuple[str, str] = (
391
+ str(cell.get("colspan", "1")),
392
+ str(cell.get("rowspan", "1")),
393
+ )
394
+ int_spans: tuple[int, int] = (
395
+ int(raw_spans[0]) if raw_spans[0].isnumeric() else 1,
396
+ int(raw_spans[1]) if raw_spans[0].isnumeric() else 1,
397
+ )
398
+
399
+ return int_spans
400
+
382
401
  @staticmethod
383
402
  def parse_table_data(element: Tag) -> Optional[TableData]: # noqa: C901
384
403
  nested_tables = element.find("table")
@@ -398,10 +417,9 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
398
417
  if not isinstance(row, Tag):
399
418
  continue
400
419
  cell_tag = cast(Tag, cell)
401
- val = cell_tag.get("colspan", "1")
402
- colspan = int(val) if (isinstance(val, str) and val.isnumeric()) else 1
403
- col_count += colspan
404
- if cell_tag.name == "td" or cell_tag.get("rowspan") is None:
420
+ col_span, row_span = HTMLDocumentBackend._get_cell_spans(cell_tag)
421
+ col_count += col_span
422
+ if cell_tag.name == "td" or row_span == 1:
405
423
  is_row_header = False
406
424
  num_cols = max(num_cols, col_count)
407
425
  if not is_row_header:
@@ -428,10 +446,11 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
428
446
  row_header = True
429
447
  for html_cell in cells:
430
448
  if isinstance(html_cell, Tag):
449
+ _, row_span = HTMLDocumentBackend._get_cell_spans(html_cell)
431
450
  if html_cell.name == "td":
432
451
  col_header = False
433
452
  row_header = False
434
- elif html_cell.get("rowspan") is None:
453
+ elif row_span == 1:
435
454
  row_header = False
436
455
  if not row_header:
437
456
  row_idx += 1
@@ -456,18 +475,7 @@ class HTMLDocumentBackend(DeclarativeDocumentBackend):
456
475
  text = html_cell.text
457
476
 
458
477
  # label = html_cell.name
459
- col_val = html_cell.get("colspan", "1")
460
- col_span = (
461
- int(col_val)
462
- if isinstance(col_val, str) and col_val.isnumeric()
463
- else 1
464
- )
465
- row_val = html_cell.get("rowspan", "1")
466
- row_span = (
467
- int(row_val)
468
- if isinstance(row_val, str) and row_val.isnumeric()
469
- else 1
470
- )
478
+ col_span, row_span = HTMLDocumentBackend._get_cell_spans(html_cell)
471
479
  if row_header:
472
480
  row_span -= 1
473
481
  while (
@@ -93,8 +93,8 @@ class JatsDocumentBackend(DeclarativeDocumentBackend):
93
93
 
94
94
  # Initialize the root of the document hierarchy
95
95
  self.root: Optional[NodeItem] = None
96
-
97
- self.valid = False
96
+ self.hlevel: int = 0
97
+ self.valid: bool = False
98
98
  try:
99
99
  if isinstance(self.path_or_stream, BytesIO):
100
100
  self.path_or_stream.seek(0)
@@ -147,6 +147,7 @@ class JatsDocumentBackend(DeclarativeDocumentBackend):
147
147
  binary_hash=self.document_hash,
148
148
  )
149
149
  doc = DoclingDocument(name=self.file.stem or "file", origin=origin)
150
+ self.hlevel = 0
150
151
 
151
152
  # Get metadata XML components
152
153
  xml_components: XMLComponents = self._parse_metadata()
@@ -304,7 +305,9 @@ class JatsDocumentBackend(DeclarativeDocumentBackend):
304
305
  title: str = abstract["label"] or DEFAULT_HEADER_ABSTRACT
305
306
  if not text:
306
307
  continue
307
- parent = doc.add_heading(parent=self.root, text=title)
308
+ parent = doc.add_heading(
309
+ parent=self.root, text=title, level=self.hlevel + 1
310
+ )
308
311
  doc.add_text(
309
312
  parent=parent,
310
313
  text=text,
@@ -637,7 +640,10 @@ class JatsDocumentBackend(DeclarativeDocumentBackend):
637
640
  elif child.tag == "ack":
638
641
  text = DEFAULT_HEADER_ACKNOWLEDGMENTS
639
642
  if text:
640
- new_parent = doc.add_heading(text=text, parent=parent)
643
+ self.hlevel += 1
644
+ new_parent = doc.add_heading(
645
+ text=text, parent=parent, level=self.hlevel
646
+ )
641
647
  elif child.tag == "list":
642
648
  new_parent = doc.add_group(
643
649
  label=GroupLabel.LIST, name="list", parent=parent
@@ -694,6 +700,8 @@ class JatsDocumentBackend(DeclarativeDocumentBackend):
694
700
  new_text = self._walk_linear(doc, new_parent, child)
695
701
  if not (node.getparent().tag == "p" and node.tag in flush_tags):
696
702
  node_text += new_text
703
+ if child.tag in ("sec", "ack") and text:
704
+ self.hlevel -= 1
697
705
 
698
706
  # pick up the tail text
699
707
  node_text += child.tail.replace("\n", " ") if child.tail else ""
@@ -217,7 +217,7 @@ smolvlm_picture_description = PictureDescriptionVlmOptions(
217
217
 
218
218
  # GraniteVision
219
219
  granite_picture_description = PictureDescriptionVlmOptions(
220
- repo_id="ibm-granite/granite-vision-3.2-2b-preview",
220
+ repo_id="ibm-granite/granite-vision-3.3-2b",
221
221
  prompt="What is shown in this image?",
222
222
  )
223
223
 
@@ -279,6 +279,9 @@ class LayoutOptions(BaseModel):
279
279
  """Options for layout processing."""
280
280
 
281
281
  create_orphan_clusters: bool = True # Whether to create clusters for orphaned cells
282
+ keep_empty_clusters: bool = (
283
+ False # Whether to keep clusters that contain no text cells
284
+ )
282
285
  model_spec: LayoutModelConfig = DOCLING_LAYOUT_V2
283
286
 
284
287
 
@@ -1,6 +1,7 @@
1
1
  import hashlib
2
2
  import logging
3
3
  import sys
4
+ import threading
4
5
  import time
5
6
  from collections.abc import Iterable, Iterator
6
7
  from functools import partial
@@ -49,6 +50,7 @@ from docling.pipeline.standard_pdf_pipeline import StandardPdfPipeline
49
50
  from docling.utils.utils import chunkify
50
51
 
51
52
  _log = logging.getLogger(__name__)
53
+ _PIPELINE_CACHE_LOCK = threading.Lock()
52
54
 
53
55
 
54
56
  class FormatOption(BaseModel):
@@ -315,17 +317,18 @@ class DocumentConverter:
315
317
  # Use a composite key to cache pipelines
316
318
  cache_key = (pipeline_class, options_hash)
317
319
 
318
- if cache_key not in self.initialized_pipelines:
319
- _log.info(
320
- f"Initializing pipeline for {pipeline_class.__name__} with options hash {options_hash}"
321
- )
322
- self.initialized_pipelines[cache_key] = pipeline_class(
323
- pipeline_options=pipeline_options
324
- )
325
- else:
326
- _log.debug(
327
- f"Reusing cached pipeline for {pipeline_class.__name__} with options hash {options_hash}"
328
- )
320
+ with _PIPELINE_CACHE_LOCK:
321
+ if cache_key not in self.initialized_pipelines:
322
+ _log.info(
323
+ f"Initializing pipeline for {pipeline_class.__name__} with options hash {options_hash}"
324
+ )
325
+ self.initialized_pipelines[cache_key] = pipeline_class(
326
+ pipeline_options=pipeline_options
327
+ )
328
+ else:
329
+ _log.debug(
330
+ f"Reusing cached pipeline for {pipeline_class.__name__} with options hash {options_hash}"
331
+ )
329
332
 
330
333
  return self.initialized_pipelines[cache_key]
331
334
 
@@ -65,6 +65,7 @@ class PictureDescriptionVlmModel(
65
65
  self.processor = AutoProcessor.from_pretrained(artifacts_path)
66
66
  self.model = AutoModelForVision2Seq.from_pretrained(
67
67
  artifacts_path,
68
+ device_map=self.device,
68
69
  torch_dtype=torch.bfloat16,
69
70
  _attn_implementation=(
70
71
  "flash_attention_2"
@@ -72,7 +73,7 @@ class PictureDescriptionVlmModel(
72
73
  and accelerator_options.cuda_use_flash_attention2
73
74
  else "eager"
74
75
  ),
75
- ).to(self.device)
76
+ )
76
77
 
77
78
  self.provenance = f"{self.options.repo_id}"
78
79
 
@@ -267,8 +267,9 @@ class LayoutPostprocessor:
267
267
  # Initial cell assignment
268
268
  clusters = self._assign_cells_to_clusters(clusters)
269
269
 
270
- # Remove clusters with no cells
271
- clusters = [cluster for cluster in clusters if cluster.cells]
270
+ # Remove clusters with no cells (if keep_empty_clusters is False)
271
+ if not self.options.keep_empty_clusters:
272
+ clusters = [cluster for cluster in clusters if cluster.cells]
272
273
 
273
274
  # Handle orphaned cells
274
275
  unassigned = self._find_unassigned_cells(clusters)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: docling
3
- Version: 2.41.0
3
+ Version: 2.42.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
  Author-email: Christoph Auer <cau@zurich.ibm.com>, Michele Dolfi <dol@zurich.ibm.com>, Maxim Lysak <mly@zurich.ibm.com>, Nikos Livathinos <nli@zurich.ibm.com>, Ahmed Nassar <ahn@zurich.ibm.com>, Panos Vagenas <pva@zurich.ibm.com>, Peter Staar <taa@zurich.ibm.com>
6
6
  License-Expression: MIT
@@ -50,6 +50,7 @@ Requires-Dist: tqdm<5.0.0,>=4.65.0
50
50
  Requires-Dist: pluggy<2.0.0,>=1.0.0
51
51
  Requires-Dist: pylatexenc<3.0,>=2.10
52
52
  Requires-Dist: scipy<2.0.0,>=1.6.0
53
+ Requires-Dist: accelerate<2,>=1.0.0
53
54
  Provides-Extra: tesserocr
54
55
  Requires-Dist: tesserocr<3.0.0,>=2.7.1; extra == "tesserocr"
55
56
  Provides-Extra: ocrmac
@@ -1,5 +1,5 @@
1
1
  docling/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- docling/document_converter.py,sha256=3jWywP_TLy-1PMvjJBUlnTM9FNzpBLRCHYA1RKFvGR4,14333
2
+ docling/document_converter.py,sha256=9aH8B30_jOYN4P_ySCCvtgEb3GoIpec15r7lEAFlMDU,14469
3
3
  docling/exceptions.py,sha256=K1WnCS1leK2JtMB5ewZWKkb0EaijFgl-tRzrO9ntgPM,134
4
4
  docling/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
5
5
  docling/backend/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -9,7 +9,7 @@ docling/backend/csv_backend.py,sha256=2g9famYG2W-ID9jEdZPxc6O8QGv1vWQfjN8pL-QMBE
9
9
  docling/backend/docling_parse_backend.py,sha256=9rUo1vPxX6QLzGqF-2B2iEYglZg6YQ3Uea00XrLluTg,7918
10
10
  docling/backend/docling_parse_v2_backend.py,sha256=3ckTfke8IICjaImlIzc3TRhG7KDuxDDba0AuCEcjA-M,9500
11
11
  docling/backend/docling_parse_v4_backend.py,sha256=qR_WRVq9JGtRioWCw6MnLWgbvXbC6Y1yds7Ol1-E6UQ,6550
12
- docling/backend/html_backend.py,sha256=Z959dzqYQO2pPE4xgPRxC5MR9j3nFGtiD6_F_osQ2iI,20670
12
+ docling/backend/html_backend.py,sha256=xyCbCGR3vYNl-wSP2YJRgSCy9kIIMKKu28AUylPEUq8,20959
13
13
  docling/backend/md_backend.py,sha256=mfwGj8g2hGC-Q_HREtl_Web65uMVXD-Ie1nRqWTXzF0,21013
14
14
  docling/backend/msexcel_backend.py,sha256=cq8MQ2RSh6pqCiVrldjOerSww7dOPTWmCQoCBI57i6w,18579
15
15
  docling/backend/mspowerpoint_backend.py,sha256=wJgB2JStEPfD7MPpWQlpPN7bffPxaHFUnKD4wj8SLxU,15114
@@ -20,11 +20,11 @@ docling/backend/pypdfium2_backend.py,sha256=8dVniLHgiTdJuDbYr66kPp6Ccv5ZDlqDMEbA
20
20
  docling/backend/docx/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
21
21
  docling/backend/docx/latex/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  docling/backend/docx/latex/latex_dict.py,sha256=tFJp4ScT_AkY2ON7nLEa560p601Jq2glcZvMKxxjn7w,6593
23
- docling/backend/docx/latex/omml.py,sha256=nEpcfyyrOucJyj6cD7wfThrIa-q0CQCoqMb3dkrhCRg,12094
23
+ docling/backend/docx/latex/omml.py,sha256=4vh9FCbXh-Tb6KJGqNwzlMUMYEnnJgBtBI24dwy6t2U,12416
24
24
  docling/backend/json/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
25
  docling/backend/json/docling_json_backend.py,sha256=LlFMVoZrrCfVwbDuRbNN4Xg96Lujh4xxrTBt9jGhY9I,1984
26
26
  docling/backend/xml/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
27
- docling/backend/xml/jats_backend.py,sha256=ghGi9bHjx3BvaOtmzLw86-wZy4UxpQPOPQL4e73-BI8,24927
27
+ docling/backend/xml/jats_backend.py,sha256=LPj33EFdi2MRCakkLWrRLlUAc-B-949f8zp5gKNvBcg,25238
28
28
  docling/backend/xml/uspto_backend.py,sha256=nyAMr5ht7dclxkVDwsKNeiOhLQrUtRLS8JdscB2AVJg,70924
29
29
  docling/chunking/__init__.py,sha256=h83TDs0AuOV6oEPLAPrn9dpGKiU-2Vg6IRNo4cv6GDA,346
30
30
  docling/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -37,7 +37,7 @@ docling/datamodel/asr_model_specs.py,sha256=Wg7z3zm_wXIWu122iPVy0RMECsA_JCFHrlFF
37
37
  docling/datamodel/base_models.py,sha256=9FslHkGUNmBp264LpLL_2JTfDAdaikldYs3SiQOHb5A,11828
38
38
  docling/datamodel/document.py,sha256=CA_dgt4V_phze5HXpfgfKNBKd1cPC1o3WE_IENX63EM,16252
39
39
  docling/datamodel/layout_model_specs.py,sha256=GSkJ-Z_0PVgwWGi7C7TsxbzRjlrWS9ZrHJjHumv-Z5U,2339
40
- docling/datamodel/pipeline_options.py,sha256=aMwpbyEMbAC-xGJnjQp8iw2ocpSU4eiD8D73gHf7T4U,10033
40
+ docling/datamodel/pipeline_options.py,sha256=nlejeQjnJx2RBMkCukDECHGuVEOol9hbsSLUi2ee9hY,10134
41
41
  docling/datamodel/pipeline_options_asr_model.py,sha256=7X068xl-qpbyPxC7-TwX7Q6tLyZXGT5h1osZ_xLNLM0,1454
42
42
  docling/datamodel/pipeline_options_vlm_model.py,sha256=z-pUqwRA8nJp6C3SEXZLem2zvSYdgavaAVYa8wkAIZY,2400
43
43
  docling/datamodel/settings.py,sha256=ajMz7Ao2m0ZGYkfArqTDDbiF89O408mtgeh06PUi0MA,1900
@@ -55,7 +55,7 @@ docling/models/page_assemble_model.py,sha256=TvN1naez7dUodLxpUUBzpuMCpqZBTf6YSpe
55
55
  docling/models/page_preprocessing_model.py,sha256=x8MI4mvjizqEqAb5511dtrNRCJSb-lSmwHw0tmHPFiI,5103
56
56
  docling/models/picture_description_api_model.py,sha256=o3EkV5aHW_6WzE_fdj_VRnNCrS_btclO_ZCLAUqrfl0,2377
57
57
  docling/models/picture_description_base_model.py,sha256=kLthLhdlgwhootQ4_xhhcAk6A-vso5-qcsFJ3TcYfO0,2991
58
- docling/models/picture_description_vlm_model.py,sha256=nAUt-eZOX2GvaCiV2BJO7VppxUbP7udVIF4oe_sEYXo,4000
58
+ docling/models/picture_description_vlm_model.py,sha256=yfyAFOy8RjxQJrafPMSAMrrpaYu3anahjRX6tCnVcs0,4028
59
59
  docling/models/rapid_ocr_model.py,sha256=AMdc66s_iWO4p6nQ0LNjQMUYVxrDSxMyLNPpjPYt6N8,5916
60
60
  docling/models/readingorder_model.py,sha256=bZoXHaSwUsa8niSmJrbCuy784ixCeBXT-RQBUfgHJ4A,14925
61
61
  docling/models/table_structure_model.py,sha256=RFXo73f2q4XuKyaSqbxpznh7JVtlLcT0FsOWl9oZbSg,12518
@@ -83,7 +83,7 @@ docling/utils/accelerator_utils.py,sha256=DSajLxVx1JEVT0zt5de26llciLNlVfIDfSa2zY
83
83
  docling/utils/api_image_request.py,sha256=_CgdzmPqdsyXmyYUFGLZcXcoH586qC6A1p5vsNbj1Q0,1416
84
84
  docling/utils/export.py,sha256=VwVUnYDk3mhGmISDbVm306fwpGNnoojouStBD4UajXI,4673
85
85
  docling/utils/glm_utils.py,sha256=TKOWQqWAHsX_w4fvoAA7_2xCi_urhnp1DsmjY8_sk5w,12274
86
- docling/utils/layout_postprocessor.py,sha256=QuTZZq4LNs1eM_n_2gubVfAuLBMkJiozfs3hp-jUpK4,24399
86
+ docling/utils/layout_postprocessor.py,sha256=m92UKjL-cIrOmOBi5Nuiby9FQWFyudcHigJKzud69-Q,24486
87
87
  docling/utils/locks.py,sha256=RzqQtD5UispgV71pGN_nU6GYfeN11BN0Sh_Dq9ycqGo,52
88
88
  docling/utils/model_downloader.py,sha256=3vijCsAIVwWqehGBDRxRq7mJ3yRb9-zBsG00iqjqegU,4076
89
89
  docling/utils/ocr_utils.py,sha256=nmresYyfin0raanpQc_GGeU3WoLsfExf6SEXNIQ7Djg,2325
@@ -91,9 +91,9 @@ docling/utils/orientation.py,sha256=jTyLxyT31FlOodZoBMlADHNQK2lAWKYVs5z7pXd_6Cg,
91
91
  docling/utils/profiling.py,sha256=YaMGoB9MMZpagF9mb5ndoHj8Lpb9aIdb7El-Pl7IcFs,1753
92
92
  docling/utils/utils.py,sha256=kJtIYuzXeOyJHYlxmLAo7dGM5rEsDa1i84qEsUj1nio,1908
93
93
  docling/utils/visualization.py,sha256=tY2ylE2aiQKkmzlSLnFW-HTfFyqUUMguW18ldd1PLfo,2868
94
- docling-2.41.0.dist-info/licenses/LICENSE,sha256=mBb7ErEcM8VS9OhiGHnQ2kk75HwPhr54W1Oiz3965MY,1088
95
- docling-2.41.0.dist-info/METADATA,sha256=KYqB0miKX2x2ESNy8tNHdAlyTCONqhwGLR2iag2PcQ0,10274
96
- docling-2.41.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
97
- docling-2.41.0.dist-info/entry_points.txt,sha256=hzVlbeE0aMSTQ9S0-NTYN0Hmgsn6qL_EA2qX4UbkAuY,149
98
- docling-2.41.0.dist-info/top_level.txt,sha256=vkIywP-USjFyYo1AIRQbWQQaL3xB5jf8vkCYdTIfNic,8
99
- docling-2.41.0.dist-info/RECORD,,
94
+ docling-2.42.0.dist-info/licenses/LICENSE,sha256=mBb7ErEcM8VS9OhiGHnQ2kk75HwPhr54W1Oiz3965MY,1088
95
+ docling-2.42.0.dist-info/METADATA,sha256=jOwKrV5DDscuvMqHevJKC7-VA_hPOpDNz2lfJA6RAVE,10310
96
+ docling-2.42.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
97
+ docling-2.42.0.dist-info/entry_points.txt,sha256=hzVlbeE0aMSTQ9S0-NTYN0Hmgsn6qL_EA2qX4UbkAuY,149
98
+ docling-2.42.0.dist-info/top_level.txt,sha256=vkIywP-USjFyYo1AIRQbWQQaL3xB5jf8vkCYdTIfNic,8
99
+ docling-2.42.0.dist-info/RECORD,,