datalab-python-sdk 0.12.0__py3-none-any.whl → 0.13.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/METADATA +13 -2
- datalab_python_sdk-0.13.0.dist-info/RECORD +13 -0
- datalab_sdk/__init__.py +4 -0
- datalab_sdk/client.py +451 -202
- datalab_sdk/exceptions.py +49 -4
- datalab_python_sdk-0.12.0.dist-info/RECORD +0 -13
- {datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/WHEEL +0 -0
- {datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/entry_points.txt +0 -0
- {datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: datalab-python-sdk
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.13.0
|
|
4
4
|
Summary: SDK for the Datalab document intelligence API
|
|
5
5
|
Project-URL: Homepage, https://www.datalab.to
|
|
6
6
|
Project-URL: Documentation, https://documentation.datalab.to
|
|
@@ -183,7 +183,18 @@ Published-agent execution is in open beta; the API may change. Processor design
|
|
|
183
183
|
|
|
184
184
|
- Send a local file with `file_path`, or a URL or a `datalab://file-...` reference with `file_url`. When you set `processing_location`, send the document as `file_url`.
|
|
185
185
|
- Processing methods poll until the result is ready. The first 8 seconds poll every 0.3 seconds. Set `max_polls` and `poll_interval` to change the limit (default: 300 polls, 1 second apart). Use `stream_response_to` to write a large result straight to disk. It returns a `FileResult` with the status, `output_path` and, when the poll response sends them, `request_id`, `page_count`, `parse_quality_score`, `versions`, `runtime` and `cost_breakdown`.
|
|
186
|
-
- The SDK retries a submission that gets HTTP 429 or 503, and polls or result downloads that get 408, 429, 5xx or a connection error, including one that happens while a result streams. It waits for the `Retry-After` header when the API sends one.
|
|
186
|
+
- The SDK retries a submission that gets HTTP 429 or 503, and polls or result downloads that get 408, 429, 5xx or a connection error, including one that happens while a result streams. It tries each request at most 10 times, and waits for the `Retry-After` header when the API sends one. `timeout` applies to each request, including the whole download of a result. A finished result that times out while it downloads from a signed `result_url`, or streams with `stream_response_to`, is not retried.
|
|
187
|
+
- Timeouts raise `DatalabTimeoutError`. `DatalabPollingTimeoutError` means the polling budget ran out and the request is still running; `DatalabTransferTimeoutError` means the request finished but its result timed out while it downloaded, so raise `timeout` before you fetch it again. An error that a document-processing method raises after the API accepted the request has `request_id` and `check_url`. Pass `check_url` to `get_result()` to fetch the result without submitting the document again. For `run_pipeline()`, `request_id` holds the execution ID for `get_pipeline_execution()`.
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from datalab_sdk import DatalabClient, DatalabPollingTimeoutError
|
|
191
|
+
|
|
192
|
+
client = DatalabClient()
|
|
193
|
+
try:
|
|
194
|
+
result = client.convert("big.pdf", max_polls=60)
|
|
195
|
+
except DatalabPollingTimeoutError as e:
|
|
196
|
+
result = client.get_result(e.check_url, max_polls=600)
|
|
197
|
+
```
|
|
187
198
|
- API errors raise `DatalabAPIError` (with `status_code` and `response_data`) or one of its subclasses: `DatalabAuthenticationError` (401, 403), `DatalabPaymentRequiredError` (402), `DatalabNotFoundError` (404), `DatalabRateLimitError` (429) and `DatalabServerError` (5xx). Invalid arguments raise `DatalabValidationError`, which is also a `ValueError`.
|
|
188
199
|
|
|
189
200
|
With SDK 0.9.0, `upload_files(paths, max_concurrency=5)` streams
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
datalab_sdk/__init__.py,sha256=MRRQMFIAhGx5sLnOzvVyKlusLzKj1F2k-_SaE6ivk6o,3455
|
|
2
|
+
datalab_sdk/auth.py,sha256=X2eccKzpdxogFEDoS4geOHIOeECQ8u3kANxKoqF0PVk,4388
|
|
3
|
+
datalab_sdk/cli.py,sha256=g_qR_xMJS19PYVOf19HRD3nl3GnzHf1bIohh7WY5AO0,63928
|
|
4
|
+
datalab_sdk/client.py,sha256=i8ykHqFiYrmNS-8oW9Z96WZZIa6qU820fsqyJIHgqRA,171577
|
|
5
|
+
datalab_sdk/exceptions.py,sha256=GFRAZQoxP2cHYJ3Q-OBjU_3I34Om3avcz11vczc_GNs,4420
|
|
6
|
+
datalab_sdk/mimetypes.py,sha256=a4ULJmHxoR_RMsD0j51hx0t2Q7In_PzNm7UlWX07eEE,932
|
|
7
|
+
datalab_sdk/models.py,sha256=j6UruxDaGp1gfcc7YHMNr7ZUg6ez0Jx-8gzJ18efNkQ,43374
|
|
8
|
+
datalab_sdk/settings.py,sha256=B_B42QQADAz6ePXQTOqENZllcppFR4JpwonEk2vlzhw,693
|
|
9
|
+
datalab_python_sdk-0.13.0.dist-info/METADATA,sha256=kEfjCus4oyKDL-HNY8R30cRr_esw_0AyiMWzvBsZDSw,11417
|
|
10
|
+
datalab_python_sdk-0.13.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
11
|
+
datalab_python_sdk-0.13.0.dist-info/entry_points.txt,sha256=rqWxO21UV57JxxyeF20iBTxHpzBYOA69hCHIqwdn-Ok,48
|
|
12
|
+
datalab_python_sdk-0.13.0.dist-info/licenses/LICENSE,sha256=XGjT-Uz6t7UZzr3JCZfilsGcZEgMm_1aONcRunohxWg,1064
|
|
13
|
+
datalab_python_sdk-0.13.0.dist-info/RECORD,,
|
datalab_sdk/__init__.py
CHANGED
|
@@ -15,6 +15,8 @@ from .exceptions import (
|
|
|
15
15
|
DatalabRateLimitError,
|
|
16
16
|
DatalabServerError,
|
|
17
17
|
DatalabTimeoutError,
|
|
18
|
+
DatalabTransferTimeoutError,
|
|
19
|
+
DatalabPollingTimeoutError,
|
|
18
20
|
DatalabFileError,
|
|
19
21
|
DatalabValidationError,
|
|
20
22
|
)
|
|
@@ -86,6 +88,8 @@ __all__ = [
|
|
|
86
88
|
"DatalabRateLimitError",
|
|
87
89
|
"DatalabServerError",
|
|
88
90
|
"DatalabTimeoutError",
|
|
91
|
+
"DatalabTransferTimeoutError",
|
|
92
|
+
"DatalabPollingTimeoutError",
|
|
89
93
|
"DatalabFileError",
|
|
90
94
|
"DatalabValidationError",
|
|
91
95
|
"ConversionResult",
|
datalab_sdk/client.py
CHANGED
|
@@ -3,6 +3,7 @@ Datalab API client - async core with sync wrapper
|
|
|
3
3
|
"""
|
|
4
4
|
|
|
5
5
|
import asyncio
|
|
6
|
+
import contextlib
|
|
6
7
|
import dataclasses
|
|
7
8
|
import decimal
|
|
8
9
|
import inspect
|
|
@@ -12,6 +13,7 @@ import os
|
|
|
12
13
|
import shutil
|
|
13
14
|
import tempfile
|
|
14
15
|
import warnings
|
|
16
|
+
from urllib.parse import urlsplit
|
|
15
17
|
|
|
16
18
|
import aiohttp
|
|
17
19
|
import ijson
|
|
@@ -28,7 +30,10 @@ from typing import Union, Optional, Dict, Any, Callable, Awaitable
|
|
|
28
30
|
|
|
29
31
|
from datalab_sdk.exceptions import (
|
|
30
32
|
DatalabAPIError,
|
|
33
|
+
DatalabError,
|
|
34
|
+
DatalabPollingTimeoutError,
|
|
31
35
|
DatalabTimeoutError,
|
|
36
|
+
DatalabTransferTimeoutError,
|
|
32
37
|
DatalabFileError,
|
|
33
38
|
DatalabValidationError,
|
|
34
39
|
api_error_for_status,
|
|
@@ -161,18 +166,14 @@ def _is_retryable_poll_error(e: BaseException) -> bool:
|
|
|
161
166
|
|
|
162
167
|
|
|
163
168
|
# Polls are GETs, so a transient failure can be retried without submitting
|
|
164
|
-
# the request again.
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
timeout limits the whole request, so a result too large to transfer
|
|
168
|
-
within it would time out again on every attempt."""
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
# the request again. A transfer timeout is not retried: the timeout limits
|
|
170
|
+
# the whole request, so a result too large to transfer within it would time
|
|
171
|
+
# out again on every attempt.
|
|
171
172
|
_poll_retry = retry(
|
|
172
173
|
retry=(
|
|
173
174
|
(
|
|
174
175
|
retry_if_exception_type(DatalabTimeoutError)
|
|
175
|
-
& retry_if_not_exception_type(
|
|
176
|
+
& retry_if_not_exception_type(DatalabTransferTimeoutError)
|
|
176
177
|
)
|
|
177
178
|
| retry_if_exception(_is_retryable_poll_error)
|
|
178
179
|
),
|
|
@@ -181,6 +182,21 @@ _poll_retry = retry(
|
|
|
181
182
|
reraise=True,
|
|
182
183
|
)
|
|
183
184
|
|
|
185
|
+
|
|
186
|
+
@contextlib.contextmanager
|
|
187
|
+
def _request_handle(request_id: Optional[str], check_url: Optional[str] = None):
|
|
188
|
+
"""Attach an accepted request to a DatalabError raised inside the block,
|
|
189
|
+
so the caller can resume it instead of submitting again."""
|
|
190
|
+
try:
|
|
191
|
+
yield
|
|
192
|
+
except DatalabError as e:
|
|
193
|
+
if e.request_id is None:
|
|
194
|
+
e.request_id = request_id
|
|
195
|
+
if e.check_url is None:
|
|
196
|
+
e.check_url = check_url
|
|
197
|
+
raise
|
|
198
|
+
|
|
199
|
+
|
|
184
200
|
# Top-level fields of a poll response that a streamed call returns on
|
|
185
201
|
# FileResult. A signed result_url download does not contain them.
|
|
186
202
|
_FILE_RESULT_FIELDS = (
|
|
@@ -331,6 +347,43 @@ class _StreamedPollFields:
|
|
|
331
347
|
self.progress_percent = value
|
|
332
348
|
|
|
333
349
|
|
|
350
|
+
def _save_to(result: Any, save_output: Union[str, Path]) -> None:
|
|
351
|
+
output_path = Path(save_output)
|
|
352
|
+
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
353
|
+
result.save_output(output_path)
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
def _save_successful(result: Any, save_output: Optional[Union[str, Path]]) -> None:
|
|
357
|
+
if save_output and result.success:
|
|
358
|
+
_save_to(result, save_output)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def _save_successful_document(
|
|
362
|
+
result: Any, save_output: Optional[Union[str, Path]]
|
|
363
|
+
) -> None:
|
|
364
|
+
if save_output and result.success and result.output_base64:
|
|
365
|
+
_save_to(result, save_output)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _save_redacted_document(
|
|
369
|
+
result: RedactResult, save_output: Optional[Union[str, Path]]
|
|
370
|
+
) -> None:
|
|
371
|
+
# A redacted PDF is saved even when verification failed; see redact().
|
|
372
|
+
if save_output and result.output_base64:
|
|
373
|
+
_save_to(result, save_output)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
@dataclasses.dataclass(frozen=True)
|
|
377
|
+
class _ResultPolicy:
|
|
378
|
+
"""How one endpoint's poll response becomes its result. The processing
|
|
379
|
+
methods and get_result() share it through AsyncDatalabClient._finish()."""
|
|
380
|
+
|
|
381
|
+
build: Callable[..., Any]
|
|
382
|
+
save: Callable[[Any, Optional[Union[str, Path]]], None]
|
|
383
|
+
result_transform: Optional[Callable[[Dict[str, Any]], Dict[str, Any]]] = None
|
|
384
|
+
streamable: bool = True
|
|
385
|
+
|
|
386
|
+
|
|
334
387
|
class AsyncDatalabClient:
|
|
335
388
|
"""Asynchronous client for Datalab API"""
|
|
336
389
|
|
|
@@ -462,6 +515,7 @@ class AsyncDatalabClient:
|
|
|
462
515
|
fast_until = loop.time() + FAST_POLL_SECONDS if 0 < poll_interval <= 1 else 0.0
|
|
463
516
|
budget = max_polls * poll_interval
|
|
464
517
|
slept = 0.0
|
|
518
|
+
status = percent = None
|
|
465
519
|
for i in range(max_polls if poll_interval <= 0 else _MAX_POLL_REQUESTS):
|
|
466
520
|
if stream_response_to:
|
|
467
521
|
result = await self._poll_get_streaming(full_url, stream_response_to)
|
|
@@ -499,8 +553,11 @@ class AsyncDatalabClient:
|
|
|
499
553
|
await asyncio.sleep(delay)
|
|
500
554
|
slept += delay
|
|
501
555
|
|
|
502
|
-
raise
|
|
503
|
-
f"Polling timed out after {max_polls * poll_interval} seconds"
|
|
556
|
+
raise DatalabPollingTimeoutError(
|
|
557
|
+
f"Polling timed out after {max_polls * poll_interval} seconds. "
|
|
558
|
+
"The request is still running; call get_result() to keep waiting.",
|
|
559
|
+
status=status,
|
|
560
|
+
progress_percent=percent if type(percent) is int else None,
|
|
504
561
|
)
|
|
505
562
|
|
|
506
563
|
@_poll_retry
|
|
@@ -530,7 +587,7 @@ class AsyncDatalabClient:
|
|
|
530
587
|
)
|
|
531
588
|
return await resp.json(content_type=None)
|
|
532
589
|
except asyncio.TimeoutError:
|
|
533
|
-
raise
|
|
590
|
+
raise DatalabTransferTimeoutError(
|
|
534
591
|
f"Result download timed out after {self.timeout} seconds"
|
|
535
592
|
)
|
|
536
593
|
except aiohttp.ClientError as e:
|
|
@@ -540,7 +597,9 @@ class AsyncDatalabClient:
|
|
|
540
597
|
"""Stream a signed result_url to a file on disk.
|
|
541
598
|
|
|
542
599
|
The bytes are copied without parsing, so the result is never held in
|
|
543
|
-
memory.
|
|
600
|
+
memory. It runs inside _poll_get_streaming, so a 5xx or connection
|
|
601
|
+
error retries the poll and the download on purpose: each retry gets
|
|
602
|
+
a fresh signed URL.
|
|
544
603
|
"""
|
|
545
604
|
try:
|
|
546
605
|
async with self._signed_url_session() as session:
|
|
@@ -556,7 +615,10 @@ class AsyncDatalabClient:
|
|
|
556
615
|
async for chunk in resp.content.iter_any():
|
|
557
616
|
f.write(chunk)
|
|
558
617
|
except asyncio.TimeoutError:
|
|
559
|
-
|
|
618
|
+
# Not retried: the request has finished, and the same download
|
|
619
|
+
# would time out again. Retrying it here also retried the poll,
|
|
620
|
+
# because _poll_get_streaming retries every other timeout.
|
|
621
|
+
raise DatalabTransferTimeoutError(
|
|
560
622
|
f"Result download timed out after {self.timeout} seconds"
|
|
561
623
|
)
|
|
562
624
|
except aiohttp.ClientError as e:
|
|
@@ -625,8 +687,16 @@ class AsyncDatalabClient:
|
|
|
625
687
|
parser.close()
|
|
626
688
|
poll.feed(events)
|
|
627
689
|
except asyncio.TimeoutError:
|
|
628
|
-
|
|
629
|
-
|
|
690
|
+
# The API sends status near the start of the body. A finished
|
|
691
|
+
# result that times out would time out again, so it is not
|
|
692
|
+
# retried. Any other body is small, so its timeout is retried
|
|
693
|
+
# like any poll timeout.
|
|
694
|
+
if poll.status == "complete":
|
|
695
|
+
raise DatalabTransferTimeoutError(
|
|
696
|
+
f"Result transfer timed out after {self.timeout} seconds"
|
|
697
|
+
)
|
|
698
|
+
raise DatalabTimeoutError(
|
|
699
|
+
f"Request timed out after {self.timeout} seconds"
|
|
630
700
|
)
|
|
631
701
|
except aiohttp.ClientError as e:
|
|
632
702
|
# The body can fail after the headers arrive.
|
|
@@ -734,8 +804,9 @@ class AsyncDatalabClient:
|
|
|
734
804
|
|
|
735
805
|
return form_data
|
|
736
806
|
|
|
807
|
+
@staticmethod
|
|
737
808
|
def _build_conversion_result(
|
|
738
|
-
|
|
809
|
+
result_data: Dict[str, Any], default_format: str = "markdown"
|
|
739
810
|
) -> ConversionResult:
|
|
740
811
|
"""Build a ConversionResult from API response data"""
|
|
741
812
|
return ConversionResult(
|
|
@@ -769,6 +840,122 @@ class AsyncDatalabClient:
|
|
|
769
840
|
progress_percent=result_data.get("progress_percent"),
|
|
770
841
|
)
|
|
771
842
|
|
|
843
|
+
@staticmethod
|
|
844
|
+
def _build_agent_result(result_data: Dict[str, Any]) -> AgentResult:
|
|
845
|
+
return AgentResult(
|
|
846
|
+
status=result_data.get("status", "complete"),
|
|
847
|
+
success=result_data.get("success"),
|
|
848
|
+
error=result_data.get("error"),
|
|
849
|
+
output_shape=result_data.get("output_shape"),
|
|
850
|
+
finished=result_data.get("finished"),
|
|
851
|
+
turns=result_data.get("turns"),
|
|
852
|
+
markdown=result_data.get("markdown"),
|
|
853
|
+
html=result_data.get("html"),
|
|
854
|
+
json=result_data.get("json"),
|
|
855
|
+
chunks=result_data.get("chunks"),
|
|
856
|
+
extraction_schema_json=result_data.get("extraction_schema_json"),
|
|
857
|
+
segments=result_data.get("segments"),
|
|
858
|
+
metadata=result_data.get("metadata"),
|
|
859
|
+
verdict=result_data.get("verdict"),
|
|
860
|
+
jats_xml=result_data.get("jats_xml"),
|
|
861
|
+
accessible_pdf=result_data.get("accessible_pdf"),
|
|
862
|
+
images=result_data.get("images"),
|
|
863
|
+
verifier_metrics=result_data.get("verifier_metrics"),
|
|
864
|
+
page_count=result_data.get("page_count"),
|
|
865
|
+
cost_breakdown=result_data.get("cost_breakdown"),
|
|
866
|
+
parse_quality_score=result_data.get("parse_quality_score"),
|
|
867
|
+
runtime=result_data.get("runtime"),
|
|
868
|
+
versions=result_data.get("versions"),
|
|
869
|
+
request_id=result_data.get("request_id"),
|
|
870
|
+
result_url=result_data.get("result_url"),
|
|
871
|
+
expires_in=result_data.get("expires_in"),
|
|
872
|
+
progress_percent=result_data.get("progress_percent"),
|
|
873
|
+
)
|
|
874
|
+
|
|
875
|
+
@staticmethod
|
|
876
|
+
def _build_create_document_result(
|
|
877
|
+
result_data: Dict[str, Any],
|
|
878
|
+
) -> CreateDocumentResult:
|
|
879
|
+
return CreateDocumentResult(
|
|
880
|
+
status=result_data.get("status", "complete"),
|
|
881
|
+
success=result_data.get("success"),
|
|
882
|
+
error=result_data.get("error"),
|
|
883
|
+
output_format=result_data.get("output_format"),
|
|
884
|
+
output_base64=result_data.get("output_base64"),
|
|
885
|
+
runtime=result_data.get("runtime"),
|
|
886
|
+
page_count=result_data.get("page_count"),
|
|
887
|
+
cost_breakdown=result_data.get("cost_breakdown"),
|
|
888
|
+
versions=result_data.get("versions"),
|
|
889
|
+
request_id=result_data.get("request_id"),
|
|
890
|
+
result_url=result_data.get("result_url"),
|
|
891
|
+
expires_in=result_data.get("expires_in"),
|
|
892
|
+
progress_percent=result_data.get("progress_percent"),
|
|
893
|
+
)
|
|
894
|
+
|
|
895
|
+
@staticmethod
|
|
896
|
+
def _build_ocr_result(result_data: Dict[str, Any]) -> OCRResult:
|
|
897
|
+
return OCRResult(
|
|
898
|
+
success=result_data.get("success", False),
|
|
899
|
+
pages=result_data.get("pages", []),
|
|
900
|
+
error=result_data.get("error"),
|
|
901
|
+
page_count=result_data.get("page_count"),
|
|
902
|
+
status=result_data.get("status", "complete"),
|
|
903
|
+
versions=result_data.get("versions"),
|
|
904
|
+
cost_breakdown=result_data.get("cost_breakdown"),
|
|
905
|
+
request_id=result_data.get("request_id"),
|
|
906
|
+
progress_percent=result_data.get("progress_percent"),
|
|
907
|
+
)
|
|
908
|
+
|
|
909
|
+
@staticmethod
|
|
910
|
+
def _build_form_filling_result(result_data: Dict[str, Any]) -> FormFillingResult:
|
|
911
|
+
return FormFillingResult(
|
|
912
|
+
status=result_data.get("status", "complete"),
|
|
913
|
+
success=result_data.get("success"),
|
|
914
|
+
error=result_data.get("error"),
|
|
915
|
+
error_in=result_data.get("error_in"),
|
|
916
|
+
output_format=result_data.get("output_format"),
|
|
917
|
+
output_base64=result_data.get("output_base64"),
|
|
918
|
+
fields_filled=result_data.get("fields_filled"),
|
|
919
|
+
fields_not_found=result_data.get("fields_not_found"),
|
|
920
|
+
runtime=result_data.get("runtime"),
|
|
921
|
+
page_count=result_data.get("page_count"),
|
|
922
|
+
cost_breakdown=result_data.get("cost_breakdown"),
|
|
923
|
+
versions=result_data.get("versions"),
|
|
924
|
+
request_id=result_data.get("request_id"),
|
|
925
|
+
result_url=result_data.get("result_url"),
|
|
926
|
+
expires_in=result_data.get("expires_in"),
|
|
927
|
+
progress_percent=result_data.get("progress_percent"),
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
@staticmethod
|
|
931
|
+
def _build_form_fields_result(result_data: Dict[str, Any]) -> FormFieldsResult:
|
|
932
|
+
return FormFieldsResult(
|
|
933
|
+
status=result_data.get("status", "complete"),
|
|
934
|
+
success=result_data.get("success"),
|
|
935
|
+
error=result_data.get("error"),
|
|
936
|
+
pages=result_data.get("pages"),
|
|
937
|
+
json=result_data.get("json"),
|
|
938
|
+
text_error=result_data.get("text_error"),
|
|
939
|
+
page_count=result_data.get("page_count"),
|
|
940
|
+
cost_breakdown=result_data.get("cost_breakdown"),
|
|
941
|
+
runtime=result_data.get("runtime"),
|
|
942
|
+
versions=result_data.get("versions"),
|
|
943
|
+
request_id=result_data.get("request_id"),
|
|
944
|
+
result_url=result_data.get("result_url"),
|
|
945
|
+
expires_in=result_data.get("expires_in"),
|
|
946
|
+
progress_percent=result_data.get("progress_percent"),
|
|
947
|
+
)
|
|
948
|
+
|
|
949
|
+
@staticmethod
|
|
950
|
+
def _build_redact_result(result_data: Dict[str, Any]) -> RedactResult:
|
|
951
|
+
return RedactResult(
|
|
952
|
+
**{
|
|
953
|
+
key: value
|
|
954
|
+
for key, value in result_data.items()
|
|
955
|
+
if key in RedactResult.__dataclass_fields__
|
|
956
|
+
}
|
|
957
|
+
)
|
|
958
|
+
|
|
772
959
|
async def _submit_and_poll(
|
|
773
960
|
self,
|
|
774
961
|
endpoint: str,
|
|
@@ -792,27 +979,66 @@ class AsyncDatalabClient:
|
|
|
792
979
|
f"Request failed: {initial_data.get('error', 'Unknown error')}"
|
|
793
980
|
)
|
|
794
981
|
|
|
795
|
-
result = await self.
|
|
982
|
+
result = await self._poll_request(
|
|
796
983
|
initial_data["request_check_url"],
|
|
984
|
+
initial_data.get("request_id"),
|
|
797
985
|
max_polls=max_polls,
|
|
798
986
|
poll_interval=poll_interval,
|
|
799
987
|
on_progress=on_progress,
|
|
800
988
|
stream_response_to=stream_response_to,
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
)
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
989
|
+
result_transform=result_transform,
|
|
990
|
+
)
|
|
991
|
+
# Only the submission response says whether a checkpoint was reused.
|
|
992
|
+
if (
|
|
993
|
+
isinstance(result, dict)
|
|
994
|
+
and result.get("checkpoint_reused") is None
|
|
995
|
+
and initial_data.get("checkpoint_reused") is not None
|
|
996
|
+
):
|
|
997
|
+
result = {**result, "checkpoint_reused": initial_data["checkpoint_reused"]}
|
|
998
|
+
return result
|
|
999
|
+
|
|
1000
|
+
async def _poll_request(
|
|
1001
|
+
self,
|
|
1002
|
+
check_url: str,
|
|
1003
|
+
request_id: Optional[str],
|
|
1004
|
+
result_transform: Optional[Callable[[Dict[str, Any]], Dict[str, Any]]] = None,
|
|
1005
|
+
**poll_kwargs,
|
|
1006
|
+
) -> Union[Dict[str, Any], FileResult]:
|
|
1007
|
+
"""Poll a request the API accepted.
|
|
1008
|
+
|
|
1009
|
+
A DatalabError gets the request_id and check_url, so the caller can
|
|
1010
|
+
resume with get_result(). The result gets request_id when the poll
|
|
1011
|
+
response does not send it.
|
|
1012
|
+
"""
|
|
1013
|
+
if result_transform is not None:
|
|
1014
|
+
poll_kwargs["result_transform"] = result_transform
|
|
1015
|
+
with _request_handle(request_id, check_url):
|
|
1016
|
+
result = await self._poll_result(check_url, **poll_kwargs)
|
|
1017
|
+
if request_id is None:
|
|
1018
|
+
return result
|
|
1019
|
+
if isinstance(result, FileResult):
|
|
1020
|
+
if result.request_id is None:
|
|
1021
|
+
result = dataclasses.replace(result, request_id=request_id)
|
|
1022
|
+
elif result.get("request_id") is None:
|
|
1023
|
+
result = {**result, "request_id": request_id}
|
|
1024
|
+
return result
|
|
1025
|
+
|
|
1026
|
+
def _finish(
|
|
1027
|
+
self,
|
|
1028
|
+
policy: _ResultPolicy,
|
|
1029
|
+
result_data: Union[Dict[str, Any], FileResult],
|
|
1030
|
+
save_output: Optional[Union[str, Path]],
|
|
1031
|
+
**build_kwargs,
|
|
1032
|
+
) -> Any:
|
|
1033
|
+
"""Build an endpoint's result from its poll response and save it.
|
|
1034
|
+
|
|
1035
|
+
The processing methods and get_result() share it, so a resumed
|
|
1036
|
+
request returns the same result as the method that submitted it.
|
|
1037
|
+
"""
|
|
1038
|
+
if isinstance(result_data, FileResult):
|
|
1039
|
+
return result_data
|
|
1040
|
+
result = policy.build(result_data, **build_kwargs)
|
|
1041
|
+
policy.save(result, save_output)
|
|
816
1042
|
return result
|
|
817
1043
|
|
|
818
1044
|
@staticmethod
|
|
@@ -832,13 +1058,6 @@ class AsyncDatalabClient:
|
|
|
832
1058
|
raise DatalabValidationError(f"Directory does not exist: {resolved.parent}")
|
|
833
1059
|
return resolved
|
|
834
1060
|
|
|
835
|
-
@staticmethod
|
|
836
|
-
def _save_if_requested(result, save_output, has_output: bool = True) -> None:
|
|
837
|
-
if save_output and result.success and has_output:
|
|
838
|
-
output_path = Path(save_output)
|
|
839
|
-
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
840
|
-
result.save_output(output_path)
|
|
841
|
-
|
|
842
1061
|
@staticmethod
|
|
843
1062
|
def _check_file_or_checkpoint(file_path, file_url, checkpoint_id) -> None:
|
|
844
1063
|
has_file = file_path is not None or file_url is not None
|
|
@@ -896,12 +1115,9 @@ class AsyncDatalabClient:
|
|
|
896
1115
|
stream_response_to=resolved_stream_response_to,
|
|
897
1116
|
)
|
|
898
1117
|
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
result = self._build_conversion_result(result_data, options.output_format)
|
|
903
|
-
self._save_if_requested(result, save_output)
|
|
904
|
-
return result
|
|
1118
|
+
return self._finish(
|
|
1119
|
+
_CONVERSION, result_data, save_output, default_format=options.output_format
|
|
1120
|
+
)
|
|
905
1121
|
|
|
906
1122
|
async def extract(
|
|
907
1123
|
self,
|
|
@@ -971,12 +1187,9 @@ class AsyncDatalabClient:
|
|
|
971
1187
|
stream_response_to=resolved_stream_response_to,
|
|
972
1188
|
)
|
|
973
1189
|
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
result = self._build_conversion_result(result_data, options.output_format)
|
|
978
|
-
self._save_if_requested(result, save_output)
|
|
979
|
-
return result
|
|
1190
|
+
return self._finish(
|
|
1191
|
+
_CONVERSION, result_data, save_output, default_format=options.output_format
|
|
1192
|
+
)
|
|
980
1193
|
|
|
981
1194
|
async def segment(
|
|
982
1195
|
self,
|
|
@@ -1030,12 +1243,7 @@ class AsyncDatalabClient:
|
|
|
1030
1243
|
stream_response_to=resolved_stream_response_to,
|
|
1031
1244
|
)
|
|
1032
1245
|
|
|
1033
|
-
|
|
1034
|
-
return result_data
|
|
1035
|
-
|
|
1036
|
-
result = self._build_conversion_result(result_data, "markdown")
|
|
1037
|
-
self._save_if_requested(result, save_output)
|
|
1038
|
-
return result
|
|
1246
|
+
return self._finish(_CONVERSION, result_data, save_output)
|
|
1039
1247
|
|
|
1040
1248
|
async def run_agent(
|
|
1041
1249
|
self,
|
|
@@ -1083,40 +1291,7 @@ class AsyncDatalabClient:
|
|
|
1083
1291
|
stream_response_to=resolved_stream_response_to,
|
|
1084
1292
|
)
|
|
1085
1293
|
|
|
1086
|
-
|
|
1087
|
-
return result_data
|
|
1088
|
-
|
|
1089
|
-
result = AgentResult(
|
|
1090
|
-
status=result_data.get("status", "complete"),
|
|
1091
|
-
success=result_data.get("success"),
|
|
1092
|
-
error=result_data.get("error"),
|
|
1093
|
-
output_shape=result_data.get("output_shape"),
|
|
1094
|
-
finished=result_data.get("finished"),
|
|
1095
|
-
turns=result_data.get("turns"),
|
|
1096
|
-
markdown=result_data.get("markdown"),
|
|
1097
|
-
html=result_data.get("html"),
|
|
1098
|
-
json=result_data.get("json"),
|
|
1099
|
-
chunks=result_data.get("chunks"),
|
|
1100
|
-
extraction_schema_json=result_data.get("extraction_schema_json"),
|
|
1101
|
-
segments=result_data.get("segments"),
|
|
1102
|
-
metadata=result_data.get("metadata"),
|
|
1103
|
-
verdict=result_data.get("verdict"),
|
|
1104
|
-
jats_xml=result_data.get("jats_xml"),
|
|
1105
|
-
accessible_pdf=result_data.get("accessible_pdf"),
|
|
1106
|
-
images=result_data.get("images"),
|
|
1107
|
-
verifier_metrics=result_data.get("verifier_metrics"),
|
|
1108
|
-
page_count=result_data.get("page_count"),
|
|
1109
|
-
cost_breakdown=result_data.get("cost_breakdown"),
|
|
1110
|
-
parse_quality_score=result_data.get("parse_quality_score"),
|
|
1111
|
-
runtime=result_data.get("runtime"),
|
|
1112
|
-
versions=result_data.get("versions"),
|
|
1113
|
-
request_id=result_data.get("request_id"),
|
|
1114
|
-
result_url=result_data.get("result_url"),
|
|
1115
|
-
expires_in=result_data.get("expires_in"),
|
|
1116
|
-
progress_percent=result_data.get("progress_percent"),
|
|
1117
|
-
)
|
|
1118
|
-
self._save_if_requested(result, save_output)
|
|
1119
|
-
return result
|
|
1294
|
+
return self._finish(_AGENT, result_data, save_output)
|
|
1120
1295
|
|
|
1121
1296
|
async def run_custom_processor(
|
|
1122
1297
|
self,
|
|
@@ -1191,12 +1366,9 @@ class AsyncDatalabClient:
|
|
|
1191
1366
|
stream_response_to=resolved_stream_response_to,
|
|
1192
1367
|
)
|
|
1193
1368
|
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
result = self._build_conversion_result(result_data, options.output_format)
|
|
1198
|
-
self._save_if_requested(result, save_output)
|
|
1199
|
-
return result
|
|
1369
|
+
return self._finish(
|
|
1370
|
+
_CONVERSION, result_data, save_output, default_format=options.output_format
|
|
1371
|
+
)
|
|
1200
1372
|
|
|
1201
1373
|
async def run_custom_pipeline(
|
|
1202
1374
|
self,
|
|
@@ -1273,12 +1445,9 @@ class AsyncDatalabClient:
|
|
|
1273
1445
|
stream_response_to=resolved_stream_response_to,
|
|
1274
1446
|
)
|
|
1275
1447
|
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
result = self._build_conversion_result(result_data, options.output_format)
|
|
1280
|
-
self._save_if_requested(result, save_output)
|
|
1281
|
-
return result
|
|
1448
|
+
return self._finish(
|
|
1449
|
+
_CONVERSION, result_data, save_output, default_format=options.output_format
|
|
1450
|
+
)
|
|
1282
1451
|
|
|
1283
1452
|
async def create_document(
|
|
1284
1453
|
self,
|
|
@@ -1333,26 +1502,7 @@ class AsyncDatalabClient:
|
|
|
1333
1502
|
stream_response_to=resolved_stream_response_to,
|
|
1334
1503
|
)
|
|
1335
1504
|
|
|
1336
|
-
|
|
1337
|
-
return result_data
|
|
1338
|
-
|
|
1339
|
-
result = CreateDocumentResult(
|
|
1340
|
-
status=result_data.get("status", "complete"),
|
|
1341
|
-
success=result_data.get("success"),
|
|
1342
|
-
error=result_data.get("error"),
|
|
1343
|
-
output_format=result_data.get("output_format"),
|
|
1344
|
-
output_base64=result_data.get("output_base64"),
|
|
1345
|
-
runtime=result_data.get("runtime"),
|
|
1346
|
-
page_count=result_data.get("page_count"),
|
|
1347
|
-
cost_breakdown=result_data.get("cost_breakdown"),
|
|
1348
|
-
versions=result_data.get("versions"),
|
|
1349
|
-
request_id=result_data.get("request_id"),
|
|
1350
|
-
result_url=result_data.get("result_url"),
|
|
1351
|
-
expires_in=result_data.get("expires_in"),
|
|
1352
|
-
progress_percent=result_data.get("progress_percent"),
|
|
1353
|
-
)
|
|
1354
|
-
self._save_if_requested(result, save_output, bool(result.output_base64))
|
|
1355
|
-
return result
|
|
1505
|
+
return self._finish(_CREATE_DOCUMENT, result_data, save_output)
|
|
1356
1506
|
|
|
1357
1507
|
async def ocr(
|
|
1358
1508
|
self,
|
|
@@ -1391,22 +1541,7 @@ class AsyncDatalabClient:
|
|
|
1391
1541
|
stream_response_to=resolved_stream_response_to,
|
|
1392
1542
|
)
|
|
1393
1543
|
|
|
1394
|
-
|
|
1395
|
-
return result_data
|
|
1396
|
-
|
|
1397
|
-
result = OCRResult(
|
|
1398
|
-
success=result_data.get("success", False),
|
|
1399
|
-
pages=result_data.get("pages", []),
|
|
1400
|
-
error=result_data.get("error"),
|
|
1401
|
-
page_count=result_data.get("page_count"),
|
|
1402
|
-
status=result_data.get("status", "complete"),
|
|
1403
|
-
versions=result_data.get("versions"),
|
|
1404
|
-
cost_breakdown=result_data.get("cost_breakdown"),
|
|
1405
|
-
request_id=result_data.get("request_id"),
|
|
1406
|
-
progress_percent=result_data.get("progress_percent"),
|
|
1407
|
-
)
|
|
1408
|
-
self._save_if_requested(result, save_output)
|
|
1409
|
-
return result
|
|
1544
|
+
return self._finish(_OCR, result_data, save_output)
|
|
1410
1545
|
|
|
1411
1546
|
@staticmethod
|
|
1412
1547
|
def _normalize_redaction_result(data: Dict[str, Any]) -> Dict[str, Any]:
|
|
@@ -1486,21 +1621,10 @@ class AsyncDatalabClient:
|
|
|
1486
1621
|
),
|
|
1487
1622
|
max_polls=max_polls,
|
|
1488
1623
|
poll_interval=poll_interval,
|
|
1489
|
-
result_transform=
|
|
1624
|
+
result_transform=_REDACT.result_transform,
|
|
1490
1625
|
on_progress=on_progress,
|
|
1491
1626
|
)
|
|
1492
|
-
|
|
1493
|
-
**{
|
|
1494
|
-
key: value
|
|
1495
|
-
for key, value in result_data.items()
|
|
1496
|
-
if key in RedactResult.__dataclass_fields__
|
|
1497
|
-
}
|
|
1498
|
-
)
|
|
1499
|
-
if save_output and result.output_base64:
|
|
1500
|
-
output_path = Path(save_output)
|
|
1501
|
-
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
1502
|
-
result.save_output(output_path)
|
|
1503
|
-
return result
|
|
1627
|
+
return self._finish(_REDACT, result_data, save_output)
|
|
1504
1628
|
|
|
1505
1629
|
async def get_redaction_presets(self) -> Dict[str, Any]:
|
|
1506
1630
|
"""Get built-in redaction preset IDs, labels, and specifications."""
|
|
@@ -1548,29 +1672,7 @@ class AsyncDatalabClient:
|
|
|
1548
1672
|
stream_response_to=resolved_stream_response_to,
|
|
1549
1673
|
)
|
|
1550
1674
|
|
|
1551
|
-
|
|
1552
|
-
return result_data
|
|
1553
|
-
|
|
1554
|
-
result = FormFillingResult(
|
|
1555
|
-
status=result_data.get("status", "complete"),
|
|
1556
|
-
success=result_data.get("success"),
|
|
1557
|
-
error=result_data.get("error"),
|
|
1558
|
-
error_in=result_data.get("error_in"),
|
|
1559
|
-
output_format=result_data.get("output_format"),
|
|
1560
|
-
output_base64=result_data.get("output_base64"),
|
|
1561
|
-
fields_filled=result_data.get("fields_filled"),
|
|
1562
|
-
fields_not_found=result_data.get("fields_not_found"),
|
|
1563
|
-
runtime=result_data.get("runtime"),
|
|
1564
|
-
page_count=result_data.get("page_count"),
|
|
1565
|
-
cost_breakdown=result_data.get("cost_breakdown"),
|
|
1566
|
-
versions=result_data.get("versions"),
|
|
1567
|
-
request_id=result_data.get("request_id"),
|
|
1568
|
-
result_url=result_data.get("result_url"),
|
|
1569
|
-
expires_in=result_data.get("expires_in"),
|
|
1570
|
-
progress_percent=result_data.get("progress_percent"),
|
|
1571
|
-
)
|
|
1572
|
-
self._save_if_requested(result, save_output, bool(result.output_base64))
|
|
1573
|
-
return result
|
|
1675
|
+
return self._finish(_FORM_FILLING, result_data, save_output)
|
|
1574
1676
|
|
|
1575
1677
|
async def form_fields(
|
|
1576
1678
|
self,
|
|
@@ -1618,27 +1720,7 @@ class AsyncDatalabClient:
|
|
|
1618
1720
|
stream_response_to=resolved_stream_response_to,
|
|
1619
1721
|
)
|
|
1620
1722
|
|
|
1621
|
-
|
|
1622
|
-
return result_data
|
|
1623
|
-
|
|
1624
|
-
result = FormFieldsResult(
|
|
1625
|
-
status=result_data.get("status", "complete"),
|
|
1626
|
-
success=result_data.get("success"),
|
|
1627
|
-
error=result_data.get("error"),
|
|
1628
|
-
pages=result_data.get("pages"),
|
|
1629
|
-
json=result_data.get("json"),
|
|
1630
|
-
text_error=result_data.get("text_error"),
|
|
1631
|
-
page_count=result_data.get("page_count"),
|
|
1632
|
-
cost_breakdown=result_data.get("cost_breakdown"),
|
|
1633
|
-
runtime=result_data.get("runtime"),
|
|
1634
|
-
versions=result_data.get("versions"),
|
|
1635
|
-
request_id=result_data.get("request_id"),
|
|
1636
|
-
result_url=result_data.get("result_url"),
|
|
1637
|
-
expires_in=result_data.get("expires_in"),
|
|
1638
|
-
progress_percent=result_data.get("progress_percent"),
|
|
1639
|
-
)
|
|
1640
|
-
self._save_if_requested(result, save_output)
|
|
1641
|
-
return result
|
|
1723
|
+
return self._finish(_FORM_FIELDS, result_data, save_output)
|
|
1642
1724
|
|
|
1643
1725
|
async def generate_extraction_schemas(
|
|
1644
1726
|
self,
|
|
@@ -1682,6 +1764,91 @@ class AsyncDatalabClient:
|
|
|
1682
1764
|
request_id=result_data.get("request_id"),
|
|
1683
1765
|
)
|
|
1684
1766
|
|
|
1767
|
+
async def get_result(
|
|
1768
|
+
self,
|
|
1769
|
+
request_id_or_check_url: str,
|
|
1770
|
+
endpoint: Optional[str] = None,
|
|
1771
|
+
save_output: Optional[Union[str, Path]] = None,
|
|
1772
|
+
stream_response_to: Optional[Union[str, Path]] = None,
|
|
1773
|
+
max_polls: int = 300,
|
|
1774
|
+
poll_interval: int = 1,
|
|
1775
|
+
on_progress: Optional[ProgressCallback] = None,
|
|
1776
|
+
) -> Union[
|
|
1777
|
+
ConversionResult,
|
|
1778
|
+
AgentResult,
|
|
1779
|
+
CreateDocumentResult,
|
|
1780
|
+
FormFillingResult,
|
|
1781
|
+
FormFieldsResult,
|
|
1782
|
+
OCRResult,
|
|
1783
|
+
RedactResult,
|
|
1784
|
+
FileResult,
|
|
1785
|
+
]:
|
|
1786
|
+
"""
|
|
1787
|
+
Wait for the result of a request that was already submitted
|
|
1788
|
+
|
|
1789
|
+
Use it after a DatalabTimeoutError or another error that has a
|
|
1790
|
+
request_id: it polls the same request again, so the document is not
|
|
1791
|
+
submitted or charged a second time. It returns the same result type
|
|
1792
|
+
as the method that submitted the request. A check URL does not carry
|
|
1793
|
+
the output_format you asked for, so if the response omits it, a
|
|
1794
|
+
conversion result has output_format "markdown".
|
|
1795
|
+
|
|
1796
|
+
Args:
|
|
1797
|
+
request_id_or_check_url: The error's check_url, or a request ID.
|
|
1798
|
+
A check URL names its endpoint. Only its path is used; the
|
|
1799
|
+
request goes to this client's base_url.
|
|
1800
|
+
endpoint: With a request ID, the endpoint that received the
|
|
1801
|
+
request, for example "convert", "extract" or "agent".
|
|
1802
|
+
save_output: Optional path to save output files
|
|
1803
|
+
stream_response_to: Optional path to stream raw JSON response to disk
|
|
1804
|
+
max_polls: Maximum number of polling attempts
|
|
1805
|
+
poll_interval: Seconds between polling attempts
|
|
1806
|
+
on_progress: Optional callback for increasing estimated percentages (0-100).
|
|
1807
|
+
"""
|
|
1808
|
+
value = request_id_or_check_url.strip()
|
|
1809
|
+
if "/" in value:
|
|
1810
|
+
if endpoint is not None:
|
|
1811
|
+
raise DatalabValidationError(
|
|
1812
|
+
"Pass endpoint only with a request ID; a check URL names its endpoint."
|
|
1813
|
+
)
|
|
1814
|
+
path = urlsplit(value).path.rstrip("/")
|
|
1815
|
+
prefix, _, request_id = path.rpartition("/")
|
|
1816
|
+
_, found, route = prefix.partition("/api/v1/")
|
|
1817
|
+
if not found or not request_id:
|
|
1818
|
+
raise DatalabValidationError(f"Not a request check URL: {value}")
|
|
1819
|
+
else:
|
|
1820
|
+
if endpoint is None:
|
|
1821
|
+
raise DatalabValidationError(
|
|
1822
|
+
"Pass endpoint with a request ID, or pass the check URL instead."
|
|
1823
|
+
)
|
|
1824
|
+
request_id, route = value, endpoint.strip("/")
|
|
1825
|
+
policy = _RESULT_ROUTES.get(route)
|
|
1826
|
+
if policy is None or not request_id:
|
|
1827
|
+
raise DatalabValidationError(
|
|
1828
|
+
f"get_result() does not support the {route!r} endpoint. "
|
|
1829
|
+
f"Supported: {', '.join(sorted(_RESULT_ROUTES))}."
|
|
1830
|
+
)
|
|
1831
|
+
if not policy.streamable and stream_response_to:
|
|
1832
|
+
raise DatalabValidationError(
|
|
1833
|
+
f"{route} results cannot use stream_response_to."
|
|
1834
|
+
)
|
|
1835
|
+
resolved_stream_response_to = self._resolve_stream_path(
|
|
1836
|
+
save_output, stream_response_to
|
|
1837
|
+
)
|
|
1838
|
+
|
|
1839
|
+
result_data = await self._poll_request(
|
|
1840
|
+
f"{self.base_url}/api/v1/{route}/{request_id}",
|
|
1841
|
+
request_id,
|
|
1842
|
+
max_polls=max_polls,
|
|
1843
|
+
poll_interval=poll_interval,
|
|
1844
|
+
stream_response_to=resolved_stream_response_to,
|
|
1845
|
+
on_progress=on_progress,
|
|
1846
|
+
result_transform=policy.result_transform,
|
|
1847
|
+
)
|
|
1848
|
+
# A check URL does not carry the output_format the caller asked for,
|
|
1849
|
+
# so a conversion result whose response omits it says "markdown".
|
|
1850
|
+
return self._finish(policy, result_data, save_output)
|
|
1851
|
+
|
|
1685
1852
|
async def delete_result(self, request_id: str) -> Dict[str, Any]:
|
|
1686
1853
|
"""
|
|
1687
1854
|
Delete the stored result of a finished request
|
|
@@ -2740,12 +2907,15 @@ class AsyncDatalabClient:
|
|
|
2740
2907
|
async def forward_progress(percent: int) -> None:
|
|
2741
2908
|
await progress.report(percent, complete=percent == 100)
|
|
2742
2909
|
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2910
|
+
# The run was accepted and is charged. An error carries the
|
|
2911
|
+
# execution ID as request_id, for get_pipeline_execution().
|
|
2912
|
+
with _request_handle(execution.execution_id):
|
|
2913
|
+
return await self.get_pipeline_execution(
|
|
2914
|
+
execution.execution_id,
|
|
2915
|
+
max_polls=max_polls - 1,
|
|
2916
|
+
poll_interval=poll_interval,
|
|
2917
|
+
on_progress=forward_progress,
|
|
2918
|
+
)
|
|
2749
2919
|
return execution
|
|
2750
2920
|
|
|
2751
2921
|
async def get_pipeline_execution(
|
|
@@ -3278,6 +3448,50 @@ class AsyncDatalabClient:
|
|
|
3278
3448
|
)
|
|
3279
3449
|
|
|
3280
3450
|
|
|
3451
|
+
_CONVERSION = _ResultPolicy(
|
|
3452
|
+
AsyncDatalabClient._build_conversion_result, _save_successful
|
|
3453
|
+
)
|
|
3454
|
+
_AGENT = _ResultPolicy(AsyncDatalabClient._build_agent_result, _save_successful)
|
|
3455
|
+
_CREATE_DOCUMENT = _ResultPolicy(
|
|
3456
|
+
AsyncDatalabClient._build_create_document_result, _save_successful_document
|
|
3457
|
+
)
|
|
3458
|
+
_FORM_FILLING = _ResultPolicy(
|
|
3459
|
+
AsyncDatalabClient._build_form_filling_result, _save_successful_document
|
|
3460
|
+
)
|
|
3461
|
+
_FORM_FIELDS = _ResultPolicy(
|
|
3462
|
+
AsyncDatalabClient._build_form_fields_result, _save_successful
|
|
3463
|
+
)
|
|
3464
|
+
_OCR = _ResultPolicy(AsyncDatalabClient._build_ocr_result, _save_successful)
|
|
3465
|
+
_REDACT = _ResultPolicy(
|
|
3466
|
+
AsyncDatalabClient._build_redact_result,
|
|
3467
|
+
_save_redacted_document,
|
|
3468
|
+
result_transform=AsyncDatalabClient._normalize_redaction_result,
|
|
3469
|
+
streamable=False,
|
|
3470
|
+
)
|
|
3471
|
+
|
|
3472
|
+
# The result routes that get_result() accepts, by the path between /api/v1/
|
|
3473
|
+
# and the request ID. Each route returns its own response shape, so the route
|
|
3474
|
+
# sets the result policy. "marker" and its subpaths are the older routes that
|
|
3475
|
+
# some check URLs still use.
|
|
3476
|
+
_RESULT_ROUTES = {
|
|
3477
|
+
"convert": _CONVERSION,
|
|
3478
|
+
"extract": _CONVERSION,
|
|
3479
|
+
"segment": _CONVERSION,
|
|
3480
|
+
"track-changes": _CONVERSION,
|
|
3481
|
+
"custom-processor": _CONVERSION,
|
|
3482
|
+
"custom-pipeline": _CONVERSION,
|
|
3483
|
+
"marker": _CONVERSION,
|
|
3484
|
+
"marker/extraction": _CONVERSION,
|
|
3485
|
+
"marker/segmentation": _CONVERSION,
|
|
3486
|
+
"agent": _AGENT,
|
|
3487
|
+
"create-document": _CREATE_DOCUMENT,
|
|
3488
|
+
"fill": _FORM_FILLING,
|
|
3489
|
+
"form-fields": _FORM_FIELDS,
|
|
3490
|
+
"ocr": _OCR,
|
|
3491
|
+
"redact": _REDACT,
|
|
3492
|
+
}
|
|
3493
|
+
|
|
3494
|
+
|
|
3281
3495
|
class DatalabClient:
|
|
3282
3496
|
"""Synchronous wrapper around AsyncDatalabClient"""
|
|
3283
3497
|
|
|
@@ -3752,6 +3966,41 @@ class DatalabClient:
|
|
|
3752
3966
|
)
|
|
3753
3967
|
)
|
|
3754
3968
|
|
|
3969
|
+
def get_result(
|
|
3970
|
+
self,
|
|
3971
|
+
request_id_or_check_url: str,
|
|
3972
|
+
endpoint: Optional[str] = None,
|
|
3973
|
+
save_output: Optional[Union[str, Path]] = None,
|
|
3974
|
+
stream_response_to: Optional[Union[str, Path]] = None,
|
|
3975
|
+
max_polls: int = 300,
|
|
3976
|
+
poll_interval: int = 1,
|
|
3977
|
+
on_progress: Optional[ProgressCallback] = None,
|
|
3978
|
+
) -> Union[
|
|
3979
|
+
ConversionResult,
|
|
3980
|
+
AgentResult,
|
|
3981
|
+
CreateDocumentResult,
|
|
3982
|
+
FormFillingResult,
|
|
3983
|
+
FormFieldsResult,
|
|
3984
|
+
OCRResult,
|
|
3985
|
+
RedactResult,
|
|
3986
|
+
FileResult,
|
|
3987
|
+
]:
|
|
3988
|
+
"""Wait for the result of a request that was already submitted (sync version).
|
|
3989
|
+
|
|
3990
|
+
See AsyncDatalabClient.get_result().
|
|
3991
|
+
"""
|
|
3992
|
+
return self._run_async(
|
|
3993
|
+
self._async_client.get_result(
|
|
3994
|
+
request_id_or_check_url,
|
|
3995
|
+
endpoint=endpoint,
|
|
3996
|
+
save_output=save_output,
|
|
3997
|
+
stream_response_to=stream_response_to,
|
|
3998
|
+
max_polls=max_polls,
|
|
3999
|
+
poll_interval=poll_interval,
|
|
4000
|
+
on_progress=on_progress,
|
|
4001
|
+
)
|
|
4002
|
+
)
|
|
4003
|
+
|
|
3755
4004
|
def delete_result(self, request_id: str) -> Dict[str, Any]:
|
|
3756
4005
|
"""Delete the stored result of a finished request (sync version)"""
|
|
3757
4006
|
return self._run_async(self._async_client.delete_result(request_id=request_id))
|
datalab_sdk/exceptions.py
CHANGED
|
@@ -6,9 +6,18 @@ from typing import Any, Optional
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
class DatalabError(Exception):
|
|
9
|
-
"""Base exception for Datalab SDK errors
|
|
9
|
+
"""Base exception for Datalab SDK errors.
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Attributes:
|
|
12
|
+
request_id: The request ID, when a document-processing method or
|
|
13
|
+
get_result() fails after the API accepted the request. Pass
|
|
14
|
+
check_url to get_result() to fetch the result without submitting
|
|
15
|
+
the document again. For run_pipeline(), the execution ID.
|
|
16
|
+
check_url: The URL that returns the request's status and result.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
request_id: Optional[str] = None
|
|
20
|
+
check_url: Optional[str] = None
|
|
12
21
|
|
|
13
22
|
|
|
14
23
|
class DatalabAPIError(DatalabError):
|
|
@@ -68,9 +77,45 @@ class DatalabServerError(DatalabAPIError):
|
|
|
68
77
|
|
|
69
78
|
|
|
70
79
|
class DatalabTimeoutError(DatalabError):
|
|
71
|
-
"""
|
|
80
|
+
"""A request or the polling budget timed out.
|
|
72
81
|
|
|
73
|
-
|
|
82
|
+
When request_id is None, the submission itself timed out, and the API
|
|
83
|
+
may still have accepted the request. Otherwise the subclass says what
|
|
84
|
+
happened to the request:
|
|
85
|
+
|
|
86
|
+
- DatalabPollingTimeoutError: the request is still running.
|
|
87
|
+
- DatalabTransferTimeoutError: the result timed out while it downloaded.
|
|
88
|
+
"""
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class DatalabTransferTimeoutError(DatalabTimeoutError):
|
|
92
|
+
"""The result timed out while it downloaded. The request has finished.
|
|
93
|
+
|
|
94
|
+
The SDK does not retry this error: the timeout limits each whole
|
|
95
|
+
request, so a result too large to download within it would time out
|
|
96
|
+
again. Raise the client's timeout and call get_result().
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class DatalabPollingTimeoutError(DatalabTimeoutError):
|
|
101
|
+
"""The polling budget (max_polls * poll_interval) ran out before the
|
|
102
|
+
request finished. The request keeps running on the server and is
|
|
103
|
+
charged; call get_result() to wait for it again.
|
|
104
|
+
|
|
105
|
+
Attributes:
|
|
106
|
+
status: The status of the last poll, for example "processing".
|
|
107
|
+
progress_percent: The estimated progress of the last poll, if any.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
def __init__(
|
|
111
|
+
self,
|
|
112
|
+
message: str,
|
|
113
|
+
status: Optional[str] = None,
|
|
114
|
+
progress_percent: Optional[int] = None,
|
|
115
|
+
):
|
|
116
|
+
super().__init__(message)
|
|
117
|
+
self.status = status
|
|
118
|
+
self.progress_percent = progress_percent
|
|
74
119
|
|
|
75
120
|
|
|
76
121
|
class DatalabFileError(DatalabError):
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
datalab_sdk/__init__.py,sha256=bgRAkvlqFmwVdUpZjPf4LfPDzkDM7GLq3FszGKAUn68,3321
|
|
2
|
-
datalab_sdk/auth.py,sha256=X2eccKzpdxogFEDoS4geOHIOeECQ8u3kANxKoqF0PVk,4388
|
|
3
|
-
datalab_sdk/cli.py,sha256=g_qR_xMJS19PYVOf19HRD3nl3GnzHf1bIohh7WY5AO0,63928
|
|
4
|
-
datalab_sdk/client.py,sha256=201rfE-nC-zlpSS0Chx1JBJgueNzk1fBjBR04wkv0ZY,161558
|
|
5
|
-
datalab_sdk/exceptions.py,sha256=5-lzlfvB7FKGOhvgyacceTFRRUxton_JqcIyuB8k_mU,2625
|
|
6
|
-
datalab_sdk/mimetypes.py,sha256=a4ULJmHxoR_RMsD0j51hx0t2Q7In_PzNm7UlWX07eEE,932
|
|
7
|
-
datalab_sdk/models.py,sha256=j6UruxDaGp1gfcc7YHMNr7ZUg6ez0Jx-8gzJ18efNkQ,43374
|
|
8
|
-
datalab_sdk/settings.py,sha256=B_B42QQADAz6ePXQTOqENZllcppFR4JpwonEk2vlzhw,693
|
|
9
|
-
datalab_python_sdk-0.12.0.dist-info/METADATA,sha256=wuvbL-9NIxLo8wNQfKyedBIldXogEzP5t-a4iZywyvI,10297
|
|
10
|
-
datalab_python_sdk-0.12.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
11
|
-
datalab_python_sdk-0.12.0.dist-info/entry_points.txt,sha256=rqWxO21UV57JxxyeF20iBTxHpzBYOA69hCHIqwdn-Ok,48
|
|
12
|
-
datalab_python_sdk-0.12.0.dist-info/licenses/LICENSE,sha256=XGjT-Uz6t7UZzr3JCZfilsGcZEgMm_1aONcRunohxWg,1064
|
|
13
|
-
datalab_python_sdk-0.12.0.dist-info/RECORD,,
|
|
File without changes
|
{datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{datalab_python_sdk-0.12.0.dist-info → datalab_python_sdk-0.13.0.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|