Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions mindee/input/inference_parameters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
import json
from dataclasses import dataclass
from typing import List, Optional
from typing import List, Optional, Union

from mindee.input.polling_options import PollingOptions


class DataSchema:
"""Modify the Data Schema."""

_replace: Optional[dict] = None

def __init__(self, replace: Optional[dict] = None):
self._replace = replace

@property
def replace(self):
"""If set, completely replaces the data schema of the model."""
return self._replace

@replace.setter
def replace(self, value: Optional[Union[dict, str]]) -> None:
if value is None:
_replace = None
elif isinstance(value, str):
_replace = json.loads(value)
elif isinstance(value, dict):
_replace = value
else:
raise TypeError("Invalid type for data schema")
if _replace is not None and _replace == {}:
raise ValueError("Empty override provided")
self._replace = _replace

def __str__(self) -> str:
return json.dumps({"replace": self.replace})


@dataclass
class InferenceParameters:
"""Inference parameters to set when sending a file."""
Expand All @@ -30,4 +62,12 @@ class InferenceParameters:
close_file: bool = True
"""Whether to close the file after parsing."""
text_context: Optional[str] = None
"""Additional text context used by the model during inference. Not recommended, for specific use only."""
"""
Additional text context used by the model during inference.
Not recommended, for specific use only.
"""
data_schema: Optional[DataSchema] = None
"""
Dynamic changes to the data schema of the model for this inference.
Not recommended, for specific use only.
"""
4 changes: 3 additions & 1 deletion mindee/mindee_http/mindee_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ def req_post_inference_enqueue(
data["webhook_ids"] = params.webhook_ids
if params.alias and len(params.alias):
data["alias"] = params.alias
if params.text_context and (params.text_context):
if params.text_context and len(params.text_context):
data["text_context"] = params.text_context
if params.data_schema is not None:
data["data_schema"] = str(params.data_schema)

if isinstance(input_source, LocalInputSource):
files = {"file": input_source.read_contents(params.close_file)}
Expand Down
16 changes: 16 additions & 0 deletions mindee/parsing/v2/inference_active_options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from mindee.parsing.common.string_dict import StringDict


class DataSchemaActiveOptions:
"""Data schema options activated during the inference."""

replace: bool

def __init__(self, raw_response: StringDict):
self.replace = raw_response["replace"]

def __str__(self) -> str:
return f"Data Schema\n-----------\n:Replace: {self.replace}"


class InferenceActiveOptions:
"""Active options for the inference."""

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

def __init__(self, raw_response: StringDict):
self.raw_text = raw_response["raw_text"]
self.polygon = raw_response["polygon"]
self.confidence = raw_response["confidence"]
self.rag = raw_response["rag"]
self.text_context = raw_response["text_context"]
self.data_schema = DataSchemaActiveOptions(raw_response["data_schema"])

def __str__(self) -> str:
return (
Expand All @@ -44,4 +59,5 @@ def __str__(self) -> str:
f"\n:Polygon: {self.polygon}"
f"\n:Confidence: {self.confidence}"
f"\n:RAG: {self.rag}"
f"\n:Text Context: {self.text_context}"
)
2 changes: 1 addition & 1 deletion tests/v2/input/test_local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def file_path() -> Path:

def _assert_local_response(local_response):
fake_hmac_signing = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
signature = "b82a515c832fd2c4f4ce3a7e6f53c12e8d10e19223f6cf0e3a9809a7a3da26be"
signature = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85"

assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
Expand Down
Loading