Skip to content

Commit 6c97e7a

Browse files
revise entire endpoint slug generation system & refactor input parameters
1 parent 30a6a75 commit 6c97e7a

26 files changed

+156
-153
lines changed

mindee/client_v2.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616
is_valid_post_response,
1717
)
1818
from mindee.parsing.v2.common_response import CommonStatus
19-
from mindee.v2 import BaseInferenceResponse
19+
from mindee.v2.parsing.inference.base_response import BaseResponse
2020
from mindee.parsing.v2.inference_response import InferenceResponse
2121
from mindee.parsing.v2.job_response import JobResponse
2222

23-
TypeBaseInferenceResponse = TypeVar(
24-
"TypeBaseInferenceResponse", bound=BaseInferenceResponse
25-
)
23+
TypeBaseInferenceResponse = TypeVar("TypeBaseInferenceResponse", bound=BaseResponse)
2624

2725

2826
class ClientV2(ClientMixin):
@@ -48,7 +46,6 @@ def enqueue_inference(
4846
self,
4947
input_source: Union[LocalInputSource, UrlInputSource],
5048
params: BaseParameters,
51-
slug: Optional[str] = None,
5249
disable_redundant_warnings: bool = False,
5350
) -> JobResponse:
5451
"""[Deprecated] Use `enqueue` instead."""
@@ -58,13 +55,12 @@ def enqueue_inference(
5855
DeprecationWarning,
5956
stacklevel=2,
6057
)
61-
return self.enqueue(input_source, params, slug)
58+
return self.enqueue(input_source, params)
6259

6360
def enqueue(
6461
self,
6562
input_source: Union[LocalInputSource, UrlInputSource],
6663
params: BaseParameters,
67-
slug: Optional[str] = None,
6864
) -> JobResponse:
6965
"""
7066
Enqueues a document to a given model.
@@ -77,7 +73,7 @@ def enqueue(
7773
"""
7874
logger.debug("Enqueuing inference using model: %s", params.model_id)
7975
response = self.mindee_api.req_post_inference_enqueue(
80-
input_source=input_source, params=params, slug=slug
76+
input_source=input_source, params=params, slug=params.get_enqueue_slug()
8177
)
8278
dict_response = response.json()
8379

@@ -105,9 +101,9 @@ def get_job(self, job_id: str) -> JobResponse:
105101
def get_inference(
106102
self,
107103
inference_id: str,
108-
response_type: Type[BaseInferenceResponse] = InferenceResponse,
104+
response_type: Type[BaseResponse] = InferenceResponse,
109105
disable_redundant_warnings: bool = False,
110-
) -> BaseInferenceResponse:
106+
) -> BaseResponse:
111107
"""[Deprecated] Use `get_result` instead."""
112108
if not disable_redundant_warnings:
113109
warnings.warn(
@@ -120,8 +116,8 @@ def get_inference(
120116
def get_result(
121117
self,
122118
inference_id: str,
123-
response_type: Type[BaseInferenceResponse] = InferenceResponse,
124-
) -> BaseInferenceResponse:
119+
response_type: Type[BaseResponse] = InferenceResponse,
120+
) -> BaseResponse:
125121
"""
126122
Get the result of an inference that was previously enqueued.
127123
@@ -132,11 +128,10 @@ def get_result(
132128
:return: An inference response.
133129
"""
134130
logger.debug("Fetching inference: %s", inference_id)
135-
slug = None
136-
if response_type and response_type is not InferenceResponse:
137-
slug = "utilities/" + response_type.get_inference_slug()
138131

139-
response = self.mindee_api.req_get_inference(inference_id, slug)
132+
response = self.mindee_api.req_get_inference(
133+
inference_id, response_type.get_result_slug()
134+
)
140135
if not is_valid_get_response(response):
141136
handle_error_v2(response.json())
142137
dict_response = response.json()
@@ -146,8 +141,8 @@ def _enqueue_and_get(
146141
self,
147142
input_source: Union[LocalInputSource, UrlInputSource],
148143
params: BaseParameters,
149-
response_type: Optional[Type[BaseInferenceResponse]] = InferenceResponse,
150-
) -> BaseInferenceResponse:
144+
response_type: Optional[Type[BaseResponse]] = InferenceResponse,
145+
) -> BaseResponse:
151146
"""
152147
Enqueues to an asynchronous endpoint and automatically polls for a response.
153148
@@ -164,10 +159,7 @@ def _enqueue_and_get(
164159
params.polling_options.delay_sec,
165160
params.polling_options.max_retries,
166161
)
167-
slug = None
168-
if response_type and response_type is not InferenceResponse:
169-
slug = "utilities/" + response_type.get_inference_slug()
170-
enqueue_response = self.enqueue_inference(input_source, params, slug, True)
162+
enqueue_response = self.enqueue_inference(input_source, params, True)
171163
logger.debug(
172164
"Successfully enqueued document with job id: %s", enqueue_response.job.id
173165
)

mindee/input/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from mindee.input.local_response import LocalResponse
22
from mindee.input.base_parameters import BaseParameters
33
from mindee.input.inference_parameters import InferenceParameters
4-
from mindee.v2.parsing.inference.split.split_parameters import SplitParameters
4+
from mindee.v2.parsing.inference.utilities.split.split_parameters import SplitParameters
55
from mindee.input.page_options import PageOptions
66
from mindee.input.polling_options import PollingOptions
77
from mindee.input.sources.base_64_input import Base64Input

mindee/input/base_parameters.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from abc import ABC
2-
from dataclasses import dataclass
2+
from dataclasses import dataclass, field
33
from typing import Dict, Optional, List, Union
44

55
from mindee.input.polling_options import PollingOptions
@@ -9,6 +9,9 @@
99
class BaseParameters(ABC):
1010
"""Base class for parameters accepted by all V2 endpoints."""
1111

12+
_slug: str = field(init=False)
13+
"""Slug of the endpoint."""
14+
1215
model_id: str
1316
"""ID of the model, required."""
1417
alias: Optional[str] = None
@@ -34,3 +37,8 @@ def get_config(self) -> Dict[str, Union[str, List[str]]]:
3437
if self.webhook_ids and len(self.webhook_ids) > 0:
3538
data["webhook_ids"] = self.webhook_ids
3639
return data
40+
41+
@classmethod
42+
def get_enqueue_slug(cls) -> str:
43+
"""Getter for the enqueue slug."""
44+
return cls._slug

mindee/input/inference_parameters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from dataclasses import dataclass, asdict
2+
from dataclasses import dataclass, asdict, field
33
from typing import Dict, List, Optional, Union
44

55
from mindee.input.base_parameters import BaseParameters
@@ -81,6 +81,9 @@ def __post_init__(self) -> None:
8181
class InferenceParameters(BaseParameters):
8282
"""Inference parameters to set when sending a file."""
8383

84+
_slug: str = field(init=False, default="inferences")
85+
"""Slug of the endpoint."""
86+
8487
rag: Optional[bool] = None
8588
"""Enhance extraction accuracy with Retrieval-Augmented Generation."""
8689
raw_text: Optional[bool] = None

mindee/mindee_http/mindee_api_v2.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def req_post_inference_enqueue(
7474
self,
7575
input_source: Union[LocalInputSource, UrlInputSource],
7676
params: BaseParameters,
77-
slug: Optional[str] = None,
77+
slug: str,
7878
) -> requests.Response:
7979
"""
8080
Make an asynchronous request to POST a document for prediction on the V2 API.
@@ -84,8 +84,6 @@ def req_post_inference_enqueue(
8484
:param slug: Slug to use for the enqueueing, defaults to 'inferences'.
8585
:return: requests response.
8686
"""
87-
if not slug:
88-
slug = "inferences"
8987
data = params.get_config()
9088
url = f"{self.url_root}/{slug}/enqueue"
9189

@@ -123,20 +121,15 @@ def req_get_job(self, job_id: str) -> requests.Response:
123121
allow_redirects=False,
124122
)
125123

126-
def req_get_inference(
127-
self, inference_id: str, slug: Optional[str]
128-
) -> requests.Response:
124+
def req_get_inference(self, inference_id: str, slug: str) -> requests.Response:
129125
"""
130126
Sends a request matching a given queue_id. Returns either a Job or a Document.
131127
132128
:param inference_id: Inference ID, returned by the job request.
133129
:param slug: Slug of the inference, defaults to nothing.
134130
"""
135131

136-
if not slug:
137-
url = f"{self.url_root}/inferences/{inference_id}"
138-
else:
139-
url = f"{self.url_root}/{slug}/{inference_id}"
132+
url = f"{self.url_root}/{slug}/{inference_id}"
140133
return requests.get(
141134
url,
142135
headers=self.base_headers,

mindee/parsing/v2/inference.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ class Inference(BaseInference):
1111
"""Result of the inference."""
1212
active_options: InferenceActiveOptions
1313
"""Active options for the inference."""
14-
_slug: str = "inferences"
15-
"""Slug of the inference."""
1614

1715
def __init__(self, raw_response: StringDict):
1816
super().__init__(raw_response)
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from mindee.parsing.common.string_dict import StringDict
22
from mindee.parsing.v2.inference import Inference
3-
from mindee.v2.parsing.inference.base_inference_response import (
4-
BaseInferenceResponse,
3+
from mindee.v2.parsing.inference.base_response import (
4+
BaseResponse,
55
)
66

77

8-
class InferenceResponse(BaseInferenceResponse[Inference]):
8+
class InferenceResponse(BaseResponse):
99
"""Represent an inference response from Mindee V2 API."""
1010

1111
inference: Inference
1212
"""Inference result."""
13-
inference_type = Inference
13+
_slug: str = "inferences"
14+
"""Slug of the inference."""
1415

1516
def __init__(self, raw_response: StringDict) -> None:
1617
super().__init__(raw_response)
@@ -19,5 +20,7 @@ def __init__(self, raw_response: StringDict) -> None:
1920
def __str__(self) -> str:
2021
return str(self.inference)
2122

22-
def _set_inference_type(self, inference_response: StringDict):
23-
return Inference
23+
@classmethod
24+
def get_result_slug(cls) -> str:
25+
"""Getter for the inference slug."""
26+
return cls._slug

mindee/v2/__init__.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
from mindee.v2.parsing import SplitParameters
2-
from mindee.v2.parsing.inference.base_inference import BaseInference
3-
from mindee.v2.parsing.inference.base_inference_response import (
4-
BaseInferenceResponse,
5-
TypeInferenceResponse,
6-
)
7-
from mindee.v2.parsing.inference.split.split_response import SplitResponse
2+
3+
from mindee.v2.parsing.inference.utilities.split.split_response import SplitResponse
84

95
__all__ = [
10-
"BaseInference",
11-
"BaseInferenceResponse",
12-
"TypeInferenceResponse",
136
"SplitResponse",
147
"SplitParameters",
158
]

mindee/v2/parsing/__init__.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
from mindee.v2.parsing.inference import SplitParameters
2-
from mindee.v2.parsing.inference.base_inference import BaseInference
3-
from mindee.v2.parsing.inference.base_inference_response import (
4-
BaseInferenceResponse,
5-
TypeInferenceResponse,
6-
)
7-
from mindee.v2.parsing.inference.split.split_response import SplitResponse
2+
from mindee.v2.parsing.inference.utilities.split.split_response import SplitResponse
83

94
__all__ = [
10-
"BaseInference",
11-
"BaseInferenceResponse",
12-
"TypeInferenceResponse",
135
"SplitResponse",
146
"SplitParameters",
157
]

mindee/v2/parsing/inference/__init__.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
from mindee.v2.parsing.inference.base_inference import BaseInference
2-
from mindee.v2.parsing.inference.base_inference_response import (
3-
BaseInferenceResponse,
4-
TypeInferenceResponse,
2+
from mindee.v2.parsing.inference.base_response import (
3+
BaseResponse,
54
)
6-
from mindee.v2.parsing.inference.split.split import Split
7-
from mindee.v2.parsing.inference.split.split_inference import SplitInference
8-
from mindee.v2.parsing.inference.split.split_parameters import SplitParameters
9-
from mindee.v2.parsing.inference.split.split_response import SplitResponse
10-
from mindee.v2.parsing.inference.split.split_result import SplitResult
5+
from mindee.v2.parsing.inference.utilities.split.split_split import SplitSplit
6+
from mindee.v2.parsing.inference.utilities.split import SplitInference
7+
from mindee.v2.parsing.inference.utilities.split.split_parameters import SplitParameters
8+
from mindee.v2.parsing.inference.utilities.split.split_response import SplitResponse
9+
from mindee.v2.parsing.inference.utilities.split.split_result import SplitResult
1110

1211
__all__ = [
1312
"BaseInference",
14-
"BaseInferenceResponse",
15-
"TypeInferenceResponse",
16-
"Split",
13+
"BaseResponse",
14+
"SplitSplit",
1715
"SplitInference",
1816
"SplitParameters",
1917
"SplitResponse",

0 commit comments

Comments
 (0)