|
22 | 22 | is_valid_async_response, |
23 | 23 | is_valid_sync_response, |
24 | 24 | ) |
| 25 | +from mindee.mindee_http.endpoint import CustomEndpoint, Endpoint |
| 26 | +from mindee.mindee_http.mindee_api import MindeeApi |
| 27 | +from mindee.mindee_http.workflow_endpoint import WorkflowEndpoint |
| 28 | +from mindee.mindee_http.workflow_settings import WorkflowSettings |
25 | 29 | from mindee.parsing.common.async_predict_response import AsyncPredictResponse |
| 30 | +from mindee.parsing.common.execution_priority import ExecutionPriority |
26 | 31 | from mindee.parsing.common.feedback_response import FeedbackResponse |
27 | 32 | from mindee.parsing.common.inference import Inference |
28 | 33 | from mindee.parsing.common.predict_response import PredictResponse |
29 | 34 | from mindee.parsing.common.string_dict import StringDict |
| 35 | +from mindee.parsing.common.workflow_response import WorkflowResponse |
| 36 | +from mindee.product import GeneratedV1 |
30 | 37 |
|
31 | 38 | OTS_OWNER = "mindee" |
32 | 39 |
|
@@ -230,6 +237,42 @@ def parse_queued( |
230 | 237 |
|
231 | 238 | return self._get_queued_document(product_class, endpoint, queue_id) |
232 | 239 |
|
| 240 | + def execute_workflow( |
| 241 | + self, |
| 242 | + input_source: Union[LocalInputSource, UrlInputSource], |
| 243 | + workflow_id: str, |
| 244 | + page_options: Optional[PageOptions] = None, |
| 245 | + alias: Optional[str] = None, |
| 246 | + priority: Optional[ExecutionPriority] = None, |
| 247 | + full_text: bool = False, |
| 248 | + ) -> WorkflowResponse: |
| 249 | + """ |
| 250 | + Send the document to an asynchronous endpoint and return its ID in the queue. |
| 251 | +
|
| 252 | + :param input_source: The document/source file to use. |
| 253 | + Has to be created beforehand. |
| 254 | + :param workflow_id: ID of the workflow. |
| 255 | + :param page_options: If set, remove pages from the document as specified. This is done before sending the file\ |
| 256 | + to the server. It is useful to avoid page limitations. |
| 257 | + :param alias: Optional alias for the document. |
| 258 | + :param priority: Optional priority for the document. |
| 259 | + :param full_text: Whether to include the full OCR text response in compatible APIs. |
| 260 | + :return: |
| 261 | + """ |
| 262 | + if isinstance(input_source, LocalInputSource): |
| 263 | + if page_options and input_source.is_pdf(): |
| 264 | + input_source.process_pdf( |
| 265 | + page_options.operation, |
| 266 | + page_options.on_min_pages, |
| 267 | + page_options.page_indexes, |
| 268 | + ) |
| 269 | + |
| 270 | + logger.debug("Sending document to workflow: %s", workflow_id) |
| 271 | + |
| 272 | + return self._send_to_workflow( |
| 273 | + GeneratedV1, input_source, workflow_id, alias, priority, full_text |
| 274 | + ) |
| 275 | + |
233 | 276 | def _validate_async_params( |
234 | 277 | self, initial_delay_sec: float, delay_sec: float, max_retries: int |
235 | 278 | ) -> None: |
@@ -438,6 +481,50 @@ def _get_queued_document( |
438 | 481 |
|
439 | 482 | return AsyncPredictResponse(product_class, queue_response.json()) |
440 | 483 |
|
| 484 | + def _send_to_workflow( |
| 485 | + self, |
| 486 | + product_class: Type[Inference], |
| 487 | + input_source: Union[LocalInputSource, UrlInputSource], |
| 488 | + workflow_id: str, |
| 489 | + alias: Optional[str] = None, |
| 490 | + priority: Optional[ExecutionPriority] = None, |
| 491 | + full_text: bool = False, |
| 492 | + ) -> WorkflowResponse: |
| 493 | + """ |
| 494 | + Sends a document to a workflow. |
| 495 | +
|
| 496 | + :param product_class: The document class to use. |
| 497 | + The response object will be instantiated based on this parameter. |
| 498 | +
|
| 499 | + :param input_source: The document/source file to use. |
| 500 | + Has to be created beforehand. |
| 501 | + :param workflow_id: ID of the workflow. |
| 502 | + :param alias: Optional alias for the document. |
| 503 | + :param priority: Priority for the document. |
| 504 | + :param full_text: Whether to include the full OCR text response in compatible APIs. |
| 505 | + :return: |
| 506 | + """ |
| 507 | + if input_source is None: |
| 508 | + raise MindeeClientError("No input document provided") |
| 509 | + |
| 510 | + workflow_endpoint = WorkflowEndpoint( |
| 511 | + WorkflowSettings(api_key=self.api_key, workflow_id=workflow_id) |
| 512 | + ) |
| 513 | + |
| 514 | + response = workflow_endpoint.workflow_execution_post( |
| 515 | + input_source, alias, priority, full_text |
| 516 | + ) |
| 517 | + |
| 518 | + dict_response = response.json() |
| 519 | + |
| 520 | + if not is_valid_async_response(response): |
| 521 | + clean_response = clean_request_json(response) |
| 522 | + raise handle_error( |
| 523 | + str(product_class.endpoint_name), |
| 524 | + clean_response, |
| 525 | + ) |
| 526 | + return WorkflowResponse(product_class, dict_response) |
| 527 | + |
441 | 528 | def _initialize_ots_endpoint(self, product_class: Type[Inference]) -> Endpoint: |
442 | 529 | if product_class.__name__ == "CustomV1": |
443 | 530 | raise MindeeClientError("Missing endpoint specifications for custom build.") |
|
0 commit comments