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: added confidence score and detected languages to Page classes
#387
Closed
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
035d8a5
feat(Token): added confidence score and detected languages to Token c…
Shifat7 e9da20b
feat(page): moved the props to BasePageElement
Shifat7 678b2d1
chore(tests): remove unused test samples and added more tests in test…
Shifat7 54921b8
chore(quickstart): added token confidence and detected languages outp…
Shifat7 c5f9d30
chore(samples): removed token confidence and detected languages sampl…
Shifat7 8905647
Formatting
holtskinner c400aa8
Merge branch 'main' into main
daniel-sanche 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
|
Shifat7 marked this conversation as resolved.
Outdated
Shifat7 marked this conversation as resolved.
Outdated
|
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,36 @@ | ||
| # Copyright 2024 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 token_confidence_sample | ||
|
|
||
|
|
||
| def test_token_confidence_sample(capsys): | ||
| # Use a test document from the resources directory | ||
| test_file_path = os.path.join( | ||
| os.path.dirname(__file__), "resources", "form_with_tables.json" | ||
| ) | ||
|
|
||
| # Run the sample | ||
| token_confidence_sample.token_confidence_sample( | ||
| document_path=test_file_path | ||
| ) | ||
|
|
||
| # Capture output | ||
| stdout, _ = capsys.readouterr() | ||
|
|
||
| # Check that the output contains expected strings | ||
| assert "Token" in stdout | ||
| assert "Confidence:" in stdout |
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,36 @@ | ||
| # Copyright 2024 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 token_detected_languages_sample | ||
|
|
||
|
|
||
| def test_token_detected_languages_sample(capsys): | ||
| # Use a test document from the resources directory | ||
| test_file_path = os.path.join( | ||
| os.path.dirname(__file__), "resources", "form_with_tables.json" | ||
| ) | ||
|
|
||
| # Run the sample | ||
| token_detected_languages_sample.token_detected_languages_sample( | ||
| document_path=test_file_path | ||
| ) | ||
|
|
||
| # Capture output | ||
| stdout, _ = capsys.readouterr() | ||
|
|
||
| # Check that the output contains expected strings | ||
| assert "Token" in stdout | ||
| assert "Detected Languages:" in stdout |
|
Shifat7 marked this conversation as resolved.
Outdated
|
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,70 @@ | ||
| # Copyright 2024 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_token_confidence] | ||
| from typing import Optional | ||
|
|
||
| from google.cloud.documentai_toolbox import document | ||
|
|
||
| # TODO(developer): Uncomment these variables before running the sample. | ||
| # gcs_uri = "gs://bucket/path/to/folder/document.json" | ||
|
|
||
|
|
||
| def token_confidence_sample( | ||
| gcs_uri: Optional[str] = None, | ||
| document_path: Optional[str] = None, | ||
| ) -> None: | ||
| """Demonstrates how to access token-level confidence scores. | ||
|
|
||
| Args: | ||
| gcs_uri (Optional[str]): | ||
| URI to a Document JSON file in GCS. | ||
| document_path (Optional[str]): | ||
| Path to a local Document JSON file. | ||
| """ | ||
| if gcs_uri: | ||
| # Load a single Document from a Google Cloud Storage URI | ||
| wrapped_document = document.Document.from_gcs_uri(gcs_uri=gcs_uri) | ||
| elif document_path: | ||
| # Load from local `Document` JSON file | ||
| wrapped_document = document.Document.from_document_path(document_path) | ||
| else: | ||
| raise ValueError("No document source provided.") | ||
|
|
||
| # Display token confidence for the first page | ||
| if wrapped_document.pages: | ||
| page = wrapped_document.pages[0] | ||
| print(f"Page {page.page_number} Tokens:") | ||
|
|
||
| for i, token in enumerate(page.tokens[:10]): # Limiting to first 10 tokens for brevity | ||
| print(f"Token {i}: '{token.text.strip()}'") | ||
| print(f" Confidence: {token.confidence:.4f}") | ||
| print() | ||
| # [END documentai_toolbox_token_confidence] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument("--gcs_uri", help="GCS URI to Document JSON.") | ||
| group.add_argument("--document_path", help="Path to local Document JSON file.") | ||
| args = parser.parse_args() | ||
|
|
||
| token_confidence_sample( | ||
| gcs_uri=args.gcs_uri, | ||
| document_path=args.document_path, | ||
| ) |
|
Shifat7 marked this conversation as resolved.
Outdated
|
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,77 @@ | ||
| # Copyright 2024 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_token_detected_languages] | ||
| from typing import Optional | ||
|
|
||
| from google.cloud.documentai_toolbox import document | ||
|
|
||
| # TODO(developer): Uncomment these variables before running the sample. | ||
| # gcs_uri = "gs://bucket/path/to/folder/document.json" | ||
|
|
||
|
|
||
| def token_detected_languages_sample( | ||
| gcs_uri: Optional[str] = None, | ||
| document_path: Optional[str] = None, | ||
| ) -> None: | ||
| """Demonstrates how to access token-level detected languages. | ||
|
|
||
| Args: | ||
| gcs_uri (Optional[str]): | ||
| URI to a Document JSON file in GCS. | ||
| document_path (Optional[str]): | ||
| Path to a local Document JSON file. | ||
| """ | ||
| if gcs_uri: | ||
| # Load a single Document from a Google Cloud Storage URI | ||
| wrapped_document = document.Document.from_gcs_uri(gcs_uri=gcs_uri) | ||
| elif document_path: | ||
| # Load from local `Document` JSON file | ||
| wrapped_document = document.Document.from_document_path(document_path) | ||
| else: | ||
| raise ValueError("No document source provided.") | ||
|
|
||
| # Display detected languages for tokens in the first page | ||
| if wrapped_document.pages: | ||
| page = wrapped_document.pages[0] | ||
| print(f"Page {page.page_number} Tokens:") | ||
|
|
||
| for i, token in enumerate(page.tokens[:10]): # Limiting to first 10 tokens for brevity | ||
| print(f"Token {i}: '{token.text.strip()}'") | ||
|
|
||
| if token.detected_languages: | ||
| print(" Detected Languages:") | ||
| for lang in token.detected_languages: | ||
| confidence_str = f", confidence: {lang.confidence:.4f}" if hasattr(lang, "confidence") else "" | ||
| print(f" - {lang.language_code}{confidence_str}") | ||
| else: | ||
| print(" No language detected") | ||
| print() | ||
| # [END documentai_toolbox_token_detected_languages] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import argparse | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument("--gcs_uri", help="GCS URI to Document JSON.") | ||
| group.add_argument("--document_path", help="Path to local Document JSON file.") | ||
| args = parser.parse_args() | ||
|
|
||
| token_detected_languages_sample( | ||
| gcs_uri=args.gcs_uri, | ||
| document_path=args.document_path, | ||
| ) |
|
Shifat7 marked this conversation as resolved.
|
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.