diff --git a/.gitignore b/.gitignore index 8e89f73d..cc311db6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # IDE .vscode/ .idea +.junie/ # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/docs/extras/code_samples/workflow_execution.txt b/docs/extras/code_samples/workflow_execution.txt deleted file mode 100644 index 4a201830..00000000 --- a/docs/extras/code_samples/workflow_execution.txt +++ /dev/null @@ -1,29 +0,0 @@ -# -# Install the Python client library by running: -# pip install mindee -# - -from mindee.v1 import Client, WorkflowResponse -from mindee.v1.parsing.common import ExecutionPriority -from mindee import PathInput - -# Init a new client -mindee_client = Client(api_key="my-api-key") - -workflow_id = "workflow-id" - -# Load a file from disk -input_doc = PathInput("/path/to/the/file.ext") - -# Send the file to the workflow. -result: WorkflowResponse = mindee_client.execute_workflow( - input_doc, - workflow_id, - # Optional parameters: - # alias="my-alias", - # priority=ExecutionPriority.LOW, - # rag=True, -) - -# Print the ID of the execution to make sure it worked. -print(result.execution.id) diff --git a/docs/v1/client_options.rst b/docs/v1/client_options.rst index f6d6a8ee..d15e38a1 100644 --- a/docs/v1/client_options.rst +++ b/docs/v1/client_options.rst @@ -7,10 +7,3 @@ Predict Options :members: :inherited-members: - -Workflow Options -================ -.. autoclass:: mindee.v1.client_options.workflow_options.WorkflowOptions - :members: - :inherited-members: - diff --git a/docs/v1/mindee_http.rst b/docs/v1/mindee_http.rst index a619a939..409a1f1c 100644 --- a/docs/v1/mindee_http.rst +++ b/docs/v1/mindee_http.rst @@ -24,15 +24,3 @@ MindeeAPI .. autoclass:: mindee.v1.mindee_http.mindee_api.MindeeAPI :members: :inherited-members: - -Workflow Endpoint -================= -.. autoclass:: mindee.v1.mindee_http.workflow_endpoint.WorkflowEndpoint - :members: - :inherited-members: - -Workflow Settings -================= -.. autoclass:: mindee.v1.mindee_http.workflow_settings.WorkflowSettings - :members: - :inherited-members: diff --git a/docs/v1/parsing/common.rst b/docs/v1/parsing/common.rst index 45f6327b..93747bb7 100644 --- a/docs/v1/parsing/common.rst +++ b/docs/v1/parsing/common.rst @@ -76,24 +76,6 @@ FeedbackResponse .. autoclass:: mindee.v1.parsing.common.feedback_response.FeedbackResponse :members: -Workflow Parsing -================ - -Execution ---------- -.. autoclass:: mindee.v1.parsing.common.execution.Execution - :members: - -Execution File --------------- -.. autoclass:: mindee.v1.parsing.common.execution_file.ExecutionFile - :members: - -WorkflowResponse ----------------- -.. autoclass:: mindee.v1.parsing.common.workflow_response.WorkflowResponse - :members: - OCR Extraction ============== diff --git a/mindee/mindee_http/response_validation.py b/mindee/mindee_http/response_validation.py index bcf0c771..9f34f3ad 100644 --- a/mindee/mindee_http/response_validation.py +++ b/mindee/mindee_http/response_validation.py @@ -41,8 +41,6 @@ def is_valid_async_response(response: httpx.Response) -> bool: return False if "job" in response_json: return not response_json["job"].get("error") - if "execution" in response_json: - return not response_json["execution"].get("error") return False diff --git a/mindee/v1/client.py b/mindee/v1/client.py index f19ee451..3b9bcd15 100644 --- a/mindee/v1/client.py +++ b/mindee/v1/client.py @@ -17,17 +17,12 @@ ) from mindee.parsing.common.string_dict import StringDict from mindee.v1.client_options.predict_options import AsyncPredictOptions, PredictOptions -from mindee.v1.client_options.workflow_options import WorkflowOptions from mindee.v1.mindee_http.endpoint import CustomEndpoint, Endpoint from mindee.v1.mindee_http.mindee_api import MindeeAPI -from mindee.v1.mindee_http.workflow_endpoint import WorkflowEndpoint -from mindee.v1.mindee_http.workflow_settings import WorkflowSettings from mindee.v1.parsing.common.async_predict_response import AsyncPredictResponse from mindee.v1.parsing.common.feedback_response import FeedbackResponse from mindee.v1.parsing.common.inference import Inference from mindee.v1.parsing.common.predict_response import PredictResponse -from mindee.v1.parsing.common.workflow_response import WorkflowResponse -from mindee.v1.product import GeneratedV1 OTS_OWNER = "mindee" @@ -258,43 +253,6 @@ def parse_queued( return self._get_queued_document(product_class, endpoint, queue_id) - def execute_workflow( - self, - input_source: LocalInputSource | URLInputSource, - workflow_id: str, - options: WorkflowOptions | None = None, - page_options: PageOptions | None = None, - ) -> WorkflowResponse: - """ - Send the document to a workflow execution. - - :param input_source: The document/source file to use. - Has to be created beforehand. - :param workflow_id: ID of the workflow. - :param page_options: If set, remove pages from the document as specified. - This is done before sending the file to the server. - It is useful to avoid page limitations. - :param options: Options for the workflow. - :return: - """ - if ( - isinstance(input_source, LocalInputSource) - and page_options - and input_source.is_pdf() - ): - input_source.process_pdf( - page_options.operation, - page_options.on_min_pages, - page_options.page_indexes, - ) - - if not options: - options = WorkflowOptions( - alias=None, priority=None, full_text=False, public_url=None, rag=False - ) - logger.debug("Sending document to workflow: %s", workflow_id) - return self._send_to_workflow(GeneratedV1, input_source, workflow_id, options) - def enqueue_and_parse( # pylint: disable=too-many-locals self, product_class: type[Inference], @@ -512,45 +470,6 @@ def _get_queued_document( return AsyncPredictResponse(product_class, queue_response.json()) - def _send_to_workflow( - self, - product_class: type[Inference], - input_source: LocalInputSource | URLInputSource, - workflow_id: str, - options: WorkflowOptions, - ) -> WorkflowResponse: - """ - Sends a document to a workflow. - - :param product_class: The document class to use. - The response object will be instantiated based on this parameter. - - :param input_source: The document/source file to use. - Has to be created beforehand. - :param workflow_id: ID of the workflow. - :param options: Optional options for the workflow. - :return: - """ - if input_source is None: - raise MindeeClientError("No input document provided") - - workflow_endpoint = WorkflowEndpoint( - WorkflowSettings(api_key=self.api_key, workflow_id=workflow_id), - self.http_client, - ) - - response = workflow_endpoint.workflow_execution_post(input_source, options) - - dict_response = response.json() - - if not is_valid_async_response(response): - clean_response = clean_request_json(response) - raise handle_error( - str(product_class.endpoint_name), - clean_response, - ) - return WorkflowResponse(product_class, dict_response) - def _initialize_ots_endpoint(self, product_class: type[Inference]) -> Endpoint: if product_class.__name__ == "CustomV1": raise MindeeClientError("Missing endpoint specifications for custom build.") diff --git a/mindee/v1/client_options/__init__.py b/mindee/v1/client_options/__init__.py index 07ab99b2..b827ff09 100644 --- a/mindee/v1/client_options/__init__.py +++ b/mindee/v1/client_options/__init__.py @@ -1,4 +1,3 @@ from mindee.v1.client_options.predict_options import PredictOptions -from mindee.v1.client_options.workflow_options import WorkflowOptions -__all__ = ["PredictOptions", "WorkflowOptions"] +__all__ = ["PredictOptions"] diff --git a/mindee/v1/client_options/workflow_options.py b/mindee/v1/client_options/workflow_options.py deleted file mode 100644 index 3a2fc3e1..00000000 --- a/mindee/v1/client_options/workflow_options.py +++ /dev/null @@ -1,30 +0,0 @@ -from mindee.v1.parsing.common import ExecutionPriority - - -class WorkflowOptions: - """Options to pass to a workflow execution.""" - - alias: str | None - """Alias for the document.""" - priority: ExecutionPriority | None - """Priority of the document.""" - full_text: bool - """Whether to include the full OCR text response in compatible APIs.""" - public_url: str | None - """A unique, encrypted URL for accessing the document validation interface without requiring authentication.""" - rag: bool - """Whether to enable Retrieval-Augmented Generation.""" - - def __init__( - self, - alias: str | None = None, - priority: ExecutionPriority | None = None, - full_text: bool | None = False, - public_url: str | None = None, - rag: bool | None = False, - ): - self.alias = alias - self.priority = priority - self.full_text = full_text if full_text else False - self.public_url = public_url - self.rag = rag if rag else False diff --git a/mindee/v1/mindee_http/__init__.py b/mindee/v1/mindee_http/__init__.py index 1abe63b5..b4902270 100644 --- a/mindee/v1/mindee_http/__init__.py +++ b/mindee/v1/mindee_http/__init__.py @@ -1,14 +1,10 @@ from mindee.v1.mindee_http.base_endpoint import BaseEndpoint from mindee.v1.mindee_http.endpoint import CustomEndpoint, Endpoint from mindee.v1.mindee_http.mindee_api import MindeeAPI -from mindee.v1.mindee_http.workflow_endpoint import WorkflowEndpoint -from mindee.v1.mindee_http.workflow_settings import WorkflowSettings __all__ = [ "BaseEndpoint", "CustomEndpoint", "Endpoint", "MindeeAPI", - "WorkflowEndpoint", - "WorkflowSettings", ] diff --git a/mindee/v1/mindee_http/workflow_endpoint.py b/mindee/v1/mindee_http/workflow_endpoint.py deleted file mode 100644 index d2e098fa..00000000 --- a/mindee/v1/mindee_http/workflow_endpoint.py +++ /dev/null @@ -1,75 +0,0 @@ -from collections.abc import Callable - -import httpx - -from mindee.input.local_input_source import LocalInputSource -from mindee.input.url_input_source import URLInputSource -from mindee.parsing.common.string_dict import StringDict -from mindee.v1.client_options.workflow_options import WorkflowOptions -from mindee.v1.mindee_http.base_endpoint import BaseEndpoint -from mindee.v1.mindee_http.workflow_settings import WorkflowSettings - - -class WorkflowEndpoint(BaseEndpoint): - """Workflow endpoint.""" - - settings: WorkflowSettings - """Settings object.""" - - def __init__( - self, settings: WorkflowSettings, http_client: httpx.Client | None = None - ) -> None: - """ - Workflow Endpoint. - - :param settings: Settings object. - """ - super().__init__(settings, http_client) - - def workflow_execution_post( - self, - input_source: LocalInputSource | URLInputSource, - options: WorkflowOptions, - ): - """ - Sends the document to the workflow. - - :param input_source: The document/source file to use. - Has to be created beforehand. - :param options: Options for the workflow. - :return: - """ - data = {} - - if options.alias: - data["alias"] = options.alias - if options.priority: - data["priority"] = options.priority.value - if options.public_url: - data["public_url"] = options.public_url - - params = {} - if options.full_text: - params["full_text_ocr"] = "true" - if options.rag: - params["rag"] = "true" - post_kwargs: StringDict = {} - files = None - if isinstance(input_source, URLInputSource): - data["document"] = input_source.url - else: - files = {"document": input_source.read_contents(True)} - post_caller: Callable - if self.http_client is None or self.http_client.is_closed: - post_caller = httpx.post - post_kwargs["timeout"] = self.settings.request_timeout - else: - post_caller = self.http_client.post - return post_caller( - self.settings.url_root, - headers=self.settings.base_headers, - data=data, - params=params, - files=files, - **post_kwargs, - ) diff --git a/mindee/v1/mindee_http/workflow_settings.py b/mindee/v1/mindee_http/workflow_settings.py deleted file mode 100644 index 894915b7..00000000 --- a/mindee/v1/mindee_http/workflow_settings.py +++ /dev/null @@ -1,24 +0,0 @@ -from dataclasses import dataclass - -from mindee.v1.error.mindee_api_error import MindeeAPIError -from mindee.v1.mindee_http.base_settings import API_KEY_ENV_NAME, BaseSettings - - -@dataclass -class WorkflowSettings(BaseSettings): - """Settings class relating to workflow requests.""" - - def __init__( - self, - api_key: str | None, - workflow_id: str, - ): - super().__init__(api_key) - if not self.api_key or len(self.api_key) == 0: - raise MindeeAPIError( - f"Missing API key for workflow '{workflow_id}'," - " check your Client configuration.\n" - "You can set this using the " - f"'{API_KEY_ENV_NAME}' environment variable." - ) - self.url_root = f"{self.base_url}/v1/workflows/{workflow_id}/executions" diff --git a/mindee/v1/parsing/common/__init__.py b/mindee/v1/parsing/common/__init__.py index be0d2c60..e15dac78 100644 --- a/mindee/v1/parsing/common/__init__.py +++ b/mindee/v1/parsing/common/__init__.py @@ -2,9 +2,6 @@ from mindee.v1.parsing.common.api_response import ApiResponse from mindee.v1.parsing.common.async_predict_response import AsyncPredictResponse from mindee.v1.parsing.common.document import Document, serialize_for_json -from mindee.v1.parsing.common.execution import Execution -from mindee.v1.parsing.common.execution_file import ExecutionFile -from mindee.v1.parsing.common.execution_priority import ExecutionPriority from mindee.v1.parsing.common.extras import CropperExtra, Extras from mindee.v1.parsing.common.feedback_response import FeedbackResponse from mindee.v1.parsing.common.inference import Inference, TypeInference @@ -15,7 +12,6 @@ from mindee.v1.parsing.common.page import Page from mindee.v1.parsing.common.predict_response import PredictResponse from mindee.v1.parsing.common.prediction import Prediction -from mindee.v1.parsing.common.workflow_response import WorkflowResponse __all__ = [ "OCR", @@ -24,9 +20,6 @@ "AsyncPredictResponse", "CropperExtra", "Document", - "Execution", - "ExecutionFile", - "ExecutionPriority", "Extras", "FeedbackResponse", "Inference", @@ -38,6 +31,5 @@ "Prediction", "RequestStatus", "TypeInference", - "WorkflowResponse", "serialize_for_json", ] diff --git a/mindee/v1/parsing/common/execution.py b/mindee/v1/parsing/common/execution.py deleted file mode 100644 index 1bc3ca19..00000000 --- a/mindee/v1/parsing/common/execution.py +++ /dev/null @@ -1,84 +0,0 @@ -from datetime import datetime -from typing import Generic, Optional - -from mindee.parsing.common.string_dict import StringDict -from mindee.v1.parsing.common.execution_file import ExecutionFile -from mindee.v1.parsing.common.execution_priority import ExecutionPriority -from mindee.v1.parsing.common.inference import Inference -from mindee.v1.parsing.common.page import Page -from mindee.v1.parsing.common.prediction import TypePrediction -from mindee.v1.product.generated import GeneratedV1Document - - -class Execution(Generic[TypePrediction]): - """Workflow execution class.""" - - batch_name: str - """Identifier for the batch to which the execution belongs.""" - - created_at: datetime | None = None - """The time at which the execution started.""" - - file: ExecutionFile - """File representation within a workflow execution.""" - - id: str - """Identifier for the execution.""" - - inference: Inference[TypePrediction, Page[TypePrediction]] | None - """Deserialized inference object.""" - - priority: Optional["ExecutionPriority"] = None - """Priority of the execution.""" - - reviewed_at: datetime | None - """The time at which the file was tagged as reviewed.""" - - available_at: datetime | None - """The time at which the file was uploaded to a workflow.""" - - reviewed_prediction: Optional["GeneratedV1Document"] = None - """Reviewed fields and values.""" - - status: str - """Execution Status.""" - - uploaded_at: datetime | None = None - """The time at which the file was uploaded to a workflow.""" - - workflow_id: str - """Identifier for the workflow.""" - - def __init__(self, inference_type: type[Inference], json_response: StringDict): - self.batch_name = json_response["batch_name"] - self.created_at = self.parse_date(json_response.get("created_at", None)) - self.file = ExecutionFile(json_response["file"]) - self.id = json_response["id"] - self.inference = ( - inference_type(json_response["inference"]) - if json_response["inference"] - else None - ) - self.priority = json_response.get("priority", None) - self.reviewed_at = self.parse_date(json_response.get("reviewed_at", None)) - self.available_at = self.parse_date(json_response.get("available_at", None)) - self.reviewed_prediction = ( - GeneratedV1Document(json_response["reviewed_prediction"]) - if json_response["reviewed_prediction"] - else None - ) - self.status = json_response["status"] - self.type = json_response.get("type", None) - self.uploaded_at = self.parse_date(json_response.get("uploaded_at", None)) - self.workflow_id = json_response["workflow_id"] - - type: str | None - """Execution type.""" - - @staticmethod - def parse_date(date_string: str | None) -> datetime | None: - """Shorthand to parse the date, if present.""" - if not date_string: - return None - date_string = date_string.replace("Z", "+00:00") - return datetime.fromisoformat(date_string) diff --git a/mindee/v1/parsing/common/execution_file.py b/mindee/v1/parsing/common/execution_file.py deleted file mode 100644 index 116a2379..00000000 --- a/mindee/v1/parsing/common/execution_file.py +++ /dev/null @@ -1,15 +0,0 @@ -from mindee.parsing.common.string_dict import StringDict - - -class ExecutionFile: - """Execution File class.""" - - name: str | None - """File name.""" - - alias: str | None - """File name.""" - - def __init__(self, raw_response: StringDict): - self.name = raw_response["name"] - self.alias = raw_response["alias"] diff --git a/mindee/v1/parsing/common/execution_priority.py b/mindee/v1/parsing/common/execution_priority.py deleted file mode 100644 index 9bb2ccca..00000000 --- a/mindee/v1/parsing/common/execution_priority.py +++ /dev/null @@ -1,9 +0,0 @@ -from enum import Enum - - -class ExecutionPriority(Enum): - """Available priorities for workflow executions.""" - - LOW = "low" - MEDIUM = "medium" - HIGH = "high" diff --git a/mindee/v1/parsing/common/workflow_response.py b/mindee/v1/parsing/common/workflow_response.py deleted file mode 100644 index 17e7ac0e..00000000 --- a/mindee/v1/parsing/common/workflow_response.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import Generic - -from mindee.parsing.common.string_dict import StringDict -from mindee.v1.parsing.common.api_response import ApiResponse -from mindee.v1.parsing.common.execution import Execution -from mindee.v1.parsing.common.inference import Inference -from mindee.v1.parsing.common.prediction import TypePrediction - - -class WorkflowResponse(Generic[TypePrediction], ApiResponse): - """Base wrapper for API requests.""" - - execution: Execution - """ - Set the prediction model used to parse the document. - The response object will be instantiated based on this parameter. - """ - - def __init__(self, inference_type: type[Inference], raw_response: StringDict): - super().__init__(raw_response) - self.execution = Execution(inference_type, raw_response["execution"]) diff --git a/tests/v1/workflows/test_workflow.py b/tests/v1/workflows/test_workflow.py deleted file mode 100644 index 31847d8e..00000000 --- a/tests/v1/workflows/test_workflow.py +++ /dev/null @@ -1,81 +0,0 @@ -import json - -import pytest - -from mindee.v1.parsing.common import WorkflowResponse -from mindee.v1.product.generated import GeneratedV1 -from tests.utils import V1_RESOURCE_PATH - -WORKFLOW_DIR = V1_RESOURCE_PATH / "workflows" - - -@pytest.fixture -def success_workflow() -> WorkflowResponse: - file_path = WORKFLOW_DIR / "success.json" - with open(file_path, encoding="utf-8") as open_file: - json_data = json.load(open_file) - return WorkflowResponse(GeneratedV1, json_data) - - -@pytest.fixture -def success_low_priority_workflow() -> WorkflowResponse: - file_path = WORKFLOW_DIR / "success_low_priority.json" - with open(file_path, encoding="utf-8") as open_file: - json_data = json.load(open_file) - return WorkflowResponse(GeneratedV1, json_data) - - -def test_deserialize_workflow(success_workflow: WorkflowResponse): - assert success_workflow is not None - assert success_workflow.api_request is not None - assert success_workflow.execution.batch_name is None - assert success_workflow.execution.created_at is None - assert success_workflow.execution.file.alias is None - assert success_workflow.execution.file.name == "default_sample.jpg" - assert success_workflow.execution.id == "8c75c035-e083-4e77-ba3b-7c3598bd1d8a" - assert success_workflow.execution.inference is None - assert success_workflow.execution.priority == "medium" - assert success_workflow.execution.reviewed_at is None - assert success_workflow.execution.reviewed_prediction is None - assert success_workflow.execution.status == "processing" - assert success_workflow.execution.type == "manual" - assert ( - success_workflow.execution.uploaded_at.strftime("%Y-%m-%dT%H:%M:%S.%f") - == "2024-11-13T13:02:31.699190" - ) - assert ( - success_workflow.execution.workflow_id == "07ebf237-ff27-4eee-b6a2-425df4a5cca6" - ) - - -def test_deserialize_workflow_with_priority_and_alias( - success_low_priority_workflow: WorkflowResponse, -): - assert success_low_priority_workflow is not None - assert success_low_priority_workflow.api_request is not None - assert success_low_priority_workflow.execution.batch_name is None - assert success_low_priority_workflow.execution.created_at is None - assert ( - success_low_priority_workflow.execution.file.alias == "low-priority-sample-test" - ) - assert success_low_priority_workflow.execution.file.name == "default_sample.jpg" - assert ( - success_low_priority_workflow.execution.id - == "b743e123-e18c-4b62-8a07-811a4f72afd3" - ) - assert success_low_priority_workflow.execution.inference is None - assert success_low_priority_workflow.execution.priority == "low" - assert success_low_priority_workflow.execution.reviewed_at is None - assert success_low_priority_workflow.execution.reviewed_prediction is None - assert success_low_priority_workflow.execution.status == "processing" - assert success_low_priority_workflow.execution.type == "manual" - assert ( - success_low_priority_workflow.execution.uploaded_at.strftime( - "%Y-%m-%dT%H:%M:%S.%f" - ) - == "2024-11-13T13:17:01.315179" - ) - assert ( - success_low_priority_workflow.execution.workflow_id - == "07ebf237-ff27-4eee-b6a2-425df4a5cca6" - ) diff --git a/tests/v1/workflows/test_workflow_integration.py b/tests/v1/workflows/test_workflow_integration.py index c5beb415..3ed96aa0 100644 --- a/tests/v1/workflows/test_workflow_integration.py +++ b/tests/v1/workflows/test_workflow_integration.py @@ -1,12 +1,9 @@ import os -from datetime import datetime import pytest from mindee import PathInput from mindee.v1.client import Client -from mindee.v1.client_options.workflow_options import WorkflowOptions -from mindee.v1.parsing.common import ExecutionPriority from mindee.v1.product import FinancialDocumentV1, GeneratedV1 from tests.utils import V1_PRODUCT_PATH @@ -26,21 +23,6 @@ def input_path(): return V1_PRODUCT_PATH / "financial_document" / "default_sample.jpg" -@pytest.mark.integration -def test_workflow_execution(mindee_client: Client, workflow_id: str, input_path: str): - input_source = PathInput(str(input_path)) - current_date_time = datetime.now().strftime("%Y-%m-%d-%H:%M:%S") - alias = f"python-{current_date_time}" - priority = ExecutionPriority.LOW - options = WorkflowOptions(alias=alias, priority=priority, rag=True) - - response = mindee_client.execute_workflow(input_source, workflow_id, options) - - assert response.api_request.status_code == 202 - assert response.execution.file.alias == f"python-{current_date_time}" - assert response.execution.priority == "low" - - @pytest.mark.integration def test_workflow_predict_ots_rag( mindee_client: Client, workflow_id: str, input_path: str