|
| 1 | +--- |
| 2 | +title: Generated API Python |
| 3 | +--- |
| 4 | + |
| 5 | +The Python OCR SDK supports generated APIs. |
| 6 | +Generated APIs can theoretically support all APIs in a catch-all generic format. |
| 7 | + |
| 8 | +# Quick-Start |
| 9 | + |
| 10 | +```python |
| 11 | +from mindee import Client, product |
| 12 | + |
| 13 | +# Init a new client |
| 14 | +mindee_client = Client(api_key="my-api-key") |
| 15 | + |
| 16 | +# Add your custom endpoint (document) |
| 17 | +my_endpoint = mindee_client.create_endpoint( |
| 18 | + account_name="my-account", |
| 19 | + endpoint_name="my-endpoint", |
| 20 | + version="my-version" # Note: version should be always provided when using for OTS products |
| 21 | +) |
| 22 | + |
| 23 | +# Load a file from disk |
| 24 | +input_doc = mindee_client.source_from_path("/path/to/the/file.ext") |
| 25 | + |
| 26 | +# Parse the file. |
| 27 | +# The endpoint must be specified since it cannot be determined from the class. |
| 28 | +result = mindee_client.parse( |
| 29 | + product.GeneratedV1, |
| 30 | + input_doc, |
| 31 | + endpoint=my_endpoint |
| 32 | +) |
| 33 | + |
| 34 | +# Print a brief summary of the parsed data |
| 35 | +print(result.document) |
| 36 | + |
| 37 | +# Iterate over all the fields in the document |
| 38 | +for field_name, field_values in result.document.fields.items(): |
| 39 | + print(field_name, "=", field_values) |
| 40 | +``` |
| 41 | + |
| 42 | +# Generated Endpoints |
| 43 | + |
| 44 | +You may have noticed in the previous step that in order to access a custom build, you will need to provide an account and an endpoint name at the very least. |
| 45 | + |
| 46 | +Although it is optional, the version number should match the latest version of your build in most use-cases. |
| 47 | +If it is not set, it will default to "1". |
| 48 | + |
| 49 | +# Field Types |
| 50 | + |
| 51 | +## Generated Fields |
| 52 | + |
| 53 | +### Generated List Field |
| 54 | + |
| 55 | +A `GeneratedListField` is a special type of custom list that implements the following: |
| 56 | + |
| 57 | +- **values** (`List[Union[StringField`[GeneratedObjectField](#Generated-object-field)`]]`): the confidence score of the field prediction. |
| 58 | +- **page_id** (`int`): only available for some documents ATM. |
| 59 | + |
| 60 | +Since the inner contents can vary, the value isn't accessed through a property, but rather through the following functions: |
| 61 | + |
| 62 | +- **contents_list()** (`-> List[Union[str, float]]`): returns a list of values for each element. |
| 63 | +- **contents_string(separator=" ")** (`-> str`): returns a list of concatenated values, with an optional **separator** `str` between them. |
| 64 | +- ****str**()**: returns a string representation of all values, appropriate spacing. |
| 65 | + |
| 66 | +#### Generated Object Field |
| 67 | + |
| 68 | +Unrecognized structures and sometimes values of `ListField`s are stored in a `GeneratedObjectField` structure, which is implemented dynamically depending on the object's structure. |
| 69 | + |
| 70 | +No matter what, the fields will be stored in a dictionary-like structure with a `key: value` pair where `key` is a string and `value` is a nullable string. The object also contains: |
| 71 | + |
| 72 | +- **page_id** (`Optional[int]`): the ID of the page, is `None` when at document-level. |
| 73 | + |
| 74 | +# Attributes |
| 75 | + |
| 76 | +Generated builds always have access to at least two attributes: |
| 77 | + |
| 78 | +## Fields |
| 79 | + |
| 80 | +**fields** (`Dict[str`: `List[Union[`[GeneratedListField](#generated-list-field)[GeneratedObjectField](#generated-object-field), `StringField]]`): |
| 81 | + |
| 82 | +```python |
| 83 | +print(str(result.document.inference.prediction.fields["my-field"])) |
| 84 | +``` |
| 85 | + |
| 86 | +# Questions? |
| 87 | + |
| 88 | +[Join our Slack](https://join.slack.com/t/mindee-community/shared_invite/zt-1jv6nawjq-FDgFcF2T5CmMmRpl9LLptw) |
0 commit comments