Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add export merged sharded Document proto (#145)
* feat: Add export merged sharded Document proto - `to_documentai_document` exports a documentai Document proto from all of the shards in the wrapped Document * fix: Refactor `_apply_text_offset()` to use original impentation with dictionary. - Found issue with implementation when trying to update test coverage * chore: Update min python client library for documentai * Update test constraints * fix: Change test to not include indent * fix: merge_document_shards_sample_test * fix: Address lint error for type checking * Fix lint error for incorrect typing * Rename `to_documentai_document` to `to_merged_documentai_document` * Change `to_merged_documentai_document()` to use a deepcopy instead of editing in place * Add more specific type annotation to `_apply_text_offset()` * fix: Fixed how template files are included in the library - Fixes #156 * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * refactor: Updated `from_document_path()` to additionally support directory of shards * fix: Fix type annotation --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e4db698
commit a5e1f5c
Showing
8 changed files
with
236 additions
and
24 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
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,44 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
|
||
# [START documentai_toolbox_merge_document_shards] | ||
|
||
from google.cloud import documentai | ||
from google.cloud.documentai_toolbox import document | ||
|
||
# TODO(developer): Uncomment these variables before running the sample. | ||
# Given a document.proto or sharded document.proto in path gs://bucket/path/to/folder | ||
# gcs_bucket_name = "bucket" | ||
# gcs_prefix = "path/to/folder" | ||
# output_file_name = "path/to/folder/file.json" | ||
|
||
|
||
def merge_document_shards_sample( | ||
gcs_bucket_name: str, gcs_prefix: str, output_file_name: str | ||
) -> None: | ||
wrapped_document = document.Document.from_gcs( | ||
gcs_bucket_name=gcs_bucket_name, gcs_prefix=gcs_prefix | ||
) | ||
|
||
merged_document = wrapped_document.to_merged_documentai_document() | ||
|
||
with open(output_file_name, "w") as f: | ||
f.write(documentai.Document.to_json(merged_document)) | ||
|
||
print(f"Document with {len(wrapped_document.shards)} shards successfully merged.") | ||
|
||
|
||
# [END documentai_toolbox_merge_document_shards] |
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,4 +1,4 @@ | ||
google-cloud-bigquery==3.11.4 | ||
google-cloud-documentai==2.18.0 | ||
google-cloud-storage==2.10.0 | ||
google-cloud-documentai-toolbox==0.4.1a0 | ||
google-cloud-documentai-toolbox==0.9.0a0 |
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,45 @@ | ||
# Copyright 2023 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import os | ||
import shutil | ||
|
||
import pytest | ||
from samples.snippets import merge_document_shards_sample | ||
|
||
gcs_bucket_name = "documentai_toolbox_samples" | ||
gcs_prefix = "output/987654321/1" | ||
output_dir = "resources/output/" | ||
output_path = f"{output_dir}merged_document.json" | ||
|
||
|
||
def test_merge_document_shards_sample(capsys: pytest.CaptureFixture) -> None: | ||
if os.path.exists(output_dir): | ||
shutil.rmtree(output_dir) | ||
|
||
os.makedirs(output_dir) | ||
|
||
merge_document_shards_sample.merge_document_shards_sample( | ||
gcs_bucket_name=gcs_bucket_name, | ||
gcs_prefix=gcs_prefix, | ||
output_file_name=output_path, | ||
) | ||
|
||
out, _ = capsys.readouterr() | ||
|
||
assert "Document with 5 shards successfully merged." in out | ||
|
||
assert os.path.exists(output_dir) | ||
shutil.rmtree(output_dir) |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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