|
| 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 | +) |
0 commit comments