Skip to content

Commit e3637b1

Browse files
committed
✨ add data schema override to v2
1 parent 90109cf commit e3637b1

File tree

8 files changed

+190
-109
lines changed

8 files changed

+190
-109
lines changed

mindee/input/inference_parameters.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,41 @@
1+
import json
12
from dataclasses import dataclass
2-
from typing import List, Optional
3+
from typing import List, Optional, Union
34

45
from mindee.input.polling_options import PollingOptions
56

67

8+
class DataSchema:
9+
"""Modify the Data Schema."""
10+
11+
_replace: Optional[dict] = None
12+
13+
def __init__(self, replace: Optional[dict] = None):
14+
self._replace = replace
15+
16+
@property
17+
def replace(self):
18+
"""If set, completely replaces the data schema of the model."""
19+
return self._replace
20+
21+
@replace.setter
22+
def replace(self, value: Optional[Union[dict, str]]) -> None:
23+
if value is None:
24+
_replace = None
25+
elif isinstance(value, str):
26+
_replace = json.loads(value)
27+
elif isinstance(value, dict):
28+
_replace = value
29+
else:
30+
raise TypeError("Invalid type for data schema")
31+
if _replace is not None and _replace == {}:
32+
raise ValueError("Empty override provided")
33+
self._replace = _replace
34+
35+
def __str__(self) -> str:
36+
return json.dumps({"replace": self.replace})
37+
38+
739
@dataclass
840
class InferenceParameters:
941
"""Inference parameters to set when sending a file."""
@@ -30,4 +62,12 @@ class InferenceParameters:
3062
close_file: bool = True
3163
"""Whether to close the file after parsing."""
3264
text_context: Optional[str] = None
33-
"""Additional text context used by the model during inference. Not recommended, for specific use only."""
65+
"""
66+
Additional text context used by the model during inference.
67+
Not recommended, for specific use only.
68+
"""
69+
data_schema: Optional[DataSchema] = None
70+
"""
71+
Dynamic changes to the data schema of the model for this inference.
72+
Not recommended, for specific use only.
73+
"""

mindee/mindee_http/mindee_api_v2.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ def req_post_inference_enqueue(
9898
data["webhook_ids"] = params.webhook_ids
9999
if params.alias and len(params.alias):
100100
data["alias"] = params.alias
101-
if params.text_context and (params.text_context):
101+
if params.text_context and len(params.text_context):
102102
data["text_context"] = params.text_context
103+
if params.data_schema is not None:
104+
data["data_schema"] = str(params.data_schema)
103105

104106
if isinstance(input_source, LocalInputSource):
105107
files = {"file": input_source.read_contents(params.close_file)}

mindee/parsing/v2/inference_active_options.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
from mindee.parsing.common.string_dict import StringDict
22

33

4+
class DataSchemaActiveOptions:
5+
"""Data schema options activated during the inference."""
6+
7+
replace: bool
8+
9+
def __init__(self, raw_response: StringDict):
10+
self.replace = raw_response["replace"]
11+
12+
def __str__(self) -> str:
13+
return f"Data Schema\n-----------\n:Replace: {self.replace}"
14+
15+
416
class InferenceActiveOptions:
517
"""Active options for the inference."""
618

@@ -29,13 +41,16 @@ class InferenceActiveOptions:
2941
Whether the text context feature was activated.
3042
When this feature is activated, the provided context is used to improve the accuracy of the inference.
3143
"""
44+
data_schema: DataSchemaActiveOptions
45+
"""Data schema options provided for the inference."""
3246

3347
def __init__(self, raw_response: StringDict):
3448
self.raw_text = raw_response["raw_text"]
3549
self.polygon = raw_response["polygon"]
3650
self.confidence = raw_response["confidence"]
3751
self.rag = raw_response["rag"]
3852
self.text_context = raw_response["text_context"]
53+
self.data_schema = DataSchemaActiveOptions(raw_response["data_schema"])
3954

4055
def __str__(self) -> str:
4156
return (
@@ -44,4 +59,5 @@ def __str__(self) -> str:
4459
f"\n:Polygon: {self.polygon}"
4560
f"\n:Confidence: {self.confidence}"
4661
f"\n:RAG: {self.rag}"
62+
f"\n:Text Context: {self.text_context}"
4763
)

tests/v2/input/test_local_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def file_path() -> Path:
1414

1515
def _assert_local_response(local_response):
1616
fake_hmac_signing = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
17-
signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be"
17+
signature = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85"
1818

1919
assert local_response._file is not None
2020
assert not local_response.is_valid_hmac_signature(

0 commit comments

Comments
 (0)