This repository was archived by the owner on Mar 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
feat: Add export merged sharded Document proto #145
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cf547fe
feat: Add export merged sharded Document proto
holtskinner 62d7758
Merge branch 'main' into export-doc
holtskinner 5511a33
fix: Refactor `_apply_text_offset()` to use original impentation with…
holtskinner 25c6af4
chore: Update min python client library for documentai
holtskinner 6990662
Update test constraints
holtskinner 85a1370
fix: Change test to not include indent
holtskinner dde22b9
fix: merge_document_shards_sample_test
holtskinner 4a90472
Merge branch 'main' into export-doc
holtskinner b91559a
Merge branch 'main' into export-doc
holtskinner 88fc3b9
Merge branch 'main' into export-doc
holtskinner 4ad742f
fix: Address lint error for type checking
holtskinner 98afe85
Merge branch 'main' into export-doc
holtskinner 01928c8
Merge branch 'main' into export-doc
holtskinner 85bb259
Fix lint error for incorrect typing
holtskinner 2c12081
Rename `to_documentai_document` to `to_merged_documentai_document`
holtskinner 9312153
Change `to_merged_documentai_document()` to use a deepcopy instead of…
holtskinner 519e678
Merge branch 'main' into export-doc
holtskinner 91e9988
Add more specific type annotation to `_apply_text_offset()`
holtskinner c3da29a
fix: Fixed how template files are included in the library
holtskinner ea34456
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 3ef91ca
refactor: Updated `from_document_path()` to additionally support dire…
holtskinner 6f26f10
fix: Fix type annotation
holtskinner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.