docling-ibm-models 1.3.2__tar.gz → 1.4.0__tar.gz
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_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/PKG-INFO +1 -2
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/layoutmodel/layout_predictor.py +31 -47
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/common.py +10 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/pyproject.toml +4 -2
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/LICENSE +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/README.md +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/functional.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/matching_post_processor.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/tf_cell_matcher.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/tf_predictor.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/data_management/transforms.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/common/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/common/base_model.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/table04_rs/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/table04_rs/bbox_decoder_rs.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/table04_rs/encoder04_rs.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/table04_rs/tablemodel04_rs.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/models/table04_rs/transformer_rs.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/otsl.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/settings.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/__init__.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/app_profiler.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/mem_monitor.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/torch_utils.py +0 -0
- {docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/utils.py +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.1
|
2
2
|
Name: docling-ibm-models
|
3
|
-
Version: 1.
|
3
|
+
Version: 1.4.0
|
4
4
|
Summary: This package contains the AI models used by the Docling PDF conversion package
|
5
5
|
License: MIT
|
6
6
|
Keywords: docling,convert,document,pdf,layout model,segmentation,table structure,table former
|
@@ -24,7 +24,6 @@ Requires-Dist: jsonlines (>=3.1.0,<4.0.0)
|
|
24
24
|
Requires-Dist: lxml (>=4.9.1,<5.0.0)
|
25
25
|
Requires-Dist: mean_average_precision (>=2021.4.26.0,<2022.0.0.0)
|
26
26
|
Requires-Dist: numpy (>=1.24.4,<2.0.0)
|
27
|
-
Requires-Dist: onnxruntime (>=1.16.2,<2.0.0)
|
28
27
|
Requires-Dist: opencv-python-headless (>=4.6.0.66,<5.0.0.0)
|
29
28
|
Requires-Dist: torch (>=2.2.2,<2.3.0) ; sys_platform == "darwin" and platform_machine == "x86_64"
|
30
29
|
Requires-Dist: torch (>=2.2.2,<3.0.0) ; sys_platform != "darwin" or platform_machine != "x86_64"
|
@@ -7,7 +7,8 @@ from collections.abc import Iterable
|
|
7
7
|
from typing import Union
|
8
8
|
|
9
9
|
import numpy as np
|
10
|
-
import
|
10
|
+
import torch
|
11
|
+
import torchvision.transforms as T
|
11
12
|
from PIL import Image
|
12
13
|
|
13
14
|
MODEL_CHECKPOINT_FN = "model.pt"
|
@@ -16,14 +17,14 @@ DEFAULT_NUM_THREADS = 4
|
|
16
17
|
|
17
18
|
class LayoutPredictor:
|
18
19
|
r"""
|
19
|
-
Document layout prediction using
|
20
|
+
Document layout prediction using torch
|
20
21
|
"""
|
21
22
|
|
22
23
|
def __init__(
|
23
24
|
self, artifact_path: str, num_threads: int = None, use_cpu_only: bool = False
|
24
25
|
):
|
25
26
|
r"""
|
26
|
-
Provide the artifact path that contains the LayoutModel
|
27
|
+
Provide the artifact path that contains the LayoutModel file
|
27
28
|
|
28
29
|
The number of threads is decided, in the following order, by:
|
29
30
|
1. The init method parameter `num_threads`, if it is set.
|
@@ -38,13 +39,13 @@ class LayoutPredictor:
|
|
38
39
|
|
39
40
|
Parameters
|
40
41
|
----------
|
41
|
-
artifact_path: Path for the model
|
42
|
+
artifact_path: Path for the model torch file.
|
42
43
|
num_threads: (Optional) Number of threads to run the inference.
|
43
44
|
use_cpu_only: (Optional) If True, it forces CPU as the execution provider.
|
44
45
|
|
45
46
|
Raises
|
46
47
|
------
|
47
|
-
FileNotFoundError when the model's
|
48
|
+
FileNotFoundError when the model's torch file is missing
|
48
49
|
"""
|
49
50
|
# Initialize classes map:
|
50
51
|
self._classes_map = {
|
@@ -75,46 +76,27 @@ class LayoutPredictor:
|
|
75
76
|
self._threshold = 0.6 # Score threshold
|
76
77
|
self._image_size = 640
|
77
78
|
self._size = np.asarray([[self._image_size, self._image_size]], dtype=np.int64)
|
79
|
+
self._use_cpu_only = use_cpu_only or ("USE_CPU_ONLY" in os.environ)
|
80
|
+
|
81
|
+
# Model file
|
82
|
+
self._torch_fn = os.path.join(artifact_path, MODEL_CHECKPOINT_FN)
|
83
|
+
if not os.path.isfile(self._torch_fn):
|
84
|
+
raise FileNotFoundError("Missing torch file: {}".format(self._torch_fn))
|
78
85
|
|
79
86
|
# Get env vars
|
80
|
-
self._use_cpu_only = use_cpu_only or ("USE_CPU_ONLY" in os.environ)
|
81
87
|
if num_threads is None:
|
82
88
|
num_threads = int(os.environ.get("OMP_NUM_THREADS", DEFAULT_NUM_THREADS))
|
83
89
|
self._num_threads = num_threads
|
84
90
|
|
85
|
-
|
86
|
-
if (
|
87
|
-
not self._use_cpu_only
|
88
|
-
and "CUDAExecutionProvider" in ort.get_available_providers()
|
89
|
-
):
|
90
|
-
providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
|
91
|
-
else:
|
92
|
-
providers = ["CPUExecutionProvider"]
|
93
|
-
self._providers = providers
|
94
|
-
|
95
|
-
# Model ONNX file
|
96
|
-
self._onnx_fn = os.path.join(artifact_path, MODEL_CHECKPOINT_FN)
|
97
|
-
if not os.path.isfile(self._onnx_fn):
|
98
|
-
raise FileNotFoundError("Missing ONNX file: {}".format(self._onnx_fn))
|
99
|
-
|
100
|
-
# ONNX options
|
101
|
-
self._options = ort.SessionOptions()
|
102
|
-
self._options.intra_op_num_threads = self._num_threads
|
103
|
-
self.sess = ort.InferenceSession(
|
104
|
-
self._onnx_fn,
|
105
|
-
sess_options=self._options,
|
106
|
-
providers=self._providers,
|
107
|
-
)
|
91
|
+
self.model = torch.jit.load(self._torch_fn)
|
108
92
|
|
109
93
|
def info(self) -> dict:
|
110
94
|
r"""
|
111
95
|
Get information about the configuration of LayoutPredictor
|
112
96
|
"""
|
113
97
|
info = {
|
114
|
-
"
|
115
|
-
"intra_op_num_threads": self._num_threads,
|
98
|
+
"torch_file": self._torch_fn,
|
116
99
|
"use_cpu_only": self._use_cpu_only,
|
117
|
-
"providers": self._providers,
|
118
100
|
"image_size": self._image_size,
|
119
101
|
"threshold": self._threshold,
|
120
102
|
}
|
@@ -147,33 +129,35 @@ class LayoutPredictor:
|
|
147
129
|
raise TypeError("Not supported input image format")
|
148
130
|
|
149
131
|
w, h = page_img.size
|
150
|
-
|
151
|
-
page_data = np.array(page_img, dtype=np.uint8) / np.float32(255.0)
|
152
|
-
page_data = np.expand_dims(np.transpose(page_data, axes=[2, 0, 1]), axis=0)
|
132
|
+
orig_size = torch.tensor([w, h])[None]
|
153
133
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
"orig_target_sizes": self._size,
|
160
|
-
},
|
134
|
+
transforms = T.Compose(
|
135
|
+
[
|
136
|
+
T.Resize((640, 640)),
|
137
|
+
T.ToTensor(),
|
138
|
+
]
|
161
139
|
)
|
140
|
+
img = transforms(page_img)[None]
|
141
|
+
# Predict
|
142
|
+
with torch.no_grad():
|
143
|
+
labels, boxes, scores = self.model(img, orig_size)
|
162
144
|
|
163
145
|
# Yield output
|
164
146
|
for label_idx, box, score in zip(labels[0], boxes[0], scores[0]):
|
165
147
|
# Filter out blacklisted classes
|
166
|
-
|
148
|
+
label_idx = int(label_idx.item())
|
149
|
+
score = float(score.item())
|
150
|
+
label = self._classes_map[label_idx + 1]
|
167
151
|
if label in self._black_classes:
|
168
152
|
continue
|
169
153
|
|
170
154
|
# Check against threshold
|
171
155
|
if score > self._threshold:
|
172
156
|
yield {
|
173
|
-
"l": box[0]
|
174
|
-
"t": box[1]
|
175
|
-
"r": box[2]
|
176
|
-
"b": box[3]
|
157
|
+
"l": box[0],
|
158
|
+
"t": box[1],
|
159
|
+
"r": box[2],
|
160
|
+
"b": box[3],
|
177
161
|
"label": label,
|
178
162
|
"confidence": score,
|
179
163
|
}
|
{docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/common.py
RENAMED
@@ -48,6 +48,16 @@ def validate_config(config):
|
|
48
48
|
return True
|
49
49
|
|
50
50
|
|
51
|
+
def read_config(config_filename):
|
52
|
+
with open(config_filename, "r") as fd:
|
53
|
+
config = json.load(fd)
|
54
|
+
|
55
|
+
# Validate the config file
|
56
|
+
validate_config(config)
|
57
|
+
|
58
|
+
return config
|
59
|
+
|
60
|
+
|
51
61
|
def safe_get_parameter(input_dict, index_path, default=None, required=False):
|
52
62
|
r"""
|
53
63
|
Safe get parameter from a nested dictionary.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
[tool.poetry]
|
2
2
|
name = "docling-ibm-models"
|
3
|
-
version = "1.
|
3
|
+
version = "1.4.0" # DO NOT EDIT, updated automatically
|
4
4
|
description = "This package contains the AI models used by the Docling PDF conversion package"
|
5
5
|
authors = ["Nikos Livathinos <nli@zurich.ibm.com>", "Maxim Lysak <mly@zurich.ibm.com>", "Ahmed Nassar <ahn@zurich.ibm.com>", "Christoph Auer <cau@zurich.ibm.com>", "Michele Dolfi <dol@zurich.ibm.com>", "Peter Staar <taa@zurich.ibm.com>"]
|
6
6
|
license = "MIT"
|
@@ -30,7 +30,6 @@ torchvision = [
|
|
30
30
|
{markers = "sys_platform != 'darwin' or platform_machine != 'x86_64'", version = "^0"},
|
31
31
|
{markers = "sys_platform == 'darwin' and platform_machine == 'x86_64'", version = "~0.17.2"}
|
32
32
|
]
|
33
|
-
onnxruntime = "^1.16.2"
|
34
33
|
numpy = "^1.24.4"
|
35
34
|
lxml = "^4.9.1"
|
36
35
|
jsonlines = "^3.1.0"
|
@@ -55,6 +54,9 @@ types-requests = "^2.31.0.2"
|
|
55
54
|
flake8-pyproject = "^1.2.3"
|
56
55
|
pylint = "^2.17.5"
|
57
56
|
|
57
|
+
[tool.poetry.group.dev.dependencies]
|
58
|
+
ipykernel = "^6.29.5"
|
59
|
+
|
58
60
|
[build-system]
|
59
61
|
requires = ["poetry-core>=1.0.0"]
|
60
62
|
build-backend = "poetry.core.masonry.api"
|
File without changes
|
File without changes
|
{docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/__init__.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/otsl.py
RENAMED
File without changes
|
{docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/settings.py
RENAMED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
{docling_ibm_models-1.3.2 → docling_ibm_models-1.4.0}/docling_ibm_models/tableformer/utils/utils.py
RENAMED
File without changes
|