Skip to content

Commit c8f10b0

Browse files
update syntaxes + add code sample
1 parent 61ce795 commit c8f10b0

4 files changed

Lines changed: 65 additions & 80 deletions

File tree

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from mindee import ClientV2, InferenceParameters, PathInput
1+
from mindee import ClientV2, InferenceParameters, InferenceResponse, PathInput
22

33
input_path = "/path/to/the/file.ext"
44
api_key = "MY_API_KEY"
@@ -29,8 +29,10 @@ params = InferenceParameters(
2929
input_source = PathInput(input_path)
3030

3131
# Send for processing
32-
response = mindee_client.enqueue_and_get_inference(
33-
input_source, params
32+
response = mindee_client.enqueue_and_get_result(
33+
InferenceResponse,
34+
input_source,
35+
params,
3436
)
3537

3638
# Print a brief summary of the parsed data
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from mindee import ClientV2, SplitParameters, SplitResponse, PathInput
2+
3+
input_path = "/path/to/the/file.ext"
4+
api_key = "MY_API_KEY"
5+
model_id = "MY_MODEL_ID"
6+
7+
# Init a new client
8+
mindee_client = ClientV2(api_key)
9+
10+
# Set inference parameters
11+
params = SplitParameters(
12+
# ID of the model, required.
13+
model_id=model_id,
14+
)
15+
16+
# Load a file from disk
17+
input_source = PathInput(input_path)
18+
19+
# Send for processing
20+
response = mindee_client.enqueue_and_get_result(
21+
SplitResponse,
22+
input_source,
23+
params,
24+
)
25+
26+
# Print a brief summary of the parsed data
27+
print(response.inference)

mindee/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from mindee import product
22
from mindee.client import Client
33
from mindee.client_v2 import ClientV2
4+
from mindee.input import LocalResponse, PageOptions, PollingOptions
45
from mindee.input.inference_parameters import (
5-
InferenceParameters,
6-
DataSchemaField,
76
DataSchema,
7+
DataSchemaField,
88
DataSchemaReplace,
9+
InferenceParameters,
910
)
10-
from mindee.input import LocalResponse, PageOptions, PollingOptions
1111
from mindee.input.sources import (
1212
Base64Input,
1313
BytesInput,
@@ -22,29 +22,33 @@
2222
from mindee.parsing.common.predict_response import PredictResponse
2323
from mindee.parsing.common.workflow_response import WorkflowResponse
2424
from mindee.parsing.v2 import InferenceResponse, JobResponse
25+
from mindee.v2.product.split.split_parameters import SplitParameters
26+
from mindee.v2.product.split.split_response import SplitResponse
2527

2628
__all__ = [
29+
"ApiResponse",
30+
"AsyncPredictResponse",
31+
"Base64Input",
32+
"BytesInput",
2733
"Client",
2834
"ClientV2",
2935
"DataSchema",
3036
"DataSchemaField",
3137
"DataSchemaReplace",
32-
"InferenceParameters",
38+
"FeedbackResponse",
3339
"FileInput",
34-
"PathInput",
35-
"BytesInput",
36-
"Base64Input",
37-
"UrlInputSource",
40+
"InferenceParameters",
41+
"InferenceResponse",
42+
"Job",
43+
"JobResponse",
3844
"LocalResponse",
3945
"PageOptions",
46+
"PathInput",
4047
"PollingOptions",
41-
"ApiResponse",
42-
"AsyncPredictResponse",
43-
"FeedbackResponse",
4448
"PredictResponse",
49+
"SplitParameters",
50+
"SplitResponse",
51+
"UrlInputSource",
4552
"WorkflowResponse",
46-
"JobResponse",
47-
"Job",
48-
"InferenceResponse",
4953
"product",
5054
]

mindee/client_v2.py

Lines changed: 15 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from mindee.parsing.v2.inference_response import InferenceResponse
2121
from mindee.parsing.v2.job_response import JobResponse
2222

23-
TypeBaseInferenceResponse = TypeVar("TypeBaseInferenceResponse", bound=BaseResponse)
23+
TypeBaseResponse = TypeVar("TypeBaseResponse", bound=BaseResponse)
2424

2525

2626
class ClientV2(ClientMixin):
@@ -100,44 +100,15 @@ def get_job(self, job_id: str) -> JobResponse:
100100
def get_inference(
101101
self,
102102
inference_id: str,
103-
response_type: Type[BaseResponse] = InferenceResponse,
104-
disable_redundant_warnings: bool = False,
105103
) -> BaseResponse:
106104
"""[Deprecated] Use `get_result` instead."""
107-
if not disable_redundant_warnings:
108-
warnings.warn(
109-
"get_inference is deprecated; use get_result instead",
110-
DeprecationWarning,
111-
stacklevel=2,
112-
)
113-
return self.get_result(inference_id, response_type)
105+
return self.get_result(InferenceResponse, inference_id)
114106

115107
def get_result(
116108
self,
109+
response_type: Type[TypeBaseResponse],
117110
inference_id: str,
118-
response_type: Optional[Type[BaseResponse]] = InferenceResponse,
119-
) -> BaseResponse:
120-
"""
121-
Get the result of an inference that was previously enqueued.
122-
123-
The inference will only be available after it has finished processing.
124-
125-
:param inference_id: UUID of the inference to retrieve.
126-
:param response_type: Class of the product to instantiate.
127-
:return: An inference response.
128-
"""
129-
response_type = response_type or InferenceResponse
130-
response = self._get_result(inference_id, response_type)
131-
assert isinstance(response, response_type), (
132-
f'Invalid response type "{type(response)}"'
133-
)
134-
return response
135-
136-
def _get_result(
137-
self,
138-
inference_id: str,
139-
response_type: Type[BaseResponse] = InferenceResponse,
140-
) -> BaseResponse:
111+
) -> TypeBaseResponse:
141112
"""
142113
Get the result of an inference that was previously enqueued.
143114
@@ -157,12 +128,12 @@ def _get_result(
157128
dict_response = response.json()
158129
return response_type(dict_response)
159130

160-
def _enqueue_and_get(
131+
def enqueue_and_get_result(
161132
self,
133+
response_type: Type[TypeBaseResponse],
162134
input_source: Union[LocalInputSource, UrlInputSource],
163135
params: BaseParameters,
164-
response_type: Optional[Type[BaseResponse]] = InferenceResponse,
165-
) -> BaseResponse:
136+
) -> TypeBaseResponse:
166137
"""
167138
Enqueues to an asynchronous endpoint and automatically polls for a response.
168139
@@ -187,6 +158,7 @@ def _enqueue_and_get(
187158
try_counter = 0
188159
while try_counter < params.polling_options.max_retries:
189160
job_response = self.get_job(enqueue_response.job.id)
161+
assert isinstance(job_response, JobResponse)
190162
if job_response.job.status == CommonStatus.FAILED.value:
191163
if job_response.job.error:
192164
detail = job_response.job.error.detail
@@ -196,8 +168,11 @@ def _enqueue_and_get(
196168
f"Parsing failed for job {job_response.job.id}: {detail}"
197169
)
198170
if job_response.job.status == CommonStatus.PROCESSED.value:
199-
result = self.get_inference(
200-
job_response.job.id, response_type or InferenceResponse, True
171+
result = self.get_result(
172+
response_type or InferenceResponse, job_response.job.id
173+
)
174+
assert isinstance(result, response_type), (
175+
f'Invalid response type "{type(result)}"'
201176
)
202177
return result
203178
try_counter += 1
@@ -212,35 +187,12 @@ def enqueue_and_get_inference(
212187
) -> InferenceResponse:
213188
"""[Deprecated] Use `enqueue_and_get_result` instead."""
214189
warnings.warn(
215-
"enqueue_and_get_inference is deprecated; use enqueue_and_get_result",
190+
"enqueue_and_get_inference is deprecated; use enqueue_and_get_result instead",
216191
DeprecationWarning,
217192
stacklevel=2,
218193
)
219-
response = self._enqueue_and_get(input_source, params)
194+
response = self.enqueue_and_get_result(InferenceResponse, input_source, params)
220195
assert isinstance(response, InferenceResponse), (
221196
f'Invalid response type "{type(response)}"'
222197
)
223198
return response
224-
225-
def enqueue_and_get_result(
226-
self,
227-
response_type: Type[TypeBaseInferenceResponse],
228-
input_source: Union[LocalInputSource, UrlInputSource],
229-
params: BaseParameters,
230-
) -> TypeBaseInferenceResponse:
231-
"""
232-
Enqueues to an asynchronous endpoint and automatically polls for a response.
233-
234-
:param input_source: The document/source file to use. Can be local or remote.
235-
236-
:param params: Parameters to set when sending a file.
237-
238-
:param response_type: The product class to use for the response object.
239-
240-
:return: A valid inference response.
241-
"""
242-
response = self._enqueue_and_get(input_source, params, response_type)
243-
assert isinstance(response, response_type), (
244-
f'Invalid response type "{type(response)}"'
245-
)
246-
return response

0 commit comments

Comments
 (0)