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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
]

- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.2
rev: v8.18.4
hooks:
- id: gitleaks

Expand Down
17 changes: 14 additions & 3 deletions mindee/parsing/v2/inference_options.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
from typing import List, Optional
from typing import List

from mindee.parsing.common.string_dict import StringDict


class RawText:
"""Raw text extracted from the document."""

page: int
content: str

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


class InferenceOptions:
"""Optional information about the document."""

raw_text: Optional[List[str]]
raw_texts: List[RawText]

def __init__(self, raw_response: StringDict):
self.raw_text = raw_response["raw_text"] if "raw_text" in raw_response else None
self.raw_texts = [RawText(raw_text) for raw_text in raw_response["raw_texts"]]
Empty file.
Empty file.
Empty file.
135 changes: 51 additions & 84 deletions tests/v2/test_inference_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

from mindee import ClientV2, LocalResponse
from mindee.parsing.common.string_dict import StringDict
from mindee.parsing.v2 import (
Inference,
InferenceFile,
Expand All @@ -17,90 +16,30 @@


@pytest.fixture
def inference_result_json() -> StringDict:
return {
"inference": {
"model": {"id": "test-model-id"},
"file": {"name": "test-file-name.jpg", "alias": None},
"result": {
"fields": {
"field_simple": {"value": "value_1"},
"field_object": {
"fields": {
"sub_object_simple": {"value": "value_2"},
"sub_object_list": {
"items": [
{
"fields": {
"sub_object_list_sub_list_simple": {
"value": "value_3"
}
}
},
{
"fields": {
"sub_object_list_sub_list_object_subobject_1": {
"value": "value_4"
},
"sub_object_list_sub_list_object_subobject_2": {
"value": "value_5"
},
}
},
]
},
"sub_object_object": {
"fields": {
"sub_object_object_sub_object_simple": {
"value": "value_6"
},
"sub_object_object_sub_object_object": {
"fields": {
"sub_object_object_sub_object_object_simple_1": {
"value": "value_7"
},
"sub_object_object_sub_object_object_simple_2": {
"value": "value_8"
},
}
},
"sub_object_object_sub_object_list": {
"items": [
{
"fields": {
"sub_object_object_sub_object_list_simple": {
"value": "value_9"
},
"sub_object_object_sub_object_list_object": {
"fields": {
"sub_object_object_sub_object_list_object_subobject_1": {
"value": "value_10"
},
"sub_object_object_sub_object_list_object_subobject_2": {
"value": "value_11"
},
}
},
}
}
]
},
}
},
}
},
},
"options": {
"raw_text": ["toto", "tata", "titi"],
},
},
}
}
def deep_nested_fields() -> dict:
with (V2_DATA_DIR / "inference/deep_nested_fields.json").open(
"r", encoding="utf-8"
) as fh:
return json.load(fh)


@pytest.fixture
def standard_field_types() -> dict:
with (V2_DATA_DIR / "inference/standard_field_types.json").open(
"r", encoding="utf-8"
) as fh:
return json.load(fh)


@pytest.fixture
def raw_texts() -> dict:
with (V2_DATA_DIR / "inference/raw_texts.json").open("r", encoding="utf-8") as fh:
return json.load(fh)


@pytest.mark.v2
def test_inference_response(inference_result_json):
inference_result = InferenceResponse(inference_result_json)
def test_deep_nested_fields(deep_nested_fields):
inference_result = InferenceResponse(deep_nested_fields)
assert isinstance(inference_result.inference, Inference)
assert isinstance(
inference_result.inference.result.fields.field_simple, SimpleField
Expand Down Expand Up @@ -166,9 +105,37 @@ def test_inference_response(inference_result_json):
== "value_9"
)


@pytest.mark.v2
def test_deep_nested_fields(standard_field_types):
inference_result = InferenceResponse(standard_field_types)
assert isinstance(inference_result.inference, Inference)
assert isinstance(
inference_result.inference.result.fields.field_simple, SimpleField
)
assert isinstance(
inference_result.inference.result.fields.field_object, ObjectField
)
assert isinstance(
inference_result.inference.result.fields.field_simple_list, ListField
)
assert isinstance(
inference_result.inference.result.fields.field_object_list, ListField
)


@pytest.mark.v2
def test_raw_texts(raw_texts):
inference_result = InferenceResponse(raw_texts)
assert isinstance(inference_result.inference, Inference)

assert inference_result.inference.result.options
assert len(inference_result.inference.result.options.raw_text) == 3
assert inference_result.inference.result.options.raw_text[0] == "toto"
assert len(inference_result.inference.result.options.raw_texts) == 2
assert inference_result.inference.result.options.raw_texts[0].page == 0
assert (
inference_result.inference.result.options.raw_texts[0].content
== "This is the raw text of the first page..."
)


@pytest.mark.v2
Expand Down