Skip to content

Commit 0747368

Browse files
tmp
1 parent f57ba8d commit 0747368

31 files changed

+824
-112
lines changed

mindee/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mindee import product
22
from mindee.client import Client
3+
from mindee.input.inference_predict_options import InferencePredictOptions
34
from mindee.input.local_response import LocalResponse
45
from mindee.input.page_options import PageOptions
56
from mindee.parsing.common.api_response import ApiResponse

mindee/client.py

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
from pathlib import Path
21
from time import sleep
3-
from typing import BinaryIO, Dict, Optional, Type, Union
2+
from typing import Dict, Optional, Type, Union
43

4+
from mindee.client_mixin import ClientMixin
55
from mindee.error.mindee_error import MindeeClientError, MindeeError
66
from mindee.error.mindee_http_error import handle_error
77
from mindee.input import WorkflowOptions
88
from mindee.input.local_response import LocalResponse
99
from mindee.input.page_options import PageOptions
1010
from mindee.input.predict_options import AsyncPredictOptions, PredictOptions
11-
from mindee.input.sources.base_64_input import Base64Input
12-
from mindee.input.sources.bytes_input import BytesInput
13-
from mindee.input.sources.file_input import FileInput
1411
from mindee.input.sources.local_input_source import LocalInputSource
15-
from mindee.input.sources.path_input import PathInput
1612
from mindee.input.sources.url_input_source import UrlInputSource
1713
from mindee.logger import logger
1814
from mindee.mindee_http.endpoint import CustomEndpoint, Endpoint
@@ -55,7 +51,7 @@ def _clean_account_name(account_name: str) -> str:
5551
return account_name
5652

5753

58-
class Client:
54+
class Client(ClientMixin):
5955
"""
6056
Mindee API Client.
6157
@@ -275,23 +271,6 @@ def execute_workflow(
275271
logger.debug("Sending document to workflow: %s", workflow_id)
276272
return self._send_to_workflow(GeneratedV1, input_source, workflow_id, options)
277273

278-
def _validate_async_params(
279-
self, initial_delay_sec: float, delay_sec: float, max_retries: int
280-
) -> None:
281-
min_delay = 1
282-
min_initial_delay = 1
283-
min_retries = 1
284-
if delay_sec < min_delay:
285-
raise MindeeClientError(
286-
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
287-
)
288-
if initial_delay_sec < min_initial_delay:
289-
raise MindeeClientError(
290-
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
291-
)
292-
if max_retries < min_retries:
293-
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")
294-
295274
def enqueue_and_parse( # pylint: disable=too-many-locals
296275
self,
297276
product_class: Type[Inference],
@@ -583,80 +562,3 @@ def create_endpoint(
583562
)
584563
version = "1"
585564
return self._build_endpoint(endpoint_name, account_name, version)
586-
587-
@staticmethod
588-
def source_from_path(
589-
input_path: Union[Path, str], fix_pdf: bool = False
590-
) -> PathInput:
591-
"""
592-
Load a document from an absolute path, as a string.
593-
594-
:param input_path: Path of file to open
595-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
596-
Setting this to `True` can modify the data sent to Mindee.
597-
"""
598-
input_doc = PathInput(input_path)
599-
if fix_pdf:
600-
input_doc.fix_pdf()
601-
return input_doc
602-
603-
@staticmethod
604-
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
605-
"""
606-
Load a document from a normal Python file object/handle.
607-
608-
:param input_file: Input file handle
609-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
610-
Setting this to `True` can modify the data sent to Mindee.
611-
"""
612-
input_doc = FileInput(input_file)
613-
if fix_pdf:
614-
input_doc.fix_pdf()
615-
return input_doc
616-
617-
@staticmethod
618-
def source_from_b64string(
619-
input_string: str, filename: str, fix_pdf: bool = False
620-
) -> Base64Input:
621-
"""
622-
Load a document from a base64 encoded string.
623-
624-
:param input_string: Input to parse as base64 string
625-
:param filename: The name of the file (without the path)
626-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
627-
Setting this to `True` can modify the data sent to Mindee.
628-
"""
629-
input_doc = Base64Input(input_string, filename)
630-
if fix_pdf:
631-
input_doc.fix_pdf()
632-
return input_doc
633-
634-
@staticmethod
635-
def source_from_bytes(
636-
input_bytes: bytes, filename: str, fix_pdf: bool = False
637-
) -> BytesInput:
638-
"""
639-
Load a document from raw bytes.
640-
641-
:param input_bytes: Raw byte input
642-
:param filename: The name of the file (without the path)
643-
:param fix_pdf: Whether to attempt fixing PDF files before sending.
644-
Setting this to `True` can modify the data sent to Mindee.
645-
"""
646-
input_doc = BytesInput(input_bytes, filename)
647-
if fix_pdf:
648-
input_doc.fix_pdf()
649-
return input_doc
650-
651-
@staticmethod
652-
def source_from_url(
653-
url: str,
654-
) -> UrlInputSource:
655-
"""
656-
Load a document from a URL.
657-
658-
:param url: Raw byte input
659-
"""
660-
return UrlInputSource(
661-
url,
662-
)

mindee/client_mixin.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
from pathlib import Path
2+
from typing import BinaryIO, Union
3+
4+
from mindee.error import MindeeClientError
5+
from mindee.input import Base64Input, BytesInput, FileInput, PathInput, UrlInputSource
6+
7+
8+
class ClientMixin:
9+
@staticmethod
10+
def source_from_path(
11+
input_path: Union[Path, str], fix_pdf: bool = False
12+
) -> PathInput:
13+
"""
14+
Load a document from an absolute path, as a string.
15+
16+
:param input_path: Path of file to open
17+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
18+
Setting this to `True` can modify the data sent to Mindee.
19+
"""
20+
input_doc = PathInput(input_path)
21+
if fix_pdf:
22+
input_doc.fix_pdf()
23+
return input_doc
24+
25+
@staticmethod
26+
def source_from_file(input_file: BinaryIO, fix_pdf: bool = False) -> FileInput:
27+
"""
28+
Load a document from a normal Python file object/handle.
29+
30+
:param input_file: Input file handle
31+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
32+
Setting this to `True` can modify the data sent to Mindee.
33+
"""
34+
input_doc = FileInput(input_file)
35+
if fix_pdf:
36+
input_doc.fix_pdf()
37+
return input_doc
38+
39+
@staticmethod
40+
def source_from_b64string(
41+
input_string: str, filename: str, fix_pdf: bool = False
42+
) -> Base64Input:
43+
"""
44+
Load a document from a base64 encoded string.
45+
46+
:param input_string: Input to parse as base64 string
47+
:param filename: The name of the file (without the path)
48+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
49+
Setting this to `True` can modify the data sent to Mindee.
50+
"""
51+
input_doc = Base64Input(input_string, filename)
52+
if fix_pdf:
53+
input_doc.fix_pdf()
54+
return input_doc
55+
56+
@staticmethod
57+
def source_from_bytes(
58+
input_bytes: bytes, filename: str, fix_pdf: bool = False
59+
) -> BytesInput:
60+
"""
61+
Load a document from raw bytes.
62+
63+
:param input_bytes: Raw byte input
64+
:param filename: The name of the file (without the path)
65+
:param fix_pdf: Whether to attempt fixing PDF files before sending.
66+
Setting this to `True` can modify the data sent to Mindee.
67+
"""
68+
input_doc = BytesInput(input_bytes, filename)
69+
if fix_pdf:
70+
input_doc.fix_pdf()
71+
return input_doc
72+
73+
@staticmethod
74+
def source_from_url(
75+
url: str,
76+
) -> UrlInputSource:
77+
"""
78+
Load a document from a URL.
79+
80+
:param url: Raw byte input
81+
"""
82+
return UrlInputSource(
83+
url,
84+
)
85+
86+
@staticmethod
87+
def _validate_async_params(
88+
initial_delay_sec: float, delay_sec: float, max_retries: int
89+
) -> None:
90+
min_delay = 1
91+
min_initial_delay = 1
92+
min_retries = 1
93+
if delay_sec < min_delay:
94+
raise MindeeClientError(
95+
f"Cannot set auto-parsing delay to less than {min_delay} second(s)."
96+
)
97+
if initial_delay_sec < min_initial_delay:
98+
raise MindeeClientError(
99+
f"Cannot set initial parsing delay to less than {min_initial_delay} second(s)."
100+
)
101+
if max_retries < min_retries:
102+
raise MindeeClientError(f"Cannot set retries to less than {min_retries}.")

0 commit comments

Comments
 (0)