arkindex-base-worker 0.5.0b3__py3-none-any.whl → 0.5.1__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.
- {arkindex_base_worker-0.5.0b3.dist-info → arkindex_base_worker-0.5.1.dist-info}/METADATA +7 -8
- {arkindex_base_worker-0.5.0b3.dist-info → arkindex_base_worker-0.5.1.dist-info}/RECORD +27 -27
- arkindex_worker/cache.py +8 -22
- arkindex_worker/image.py +5 -1
- arkindex_worker/models.py +5 -0
- arkindex_worker/utils.py +27 -0
- arkindex_worker/worker/__init__.py +62 -6
- arkindex_worker/worker/base.py +53 -1
- arkindex_worker/worker/element.py +20 -0
- arkindex_worker/worker/entity.py +17 -126
- arkindex_worker/worker/metadata.py +3 -14
- tests/conftest.py +113 -12
- tests/test_base_worker.py +99 -125
- tests/test_cache.py +2 -3
- tests/test_dataset_worker.py +5 -0
- tests/test_element.py +52 -12
- tests/test_elements_worker/__init__.py +4 -0
- tests/test_elements_worker/{test_entity_create.py → test_entity.py} +220 -227
- tests/test_elements_worker/test_metadata.py +0 -47
- tests/test_elements_worker/test_worker.py +106 -0
- tests/test_image.py +19 -3
- tests/test_merge.py +0 -7
- tests/test_modern_config.py +81 -0
- tests/test_utils.py +42 -0
- tests/test_elements_worker/test_entity_list_and_check.py +0 -293
- {arkindex_base_worker-0.5.0b3.dist-info → arkindex_base_worker-0.5.1.dist-info}/WHEEL +0 -0
- {arkindex_base_worker-0.5.0b3.dist-info → arkindex_base_worker-0.5.1.dist-info}/licenses/LICENSE +0 -0
- {arkindex_base_worker-0.5.0b3.dist-info → arkindex_base_worker-0.5.1.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: arkindex-base-worker
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.1
|
|
4
4
|
Summary: Base Worker to easily build Arkindex ML workflows
|
|
5
5
|
Author-email: Teklia <contact@teklia.com>
|
|
6
6
|
Maintainer-email: Teklia <contact@teklia.com>
|
|
@@ -41,16 +41,15 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
41
41
|
Requires-Python: >=3.10
|
|
42
42
|
Description-Content-Type: text/markdown
|
|
43
43
|
License-File: LICENSE
|
|
44
|
-
Requires-Dist: humanize==4.
|
|
44
|
+
Requires-Dist: humanize==4.14.0
|
|
45
45
|
Requires-Dist: peewee~=3.17
|
|
46
|
-
Requires-Dist: Pillow==11.
|
|
47
|
-
Requires-Dist: python-gnupg==0.5.
|
|
46
|
+
Requires-Dist: Pillow==11.3.0
|
|
47
|
+
Requires-Dist: python-gnupg==0.5.5
|
|
48
48
|
Requires-Dist: shapely==2.0.6
|
|
49
|
-
Requires-Dist: teklia-toolbox==0.1.
|
|
50
|
-
Requires-Dist: zstandard==0.
|
|
49
|
+
Requires-Dist: teklia-toolbox==0.1.11
|
|
50
|
+
Requires-Dist: zstandard==0.25.0
|
|
51
51
|
Provides-Extra: tests
|
|
52
|
-
Requires-Dist: pytest==
|
|
53
|
-
Requires-Dist: pytest-mock==3.14.0; extra == "tests"
|
|
52
|
+
Requires-Dist: pytest-mock==3.15.1; extra == "tests"
|
|
54
53
|
Requires-Dist: pytest-responses==0.5.1; extra == "tests"
|
|
55
54
|
Dynamic: license-file
|
|
56
55
|
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
arkindex_base_worker-0.5.
|
|
1
|
+
arkindex_base_worker-0.5.1.dist-info/licenses/LICENSE,sha256=NVshRi1efwVezMfW7xXYLrdDr2Li1AfwfGOd5WuH1kQ,1063
|
|
2
2
|
arkindex_worker/__init__.py,sha256=Sdt5KXn8EgURb2MurYVrUWaHbH3iFA1XLRo0Lc5AJ44,250
|
|
3
|
-
arkindex_worker/cache.py,sha256=
|
|
4
|
-
arkindex_worker/image.py,sha256=
|
|
5
|
-
arkindex_worker/models.py,sha256=
|
|
6
|
-
arkindex_worker/utils.py,sha256=
|
|
7
|
-
arkindex_worker/worker/__init__.py,sha256=
|
|
8
|
-
arkindex_worker/worker/base.py,sha256
|
|
3
|
+
arkindex_worker/cache.py,sha256=XpEXMSnbhYCvrJquwA9XXqZo-ajMLpaCxKG5wH3Gp6Y,10959
|
|
4
|
+
arkindex_worker/image.py,sha256=sGE8to5iykXv25bpkftOEWzlh5NzBZSKy4lSRoHYHPU,20929
|
|
5
|
+
arkindex_worker/models.py,sha256=7GnKqpWPOSxyR_eKlDNVBe_r3TcE4ofK-1GzaonJEdM,10132
|
|
6
|
+
arkindex_worker/utils.py,sha256=yq_LmRlqfWOzB09Aiz2XYx4xPZnoEXR3As48h2HxOVc,10974
|
|
7
|
+
arkindex_worker/worker/__init__.py,sha256=SzD0s1_m6gMV02EUF-NeciqZdVPA4dpXI84tSj-g494,17869
|
|
8
|
+
arkindex_worker/worker/base.py,sha256=-R_aLMJHbR6X1uM-U0zExsF_KLy5Wl3WJ_YMGO9We0I,22153
|
|
9
9
|
arkindex_worker/worker/classification.py,sha256=qvykymkgd4nGywHCxL8obo4egstoGsmWNS4Ztc1qNWQ,11024
|
|
10
10
|
arkindex_worker/worker/corpus.py,sha256=MeIMod7jkWyX0frtD0a37rhumnMV3p9ZOC1xwAoXrAA,2291
|
|
11
11
|
arkindex_worker/worker/dataset.py,sha256=tVaPx43vaH-KTtx4w5V06e26ha8XPfiJTRzBXlu928Y,5273
|
|
12
|
-
arkindex_worker/worker/element.py,sha256=
|
|
13
|
-
arkindex_worker/worker/entity.py,sha256=
|
|
12
|
+
arkindex_worker/worker/element.py,sha256=sLfnf09AfJ5tSCKQ7cAkl7WsGhjsfq14swsT30MDnYk,47385
|
|
13
|
+
arkindex_worker/worker/entity.py,sha256=Aj6EOfzHEm7qQV-Egm0YKLZgCrLS_3ggOKTY81M2JbI,12323
|
|
14
14
|
arkindex_worker/worker/image.py,sha256=L6Ikuf0Z0RxJk7JarY5PggJGrYSHLaPK0vn0dy0CIaQ,623
|
|
15
|
-
arkindex_worker/worker/metadata.py,sha256=
|
|
15
|
+
arkindex_worker/worker/metadata.py,sha256=keZdOdUthSH2hAw9iet5pN7rzWihTUYjZHRGTEjaltw,6843
|
|
16
16
|
arkindex_worker/worker/process.py,sha256=9TEHpMcBax1wc6PrWMMrdXe2uNfqyVj7n_dAYZRBGnY,1854
|
|
17
17
|
arkindex_worker/worker/task.py,sha256=nYfMSFm_d-4t8y4PO4HjFBnLsZf7IsDjkS7-A2Pgnac,1525
|
|
18
18
|
arkindex_worker/worker/training.py,sha256=tyQOHcwv--_wdYz6CgLEe1YM7kwwwKN30LvGTsnWd78,10923
|
|
@@ -21,15 +21,16 @@ examples/standalone/python/worker.py,sha256=Zr4s4pHvgexEjlkixLFYZp1UuwMLeoTxjyNG
|
|
|
21
21
|
examples/tooled/python/worker.py,sha256=kIYlHLsO5UpwX4XtERRq4tf2qTsvqKK30C-w8t0yyhA,1821
|
|
22
22
|
hooks/pre_gen_project.py,sha256=xQJERv3vv9VzIqcBHI281eeWLWREXUF4mMw7PvJHHXM,269
|
|
23
23
|
tests/__init__.py,sha256=DG--S6IpGl399rzSAjDdHL76CkOIeZIjajCcyUSDhOQ,241
|
|
24
|
-
tests/conftest.py,sha256=
|
|
25
|
-
tests/test_base_worker.py,sha256=
|
|
26
|
-
tests/test_cache.py,sha256=
|
|
27
|
-
tests/test_dataset_worker.py,sha256=
|
|
28
|
-
tests/test_element.py,sha256=
|
|
29
|
-
tests/test_image.py,sha256=
|
|
30
|
-
tests/test_merge.py,sha256=
|
|
31
|
-
tests/
|
|
32
|
-
tests/
|
|
24
|
+
tests/conftest.py,sha256=Tp7YFK17NATwF2yAcBwi0QFNyKSXtLS0VhZ-zZngsQI,24343
|
|
25
|
+
tests/test_base_worker.py,sha256=lwS4X3atS2ktEKd1XdogmN3mbzq-tO206-k_0EDITlw,29302
|
|
26
|
+
tests/test_cache.py,sha256=_wztzh94EwVrb8UvpFqgl2aa2_FLaCcJKaqunCYR5Dw,10435
|
|
27
|
+
tests/test_dataset_worker.py,sha256=iDJM2C4PfQNH0r4_QqSWoPt8BcM0geUUdODtWY0Z9PA,22412
|
|
28
|
+
tests/test_element.py,sha256=hlj5VSF4plwC7uz9R4LGOOXZJQcHZiYCIDZT5V6EIB8,14334
|
|
29
|
+
tests/test_image.py,sha256=yAM5mMfpQcIurT1KLHmu0AhSX2Qm3YvCu7afyZ3XUdU,28314
|
|
30
|
+
tests/test_merge.py,sha256=REpZ13jkq_qm_4L5URQgFy5lxvPZtXxQEiWfYLMdmF0,7956
|
|
31
|
+
tests/test_modern_config.py,sha256=Bm-a4LYQXgLZWQX7AmVyfJW0LNoLy1wj2d2GjzDkcBk,2683
|
|
32
|
+
tests/test_utils.py,sha256=tgzNqyJMpddpeFWEjgsew_yDzmqnCA9HDaA5IpevAcM,5353
|
|
33
|
+
tests/test_elements_worker/__init__.py,sha256=2t3NciCIOun_N-Wv63FWGsTm5W9N3mbwAWVuFORlMg8,308
|
|
33
34
|
tests/test_elements_worker/test_classification.py,sha256=nya7veSPR_O9G41Enodp2-o6AifMBcaSTWJP2vXSSJ4,30133
|
|
34
35
|
tests/test_elements_worker/test_cli.py,sha256=a23i1pUDbXi23MUtbWwGEcLLrmc_YlrbDgOG3h66wLM,2620
|
|
35
36
|
tests/test_elements_worker/test_corpus.py,sha256=kscJyM8k1njYJJFGuvliVzn89lWh41mEyDCCawnp3W8,5483
|
|
@@ -39,23 +40,22 @@ tests/test_elements_worker/test_element_create_multiple.py,sha256=arYFGmxc0517ZU
|
|
|
39
40
|
tests/test_elements_worker/test_element_create_single.py,sha256=Fa9zm12J2rQ3VrUe3yIlHAc7Vty_eQYb_YGnNPQB3IE,16697
|
|
40
41
|
tests/test_elements_worker/test_element_list_children.py,sha256=2zH4h663w3EduqpzQr-7bf9zIDzO1x2WxdUYYHsIHkI,31358
|
|
41
42
|
tests/test_elements_worker/test_element_list_parents.py,sha256=TXeGW-a3W-7GmB2QrhJH9mMnvxuybeAwQ4tL3iIxwXo,16734
|
|
42
|
-
tests/test_elements_worker/
|
|
43
|
-
tests/test_elements_worker/test_entity_list_and_check.py,sha256=zAfwa49D8lHZdB7dqQu14R0P0SQu40qNalW7RjOPYic,9456
|
|
43
|
+
tests/test_elements_worker/test_entity.py,sha256=SNAZEsVVLnqlliOmjkgv_cZhw0bAuJUY70_z57PpEE0,29624
|
|
44
44
|
tests/test_elements_worker/test_image.py,sha256=BljMNKgec_9a5bzNzFpYZIvSbuvwsWDfdqLHVJaTa7M,2079
|
|
45
|
-
tests/test_elements_worker/test_metadata.py,sha256=
|
|
45
|
+
tests/test_elements_worker/test_metadata.py,sha256=qtTDtlp3VnBkfck7PAguK2dEgTLlr1i1EVnmNTeNf3A,20515
|
|
46
46
|
tests/test_elements_worker/test_process.py,sha256=y4RoVhPfyHzR795fw7-_FXElBcKo3fy4Ew_HI-kxJic,3088
|
|
47
47
|
tests/test_elements_worker/test_task.py,sha256=wTUWqN9UhfKmJn3IcFY75EW4I1ulRhisflmY1kmP47s,5574
|
|
48
48
|
tests/test_elements_worker/test_training.py,sha256=qgK7BLucddRzc8ePbQtY75x17QvGDEq5XCwgyyvmAJE,8717
|
|
49
49
|
tests/test_elements_worker/test_transcription_create.py,sha256=yznO9B_BVsOR0Z_VY5ZL8gJp0ZPCz_4sPUs5dXtixAg,29281
|
|
50
50
|
tests/test_elements_worker/test_transcription_create_with_elements.py,sha256=tmcyglgssEqMnt1Mdy_u6X1m2wgLWTo_HdWst3GrK2k,33056
|
|
51
51
|
tests/test_elements_worker/test_transcription_list.py,sha256=ikz7HYPCoQWTdTRCd382SB-y-T2BbigPLlIcx5Eow-I,15324
|
|
52
|
-
tests/test_elements_worker/test_worker.py,sha256=
|
|
52
|
+
tests/test_elements_worker/test_worker.py,sha256=ypAQS_DJj9qGlRJCs9g5qUXe7IgqaKXWDcxqwlhAqSg,28598
|
|
53
53
|
worker-demo/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
54
54
|
worker-demo/tests/conftest.py,sha256=XzNMNeg6pmABUAH8jN6eZTlZSFGLYjS3-DTXjiRN6Yc,1002
|
|
55
55
|
worker-demo/tests/test_worker.py,sha256=3DLd4NRK4bfyatG5P_PK4k9P9tJHx9XQq5_ryFEEFVg,304
|
|
56
56
|
worker-demo/worker_demo/__init__.py,sha256=2BPomV8ZMNf3YXJgloatKeHQCE6QOkwmsHGkO6MkQuM,125
|
|
57
57
|
worker-demo/worker_demo/worker.py,sha256=Rt-DjWa5iBP08k58NDZMfeyPuFbtNcbX6nc5jFX7GNo,440
|
|
58
|
-
arkindex_base_worker-0.5.
|
|
59
|
-
arkindex_base_worker-0.5.
|
|
60
|
-
arkindex_base_worker-0.5.
|
|
61
|
-
arkindex_base_worker-0.5.
|
|
58
|
+
arkindex_base_worker-0.5.1.dist-info/METADATA,sha256=U24JorNb4RsZFkjF25jbfg0VNacYMpwrceaQCCYSQQ0,3088
|
|
59
|
+
arkindex_base_worker-0.5.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
60
|
+
arkindex_base_worker-0.5.1.dist-info/top_level.txt,sha256=-vNjP2VfROx0j83mdi9aIqRZ88eoJjxeWz-R_gPgyXU,49
|
|
61
|
+
arkindex_base_worker-0.5.1.dist-info/RECORD,,
|
arkindex_worker/cache.py
CHANGED
|
@@ -73,6 +73,7 @@ class CachedImage(Model):
|
|
|
73
73
|
width = IntegerField()
|
|
74
74
|
height = IntegerField()
|
|
75
75
|
url = TextField()
|
|
76
|
+
version = IntegerField(default=2)
|
|
76
77
|
|
|
77
78
|
class Meta:
|
|
78
79
|
database = db
|
|
@@ -157,6 +158,10 @@ class CachedElement(Model):
|
|
|
157
158
|
else:
|
|
158
159
|
resize = f"{max_width or ''},{max_height or ''}"
|
|
159
160
|
|
|
161
|
+
# Use `max` instead of `full` for IIIF 3, since `full` was deprecated in 2.1 then removed in 3.0
|
|
162
|
+
if self.image.version == 3 and resize == "full":
|
|
163
|
+
resize = "max"
|
|
164
|
+
|
|
160
165
|
url = self.image.url
|
|
161
166
|
if not url.endswith("/"):
|
|
162
167
|
url += "/"
|
|
@@ -206,23 +211,6 @@ class CachedClassification(Model):
|
|
|
206
211
|
table_name = "classifications"
|
|
207
212
|
|
|
208
213
|
|
|
209
|
-
class CachedEntity(Model):
|
|
210
|
-
"""
|
|
211
|
-
Cache entity table
|
|
212
|
-
"""
|
|
213
|
-
|
|
214
|
-
id = UUIDField(primary_key=True)
|
|
215
|
-
type = CharField(max_length=50)
|
|
216
|
-
name = TextField()
|
|
217
|
-
validated = BooleanField(default=False)
|
|
218
|
-
metas = JSONField(null=True)
|
|
219
|
-
worker_run_id = UUIDField(null=True)
|
|
220
|
-
|
|
221
|
-
class Meta:
|
|
222
|
-
database = db
|
|
223
|
-
table_name = "entities"
|
|
224
|
-
|
|
225
|
-
|
|
226
214
|
class CachedTranscriptionEntity(Model):
|
|
227
215
|
"""
|
|
228
216
|
Cache transcription entity table
|
|
@@ -231,14 +219,14 @@ class CachedTranscriptionEntity(Model):
|
|
|
231
219
|
transcription = ForeignKeyField(
|
|
232
220
|
CachedTranscription, backref="transcription_entities"
|
|
233
221
|
)
|
|
234
|
-
|
|
222
|
+
type = CharField(max_length=50)
|
|
235
223
|
offset = IntegerField(constraints=[Check("offset >= 0")])
|
|
236
224
|
length = IntegerField(constraints=[Check("length > 0")])
|
|
237
225
|
worker_run_id = UUIDField(null=True)
|
|
238
226
|
confidence = FloatField(null=True)
|
|
239
227
|
|
|
240
228
|
class Meta:
|
|
241
|
-
primary_key = CompositeKey("transcription", "
|
|
229
|
+
primary_key = CompositeKey("transcription", "type")
|
|
242
230
|
database = db
|
|
243
231
|
table_name = "transcription_entities"
|
|
244
232
|
|
|
@@ -272,12 +260,11 @@ MODELS = [
|
|
|
272
260
|
CachedElement,
|
|
273
261
|
CachedTranscription,
|
|
274
262
|
CachedClassification,
|
|
275
|
-
CachedEntity,
|
|
276
263
|
CachedTranscriptionEntity,
|
|
277
264
|
CachedDataset,
|
|
278
265
|
CachedDatasetElement,
|
|
279
266
|
]
|
|
280
|
-
SQL_VERSION =
|
|
267
|
+
SQL_VERSION = 5
|
|
281
268
|
|
|
282
269
|
|
|
283
270
|
def init_cache_db(path: Path):
|
|
@@ -365,7 +352,6 @@ def merge_parents_cache(paths: list, current_database: Path):
|
|
|
365
352
|
f"REPLACE INTO elements SELECT * FROM source_{idx}.elements;",
|
|
366
353
|
f"REPLACE INTO transcriptions SELECT * FROM source_{idx}.transcriptions;",
|
|
367
354
|
f"REPLACE INTO classifications SELECT * FROM source_{idx}.classifications;",
|
|
368
|
-
f"REPLACE INTO entities SELECT * FROM source_{idx}.entities;",
|
|
369
355
|
f"REPLACE INTO transcription_entities SELECT * FROM source_{idx}.transcription_entities;",
|
|
370
356
|
f"REPLACE INTO datasets SELECT * FROM source_{idx}.datasets;",
|
|
371
357
|
f"REPLACE INTO dataset_elements SELECT * FROM source_{idx}.dataset_elements;",
|
arkindex_worker/image.py
CHANGED
|
@@ -366,6 +366,10 @@ def download_tiles(url: str) -> Image:
|
|
|
366
366
|
logger.debug("Downloading image information")
|
|
367
367
|
info = _retried_request(url + "info.json").json()
|
|
368
368
|
|
|
369
|
+
# Use `max` instead of `full` for IIIF 3, since `full` was deprecated in 2.1 then removed in 3.0
|
|
370
|
+
# With IIIF 3, the image's ID will be at `id`, while IIIF 2 will use `@id``
|
|
371
|
+
resize = "max" if "id" in info else "full"
|
|
372
|
+
|
|
369
373
|
image_width, image_height = info.get("width"), info.get("height")
|
|
370
374
|
assert image_width and image_height, "Missing image dimensions in info.json"
|
|
371
375
|
assert info.get("tiles"), (
|
|
@@ -391,7 +395,7 @@ def download_tiles(url: str) -> Image:
|
|
|
391
395
|
|
|
392
396
|
logger.debug(f"Downloading tile {tile_x},{tile_y}")
|
|
393
397
|
resp = _retried_request(
|
|
394
|
-
f"{url}{region_x},{region_y},{region_width},{region_height}/
|
|
398
|
+
f"{url}{region_x},{region_y},{region_width},{region_height}/{resize}/0/default.jpg"
|
|
395
399
|
)
|
|
396
400
|
|
|
397
401
|
tile_img = Image.open(BytesIO(resp.content))
|
arkindex_worker/models.py
CHANGED
|
@@ -87,6 +87,11 @@ class Element(MagicDict):
|
|
|
87
87
|
url = self.zone.image.get("s3_url")
|
|
88
88
|
if url:
|
|
89
89
|
return url
|
|
90
|
+
|
|
91
|
+
# Use `max` instead of `full` for IIIF 3, since `full` was deprecated in 2.1 then removed in 3.0
|
|
92
|
+
if self.zone.image.server.get("version", 2) == 3 and size == "full":
|
|
93
|
+
size = "max"
|
|
94
|
+
|
|
90
95
|
url = self.zone.image.url
|
|
91
96
|
if not url.endswith("/"):
|
|
92
97
|
url += "/"
|
arkindex_worker/utils.py
CHANGED
|
@@ -4,6 +4,7 @@ import logging
|
|
|
4
4
|
import os
|
|
5
5
|
import tarfile
|
|
6
6
|
import tempfile
|
|
7
|
+
import zipfile
|
|
7
8
|
from collections.abc import Callable, Generator
|
|
8
9
|
from itertools import islice
|
|
9
10
|
from pathlib import Path
|
|
@@ -225,6 +226,32 @@ def create_tar_zst_archive(
|
|
|
225
226
|
return zst_fd, zst_archive, zst_hash, tar_hash
|
|
226
227
|
|
|
227
228
|
|
|
229
|
+
def create_zip_archive(source: Path, destination: Path | None = None) -> Path:
|
|
230
|
+
"""Helper to create a ZIP archive from a source folder.
|
|
231
|
+
|
|
232
|
+
:param source: Path to the folder whose content should be archived.
|
|
233
|
+
:param destination: Path to the created archive, defaults to None. If unspecified, a temporary file will be created.
|
|
234
|
+
:return: The file descriptor of the created tempfile (if one was created), path to the archive.
|
|
235
|
+
"""
|
|
236
|
+
# Parse destination and create a tmpfile if none was specified
|
|
237
|
+
file_d, destination = (
|
|
238
|
+
tempfile.mkstemp(prefix="teklia-", suffix=".zip")
|
|
239
|
+
if destination is None
|
|
240
|
+
else (None, destination)
|
|
241
|
+
)
|
|
242
|
+
destination = Path(destination)
|
|
243
|
+
logger.debug(f"Compressing file to {destination}")
|
|
244
|
+
|
|
245
|
+
with zipfile.ZipFile(
|
|
246
|
+
destination, mode="w", compression=zipfile.ZIP_BZIP2
|
|
247
|
+
) as archive:
|
|
248
|
+
for p in source.rglob("*"):
|
|
249
|
+
relpath = p.relative_to(source)
|
|
250
|
+
archive.write(p, arcname=relpath)
|
|
251
|
+
|
|
252
|
+
return archive, destination
|
|
253
|
+
|
|
254
|
+
|
|
228
255
|
DEFAULT_BATCH_SIZE = 50
|
|
229
256
|
"""Batch size used for bulk publication to Arkindex"""
|
|
230
257
|
|
|
@@ -32,6 +32,41 @@ from arkindex_worker.worker.task import TaskMixin
|
|
|
32
32
|
from arkindex_worker.worker.transcription import TranscriptionMixin
|
|
33
33
|
|
|
34
34
|
|
|
35
|
+
class WorkerActivityIterator:
|
|
36
|
+
def __init__(self, api_client):
|
|
37
|
+
# Use same api client as main class
|
|
38
|
+
self.api_client = api_client
|
|
39
|
+
|
|
40
|
+
logger.info(
|
|
41
|
+
"Using StartWorkerActivity instead of reading init_elements JSON file"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
def __bool__(self):
|
|
45
|
+
# Needed to bypass `not elements` check
|
|
46
|
+
return True
|
|
47
|
+
|
|
48
|
+
def __iter__(self):
|
|
49
|
+
return self
|
|
50
|
+
|
|
51
|
+
def __next__(self):
|
|
52
|
+
"""
|
|
53
|
+
Provide a new element ID from a worker activity upon each iteration
|
|
54
|
+
"""
|
|
55
|
+
try:
|
|
56
|
+
data = self.api_client.request("StartWorkerActivity")
|
|
57
|
+
except ErrorResponse as e:
|
|
58
|
+
# Arkindex will provide a 404 or 400 when there are no worker activities left or the task has completed
|
|
59
|
+
if e.status_code in (400, 404):
|
|
60
|
+
raise StopIteration from e
|
|
61
|
+
|
|
62
|
+
logger.warning(
|
|
63
|
+
f"Failed to start a new worker activity of element due to an API error: {e.content}"
|
|
64
|
+
)
|
|
65
|
+
raise e
|
|
66
|
+
|
|
67
|
+
return data["id"]
|
|
68
|
+
|
|
69
|
+
|
|
35
70
|
class ElementsWorker(
|
|
36
71
|
ElementMixin,
|
|
37
72
|
DatasetMixin,
|
|
@@ -60,7 +95,9 @@ class ElementsWorker(
|
|
|
60
95
|
"""
|
|
61
96
|
super().__init__(description, support_cache)
|
|
62
97
|
|
|
63
|
-
def get_elements(
|
|
98
|
+
def get_elements(
|
|
99
|
+
self,
|
|
100
|
+
) -> Iterable[CachedElement] | list[str] | list[Element] | WorkerActivityIterator:
|
|
64
101
|
"""
|
|
65
102
|
List the elements to be processed, either from the CLI arguments or
|
|
66
103
|
the cache database when enabled.
|
|
@@ -109,6 +146,9 @@ class ElementsWorker(
|
|
|
109
146
|
elif self.process_mode == ProcessMode.Export:
|
|
110
147
|
# For export mode processes, use list_process_elements and return element IDs
|
|
111
148
|
return {item["id"] for item in self.list_process_elements()}
|
|
149
|
+
elif self.consume_worker_activities:
|
|
150
|
+
# Consume worker activitives one by one
|
|
151
|
+
return WorkerActivityIterator(self.api_client)
|
|
112
152
|
|
|
113
153
|
invalid_element_ids = list(filter(invalid_element_id, out))
|
|
114
154
|
assert not invalid_element_ids, (
|
|
@@ -135,6 +175,15 @@ class ElementsWorker(
|
|
|
135
175
|
)
|
|
136
176
|
return self.process_information.get("activity_state") == "ready"
|
|
137
177
|
|
|
178
|
+
@property
|
|
179
|
+
def unknown_nb_elements(self) -> bool:
|
|
180
|
+
"""
|
|
181
|
+
Whether or not the worker knows the total number of elements to process
|
|
182
|
+
- when running with init_elements, we have a known list
|
|
183
|
+
- when running with StartWorkerActivity, we have a queue of unknown size
|
|
184
|
+
"""
|
|
185
|
+
return self.consume_worker_activities
|
|
186
|
+
|
|
138
187
|
def run(self):
|
|
139
188
|
"""
|
|
140
189
|
Implements an Arkindex worker that goes through each element returned by
|
|
@@ -157,7 +206,8 @@ class ElementsWorker(
|
|
|
157
206
|
)
|
|
158
207
|
|
|
159
208
|
# Process every element
|
|
160
|
-
|
|
209
|
+
# We cannot know the number of elements when consuming a list of worker activities
|
|
210
|
+
count = None if self.unknown_nb_elements else len(elements)
|
|
161
211
|
failed = 0
|
|
162
212
|
for i, item in enumerate(elements, start=1):
|
|
163
213
|
element = None
|
|
@@ -171,10 +221,16 @@ class ElementsWorker(
|
|
|
171
221
|
**self.api_client.request("RetrieveElement", id=item)
|
|
172
222
|
)
|
|
173
223
|
|
|
174
|
-
|
|
224
|
+
if self.unknown_nb_elements:
|
|
225
|
+
logger.info(f"Processing {element} (n°{i})")
|
|
226
|
+
else:
|
|
227
|
+
logger.info(f"Processing {element} ({i}/{count})")
|
|
175
228
|
|
|
176
229
|
# Process the element and report its progress if activities are enabled
|
|
177
|
-
|
|
230
|
+
# We do not update the worker activity to "Started" state when consuming them
|
|
231
|
+
if self.consume_worker_activities or self.update_activity(
|
|
232
|
+
element.id, ActivityState.Started
|
|
233
|
+
):
|
|
178
234
|
self.process_element(element)
|
|
179
235
|
self.update_activity(element.id, ActivityState.Processed)
|
|
180
236
|
else:
|
|
@@ -207,10 +263,10 @@ class ElementsWorker(
|
|
|
207
263
|
with contextlib.suppress(Exception):
|
|
208
264
|
self.update_activity(element.id, ActivityState.Error)
|
|
209
265
|
|
|
210
|
-
message = f"Ran on {
|
|
266
|
+
message = f"Ran on {i} {pluralize('element', i)}: {i - failed} completed, {failed} failed"
|
|
211
267
|
if failed:
|
|
212
268
|
logger.error(message)
|
|
213
|
-
if failed >=
|
|
269
|
+
if failed >= i: # Everything failed!
|
|
214
270
|
sys.exit(1)
|
|
215
271
|
else:
|
|
216
272
|
logger.info(message)
|
arkindex_worker/worker/base.py
CHANGED
|
@@ -9,12 +9,13 @@ import os
|
|
|
9
9
|
import shutil
|
|
10
10
|
from pathlib import Path
|
|
11
11
|
from tempfile import mkdtemp
|
|
12
|
+
from typing import Any
|
|
12
13
|
|
|
13
14
|
import gnupg
|
|
14
15
|
import yaml
|
|
15
16
|
|
|
16
17
|
from arkindex import options_from_env
|
|
17
|
-
from arkindex.exceptions import ErrorResponse
|
|
18
|
+
from arkindex.exceptions import ClientError, ErrorResponse
|
|
18
19
|
from arkindex_worker import logger
|
|
19
20
|
from arkindex_worker.cache import (
|
|
20
21
|
check_version,
|
|
@@ -260,7 +261,28 @@ class BaseWorker:
|
|
|
260
261
|
|
|
261
262
|
logger.info(f"Loaded {worker_run['summary']} from API")
|
|
262
263
|
|
|
264
|
+
def _process_config_item(item: dict) -> tuple[str, Any]:
|
|
265
|
+
if not item["secret"]:
|
|
266
|
+
return (item["key"], item["value"])
|
|
267
|
+
|
|
268
|
+
# The secret may not be picked by the user
|
|
269
|
+
if item["value"] is None:
|
|
270
|
+
logger.info(f"Optional secret `{item['key']}` is not set")
|
|
271
|
+
return (item["key"], None)
|
|
272
|
+
|
|
273
|
+
# Load secret, only available in Arkindex EE
|
|
274
|
+
try:
|
|
275
|
+
secret = self.load_secret(Path(item["value"]))
|
|
276
|
+
except ClientError as e:
|
|
277
|
+
logger.error(
|
|
278
|
+
f"Failed to retrieve the secret {item['value']}, probably an Arkindex Community Edition: {e}"
|
|
279
|
+
)
|
|
280
|
+
return (item["key"], None)
|
|
281
|
+
|
|
282
|
+
return (item["key"], secret)
|
|
283
|
+
|
|
263
284
|
# Load model version configuration when available
|
|
285
|
+
# Workers will use model version ID and details to download the model
|
|
264
286
|
model_version = worker_run.get("model_version")
|
|
265
287
|
if model_version:
|
|
266
288
|
logger.info("Loaded model version configuration from WorkerRun")
|
|
@@ -272,6 +294,36 @@ class BaseWorker:
|
|
|
272
294
|
# Set model details as worker attribute
|
|
273
295
|
self.model_details = model_version["model"]
|
|
274
296
|
|
|
297
|
+
# Load worker run information
|
|
298
|
+
try:
|
|
299
|
+
config = self.api_client.request(
|
|
300
|
+
"RetrieveWorkerRunConfiguration", id=self.worker_run_id
|
|
301
|
+
)
|
|
302
|
+
|
|
303
|
+
# Provide the same configuration through all previous attributes
|
|
304
|
+
self.config = self.user_configuration = dict(
|
|
305
|
+
map(_process_config_item, config["configuration"])
|
|
306
|
+
)
|
|
307
|
+
|
|
308
|
+
# Provide secret values through the previous attribute
|
|
309
|
+
self.secrets = {
|
|
310
|
+
item["key"]: self.config[item["key"]]
|
|
311
|
+
for item in config["configuration"]
|
|
312
|
+
if item["secret"]
|
|
313
|
+
}
|
|
314
|
+
logger.info("Using modern configuration")
|
|
315
|
+
|
|
316
|
+
# Reset the model configuration to make sure workers rely on the single new source
|
|
317
|
+
self.model_configuration = {}
|
|
318
|
+
|
|
319
|
+
return # Stop here once we have modern configuration
|
|
320
|
+
|
|
321
|
+
except ErrorResponse as e:
|
|
322
|
+
if e.status_code != 400:
|
|
323
|
+
raise
|
|
324
|
+
logger.info("Modern configuration is not available")
|
|
325
|
+
|
|
326
|
+
# Use old-style configuration with local merge
|
|
275
327
|
# Retrieve initial configuration from API
|
|
276
328
|
self.config = worker_version["configuration"].get("configuration", {})
|
|
277
329
|
if "user_configuration" in worker_version["configuration"]:
|
|
@@ -38,6 +38,15 @@ class ElementMixin:
|
|
|
38
38
|
type=open,
|
|
39
39
|
default=os.environ.get("TASK_ELEMENTS"),
|
|
40
40
|
)
|
|
41
|
+
self.parser.add_argument(
|
|
42
|
+
"--no-elements-list",
|
|
43
|
+
help=(
|
|
44
|
+
"Consume worker activities from Arkindex API instead of using a static elements list"
|
|
45
|
+
),
|
|
46
|
+
dest="consume_worker_activities",
|
|
47
|
+
action="store_true",
|
|
48
|
+
default=os.environ.get("SKIP_TASK_ELEMENTS") is not None,
|
|
49
|
+
)
|
|
41
50
|
self.parser.add_argument(
|
|
42
51
|
"--element",
|
|
43
52
|
type=str,
|
|
@@ -46,6 +55,17 @@ class ElementMixin:
|
|
|
46
55
|
)
|
|
47
56
|
super().add_arguments()
|
|
48
57
|
|
|
58
|
+
@property
|
|
59
|
+
def consume_worker_activities(self) -> bool:
|
|
60
|
+
"""
|
|
61
|
+
Helper to detect if the worker rely on an elements.json or consume directly worker activities
|
|
62
|
+
Uses the process information when available, fallback to CLI args
|
|
63
|
+
"""
|
|
64
|
+
if self.process_information is not None:
|
|
65
|
+
return self.process_information.get("skip_elements_json") is True
|
|
66
|
+
|
|
67
|
+
return self.args.consume_worker_activities
|
|
68
|
+
|
|
49
69
|
def list_corpus_types(self):
|
|
50
70
|
"""
|
|
51
71
|
Loads available element types in corpus.
|