-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mindee
committed
Dec 26, 2024
1 parent
30d2b9e
commit 9318c94
Showing
17 changed files
with
478 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from mindee import Client, product, AsyncPredictResponse | ||
|
||
# Init a new client | ||
mindee_client = Client(api_key="my-api-key") | ||
|
||
# Load a file from disk | ||
input_doc = mindee_client.source_from_path("/path/to/the/file.ext") | ||
|
||
# Load a file from disk and enqueue it. | ||
result: AsyncPredictResponse = mindee_client.enqueue_and_parse( | ||
product.us.UsMailV3, | ||
input_doc, | ||
) | ||
|
||
# Print a brief summary of the parsed data | ||
print(result.document) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
US Mail V3 | ||
---------- | ||
|
||
**Sample Code:** | ||
|
||
.. literalinclude:: /extras/code_samples/us_mail_v3_async.txt | ||
:language: Python | ||
|
||
.. autoclass:: mindee.product.us.us_mail.us_mail_v3.UsMailV3 | ||
:members: | ||
:inherited-members: | ||
|
||
.. autoclass:: mindee.product.us.us_mail.us_mail_v3_document.UsMailV3Document | ||
:members: | ||
:inherited-members: | ||
|
||
.. autoclass:: mindee.product.us.us_mail.us_mail_v3_sender_address.UsMailV3SenderAddress | ||
:members: | ||
:inherited-members: | ||
|
||
.. autoclass:: mindee.product.us.us_mail.us_mail_v3_recipient_address.UsMailV3RecipientAddress | ||
:members: | ||
:inherited-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import List | ||
|
||
from mindee.parsing.common.inference import Inference | ||
from mindee.parsing.common.page import Page | ||
from mindee.parsing.common.string_dict import StringDict | ||
from mindee.product.us.us_mail.us_mail_v3_document import ( | ||
UsMailV3Document, | ||
) | ||
|
||
|
||
class UsMailV3(Inference): | ||
"""US Mail API version 3 inference prediction.""" | ||
|
||
prediction: UsMailV3Document | ||
"""Document-level prediction.""" | ||
pages: List[Page[UsMailV3Document]] | ||
"""Page-level prediction(s).""" | ||
endpoint_name = "us_mail" | ||
"""Name of the endpoint.""" | ||
endpoint_version = "3" | ||
"""Version of the endpoint.""" | ||
|
||
def __init__(self, raw_prediction: StringDict): | ||
""" | ||
US Mail v3 inference. | ||
:param raw_prediction: Raw prediction from the HTTP response. | ||
""" | ||
super().__init__(raw_prediction) | ||
|
||
self.prediction = UsMailV3Document(raw_prediction["prediction"]) | ||
self.pages = [] | ||
for page in raw_prediction["pages"]: | ||
try: | ||
page_prediction = page["prediction"] | ||
except KeyError: | ||
continue | ||
if page_prediction: | ||
self.pages.append(Page(UsMailV3Document, page)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
from typing import List, Optional | ||
|
||
from mindee.parsing.common.prediction import Prediction | ||
from mindee.parsing.common.string_dict import StringDict | ||
from mindee.parsing.common.summary_helper import clean_out_string | ||
from mindee.parsing.standard.boolean import BooleanField | ||
from mindee.parsing.standard.text import StringField | ||
from mindee.product.us.us_mail.us_mail_v3_recipient_address import ( | ||
UsMailV3RecipientAddress, | ||
) | ||
from mindee.product.us.us_mail.us_mail_v3_sender_address import UsMailV3SenderAddress | ||
|
||
|
||
class UsMailV3Document(Prediction): | ||
"""US Mail API version 3.0 document data.""" | ||
|
||
is_return_to_sender: BooleanField | ||
"""Whether the mailing is marked as return to sender.""" | ||
recipient_addresses: List[UsMailV3RecipientAddress] | ||
"""The addresses of the recipients.""" | ||
recipient_names: List[StringField] | ||
"""The names of the recipients.""" | ||
sender_address: UsMailV3SenderAddress | ||
"""The address of the sender.""" | ||
sender_name: StringField | ||
"""The name of the sender.""" | ||
|
||
def __init__( | ||
self, | ||
raw_prediction: StringDict, | ||
page_id: Optional[int] = None, | ||
): | ||
""" | ||
US Mail document. | ||
:param raw_prediction: Raw prediction from HTTP response | ||
:param page_id: Page number for multi pages pdf input | ||
""" | ||
super().__init__(raw_prediction, page_id) | ||
self.is_return_to_sender = BooleanField( | ||
raw_prediction["is_return_to_sender"], | ||
page_id=page_id, | ||
) | ||
self.recipient_addresses = [ | ||
UsMailV3RecipientAddress(prediction, page_id=page_id) | ||
for prediction in raw_prediction["recipient_addresses"] | ||
] | ||
self.recipient_names = [ | ||
StringField(prediction, page_id=page_id) | ||
for prediction in raw_prediction["recipient_names"] | ||
] | ||
self.sender_address = UsMailV3SenderAddress( | ||
raw_prediction["sender_address"], | ||
page_id=page_id, | ||
) | ||
self.sender_name = StringField( | ||
raw_prediction["sender_name"], | ||
page_id=page_id, | ||
) | ||
|
||
@staticmethod | ||
def _recipient_addresses_separator(char: str) -> str: | ||
out_str = " " | ||
out_str += f"+{char * 17}" | ||
out_str += f"+{char * 37}" | ||
out_str += f"+{char * 19}" | ||
out_str += f"+{char * 13}" | ||
out_str += f"+{char * 24}" | ||
out_str += f"+{char * 7}" | ||
out_str += f"+{char * 27}" | ||
out_str += f"+{char * 17}" | ||
return out_str + "+" | ||
|
||
def _recipient_addresses_to_str(self) -> str: | ||
if not self.recipient_addresses: | ||
return "" | ||
|
||
lines = f"\n{self._recipient_addresses_separator('-')}\n ".join( | ||
[item.to_table_line() for item in self.recipient_addresses] | ||
) | ||
out_str = "" | ||
out_str += f"\n{self._recipient_addresses_separator('-')}\n " | ||
out_str += " | City " | ||
out_str += " | Complete Address " | ||
out_str += " | Is Address Change" | ||
out_str += " | Postal Code" | ||
out_str += " | Private Mailbox Number" | ||
out_str += " | State" | ||
out_str += " | Street " | ||
out_str += " | Unit " | ||
out_str += f" |\n{self._recipient_addresses_separator('=')}" | ||
out_str += f"\n {lines}" | ||
out_str += f"\n{self._recipient_addresses_separator('-')}" | ||
return out_str | ||
|
||
def __str__(self) -> str: | ||
recipient_names = f"\n { ' ' * 17 }".join( | ||
[str(item) for item in self.recipient_names], | ||
) | ||
out_str: str = f":Sender Name: {self.sender_name}\n" | ||
out_str += f":Sender Address:\n{self.sender_address.to_field_list()}\n" | ||
out_str += f":Recipient Names: {recipient_names}\n" | ||
out_str += f":Recipient Addresses: {self._recipient_addresses_to_str()}\n" | ||
out_str += f":Return to Sender: {self.is_return_to_sender}\n" | ||
return clean_out_string(out_str) |
Oops, something went wrong.