anyformat 2.4.0__tar.gz → 2.6.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.
- {anyformat-2.4.0 → anyformat-2.6.0}/PKG-INFO +3 -3
- {anyformat-2.4.0 → anyformat-2.6.0}/README.md +2 -2
- {anyformat-2.4.0 → anyformat-2.6.0}/pyproject.toml +1 -1
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/builder.py +4 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/builder.py +10 -1
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/checks.py +13 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/nodes.py +43 -1
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/results.py +85 -43
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/topology.py +125 -2
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/__init__.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/_banner.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/_http.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/_source.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/_viz.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/cli.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/client.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/errors.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/handles.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/result.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/sdk/views.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/__init__.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/definition.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/fields.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/schema.py +0 -0
- {anyformat-2.4.0 → anyformat-2.6.0}/src/anyformat/workflow/template_tokens.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: anyformat
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.6.0
|
|
4
4
|
Summary: Python SDK for the AnyFormat API
|
|
5
5
|
Project-URL: Homepage, https://anyformat.ai
|
|
6
6
|
Project-URL: Documentation, https://docs.anyformat.ai
|
|
@@ -567,14 +567,14 @@ afx parse invoice.pdf
|
|
|
567
567
|
Every knob is a first-class `.parse()` / `.extract()` keyword argument.
|
|
568
568
|
|
|
569
569
|
**`.parse(...)`** — `mode` (`"standard"`/`"agentic"`/`"lite"`/`"flash"`), `prompt_hint`, `figure_enhancement`,
|
|
570
|
-
`cache
|
|
570
|
+
`cache`, `confidence` (`False` runs no confidence scoring, so every confidence is null); **standard-only** `annotation_reading` (experimental: re-reads handwritten numbers); **agentic-only** `effort` (`"low"`/`"mid"`/`"accurate"`); **lite**: no knobs of its
|
|
571
571
|
own — one OCR-only recipe that never reads `prompt_hint`; **flash-only** `scanned_pages`
|
|
572
572
|
(`"skip"`/`"fail"`) — no model call, no OCR.
|
|
573
573
|
Mode-discriminated via per-mode `@overload`s — a wrong-mode knob is a type error.
|
|
574
574
|
|
|
575
575
|
**`.extract(...)`** — `mode` (`"standard"`/`"agentic"`/`"lite"`),
|
|
576
576
|
`lookup_suggestion`, `lookup_reasoning_effort` (`"minimal"`/`"low"`/`"medium"`/`"high"`),
|
|
577
|
-
`lookup_file_uploads` (inline base64).
|
|
577
|
+
`lookup_file_uploads` (inline base64), `confidence` (`False` runs no confidence scoring, so every field confidence is null).
|
|
578
578
|
|
|
579
579
|
**`Schema.<type>(...)`** — `source="smart_lookup"` (lookup overwrites) or `source="lookup_if_missing"` (extract too; lookup only fills empty slots), `persistent_id=` (pin field identity
|
|
580
580
|
across updates).
|
|
@@ -549,14 +549,14 @@ afx parse invoice.pdf
|
|
|
549
549
|
Every knob is a first-class `.parse()` / `.extract()` keyword argument.
|
|
550
550
|
|
|
551
551
|
**`.parse(...)`** — `mode` (`"standard"`/`"agentic"`/`"lite"`/`"flash"`), `prompt_hint`, `figure_enhancement`,
|
|
552
|
-
`cache
|
|
552
|
+
`cache`, `confidence` (`False` runs no confidence scoring, so every confidence is null); **standard-only** `annotation_reading` (experimental: re-reads handwritten numbers); **agentic-only** `effort` (`"low"`/`"mid"`/`"accurate"`); **lite**: no knobs of its
|
|
553
553
|
own — one OCR-only recipe that never reads `prompt_hint`; **flash-only** `scanned_pages`
|
|
554
554
|
(`"skip"`/`"fail"`) — no model call, no OCR.
|
|
555
555
|
Mode-discriminated via per-mode `@overload`s — a wrong-mode knob is a type error.
|
|
556
556
|
|
|
557
557
|
**`.extract(...)`** — `mode` (`"standard"`/`"agentic"`/`"lite"`),
|
|
558
558
|
`lookup_suggestion`, `lookup_reasoning_effort` (`"minimal"`/`"low"`/`"medium"`/`"high"`),
|
|
559
|
-
`lookup_file_uploads` (inline base64).
|
|
559
|
+
`lookup_file_uploads` (inline base64), `confidence` (`False` runs no confidence scoring, so every field confidence is null).
|
|
560
560
|
|
|
561
561
|
**`Schema.<type>(...)`** — `source="smart_lookup"` (lookup overwrites) or `source="lookup_if_missing"` (extract too; lookup only fills empty slots), `persistent_id=` (pin field identity
|
|
562
562
|
across updates).
|
|
@@ -42,6 +42,7 @@ class ClientBoundWorkflowBuilder(WorkflowBuilder):
|
|
|
42
42
|
lookup_reasoning_effort: ReasoningEffort | None = None,
|
|
43
43
|
lookup_files: list[str] | None = None,
|
|
44
44
|
lookup_file_uploads: list[LookupFileUpload] | None = None,
|
|
45
|
+
confidence: bool = True,
|
|
45
46
|
) -> Self:
|
|
46
47
|
uploads = [
|
|
47
48
|
LookupFileUpload(filename=Path(p).name, content=base64.b64encode(Path(p).read_bytes()).decode())
|
|
@@ -55,6 +56,7 @@ class ClientBoundWorkflowBuilder(WorkflowBuilder):
|
|
|
55
56
|
lookup_suggestion=lookup_suggestion,
|
|
56
57
|
lookup_reasoning_effort=lookup_reasoning_effort,
|
|
57
58
|
lookup_file_uploads=(uploads + (lookup_file_uploads or [])) or None,
|
|
59
|
+
confidence=confidence,
|
|
58
60
|
)
|
|
59
61
|
|
|
60
62
|
def create(self) -> Workflow:
|
|
@@ -111,6 +113,7 @@ class AsyncClientBoundWorkflowBuilder(WorkflowBuilder):
|
|
|
111
113
|
lookup_reasoning_effort: ReasoningEffort | None = None,
|
|
112
114
|
lookup_files: list[str] | None = None,
|
|
113
115
|
lookup_file_uploads: list[LookupFileUpload] | None = None,
|
|
116
|
+
confidence: bool = True,
|
|
114
117
|
) -> Self:
|
|
115
118
|
uploads = [
|
|
116
119
|
LookupFileUpload(filename=Path(p).name, content=base64.b64encode(Path(p).read_bytes()).decode())
|
|
@@ -124,6 +127,7 @@ class AsyncClientBoundWorkflowBuilder(WorkflowBuilder):
|
|
|
124
127
|
lookup_suggestion=lookup_suggestion,
|
|
125
128
|
lookup_reasoning_effort=lookup_reasoning_effort,
|
|
126
129
|
lookup_file_uploads=(uploads + (lookup_file_uploads or [])) or None,
|
|
130
|
+
confidence=confidence,
|
|
127
131
|
)
|
|
128
132
|
|
|
129
133
|
async def update(self, workflow_id: str) -> AsyncWorkflow:
|
|
@@ -118,6 +118,7 @@ class WorkflowBuilder:
|
|
|
118
118
|
figure_enhancement: bool = False,
|
|
119
119
|
annotation_reading: bool = False,
|
|
120
120
|
cache: bool = True,
|
|
121
|
+
confidence: bool = True,
|
|
121
122
|
id: str | None = None,
|
|
122
123
|
) -> Self: ...
|
|
123
124
|
@overload
|
|
@@ -129,6 +130,7 @@ class WorkflowBuilder:
|
|
|
129
130
|
prompt_hint: str | None = None,
|
|
130
131
|
figure_enhancement: bool = False,
|
|
131
132
|
cache: bool = True,
|
|
133
|
+
confidence: bool = True,
|
|
132
134
|
id: str | None = None,
|
|
133
135
|
) -> Self: ...
|
|
134
136
|
@overload
|
|
@@ -137,6 +139,7 @@ class WorkflowBuilder:
|
|
|
137
139
|
*,
|
|
138
140
|
mode: Literal["lite"],
|
|
139
141
|
cache: bool = True,
|
|
142
|
+
confidence: bool = True,
|
|
140
143
|
id: str | None = None,
|
|
141
144
|
) -> Self: ...
|
|
142
145
|
@overload
|
|
@@ -148,6 +151,7 @@ class WorkflowBuilder:
|
|
|
148
151
|
prompt_hint: str | None = None,
|
|
149
152
|
figure_enhancement: bool = False,
|
|
150
153
|
cache: bool = True,
|
|
154
|
+
confidence: bool = True,
|
|
151
155
|
id: str | None = None,
|
|
152
156
|
) -> Self: ...
|
|
153
157
|
|
|
@@ -159,6 +163,7 @@ class WorkflowBuilder:
|
|
|
159
163
|
figure_enhancement: bool = False,
|
|
160
164
|
annotation_reading: bool = False,
|
|
161
165
|
cache: bool = True,
|
|
166
|
+
confidence: bool = True,
|
|
162
167
|
effort: Literal["low", "mid", "accurate"] | None = None,
|
|
163
168
|
scanned_pages: Literal["ocr", "skip", "fail"] | None = None,
|
|
164
169
|
id: str | None = None,
|
|
@@ -176,6 +181,7 @@ class WorkflowBuilder:
|
|
|
176
181
|
"figure_enhancement": figure_enhancement,
|
|
177
182
|
"annotation_reading": annotation_reading,
|
|
178
183
|
"cache": cache,
|
|
184
|
+
"confidence": confidence,
|
|
179
185
|
}
|
|
180
186
|
# Map only the mode-specific knobs the caller set; ParseNode supplies per-field defaults.
|
|
181
187
|
for key, value in (
|
|
@@ -230,6 +236,7 @@ class WorkflowBuilder:
|
|
|
230
236
|
lookup_suggestion: str | None = None,
|
|
231
237
|
lookup_reasoning_effort: ReasoningEffort | None = None,
|
|
232
238
|
lookup_file_uploads: list[LookupFileUpload] | None = None,
|
|
239
|
+
confidence: bool = True,
|
|
233
240
|
) -> Self:
|
|
234
241
|
"""Add an extract node.
|
|
235
242
|
|
|
@@ -243,7 +250,8 @@ class WorkflowBuilder:
|
|
|
243
250
|
``lookup_reasoning_effort`` tune the smart-lookup matcher (effort applies
|
|
244
251
|
only when ≥1 field has ``source="smart_lookup"``). ``lookup_file_uploads``
|
|
245
252
|
carries inline file content (base64) that the backend uploads to S3 during
|
|
246
|
-
workflow create — pure, no filesystem IO.
|
|
253
|
+
workflow create — pure, no filesystem IO. ``confidence=False`` runs no confidence
|
|
254
|
+
scoring: every field confidence is null.
|
|
247
255
|
"""
|
|
248
256
|
self._require(self._parse is not None, "extract() requires parse() first")
|
|
249
257
|
self._require(fields, "extract() requires at least one field")
|
|
@@ -267,6 +275,7 @@ class WorkflowBuilder:
|
|
|
267
275
|
lookup_suggestion=lookup_suggestion,
|
|
268
276
|
lookup_reasoning_effort=lookup_reasoning_effort,
|
|
269
277
|
lookup_file_uploads=lookup_file_uploads or [],
|
|
278
|
+
confidence=confidence,
|
|
270
279
|
)
|
|
271
280
|
self._extracts.append((extract_node, branch_id))
|
|
272
281
|
return self
|
|
@@ -227,6 +227,18 @@ def contains_validation_check(check: Check) -> bool:
|
|
|
227
227
|
return False
|
|
228
228
|
|
|
229
229
|
|
|
230
|
+
def confidence_check_fields(check: Check) -> list[str]:
|
|
231
|
+
"""The ``field`` of every ``ConfidenceCheck`` in ``check``, walking combinators.
|
|
232
|
+
|
|
233
|
+
A check still a raw dict (a node built without validation) yields nothing.
|
|
234
|
+
"""
|
|
235
|
+
if isinstance(check, ConfidenceCheck):
|
|
236
|
+
return [check.field]
|
|
237
|
+
if isinstance(check, (AllOfCheck, AnyOfCheck)):
|
|
238
|
+
return [field for c in check.checks for field in confidence_check_fields(c)]
|
|
239
|
+
return []
|
|
240
|
+
|
|
241
|
+
|
|
230
242
|
def field_ids(check: Check) -> list[str]:
|
|
231
243
|
"""persistent_ids a check references — what the executor must resolve to field paths."""
|
|
232
244
|
if isinstance(check, ArithmeticCheck):
|
|
@@ -263,6 +275,7 @@ __all__ = [
|
|
|
263
275
|
"RegexCheck",
|
|
264
276
|
"RequiredCheck",
|
|
265
277
|
"ValidationCheck",
|
|
278
|
+
"confidence_check_fields",
|
|
266
279
|
"contains_validation_check",
|
|
267
280
|
"field_ids",
|
|
268
281
|
]
|
|
@@ -157,6 +157,11 @@ class ParseNode(BaseNode):
|
|
|
157
157
|
description="Reuse parsed output when the same file was already parsed with the same mode "
|
|
158
158
|
"and settings — a hit skips the whole parse. Disable to force a fresh parse.",
|
|
159
159
|
)
|
|
160
|
+
confidence: bool = Field(
|
|
161
|
+
default=True,
|
|
162
|
+
description="Score how reliable each parsed block is. Disable to skip the confidence "
|
|
163
|
+
"judge: every parse confidence is then null.",
|
|
164
|
+
)
|
|
160
165
|
# "agentic"-mode knob — ignored for standard/lite.
|
|
161
166
|
effort: Literal["low", "mid", "accurate"] = Field(
|
|
162
167
|
default="mid",
|
|
@@ -366,6 +371,11 @@ class ExtractNode(BaseNode):
|
|
|
366
371
|
description="After row extraction, independently re-check each page's rows against "
|
|
367
372
|
"document totals and re-extract pages that don't reconcile ('max' mode implies this).",
|
|
368
373
|
)
|
|
374
|
+
confidence: bool = Field(
|
|
375
|
+
default=True,
|
|
376
|
+
description="Score how reliable each extracted field is. Disable to skip the confidence "
|
|
377
|
+
"judge: every field confidence is then null.",
|
|
378
|
+
)
|
|
369
379
|
# Internal pipeline-routing knob. Not on the public API surface — the
|
|
370
380
|
# gateway declares its own ``PublicExtractNode`` without it.
|
|
371
381
|
context_tracker: str = "group_headers"
|
|
@@ -379,6 +389,24 @@ class ExtractNode(BaseNode):
|
|
|
379
389
|
serialization_alias="extract_model_alias",
|
|
380
390
|
)
|
|
381
391
|
|
|
392
|
+
@property
|
|
393
|
+
def no_confidence_reason(self) -> str | None:
|
|
394
|
+
"""Why this extract scores no confidence, or None when it does. An agentic extract
|
|
395
|
+
compiles before the row extractor, so it scores either way."""
|
|
396
|
+
if not self.confidence:
|
|
397
|
+
return "confidence is off"
|
|
398
|
+
if self.mode == "agentic":
|
|
399
|
+
return None
|
|
400
|
+
if self.mode == "max":
|
|
401
|
+
return "max mode extracts table rows page by page"
|
|
402
|
+
if self.row_extractor_enabled:
|
|
403
|
+
return "it extracts table rows page by page"
|
|
404
|
+
return None
|
|
405
|
+
|
|
406
|
+
@property
|
|
407
|
+
def produces_confidence(self) -> bool:
|
|
408
|
+
return self.no_confidence_reason is None
|
|
409
|
+
|
|
382
410
|
@classmethod
|
|
383
411
|
def accepts(cls, predecessor: type["BaseNode"]) -> bool:
|
|
384
412
|
# Structural predecessors — the shapes an Extract can sit downstream of.
|
|
@@ -888,6 +916,13 @@ PUBLIC_DEPRECATED_EMPTY_KEYS: dict[str, frozenset[str]] = {
|
|
|
888
916
|
"code": frozenset({"instructions", "template"}),
|
|
889
917
|
}
|
|
890
918
|
|
|
919
|
+
# Keys dropped from the public wire while they equal the default, so an older
|
|
920
|
+
# client's strict node shape still accepts a default node.
|
|
921
|
+
PUBLIC_DEFAULT_OMITTED_KEYS: dict[str, frozenset[str]] = {
|
|
922
|
+
"parse": frozenset({"confidence"}),
|
|
923
|
+
"extract": frozenset({"confidence"}),
|
|
924
|
+
}
|
|
925
|
+
|
|
891
926
|
|
|
892
927
|
def strip_public_hidden_keys(nodes: list[dict]) -> list[dict]:
|
|
893
928
|
"""Return ``nodes`` (list of dumped node dicts) with hidden keys removed.
|
|
@@ -900,7 +935,14 @@ def strip_public_hidden_keys(nodes: list[dict]) -> list[dict]:
|
|
|
900
935
|
for n in nodes:
|
|
901
936
|
hidden = PUBLIC_HIDDEN_NODE_KEYS.get(n.get("type"), frozenset())
|
|
902
937
|
deprecated = PUBLIC_DEPRECATED_EMPTY_KEYS.get(n.get("type"), frozenset())
|
|
903
|
-
|
|
938
|
+
default_omitted = PUBLIC_DEFAULT_OMITTED_KEYS.get(n.get("type"), frozenset())
|
|
939
|
+
out.append(
|
|
940
|
+
{
|
|
941
|
+
k: v
|
|
942
|
+
for k, v in n.items()
|
|
943
|
+
if k not in hidden and not (k in deprecated and not v) and not (k in default_omitted and v is True)
|
|
944
|
+
}
|
|
945
|
+
)
|
|
904
946
|
return out
|
|
905
947
|
|
|
906
948
|
|
|
@@ -9,6 +9,7 @@ from typing import Annotated, Any, Generic, Literal, Optional, TypeVar, Union
|
|
|
9
9
|
|
|
10
10
|
from pydantic import (
|
|
11
11
|
BaseModel,
|
|
12
|
+
ConfigDict,
|
|
12
13
|
Discriminator,
|
|
13
14
|
Field,
|
|
14
15
|
SerializerFunctionWrapHandler,
|
|
@@ -270,7 +271,8 @@ class Block(_OmitsUnsetFileRefs):
|
|
|
270
271
|
description=(
|
|
271
272
|
"The layout detections this block is made of, each with the words printed inside it. "
|
|
272
273
|
"A block the detector reported as one box has one region, that box. Empty only for a "
|
|
273
|
-
"block parsed before detections were reported,
|
|
274
|
+
"block parsed before detections were reported, one that has no box at all, or one whose parse "
|
|
275
|
+
"stored no detections object or one that could not be read back."
|
|
274
276
|
),
|
|
275
277
|
)
|
|
276
278
|
content: str = Field(
|
|
@@ -393,67 +395,103 @@ class RunParseLayerAbsence(BaseModel):
|
|
|
393
395
|
|
|
394
396
|
absent: str = Field(
|
|
395
397
|
description=(
|
|
396
|
-
"The stored reason, such as `build_failed`, or why a read could not serve a stored layer: `
|
|
397
|
-
"
|
|
398
|
-
"its own."
|
|
398
|
+
"The stored reason, such as `build_failed`, or why a read could not serve a stored layer: `build_failed` "
|
|
399
|
+
"when its copy failed to build on each recent attempt, `unreadable` when its object cannot be read back. "
|
|
400
|
+
"The public API words each as its own."
|
|
399
401
|
)
|
|
400
402
|
)
|
|
401
403
|
|
|
402
404
|
|
|
403
|
-
|
|
404
|
-
mean: Percent = Field(description="The mean confidence of the sequence's elements, a whole number from 0 to 100.")
|
|
405
|
-
min: Percent = Field(description="The lowest confidence of the sequence's elements, a whole number from 0 to 100.")
|
|
405
|
+
ElementSource = Literal["embedded", "ocr", "transcribed"]
|
|
406
406
|
|
|
407
407
|
|
|
408
|
-
|
|
409
|
-
""
|
|
408
|
+
def _open_in_the_spec(schema: dict[str, Any]) -> None:
|
|
409
|
+
schema.pop("additionalProperties", None)
|
|
410
410
|
|
|
411
|
-
|
|
411
|
+
|
|
412
|
+
# Strict, so a record holding a key the contract does not name fails validation; open in the spec, where a new key is
|
|
413
|
+
# additive.
|
|
414
|
+
_PUBLIC_RECORD = ConfigDict(extra="forbid", json_schema_extra=_open_in_the_spec)
|
|
415
|
+
|
|
416
|
+
_PACKET_PAGE = Field(description="1-indexed page number, counted across the packet's files in packet order.")
|
|
417
|
+
_FILE_OF_A_RECORD = Field(description="The file this belongs to, also in a packet of one file.")
|
|
418
|
+
_PAGE_OF_A_RECORD = Field(description="1-indexed page of that file. Equal to `page_number` in a packet of one file.")
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
class BoxV3(BaseModel):
|
|
422
|
+
"""A box on its page, in [0, 1] from the top-left corner, so `y` grows down the page; each coordinate rounded half
|
|
423
|
+
up to 6 decimals, as it is stored.
|
|
424
|
+
|
|
425
|
+
The page is in the frame its `width` and `height` measure: as a PDF viewer shows the file, without turning it by
|
|
426
|
+
the page's `rotation`. On a page whose content is turned, a box frames the turned content, and turns with it.
|
|
427
|
+
"""
|
|
428
|
+
|
|
429
|
+
model_config = _PUBLIC_RECORD
|
|
430
|
+
|
|
431
|
+
x0: float
|
|
432
|
+
y0: float
|
|
433
|
+
x1: float
|
|
434
|
+
y1: float
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
class SequenceConfidenceV3(BaseModel):
|
|
438
|
+
model_config = _PUBLIC_RECORD
|
|
439
|
+
|
|
440
|
+
mean: Percent = Field(description="The mean confidence of its elements, a whole number from 0 to 100.")
|
|
441
|
+
min: Percent = Field(description="The lowest confidence of its elements, a whole number from 0 to 100.")
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
class SequenceV3(BaseModel):
|
|
445
|
+
"""A run of elements grouped by horizontal proximity inside one block: often a line, never guaranteed."""
|
|
446
|
+
|
|
447
|
+
model_config = _PUBLIC_RECORD
|
|
448
|
+
|
|
449
|
+
id: str = Field(description="`p<page_number>_s<index>`, numbered by the packet page.", examples=["p3_s4"])
|
|
412
450
|
block_id: str = Field(description="The `blocks[].id` of the block that holds it.")
|
|
413
|
-
|
|
414
|
-
file_id:
|
|
415
|
-
file_page:
|
|
416
|
-
text: str
|
|
417
|
-
bbox:
|
|
418
|
-
|
|
419
|
-
)
|
|
420
|
-
confidence: Optional[RunParseSequenceConfidence] = Field(
|
|
451
|
+
page_number: int = _PACKET_PAGE
|
|
452
|
+
file_id: str = _FILE_OF_A_RECORD
|
|
453
|
+
file_page: int = _PAGE_OF_A_RECORD
|
|
454
|
+
text: str
|
|
455
|
+
bbox: BoxV3
|
|
456
|
+
confidence: Optional[SequenceConfidenceV3] = Field(
|
|
421
457
|
description="`null` when nothing scored it: a model's reading of handwriting."
|
|
422
458
|
)
|
|
423
459
|
cell_id: Optional[str] = Field(
|
|
424
|
-
default=None,
|
|
425
460
|
description=(
|
|
426
|
-
"The
|
|
427
|
-
"
|
|
461
|
+
"The cell every one of its elements was placed in: its `blocks[].table.cells[].id`, as an evidence item's "
|
|
462
|
+
"`cell_ids` name cells. `null` when none or two."
|
|
428
463
|
),
|
|
464
|
+
examples=["p3_b0.r0c0"],
|
|
429
465
|
)
|
|
430
466
|
|
|
431
467
|
|
|
432
|
-
class
|
|
468
|
+
class ElementV3(BaseModel):
|
|
433
469
|
"""One piece of text as read: a word, or a whole line when the OCR engine boxed it as one."""
|
|
434
470
|
|
|
435
|
-
|
|
471
|
+
model_config = _PUBLIC_RECORD
|
|
472
|
+
|
|
473
|
+
id: str = Field(description="`p<page_number>_e<index>`, numbered by the packet page.", examples=["p3_e17"])
|
|
436
474
|
block_id: str = Field(description="The `blocks[].id` of the block that owns it.")
|
|
437
|
-
sequence_id: Optional[str] = Field(description="The sequence
|
|
438
|
-
|
|
439
|
-
file_id:
|
|
440
|
-
file_page:
|
|
441
|
-
text: str
|
|
442
|
-
bbox:
|
|
443
|
-
description="Normalised bounding box in [0, 1] page coordinates with keys `x0`/`y0`/`x1`/`y1`.",
|
|
444
|
-
)
|
|
475
|
+
sequence_id: Optional[str] = Field(description="The sequence that holds it; `null` when none does.")
|
|
476
|
+
page_number: int = _PACKET_PAGE
|
|
477
|
+
file_id: str = _FILE_OF_A_RECORD
|
|
478
|
+
file_page: int = _PAGE_OF_A_RECORD
|
|
479
|
+
text: str
|
|
480
|
+
bbox: BoxV3
|
|
445
481
|
confidence: Optional[Percent] = Field(
|
|
446
|
-
description="A whole number from 0 to 100; `null` when nothing scored it."
|
|
482
|
+
description="A whole number from 0 to 100; `null` when nothing scored it, as a model's reading of handwriting."
|
|
483
|
+
)
|
|
484
|
+
source: ElementSource = Field(
|
|
485
|
+
description="Where its text came from: the file's own text, OCR, or a model's reading of handwriting."
|
|
447
486
|
)
|
|
448
|
-
source: str = Field(description="Where its text came from: `embedded`, `ocr` or `transcribed`.")
|
|
449
487
|
|
|
450
488
|
|
|
451
489
|
class RunParseLayerLink(BaseModel):
|
|
452
|
-
"""A
|
|
490
|
+
"""A layer too large to serve inline, as a copy built in the public shape: ``{"<layer>": [record, …]}``, each
|
|
491
|
+
record as it is served inline."""
|
|
453
492
|
|
|
454
|
-
url: str = Field(description="A presigned GET of the
|
|
455
|
-
|
|
456
|
-
size_bytes: int = Field(description="The object's size as stored, gzipped.")
|
|
493
|
+
url: str = Field(description="A presigned GET of the copy, valid for 15 minutes; it is served gzip-encoded.")
|
|
494
|
+
size_bytes: int = Field(description="The copy's size as stored, gzipped.")
|
|
457
495
|
|
|
458
496
|
|
|
459
497
|
class RunParseTableCell(BaseModel):
|
|
@@ -534,8 +572,9 @@ class RunParseV3(ParseResult):
|
|
|
534
572
|
layers: Optional[dict[str, RunParseLayerAbsence]] = Field(
|
|
535
573
|
default=None,
|
|
536
574
|
description=(
|
|
537
|
-
"Each layer the parse stored without its object, by name, with the reason, and each layer
|
|
538
|
-
"
|
|
575
|
+
"Each layer the parse stored without its object, by name, with the reason, and each layer the read could "
|
|
576
|
+
"not serve: one asked for, or `cells` or `regions`, which every read serves; any other stored layer is not "
|
|
577
|
+
"listed. A parse with a file parsed by a tier that "
|
|
539
578
|
"measures neither `sequences` nor `elements` (`lite`) lists both as `not_for_tier`, whatever it stored. "
|
|
540
579
|
"Otherwise `null` when the run stored no listing of this node's packet parse, as for a parse stored before "
|
|
541
580
|
"listings were."
|
|
@@ -545,11 +584,11 @@ class RunParseV3(ParseResult):
|
|
|
545
584
|
default_factory=list,
|
|
546
585
|
description="Each block, in packet page and reading order, with its table cells.",
|
|
547
586
|
)
|
|
548
|
-
sequences: Optional[list[
|
|
587
|
+
sequences: Optional[list[SequenceV3]] = Field(
|
|
549
588
|
default=None,
|
|
550
589
|
description="Each sequence, in packet page and reading order, when asked for and served; else `null`.",
|
|
551
590
|
)
|
|
552
|
-
elements: Optional[list[
|
|
591
|
+
elements: Optional[list[ElementV3]] = Field(
|
|
553
592
|
default=None,
|
|
554
593
|
description="Each element, in packet page and reading order, when asked for and served; else `null`.",
|
|
555
594
|
)
|
|
@@ -1003,10 +1042,13 @@ class ResultsResponseV3(ResultsResponse):
|
|
|
1003
1042
|
|
|
1004
1043
|
__all__ = [
|
|
1005
1044
|
"Block",
|
|
1045
|
+
"BoxV3",
|
|
1006
1046
|
"ClassificationResult",
|
|
1007
1047
|
"Detection",
|
|
1008
1048
|
"Edit",
|
|
1009
1049
|
"EditFormField",
|
|
1050
|
+
"ElementSource",
|
|
1051
|
+
"ElementV3",
|
|
1010
1052
|
"Evidence",
|
|
1011
1053
|
"ExtractedField",
|
|
1012
1054
|
"Extraction",
|
|
@@ -1023,13 +1065,13 @@ __all__ = [
|
|
|
1023
1065
|
"ResultsResponse",
|
|
1024
1066
|
"ResultsResponseV3",
|
|
1025
1067
|
"CodeOutput",
|
|
1026
|
-
"RunParseElement",
|
|
1027
1068
|
"RunParseLayerAbsence",
|
|
1028
1069
|
"RunParseLayerLink",
|
|
1029
1070
|
"RunParsePage",
|
|
1030
1071
|
"RunParsePageStatus",
|
|
1031
|
-
"RunParseSequence",
|
|
1032
1072
|
"RunParseV3",
|
|
1073
|
+
"SequenceConfidenceV3",
|
|
1074
|
+
"SequenceV3",
|
|
1033
1075
|
"SplitPartition",
|
|
1034
1076
|
"SplitResult",
|
|
1035
1077
|
"Usage",
|
|
@@ -14,17 +14,19 @@ the backend to run :func:`validate` on the graph it is building, so neither
|
|
|
14
14
|
half is restated in the frontend.
|
|
15
15
|
"""
|
|
16
16
|
|
|
17
|
+
import uuid
|
|
17
18
|
from collections.abc import Callable, Mapping, Sequence
|
|
18
19
|
from dataclasses import dataclass, field
|
|
19
20
|
from typing import TYPE_CHECKING, Protocol, runtime_checkable
|
|
20
21
|
|
|
21
|
-
from anyformat.workflow.checks import AllOfCheck, AnyOfCheck, Check, ValidationCheck
|
|
22
|
+
from anyformat.workflow.checks import AllOfCheck, AnyOfCheck, Check, ValidationCheck, confidence_check_fields
|
|
22
23
|
from anyformat.workflow.fields import AnyField, ObjectField, sanitize_field_name
|
|
23
24
|
from anyformat.workflow.nodes import (
|
|
24
25
|
NODE_CLASSES_BY_TYPE,
|
|
25
26
|
BaseNode,
|
|
26
27
|
ClassifyNode,
|
|
27
28
|
CodeNode,
|
|
29
|
+
ExtractionSchema,
|
|
28
30
|
ExtractNode,
|
|
29
31
|
FilterNode,
|
|
30
32
|
IfElseNode,
|
|
@@ -33,6 +35,7 @@ from anyformat.workflow.nodes import (
|
|
|
33
35
|
SlackAlertNode,
|
|
34
36
|
SplitterNode,
|
|
35
37
|
ValidateNode,
|
|
38
|
+
ValidationRule,
|
|
36
39
|
)
|
|
37
40
|
from anyformat.workflow.template_tokens import referenced_rule_ids
|
|
38
41
|
|
|
@@ -561,7 +564,9 @@ class EveryNonParseNodeHasInboundEdge:
|
|
|
561
564
|
targets = {e.target for e in graph.edges}
|
|
562
565
|
violations: list[Violation] = []
|
|
563
566
|
for node in graph.nodes:
|
|
564
|
-
|
|
567
|
+
# A knowledge node opts in by presence and has no handles, so it is
|
|
568
|
+
# saved unwired; it is still no entry, which ``needs_inbound_edge`` reports.
|
|
569
|
+
if not self.needs_inbound_edge(type(node)) or isinstance(node, KnowledgeNode):
|
|
565
570
|
continue
|
|
566
571
|
if node.id not in targets:
|
|
567
572
|
violations.append(
|
|
@@ -856,6 +861,122 @@ class ValidationRuleIdReferencesExist:
|
|
|
856
861
|
return violations
|
|
857
862
|
|
|
858
863
|
|
|
864
|
+
def _upstream_extracts(
|
|
865
|
+
consumer_id: str,
|
|
866
|
+
nodes_by_id: dict[str, "BaseNode"],
|
|
867
|
+
edges_by_target: dict[str, list[str]],
|
|
868
|
+
) -> list[ExtractNode]:
|
|
869
|
+
"""The Extracts a check on ``consumer_id`` reads: those upstream, looking through Validate nodes."""
|
|
870
|
+
extracts: list[ExtractNode] = []
|
|
871
|
+
seen: set[str] = set()
|
|
872
|
+
frontier = list(edges_by_target.get(consumer_id, []))
|
|
873
|
+
while frontier:
|
|
874
|
+
current = frontier.pop()
|
|
875
|
+
if current in seen:
|
|
876
|
+
continue
|
|
877
|
+
seen.add(current)
|
|
878
|
+
node = nodes_by_id.get(current)
|
|
879
|
+
if isinstance(node, ExtractNode):
|
|
880
|
+
extracts.append(node)
|
|
881
|
+
elif isinstance(node, ValidateNode):
|
|
882
|
+
frontier.extend(edges_by_target.get(current, []))
|
|
883
|
+
return extracts
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
def _smart_lookup_fields(extract: ExtractNode) -> list[tuple[AnyField, bool]]:
|
|
887
|
+
"""The fields a smart lookup always fills, which carry no confidence, each flagged nested or not."""
|
|
888
|
+
schema = extract.extraction_schema
|
|
889
|
+
declared = schema.fields if isinstance(schema, ExtractionSchema) else []
|
|
890
|
+
return [
|
|
891
|
+
(f, nested)
|
|
892
|
+
for f, nested in _with_nested([*extract.lookup_schema, *declared])
|
|
893
|
+
if getattr(f, "source", None) == "smart_lookup"
|
|
894
|
+
]
|
|
895
|
+
|
|
896
|
+
|
|
897
|
+
def _with_nested(fields: "Sequence[AnyField | dict]", *, nested: bool = False) -> list[tuple[AnyField, bool]]:
|
|
898
|
+
"""Every field and its nested children. A field relaxed on read can still be a dict, so it is skipped."""
|
|
899
|
+
flat: list[tuple[AnyField, bool]] = []
|
|
900
|
+
for f in fields:
|
|
901
|
+
if isinstance(f, dict):
|
|
902
|
+
continue
|
|
903
|
+
flat.append((f, nested))
|
|
904
|
+
if isinstance(f, ObjectField):
|
|
905
|
+
flat.extend(_with_nested(f.nested_fields, nested=True))
|
|
906
|
+
return flat
|
|
907
|
+
|
|
908
|
+
|
|
909
|
+
def _normalized_id(value: str | None) -> str | None:
|
|
910
|
+
"""Hyphenated, hex and upper-case spellings of one UUID compare equal, as in the executor."""
|
|
911
|
+
try:
|
|
912
|
+
return uuid.UUID(str(value)).hex
|
|
913
|
+
except ValueError:
|
|
914
|
+
return value
|
|
915
|
+
|
|
916
|
+
|
|
917
|
+
def _names_field(field: AnyField, ref: str, *, nested: bool) -> bool:
|
|
918
|
+
"""The executor resolves a bare name to the top-level key, so a nested field is named by its id only."""
|
|
919
|
+
if field.persistent_id is not None and _normalized_id(field.persistent_id) == _normalized_id(ref):
|
|
920
|
+
return True
|
|
921
|
+
return not nested and ref in (field.name, sanitize_field_name(field.name))
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
class ConfidenceCheckCanPass:
|
|
925
|
+
"""A ``ConfidenceCheck`` on a Validate rule or an If-Else condition must read a
|
|
926
|
+
confidence its Extract produces.
|
|
927
|
+
|
|
928
|
+
An Extract with confidence off, in max mode, or with the row extractor on returns
|
|
929
|
+
every confidence as null, and so does a smart-lookup field. The check then never
|
|
930
|
+
passes, and routing silently takes the fallback branch. Fail at save instead.
|
|
931
|
+
"""
|
|
932
|
+
|
|
933
|
+
name = "confidence_check_can_pass"
|
|
934
|
+
|
|
935
|
+
def check(self, graph: "WorkflowDefinition") -> list[Violation]:
|
|
936
|
+
nodes_by_id: dict[str, BaseNode] = {n.id: n for n in graph.nodes}
|
|
937
|
+
edges_by_target: dict[str, list[str]] = {}
|
|
938
|
+
for edge in graph.edges:
|
|
939
|
+
edges_by_target.setdefault(edge.target, []).append(edge.source)
|
|
940
|
+
violations: list[Violation] = []
|
|
941
|
+
for node in graph.nodes:
|
|
942
|
+
refs = self._confidence_refs(node)
|
|
943
|
+
if not refs:
|
|
944
|
+
continue
|
|
945
|
+
for extract in _upstream_extracts(node.id, nodes_by_id, edges_by_target):
|
|
946
|
+
reason = self._unscored_reason(extract, refs)
|
|
947
|
+
if reason:
|
|
948
|
+
violations.append(
|
|
949
|
+
Violation(
|
|
950
|
+
rule=self.name,
|
|
951
|
+
message=f'This confidence check can never pass: extract "{extract.id}" {reason}.',
|
|
952
|
+
node_ids=[node.id],
|
|
953
|
+
)
|
|
954
|
+
)
|
|
955
|
+
return violations
|
|
956
|
+
|
|
957
|
+
@staticmethod
|
|
958
|
+
def _confidence_refs(node: "BaseNode") -> list[str]:
|
|
959
|
+
if isinstance(node, IfElseNode):
|
|
960
|
+
return confidence_check_fields(node.condition)
|
|
961
|
+
if isinstance(node, ValidateNode):
|
|
962
|
+
return [
|
|
963
|
+
ref
|
|
964
|
+
for rule in node.rules
|
|
965
|
+
if isinstance(rule, ValidationRule) and rule.check is not None
|
|
966
|
+
for ref in confidence_check_fields(rule.check)
|
|
967
|
+
]
|
|
968
|
+
return []
|
|
969
|
+
|
|
970
|
+
@staticmethod
|
|
971
|
+
def _unscored_reason(extract: ExtractNode, refs: list[str]) -> str | None:
|
|
972
|
+
if extract.no_confidence_reason:
|
|
973
|
+
return f"does not score confidence because {extract.no_confidence_reason}"
|
|
974
|
+
for looked_up, nested in _smart_lookup_fields(extract):
|
|
975
|
+
if any(_names_field(looked_up, ref, nested=nested) for ref in refs):
|
|
976
|
+
return f'does not score confidence because field "{looked_up.name}" is set to always look up'
|
|
977
|
+
return None
|
|
978
|
+
|
|
979
|
+
|
|
859
980
|
class NoCodeInstructions:
|
|
860
981
|
"""Reject a code node that holds the retired ``instructions`` or ``template``.
|
|
861
982
|
|
|
@@ -901,6 +1022,7 @@ RULES: list[TopologyRule] = [
|
|
|
901
1022
|
NoRouterUnderSplitter(),
|
|
902
1023
|
NoCodeInstructions(),
|
|
903
1024
|
ValidationRuleIdReferencesExist(),
|
|
1025
|
+
ConfidenceCheckCanPass(),
|
|
904
1026
|
NoCycles(),
|
|
905
1027
|
]
|
|
906
1028
|
|
|
@@ -1054,6 +1176,7 @@ __all__ = [
|
|
|
1054
1176
|
"BranchForbiddenFromNonBranchingSource",
|
|
1055
1177
|
"BranchMatchesSourceId",
|
|
1056
1178
|
"BranchRequiredFromBranchingSource",
|
|
1179
|
+
"ConfidenceCheckCanPass",
|
|
1057
1180
|
"EdgeAcceptsPredecessor",
|
|
1058
1181
|
"EdgesReferenceExistingNodes",
|
|
1059
1182
|
"EveryExtractNodeHasFields",
|
|
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
|
|
File without changes
|