Skip to content

Commit 941b7b4

Browse files
committed
✨ add support for material certificate v1
1 parent c5316f8 commit 941b7b4

File tree

9 files changed

+146
-1
lines changed

9 files changed

+146
-1
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from mindee import Client, documents
2+
3+
# Init a new client
4+
mindee_client = Client(api_key="my-api-key")
5+
6+
# Load a file from disk
7+
input_doc = mindee_client.doc_from_path("/path/to/the/file.ext")
8+
9+
# Parse the Material Certificate by passing the appropriate type
10+
result = input_doc.parse(documents.TypeMaterialCertificateV1)
11+
12+
# Print a brief summary of the parsed data
13+
print(result.document)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Material Certificate V1
2+
-----------------------
3+
4+
**Sample Code:**
5+
6+
.. literalinclude:: /extras/code_samples/material_certificate_v1.txt
7+
:language: Python
8+
9+
.. autoclass:: mindee.documents.MaterialCertificateV1
10+
:members:

docs/predictions/standard/international.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ International
1212
.. include:: ./documents/invoice_splitter_v1.rst
1313
.. include:: ./documents/proof_of_address_v1.rst
1414
.. include:: ./documents/cropper_v1.rst
15+
.. include:: ./documents/material_certificate.rst

mindee/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ def _init_default_endpoints(self) -> None:
455455
url_name="invoice_splitter",
456456
version="1",
457457
),
458+
ConfigSpec(
459+
doc_class=documents.MaterialCertificateV1,
460+
url_name="material_certificate",
461+
version="1",
462+
),
458463
]
459464

460465
for config in configs:

mindee/documents/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
)
1010
from mindee.documents.invoice import InvoiceV3, InvoiceV4, TypeInvoiceV3, TypeInvoiceV4
1111
from mindee.documents.invoice_splitter import InvoiceSplitterV1, TypeInvoiceSplitterV1
12+
from mindee.documents.material_certificate import (
13+
MaterialCertificateV1,
14+
TypeMaterialCertificateV1,
15+
)
1216
from mindee.documents.passport import PassportV1, TypePassportV1
1317
from mindee.documents.proof_of_address import ProofOfAddressV1, TypeProofOfAddressV1
1418
from mindee.documents.receipt import (
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .material_certificate_v1 import MaterialCertificateV1, TypeMaterialCertificateV1
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from typing import Optional, TypeVar
2+
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
4+
from mindee.fields.text import TextField
5+
6+
7+
class MaterialCertificateV1(Document):
8+
"""Material Certificate v1 prediction results."""
9+
10+
certificate_type: TextField
11+
"""Material Type field is the type of material used in the product, such as metal, plastic, or wood."""
12+
heat_number: TextField
13+
"""Heat Number is a unique identifier assigned to a batch of material produced in a manufacturing process."""
14+
norm: TextField
15+
"""Material Grade field is the designation of the material's chemical and physical properties."""
16+
17+
def __init__(
18+
self,
19+
api_prediction=None,
20+
input_source=None,
21+
page_n: Optional[int] = None,
22+
):
23+
"""
24+
Material Certificate v1 prediction results.
25+
26+
:param api_prediction: Raw prediction from HTTP response
27+
:param input_source: Input object
28+
:param page_n: Page number for multi pages pdf input
29+
"""
30+
super().__init__(
31+
input_source=input_source,
32+
document_type="material_certificate",
33+
api_prediction=api_prediction,
34+
page_n=page_n,
35+
)
36+
self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n)
37+
38+
def _build_from_api_prediction(
39+
self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None
40+
) -> None:
41+
"""
42+
Build the object from the prediction API JSON.
43+
44+
:param api_prediction: Raw prediction from HTTP response
45+
:param page_n: Page number
46+
"""
47+
self.certificate_type = TextField(
48+
api_prediction["certificate_type"],
49+
page_id=page_n,
50+
)
51+
self.heat_number = TextField(
52+
api_prediction["heat_number"],
53+
page_id=page_n,
54+
)
55+
self.norm = TextField(
56+
api_prediction["norm"],
57+
page_id=page_n,
58+
)
59+
60+
def __str__(self) -> str:
61+
return clean_out_string(
62+
"Material Certificate V1 Prediction\n"
63+
"==================================\n"
64+
f":Filename: {self.filename or ''}\n"
65+
f":Material Type: {self.certificate_type}\n"
66+
f":Material Grade: {self.norm}\n"
67+
f":Heat Number: {self.heat_number}\n"
68+
)
69+
70+
71+
TypeMaterialCertificateV1 = TypeVar(
72+
"TypeMaterialCertificateV1", bound=MaterialCertificateV1
73+
)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
3+
import pytest
4+
5+
from mindee.documents import MaterialCertificateV1
6+
7+
MATERIAL_CERTIFICATE_DATA_DIR = "./tests/data/material_certificate"
8+
9+
FILE_PATH_MATERIAL_CERTIFICATE_V1_COMPLETE = (
10+
f"{ MATERIAL_CERTIFICATE_DATA_DIR }/response_v1/complete.json"
11+
)
12+
FILE_PATH_MATERIAL_CERTIFICATE_V1_EMPTY = (
13+
f"{ MATERIAL_CERTIFICATE_DATA_DIR }/response_v1/empty.json"
14+
)
15+
16+
17+
@pytest.fixture
18+
def material_certificate_v1_doc() -> MaterialCertificateV1:
19+
json_data = json.load(open(FILE_PATH_MATERIAL_CERTIFICATE_V1_COMPLETE))
20+
return MaterialCertificateV1(json_data["document"]["inference"], page_n=None)
21+
22+
23+
@pytest.fixture
24+
def material_certificate_v1_doc_empty() -> MaterialCertificateV1:
25+
json_data = json.load(open(FILE_PATH_MATERIAL_CERTIFICATE_V1_EMPTY))
26+
return MaterialCertificateV1(json_data["document"]["inference"], page_n=None)
27+
28+
29+
def test_empty_doc_constructor(material_certificate_v1_doc_empty):
30+
assert material_certificate_v1_doc_empty.certificate_type.value is None
31+
assert material_certificate_v1_doc_empty.norm.value is None
32+
assert material_certificate_v1_doc_empty.heat_number.value is None
33+
34+
35+
def test_doc_constructor(material_certificate_v1_doc):
36+
file_path = f"{ MATERIAL_CERTIFICATE_DATA_DIR }/response_v1/doc_to_string.rst"
37+
reference_str = open(file_path, "r", encoding="utf-8").read()
38+
assert str(material_certificate_v1_doc) == reference_str

0 commit comments

Comments
 (0)