11from time import sleep
2- from typing import Optional , Union
2+ from typing import Optional , Union , Type
33
44from mindee .client_mixin import ClientMixin
55from mindee .error .mindee_error import MindeeError
66from mindee .error .mindee_http_error_v2 import handle_error_v2
7- from mindee .input import UrlInputSource
7+ from mindee .input import UrlInputSource , UtilityParameters
88from mindee .input .inference_parameters import InferenceParameters
99from mindee .input .polling_options import PollingOptions
1010from mindee .input .sources .local_input_source import LocalInputSource
1515 is_valid_post_response ,
1616)
1717from mindee .parsing .v2 .common_response import CommonStatus
18+ from mindee .parsing .v2 .base_inference import BaseInference , BaseInferenceResponse , TypeInferenceV2
1819from mindee .parsing .v2 .inference_response import InferenceResponse
1920from mindee .parsing .v2 .job_response import JobResponse
2021
@@ -41,7 +42,8 @@ def __init__(self, api_key: Optional[str] = None) -> None:
4142 def enqueue_inference (
4243 self ,
4344 input_source : Union [LocalInputSource , UrlInputSource ],
44- params : InferenceParameters ,
45+ params : Union [InferenceParameters , UtilityParameters ],
46+ slug : Optional [str ] = None
4547 ) -> JobResponse :
4648 """
4749 Enqueues a document to a given model.
@@ -52,16 +54,18 @@ def enqueue_inference(
5254 :return: A valid inference response.
5355 """
5456 logger .debug ("Enqueuing inference using model: %s" , params .model_id )
55-
5657 response = self .mindee_api .req_post_inference_enqueue (
57- input_source = input_source , params = params
58+ input_source = input_source ,
59+ params = params ,
60+ slug = slug
5861 )
5962 dict_response = response .json ()
6063
6164 if not is_valid_post_response (response ):
6265 handle_error_v2 (dict_response )
6366 return JobResponse (dict_response )
6467
68+
6569 def get_job (self , job_id : str ) -> JobResponse :
6670 """
6771 Get the status of an inference that was previously enqueued.
@@ -79,13 +83,18 @@ def get_job(self, job_id: str) -> JobResponse:
7983 dict_response = response .json ()
8084 return JobResponse (dict_response )
8185
82- def get_inference (self , inference_id : str ) -> InferenceResponse :
86+ def get_inference (
87+ self ,
88+ inference_id : str ,
89+ inference_type : Type [BaseInference ] = InferenceResponse
90+ ) -> InferenceResponse :
8391 """
8492 Get the result of an inference that was previously enqueued.
8593
8694 The inference will only be available after it has finished processing.
8795
8896 :param inference_id: UUID of the inference to retrieve.
97+ :param inference_type: Class of the product to instantiate.
8998 :return: An inference response.
9099 """
91100 logger .debug ("Fetching inference: %s" , inference_id )
@@ -94,19 +103,20 @@ def get_inference(self, inference_id: str) -> InferenceResponse:
94103 if not is_valid_get_response (response ):
95104 handle_error_v2 (response .json ())
96105 dict_response = response .json ()
97- return InferenceResponse (dict_response )
106+ return inference_type (dict_response )
98107
99- def enqueue_and_get_inference (
108+ def _enqueue_and_get (
100109 self ,
101110 input_source : Union [LocalInputSource , UrlInputSource ],
102- params : InferenceParameters ,
103- ) -> InferenceResponse :
111+ params : Union [InferenceParameters , UtilityParameters ],
112+ inference_type : Optional [Type [BaseInference ]] = BaseInference
113+ ) -> Union [InferenceResponse , BaseInferenceResponse ]:
104114 """
105115 Enqueues to an asynchronous endpoint and automatically polls for a response.
106116
107117 :param input_source: The document/source file to use. Can be local or remote.
108-
109118 :param params: Parameters to set when sending a file.
119+ :param inference_type: The product class to use for the response object.
110120
111121 :return: A valid inference response.
112122 """
@@ -117,9 +127,14 @@ def enqueue_and_get_inference(
117127 params .polling_options .delay_sec ,
118128 params .polling_options .max_retries ,
119129 )
120- enqueue_response = self .enqueue_inference (input_source , params )
130+ slug = inference_type if inference_type .get_slug () else None
131+ enqueue_response = self .enqueue_inference (
132+ input_source ,
133+ params ,
134+ slug
135+ )
121136 logger .debug (
122- "Successfully enqueued inference with job id: %s" , enqueue_response .job .id
137+ "Successfully enqueued document with job id: %s" , enqueue_response .job .id
123138 )
124139 sleep (params .polling_options .initial_delay_sec )
125140 try_counter = 0
@@ -134,8 +149,48 @@ def enqueue_and_get_inference(
134149 f"Parsing failed for job { job_response .job .id } : { detail } "
135150 )
136151 if job_response .job .status == CommonStatus .PROCESSED .value :
137- return self .get_inference (job_response .job .id )
152+ return self .get_inference (job_response .job .id , inference_type )
138153 try_counter += 1
139154 sleep (params .polling_options .delay_sec )
140155
141156 raise MindeeError (f"Couldn't retrieve document after { try_counter + 1 } tries." )
157+
158+ def enqueue_and_get_inference (
159+ self ,
160+ input_source : Union [LocalInputSource , UrlInputSource ],
161+ params : InferenceParameters
162+ ) -> InferenceResponse :
163+ """
164+ Enqueues to an asynchronous endpoint and automatically polls for a response.
165+
166+ :param input_source: The document/source file to use. Can be local or remote.
167+
168+ :param params: Parameters to set when sending a file.
169+
170+ :return: A valid inference response.
171+ """
172+ response = self ._enqueue_and_get (input_source , params )
173+ assert isinstance (response , InferenceResponse ), f'Invalid response type "{ type (response )} "'
174+ return response
175+
176+
177+ def enqueue_and_get_utility (
178+ self ,
179+ inference_type : Type [TypeInferenceV2 ],
180+ input_source : Union [LocalInputSource , UrlInputSource ],
181+ params : UtilityParameters
182+ ) -> TypeInferenceV2 :
183+ """
184+ Enqueues to an asynchronous endpoint and automatically polls for a response.
185+
186+ :param input_source: The document/source file to use. Can be local or remote.
187+
188+ :param params: Parameters to set when sending a file.
189+
190+ :param inference_type: The product class to use for the response object.
191+
192+ :return: A valid inference response.
193+ """
194+ response = self ._enqueue_and_get (input_source , params , inference_type )
195+ assert isinstance (response , inference_type ), f'Invalid response type "{ type (response )} "'
196+ return response
0 commit comments