-
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.
:arrow_up better custom line items support (#182)
- Loading branch information
1 parent
9f706aa
commit f541bfc
Showing
13 changed files
with
364 additions
and
119 deletions.
There are no files selected for viewing
File renamed without changes.
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,55 @@ | ||
import os | ||
|
||
from mindee import Client, product | ||
from mindee.parsing.common.predict_response import PredictResponse | ||
|
||
CUSTOM_ENDPOINT_NAME = os.getenv("CUSTOM_ENDPOINT_NAME", "my-endpoint-name") | ||
CUSTOM_ACCOUNT_NAME = os.getenv("CUSTOM_ACCOUNT_NAME", "my-account-name") | ||
CUSTOM_VERSION = os.getenv("CUSTOM_VERSION", "1") | ||
CUSTOM_DOCUMENT_PATH = os.getenv("CUSTOM_DOCUMENT_PATH", "path/to/your/file.ext") | ||
|
||
# This example assumes you are following the associated tutorial: | ||
# https://developers.mindee.com/docs/extracting-line-items-tutorial#line-reconstruction-code | ||
anchors = ["category"] | ||
columns = ["category", "previous_year_actual", "year_actual", "year_projection"] | ||
|
||
|
||
def get_field_content(line, field) -> str: | ||
if field in line.fields: | ||
return str(line.fields[field].content) | ||
return "" | ||
|
||
|
||
def print_line(line) -> None: | ||
category = get_field_content(line, "category") | ||
previous_year_actual = get_field_content(line, "previous_year_actual") | ||
year_actual = get_field_content(line, "year_actual") | ||
year_projection = get_field_content(line, "year_projection") | ||
# here ljust() fills the rest of the given size with spaces | ||
string_line = ( | ||
category.ljust(20, " ") | ||
+ previous_year_actual.ljust(10, " ") | ||
+ year_projection.ljust(10, " ") | ||
+ year_actual | ||
) | ||
|
||
print(string_line) | ||
|
||
|
||
client = Client() | ||
|
||
custom_endpoint = client.create_endpoint( | ||
CUSTOM_ENDPOINT_NAME, CUSTOM_ACCOUNT_NAME, CUSTOM_VERSION | ||
) | ||
input_doc = client.source_from_path(CUSTOM_DOCUMENT_PATH) | ||
|
||
|
||
response: PredictResponse[product.CustomV1] = client.parse( | ||
product.CustomV1, input_doc, endpoint=custom_endpoint | ||
) | ||
line_items = response.document.inference.prediction.columns_to_line_items( | ||
anchors, columns | ||
) | ||
|
||
for line in line_items: | ||
print_line(line) |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from mindee.parsing.custom.classification import ClassificationFieldV1 | ||
from mindee.parsing.custom.line_items import LineV1, get_line_items | ||
from mindee.parsing.custom.line_items import CustomLineV1, get_line_items | ||
from mindee.parsing.custom.list import ListFieldV1, ListFieldValueV1 |
Oops, something went wrong.