Skip to content

Transcription

arkindex_worker.worker.transcription

ElementsWorker methods for transcriptions.

Attributes

Classes

TextOrientation

Bases: Enum

Orientation of a transcription’s text.

Attributes
HorizontalLeftToRight class-attribute instance-attribute
HorizontalLeftToRight = 'horizontal-lr'

The text is read from top to bottom then left to right. This is the default when no orientation is specified.

HorizontalRightToLeft class-attribute instance-attribute
HorizontalRightToLeft = 'horizontal-rl'

The text is read from top to bottom then right to left.

VerticalRightToLeft class-attribute instance-attribute
VerticalRightToLeft = 'vertical-rl'

The text is read from right to left then top to bottom.

VerticalLeftToRight class-attribute instance-attribute
VerticalLeftToRight = 'vertical-lr'

The text is read from left to right then top to bottom.

TranscriptionMixin

Functions
create_transcription
create_transcription(
    element: Element | CachedElement,
    text: str,
    confidence: float,
    orientation: TextOrientation = TextOrientation.HorizontalLeftToRight,
) -> dict[str, str | float] | None

Create a transcription on the given element through the API.

Parameters:

Name Type Description Default
element Element | CachedElement

Element to create a transcription on.

required
text str

Text of the transcription.

required
confidence float

Confidence score, between 0 and 1.

required
orientation TextOrientation

Orientation of the transcription’s text.

HorizontalLeftToRight

Returns:

Type Description
dict[str, str | float] | None

A dict as returned by the CreateTranscription API endpoint, or None if the worker is in read-only mode.

Source code in arkindex_worker/worker/transcription.py
 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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def create_transcription(
    self,
    element: Element | CachedElement,
    text: str,
    confidence: float,
    orientation: TextOrientation = TextOrientation.HorizontalLeftToRight,
) -> dict[str, str | float] | None:
    """
    Create a transcription on the given element through the API.

    :param element: Element to create a transcription on.
    :param text: Text of the transcription.
    :param confidence: Confidence score, between 0 and 1.
    :param orientation: Orientation of the transcription's text.
    :returns: A dict as returned by the ``CreateTranscription`` API endpoint,
       or None if the worker is in read-only mode.
    """
    assert element and isinstance(
        element, Element | CachedElement
    ), "element shouldn't be null and should be an Element or CachedElement"
    assert text and isinstance(
        text, str
    ), "text shouldn't be null and should be of type str"
    assert orientation and isinstance(
        orientation, TextOrientation
    ), "orientation shouldn't be null and should be of type TextOrientation"
    assert (
        isinstance(confidence, float) and 0 <= confidence <= 1
    ), "confidence shouldn't be null and should be a float in [0..1] range"

    if self.is_read_only:
        logger.warning(
            "Cannot create transcription as this worker is in read-only mode"
        )
        return

    created = self.api_client.request(
        "CreateTranscription",
        id=element.id,
        body={
            "text": text,
            "worker_run_id": self.worker_run_id,
            "confidence": confidence,
            "orientation": orientation.value,
        },
    )

    if self.use_cache:
        # Store transcription in local cache
        try:
            to_insert = [
                {
                    "id": created["id"],
                    "element_id": element.id,
                    "text": created["text"],
                    "confidence": created["confidence"],
                    "orientation": created["orientation"],
                    "worker_run_id": self.worker_run_id,
                }
            ]
            CachedTranscription.insert_many(to_insert).execute()
        except IntegrityError as e:
            logger.warning(
                f"Couldn't save created transcription in local cache: {e}"
            )

    return created
create_transcriptions
create_transcriptions(
    transcriptions: list[
        dict[str, str | float | TextOrientation | None]
    ],
    batch_size: int = DEFAULT_BATCH_SIZE,
) -> list[dict[str, str | float]]

Create multiple transcriptions at once on existing elements through the API, and creates CachedTranscription instances if cache support is enabled.

Parameters:

Name Type Description Default
transcriptions list[dict[str, str | float | TextOrientation | None]]

A list of dicts representing a transcription each, with the following keys: element_id (str) Required. UUID of the element to create this transcription on. text (str) Required. Text of the transcription. confidence (float) Required. Confidence score between 0 and 1. orientation (TextOrientation) Optional. Orientation of the transcription’s text.

required
batch_size int

The size of each batch, which will be used to split the publication to avoid API errors.

DEFAULT_BATCH_SIZE

Returns:

Type Description
list[dict[str, str | float]]

A list of dicts as returned in the transcriptions field by the CreateTranscriptions API endpoint.

Source code in arkindex_worker/worker/transcription.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@batch_publication
def create_transcriptions(
    self,
    transcriptions: list[dict[str, str | float | TextOrientation | None]],
    batch_size: int = DEFAULT_BATCH_SIZE,
) -> list[dict[str, str | float]]:
    """
    Create multiple transcriptions at once on existing elements through the API,
    and creates [CachedTranscription][arkindex_worker.cache.CachedTranscription] instances if cache support is enabled.

    :param transcriptions: A list of dicts representing a transcription each, with the following keys:

        element_id (str)
            Required. UUID of the element to create this transcription on.
        text (str)
            Required. Text of the transcription.
        confidence (float)
            Required. Confidence score between 0 and 1.
        orientation (TextOrientation)
            Optional. Orientation of the transcription's text.

    :param batch_size: The size of each batch, which will be used to split the publication to avoid API errors.

    :returns: A list of dicts as returned in the ``transcriptions`` field by the ``CreateTranscriptions`` API endpoint.
    """

    assert transcriptions and isinstance(
        transcriptions, list
    ), "transcriptions shouldn't be null and should be of type list"

    # Create shallow copies of every transcription to avoid mutating the original payload
    transcriptions_payload = list(map(dict, transcriptions))

    for index, transcription in enumerate(transcriptions_payload):
        element_id = transcription.get("element_id")
        assert (
            element_id and isinstance(element_id, str)
        ), f"Transcription at index {index} in transcriptions: element_id shouldn't be null and should be of type str"

        text = transcription.get("text")
        assert (
            text and isinstance(text, str)
        ), f"Transcription at index {index} in transcriptions: text shouldn't be null and should be of type str"

        confidence = transcription.get("confidence")
        assert (
            confidence is not None
            and isinstance(confidence, float)
            and 0 <= confidence <= 1
        ), f"Transcription at index {index} in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"

        orientation = transcription.get(
            "orientation", TextOrientation.HorizontalLeftToRight
        )
        assert (
            orientation and isinstance(orientation, TextOrientation)
        ), f"Transcription at index {index} in transcriptions: orientation shouldn't be null and should be of type TextOrientation"
        if orientation:
            transcription["orientation"] = orientation.value

    if self.is_read_only:
        logger.warning(
            "Cannot create transcription as this worker is in read-only mode"
        )
        return

    created_trs = [
        created_tr
        for batch in make_batches(
            transcriptions_payload, "transcription", batch_size
        )
        for created_tr in self.api_client.request(
            "CreateTranscriptions",
            body={
                "worker_run_id": self.worker_run_id,
                "transcriptions": batch,
            },
        )["transcriptions"]
    ]

    if self.use_cache:
        # Store transcriptions in local cache
        try:
            to_insert = [
                {
                    "id": created_tr["id"],
                    "element_id": created_tr["element_id"],
                    "text": created_tr["text"],
                    "confidence": created_tr["confidence"],
                    "orientation": created_tr["orientation"],
                    "worker_run_id": self.worker_run_id,
                }
                for created_tr in created_trs
            ]
            CachedTranscription.insert_many(to_insert).execute()
        except IntegrityError as e:
            logger.warning(
                f"Couldn't save created transcriptions in local cache: {e}"
            )

    return created_trs
create_element_transcriptions
create_element_transcriptions(
    element: Element | CachedElement,
    sub_element_type: str,
    transcriptions: list[dict[str, str | float]],
    batch_size: int = DEFAULT_BATCH_SIZE,
) -> dict[str, str | bool]

Create multiple elements and transcriptions at once on a single parent element through the API.

Parameters:

Name Type Description Default
element Element | CachedElement

Element to create elements and transcriptions on.

required
sub_element_type str

Slug of the element type to use for the new elements.

required
transcriptions list[dict[str, str | float]]

A list of dicts representing an element and transcription each, with the following keys: polygon (list(list(int or float))) Required. Polygon of the element. text (str) Required. Text of the transcription. confidence (float) Required. Confidence score between 0 and 1. orientation (TextOrientation) Optional. Orientation of the transcription’s text. element_confidence (float) Optional. Confidence score of the element between 0 and 1.

required
batch_size int

The size of each batch, which will be used to split the publication to avoid API errors.

DEFAULT_BATCH_SIZE

Returns:

Type Description
dict[str, str | bool]

A list of dicts as returned by the CreateElementTranscriptions API endpoint.

Source code in arkindex_worker/worker/transcription.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
@batch_publication
def create_element_transcriptions(
    self,
    element: Element | CachedElement,
    sub_element_type: str,
    transcriptions: list[dict[str, str | float]],
    batch_size: int = DEFAULT_BATCH_SIZE,
) -> dict[str, str | bool]:
    """
    Create multiple elements and transcriptions at once on a single parent element through the API.

    :param element: Element to create elements and transcriptions on.
    :param sub_element_type: Slug of the element type to use for the new elements.
    :param transcriptions: A list of dicts representing an element and transcription each, with the following keys:

        polygon (list(list(int or float)))
            Required. Polygon of the element.
        text (str)
            Required. Text of the transcription.
        confidence (float)
            Required. Confidence score between 0 and 1.
        orientation ([TextOrientation][arkindex_worker.worker.transcription.TextOrientation])
            Optional. Orientation of the transcription's text.
        element_confidence (float)
            Optional. Confidence score of the element between 0 and 1.

    :param batch_size: The size of each batch, which will be used to split the publication to avoid API errors.

    :returns: A list of dicts as returned by the ``CreateElementTranscriptions`` API endpoint.
    """
    assert element and isinstance(
        element, Element | CachedElement
    ), "element shouldn't be null and should be an Element or CachedElement"
    assert sub_element_type and isinstance(
        sub_element_type, str
    ), "sub_element_type shouldn't be null and should be of type str"
    assert transcriptions and isinstance(
        transcriptions, list
    ), "transcriptions shouldn't be null and should be of type list"

    # Create shallow copies of every transcription to avoid mutating the original payload
    transcriptions_payload = list(map(dict, transcriptions))

    for index, transcription in enumerate(transcriptions_payload):
        text = transcription.get("text")
        assert (
            text and isinstance(text, str)
        ), f"Transcription at index {index} in transcriptions: text shouldn't be null and should be of type str"

        confidence = transcription.get("confidence")
        assert (
            confidence is not None
            and isinstance(confidence, float)
            and 0 <= confidence <= 1
        ), f"Transcription at index {index} in transcriptions: confidence shouldn't be null and should be a float in [0..1] range"

        orientation = transcription.get(
            "orientation", TextOrientation.HorizontalLeftToRight
        )
        assert (
            orientation and isinstance(orientation, TextOrientation)
        ), f"Transcription at index {index} in transcriptions: orientation shouldn't be null and should be of type TextOrientation"
        if orientation:
            transcription["orientation"] = orientation.value

        polygon = transcription.get("polygon")
        assert (
            polygon and isinstance(polygon, list)
        ), f"Transcription at index {index} in transcriptions: polygon shouldn't be null and should be of type list"
        assert (
            len(polygon) >= 3
        ), f"Transcription at index {index} in transcriptions: polygon should have at least three points"
        assert all(
            isinstance(point, list) and len(point) == 2 for point in polygon
        ), f"Transcription at index {index} in transcriptions: polygon points should be lists of two items"
        assert all(
            isinstance(coord, int | float) for point in polygon for coord in point
        ), f"Transcription at index {index} in transcriptions: polygon points should be lists of two numbers"

        element_confidence = transcription.get("element_confidence")
        assert (
            element_confidence is None
            or (
                isinstance(element_confidence, float)
                and 0 <= element_confidence <= 1
            )
        ), f"Transcription at index {index} in transcriptions: element_confidence should be either null or a float in [0..1] range"

    if self.is_read_only:
        logger.warning(
            "Cannot create transcriptions as this worker is in read-only mode"
        )
        return

    annotations = [
        annotation
        for batch in make_batches(
            transcriptions_payload, "transcription", batch_size
        )
        for annotation in self.api_client.request(
            "CreateElementTranscriptions",
            id=element.id,
            body={
                "element_type": sub_element_type,
                "worker_run_id": self.worker_run_id,
                "transcriptions": batch,
                "return_elements": True,
            },
        )
    ]

    for annotation in annotations:
        if annotation["created"]:
            logger.debug(
                f"A sub_element of {element.id} with type {sub_element_type} was created during transcriptions bulk creation"
            )

    if self.use_cache:
        # Store transcriptions and their associated element (if created) in local cache
        created_ids = set()
        elements_to_insert = []
        transcriptions_to_insert = []
        for index, annotation in enumerate(annotations):
            transcription = transcriptions[index]

            if annotation["element_id"] not in created_ids:
                # Even if the API says the element already existed in the DB,
                # we need to check if it is available in the local cache.
                # Peewee does not have support for SQLite's INSERT OR IGNORE,
                # so we do the check here, element by element.
                try:
                    CachedElement.get_by_id(annotation["element_id"])
                except CachedElement.DoesNotExist:
                    elements_to_insert.append(
                        {
                            "id": annotation["element_id"],
                            "parent_id": element.id,
                            "type": sub_element_type,
                            "image_id": element.image_id,
                            "polygon": transcription["polygon"],
                            "worker_run_id": self.worker_run_id,
                            "confidence": transcription.get("element_confidence"),
                        }
                    )

                created_ids.add(annotation["element_id"])

            transcriptions_to_insert.append(
                {
                    "id": annotation["id"],
                    "element_id": annotation["element_id"],
                    "text": transcription["text"],
                    "confidence": transcription["confidence"],
                    "orientation": transcription.get(
                        "orientation", TextOrientation.HorizontalLeftToRight
                    ).value,
                    "worker_run_id": self.worker_run_id,
                }
            )

        try:
            CachedElement.insert_many(elements_to_insert).execute()
            CachedTranscription.insert_many(transcriptions_to_insert).execute()
        except IntegrityError as e:
            logger.warning(
                f"Couldn't save created transcriptions in local cache: {e}"
            )

    return annotations
list_transcriptions
list_transcriptions(
    element: Element | CachedElement,
    element_type: str | None = None,
    recursive: bool | None = None,
    worker_version: str | bool | None = None,
    worker_run: str | bool | None = None,
) -> Iterable[dict] | Iterable[CachedTranscription]

List transcriptions on an element.

Warns:

The following parameters are deprecated:

  • worker_version in favor of worker_run

Parameters:

Name Type Description Default
element Element | CachedElement

The element to list transcriptions on.

required
element_type str | None

Restrict to transcriptions whose elements have an element type with this slug.

None
recursive bool | None

Include transcriptions of any descendant of this element, recursively.

None
worker_version str | bool | None

Deprecated Restrict to transcriptions created by a worker version with this UUID. Set to False to look for manually created transcriptions.

None
worker_run str | bool | None

Restrict to transcriptions created by a worker run with this UUID. Set to False to look for manually created transcriptions.

None

Returns:

Type Description
Iterable[dict] | Iterable[CachedTranscription]

An iterable of dicts representing each transcription, or an iterable of CachedTranscription when cache support is enabled.

Source code in arkindex_worker/worker/transcription.py
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
def list_transcriptions(
    self,
    element: Element | CachedElement,
    element_type: str | None = None,
    recursive: bool | None = None,
    worker_version: str | bool | None = None,
    worker_run: str | bool | None = None,
) -> Iterable[dict] | Iterable[CachedTranscription]:
    """
    List transcriptions on an element.

    Warns:
    ----
    The following parameters are **deprecated**:

    - `worker_version` in favor of `worker_run`

    :param element: The element to list transcriptions on.
    :param element_type: Restrict to transcriptions whose elements have an element type with this slug.
    :param recursive: Include transcriptions of any descendant of this element, recursively.
    :param worker_version: **Deprecated** Restrict to transcriptions created by a worker version with this UUID. Set to False to look for manually created transcriptions.
    :param worker_run: Restrict to transcriptions created by a worker run with this UUID. Set to False to look for manually created transcriptions.
    :returns: An iterable of dicts representing each transcription,
       or an iterable of CachedTranscription when cache support is enabled.
    """
    assert element and isinstance(
        element, Element | CachedElement
    ), "element shouldn't be null and should be an Element or CachedElement"
    query_params = {}
    if element_type:
        assert isinstance(element_type, str), "element_type should be of type str"
        query_params["element_type"] = element_type
    if recursive is not None:
        assert isinstance(recursive, bool), "recursive should be of type bool"
        query_params["recursive"] = recursive
    if worker_version is not None:
        warn(
            "`worker_version` usage is deprecated. Consider using `worker_run` instead.",
            DeprecationWarning,
            stacklevel=1,
        )
        assert isinstance(
            worker_version, str | bool
        ), "worker_version should be of type str or bool"
        if isinstance(worker_version, bool):
            assert (
                worker_version is False
            ), "if of type bool, worker_version can only be set to False"
        query_params["worker_version"] = worker_version
    if worker_run is not None:
        assert isinstance(
            worker_run, str | bool
        ), "worker_run should be of type str or bool"
        if isinstance(worker_run, bool):
            assert (
                worker_run is False
            ), "if of type bool, worker_run can only be set to False"
        query_params["worker_run"] = worker_run

    if not self.use_cache:
        return self.api_client.paginate(
            "ListTranscriptions", id=element.id, **query_params
        )

    if not recursive:
        # In this case we don't have to return anything, it's easier to use an
        # impossible condition (False) rather than filtering by type for nothing
        if element_type and element_type != element.type:
            return CachedTranscription.select().where(False)
        transcriptions = CachedTranscription.select().where(
            CachedTranscription.element_id == element.id
        )
    else:
        base_case = (
            CachedElement.select()
            .where(CachedElement.id == element.id)
            .cte("base", recursive=True)
        )
        recursive = CachedElement.select().join(
            base_case, on=(CachedElement.parent_id == base_case.c.id)
        )
        cte = base_case.union_all(recursive)
        transcriptions = (
            CachedTranscription.select()
            .join(cte, on=(CachedTranscription.element_id == cte.c.id))
            .with_cte(cte)
        )

        if element_type:
            transcriptions = transcriptions.where(cte.c.type == element_type)

    if worker_version is not None:
        # If worker_version=False, filter by manual worker_version e.g. None
        worker_version_id = worker_version or None
        if worker_version_id:
            transcriptions = transcriptions.where(
                CachedTranscription.worker_version_id == worker_version_id
            )
        else:
            transcriptions = transcriptions.where(
                CachedTranscription.worker_version_id.is_null()
            )

    if worker_run is not None:
        # If worker_run=False, filter by manual worker_run e.g. None
        worker_run_id = worker_run or None
        if worker_run_id:
            transcriptions = transcriptions.where(
                CachedTranscription.worker_run_id == worker_run_id
            )
        else:
            transcriptions = transcriptions.where(
                CachedTranscription.worker_run_id.is_null()
            )

    return transcriptions

Functions