From 20dd56cd8dc08bde57aeb430d3e328e9cebb26e5 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 16:00:22 +0000 Subject: [PATCH 1/6] chore: use gapic generator python 1.4.1 (#377) - [ ] Regenerate this pull request now. PiperOrigin-RevId: 473833416 Source-Link: https://github.com/googleapis/googleapis/commit/565a5508869557a3228b871101e4e4ebd8f93d11 Source-Link: https://github.com/googleapis/googleapis-gen/commit/1ee1a06c6de3ca8b843572c1fde0548f84236989 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiMWVlMWEwNmM2ZGUzY2E4Yjg0MzU3MmMxZmRlMDU0OGY4NDIzNjk4OSJ9 --- .../unit/gapic/documentai_v1/test_document_processor_service.py | 1 + .../documentai_v1beta2/test_document_understanding_service.py | 1 + .../gapic/documentai_v1beta3/test_document_processor_service.py | 1 + 3 files changed, 3 insertions(+) diff --git a/tests/unit/gapic/documentai_v1/test_document_processor_service.py b/tests/unit/gapic/documentai_v1/test_document_processor_service.py index 8b0756b0..e56ceba9 100644 --- a/tests/unit/gapic/documentai_v1/test_document_processor_service.py +++ b/tests/unit/gapic/documentai_v1/test_document_processor_service.py @@ -55,6 +55,7 @@ from google.type import postal_address_pb2 # type: ignore import grpc from grpc.experimental import aio +from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest diff --git a/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py b/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py index 0b51c636..782f9b60 100644 --- a/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py +++ b/tests/unit/gapic/documentai_v1beta2/test_document_understanding_service.py @@ -44,6 +44,7 @@ from google.rpc import status_pb2 # type: ignore import grpc from grpc.experimental import aio +from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest diff --git a/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py b/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py index 14821a65..559c60d8 100644 --- a/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py +++ b/tests/unit/gapic/documentai_v1beta3/test_document_processor_service.py @@ -55,6 +55,7 @@ from google.type import postal_address_pb2 # type: ignore import grpc from grpc.experimental import aio +from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest From 74ab2a5dcbe16f3e7ce3995b92e271e122e546bf Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 16:40:20 +0000 Subject: [PATCH 2/6] chore: detect samples tests in nested directories (#379) Source-Link: https://github.com/googleapis/synthtool/commit/50db768f450a50d7c1fd62513c113c9bb96fd434 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:e09366bdf0fd9c8976592988390b24d53583dd9f002d476934da43725adbb978 --- .github/.OwlBot.lock.yaml | 2 +- samples/snippets/noxfile.py | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml index b8dcb4a4..aa547962 100644 --- a/.github/.OwlBot.lock.yaml +++ b/.github/.OwlBot.lock.yaml @@ -13,4 +13,4 @@ # limitations under the License. docker: image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest - digest: sha256:993a058718e84a82fda04c3177e58f0a43281a996c7c395e0a56ccc4d6d210d7 + digest: sha256:e09366bdf0fd9c8976592988390b24d53583dd9f002d476934da43725adbb978 diff --git a/samples/snippets/noxfile.py b/samples/snippets/noxfile.py index e9eb1cbf..c1715136 100644 --- a/samples/snippets/noxfile.py +++ b/samples/snippets/noxfile.py @@ -207,8 +207,10 @@ def _session_tests( session: nox.sessions.Session, post_install: Callable = None ) -> None: # check for presence of tests - test_list = glob.glob("*_test.py") + glob.glob("test_*.py") - test_list.extend(glob.glob("tests")) + test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob( + "**/test_*.py", recursive=True + ) + test_list.extend(glob.glob("**/tests", recursive=True)) if len(test_list) == 0: print("No tests found, skipping directory.") From 6ef8b9d8f22618667f3b9c8bdd0eba745bfea5ba Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Wed, 14 Sep 2022 15:53:38 +0200 Subject: [PATCH 3/6] chore(deps): update dependency google-cloud-documentai to v2.0.1 (#380) --- samples/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 31caa748..e62b9a26 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-documentai==2.0.0 +google-cloud-documentai==2.0.1 google-cloud-storage==2.5.0 From f9ce801119f96b632fddb73a4d314be1bb188639 Mon Sep 17 00:00:00 2001 From: Holt Skinner <13262395+holtskinner@users.noreply.github.com> Date: Mon, 26 Sep 2022 10:56:29 -0500 Subject: [PATCH 4/6] docs(samples): Added Processor Version Samples (#382) * docs(samples): Added Processor Version Samples To Be Published in documentation: https://cloud.google.com/document-ai/docs/manage-processor - `get_processor_version` - `list_processor_versions` - `set_default_processor_version` * docs(samples): Adjusted Bad Batch Input test to * docs(samples): Added Deploy/Undeploy Samples * docs(samples): Added process & batchProcess examples for processorVersions - Removed Processor Version from basic process and batchProcess examples - Removed Note about must create processors in the Cloud Console - Added note that processor must be created before running sample where missing * docs(samples): Adjusted Enable/Disable Processor Test to avoid Race Conditions * docs(samples): Added Delete Processor Version Sample - Also Fixed Spelling error in Undeploy Comments * docs(samples): Updated non-idempotent unit tests to use mocks - Also replaced test ocr processor id after making a breaking change to the project - Added `field_mask` to process_documents tests --- ...cess_documents_processor_version_sample.py | 153 ++++++++++++++++++ ...documents_processor_version_sample_test.py | 64 ++++++++ .../batch_process_documents_sample.py | 10 -- ...process_documents_sample_bad_input_test.py | 2 +- .../delete_processor_version_sample.py | 58 +++++++ .../delete_processor_version_sample_test.py | 47 ++++++ .../deploy_processor_version_sample.py | 56 +++++++ .../deploy_processor_version_sample_test.py | 48 ++++++ .../snippets/disable_processor_sample_test.py | 4 +- .../snippets/enable_processor_sample_test.py | 4 +- samples/snippets/get_processor_sample_test.py | 2 +- .../snippets/get_processor_version_sample.py | 51 ++++++ .../get_processor_version_sample_test.py | 37 +++++ .../list_processor_versions_sample.py | 52 ++++++ .../list_processor_versions_sample_test.py | 34 ++++ .../snippets/process_document_form_sample.py | 1 - .../process_document_ocr_sample_test.py | 2 +- ...ocess_document_processor_version_sample.py | 74 +++++++++ ..._document_processor_version_sample_test.py | 42 +++++ .../process_document_quality_sample.py | 1 - samples/snippets/process_document_sample.py | 12 +- .../snippets/process_document_sample_test.py | 2 + .../process_document_specialized_sample.py | 1 - .../process_document_splitter_sample.py | 1 - samples/snippets/quickstart_sample.py | 1 - samples/snippets/review_document_sample.py | 1 - .../set_default_processor_version_sample.py | 62 +++++++ ...t_default_processor_version_sample_test.py | 44 +++++ .../undeploy_processor_version_sample.py | 57 +++++++ .../undeploy_processor_version_sample_test.py | 48 ++++++ 30 files changed, 935 insertions(+), 36 deletions(-) create mode 100644 samples/snippets/batch_process_documents_processor_version_sample.py create mode 100644 samples/snippets/batch_process_documents_processor_version_sample_test.py create mode 100644 samples/snippets/delete_processor_version_sample.py create mode 100644 samples/snippets/delete_processor_version_sample_test.py create mode 100644 samples/snippets/deploy_processor_version_sample.py create mode 100644 samples/snippets/deploy_processor_version_sample_test.py create mode 100644 samples/snippets/get_processor_version_sample.py create mode 100644 samples/snippets/get_processor_version_sample_test.py create mode 100644 samples/snippets/list_processor_versions_sample.py create mode 100644 samples/snippets/list_processor_versions_sample_test.py create mode 100644 samples/snippets/process_document_processor_version_sample.py create mode 100644 samples/snippets/process_document_processor_version_sample_test.py create mode 100644 samples/snippets/set_default_processor_version_sample.py create mode 100644 samples/snippets/set_default_processor_version_sample_test.py create mode 100644 samples/snippets/undeploy_processor_version_sample.py create mode 100644 samples/snippets/undeploy_processor_version_sample_test.py diff --git a/samples/snippets/batch_process_documents_processor_version_sample.py b/samples/snippets/batch_process_documents_processor_version_sample.py new file mode 100644 index 00000000..7050c3ec --- /dev/null +++ b/samples/snippets/batch_process_documents_processor_version_sample.py @@ -0,0 +1,153 @@ +# Copyright 2020 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_batch_process_documents_processor_version] +import re + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai, storage + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Example: aeb8cea219b7c272 +# processor_version_id = "YOUR_PROCESSOR_VERSION_ID" # Example: pretrained-ocr-v1.0-2020-09-23 +# gcs_input_uri = "YOUR_INPUT_URI" # Format: gs://bucket/directory/file.pdf +# input_mime_type = "application/pdf" +# gcs_output_bucket = "YOUR_OUTPUT_BUCKET_NAME" # Format: gs://bucket +# gcs_output_uri_prefix = "YOUR_OUTPUT_URI_PREFIX" # Format: directory/subdirectory/ + + +def batch_process_documents_processor_version( + project_id: str, + location: str, + processor_id: str, + processor_version_id: str, + gcs_input_uri: str, + input_mime_type: str, + gcs_output_bucket: str, + gcs_output_uri_prefix: str, + timeout: int = 300, +): + + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + gcs_document = documentai.GcsDocument( + gcs_uri=gcs_input_uri, mime_type=input_mime_type + ) + + # Load GCS Input URI into a List of document files + gcs_documents = documentai.GcsDocuments(documents=[gcs_document]) + input_config = documentai.BatchDocumentsInputConfig(gcs_documents=gcs_documents) + + # NOTE: Alternatively, specify a GCS URI Prefix to process an entire directory + # + # gcs_input_uri = "gs://bucket/directory/" + # gcs_prefix = documentai.GcsPrefix(gcs_uri_prefix=gcs_input_uri) + # input_config = documentai.BatchDocumentsInputConfig(gcs_prefix=gcs_prefix) + # + + # Cloud Storage URI for the Output Directory + destination_uri = f"{gcs_output_bucket}/{gcs_output_uri_prefix}/" + + gcs_output_config = documentai.DocumentOutputConfig.GcsOutputConfig( + gcs_uri=destination_uri + ) + + # Where to write results + output_config = documentai.DocumentOutputConfig(gcs_output_config=gcs_output_config) + + # The full resource name of the processor version + # e.g. projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id} + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + request = documentai.BatchProcessRequest( + name=name, + input_documents=input_config, + document_output_config=output_config, + ) + + # BatchProcess returns a Long Running Operation (LRO) + operation = client.batch_process_documents(request) + + # Continually polls the operation until it is complete. + # This could take some time for larger files + # Format: projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID + print(f"Waiting for operation {operation.operation.name} to complete...") + operation.result(timeout=timeout) + + # NOTE: Can also use callbacks for asynchronous processing + # + # def my_callback(future): + # result = future.result() + # + # operation.add_done_callback(my_callback) + + # Once the operation is complete, + # get output document information from operation metadata + metadata = documentai.BatchProcessMetadata(operation.metadata) + + if metadata.state != documentai.BatchProcessMetadata.State.SUCCEEDED: + raise ValueError(f"Batch Process Failed: {metadata.state_message}") + + storage_client = storage.Client() + + print("Output files:") + # One process per Input Document + for process in metadata.individual_process_statuses: + # output_gcs_destination format: gs://BUCKET/PREFIX/OPERATION_NUMBER/INPUT_FILE_NUMBER/ + # The Cloud Storage API requires the bucket name and URI prefix separately + matches = re.match(r"gs://(.*?)/(.*)", process.output_gcs_destination) + if not matches: + print( + "Could not parse output GCS destination:", + process.output_gcs_destination, + ) + continue + + output_bucket, output_prefix = matches.groups() + + # Get List of Document Objects from the Output Bucket + output_blobs = storage_client.list_blobs(output_bucket, prefix=output_prefix) + + # Document AI may output multiple JSON files per source file + for blob in output_blobs: + # Document AI should only output JSON files to GCS + if ".json" not in blob.name: + print( + f"Skipping non-supported file: {blob.name} - Mimetype: {blob.content_type}" + ) + continue + + # Download JSON File as bytes object and convert to Document Object + print(f"Fetching {blob.name}") + document = documentai.Document.from_json( + blob.download_as_bytes(), ignore_unknown_fields=True + ) + + # For a full list of Document object attributes, please reference this page: + # https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.Document + + # Read the text recognition output from the processor + print("The document contains the following text:") + print(document.text) + + +# [END documentai_batch_process_documents_processor_version] diff --git a/samples/snippets/batch_process_documents_processor_version_sample_test.py b/samples/snippets/batch_process_documents_processor_version_sample_test.py new file mode 100644 index 00000000..b39c8ab2 --- /dev/null +++ b/samples/snippets/batch_process_documents_processor_version_sample_test.py @@ -0,0 +1,64 @@ +# Copyright 2020 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 +from uuid import uuid4 + +from google.cloud import storage +from google.cloud.exceptions import NotFound +import pytest +from samples.snippets import batch_process_documents_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "90484cfdedb024f6" +processor_version_id = "pretrained-form-parser-v1.0-2020-09-23" +gcs_input_uri = "gs://cloud-samples-data/documentai/invoice.pdf" +input_mime_type = "application/pdf" +gcs_output_uri_prefix = uuid4() +BUCKET_NAME = f"document-ai-python-{uuid4()}" + + +@pytest.fixture(scope="module") +def test_bucket(): + storage_client = storage.Client() + bucket = storage_client.create_bucket(BUCKET_NAME) + yield bucket.name + + try: + blobs = list(bucket.list_blobs()) + for blob in blobs: + blob.delete() + bucket.delete() + except NotFound: + print("Bucket already deleted.") + + +def test_batch_process_documents_processor_version(capsys, test_bucket): + batch_process_documents_processor_version_sample.batch_process_documents_processor_version( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + gcs_input_uri=gcs_input_uri, + input_mime_type=input_mime_type, + gcs_output_bucket=f"gs://{test_bucket}", + gcs_output_uri_prefix=gcs_output_uri_prefix, + ) + out, _ = capsys.readouterr() + + assert "operation" in out + assert "Fetching" in out + assert "text:" in out diff --git a/samples/snippets/batch_process_documents_sample.py b/samples/snippets/batch_process_documents_sample.py index f3e01936..f01d8787 100644 --- a/samples/snippets/batch_process_documents_sample.py +++ b/samples/snippets/batch_process_documents_sample.py @@ -23,7 +23,6 @@ # project_id = 'YOUR_PROJECT_ID' # location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' # processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample -# processor_version = "pretrained" # Optional. Processor version to use # gcs_input_uri = "YOUR_INPUT_URI" # Format: gs://bucket/directory/file.pdf # input_mime_type = "application/pdf" # gcs_output_bucket = "YOUR_OUTPUT_BUCKET_NAME" # Format: gs://bucket @@ -73,17 +72,8 @@ def batch_process_documents( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) - # NOTE: Alternatively, specify the processor_version to specify a particular version of the processor to use - # projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processorVersion} - # - # name = client.processor_version_path( - # project_id, location, processor_id, processor_version - # ) - # - request = documentai.BatchProcessRequest( name=name, input_documents=input_config, diff --git a/samples/snippets/batch_process_documents_sample_bad_input_test.py b/samples/snippets/batch_process_documents_sample_bad_input_test.py index 7d2ffd75..a130f63d 100644 --- a/samples/snippets/batch_process_documents_sample_bad_input_test.py +++ b/samples/snippets/batch_process_documents_sample_bad_input_test.py @@ -44,4 +44,4 @@ def test_batch_process_documents_with_bad_input(capsys): out, _ = capsys.readouterr() assert "Failed" in out except Exception as e: - assert "Internal error" in e.message + assert "Failed" in e.message diff --git a/samples/snippets/delete_processor_version_sample.py b/samples/snippets/delete_processor_version_sample.py new file mode 100644 index 00000000..8ff8703d --- /dev/null +++ b/samples/snippets/delete_processor_version_sample.py @@ -0,0 +1,58 @@ +# Copyright 2022 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_delete_processor_version] + +from google.api_core.client_options import ClientOptions +from google.api_core.exceptions import FailedPrecondition, InvalidArgument +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' + + +def delete_processor_version_sample( + project_id: str, location: str, processor_id: str, processor_version_id: str +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor version + # e.g.: projects/project_id/locations/location/processors/processor_id/processorVersions/processor_version_id + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + # Make DeleteProcessorVersion request + try: + operation = client.delete_processor_version(name=name) + # Print operation details + print(operation.operation.name) + # Wait for operation to complete + operation.result() + # Delete request will fail if the + # processor version doesn't exist + # or if a request is made on a pretrained processor version + # or the default processor version + except (FailedPrecondition, InvalidArgument) as e: + print(e.message) + + +# [END documentai_delete_processor_version] diff --git a/samples/snippets/delete_processor_version_sample_test.py b/samples/snippets/delete_processor_version_sample_test.py new file mode 100644 index 00000000..4104cd07 --- /dev/null +++ b/samples/snippets/delete_processor_version_sample_test.py @@ -0,0 +1,47 @@ +# Copyright 2022 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 mock +from samples.snippets import delete_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "aaaaaaaaa" +processor_version_id = "xxxxxxxxxx" + + +@mock.patch( + "google.cloud.documentai.DocumentProcessorServiceClient.delete_processor_version" +) +@mock.patch("google.api_core.operation.Operation") +def test_delete_processor_version( + operation_mock, delete_processor_version_mock, capsys +): + delete_processor_version_mock.return_value = operation_mock + + delete_processor_version_sample.delete_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + ) + + delete_processor_version_mock.assert_called_once() + + out, _ = capsys.readouterr() + + assert "operation" in out diff --git a/samples/snippets/deploy_processor_version_sample.py b/samples/snippets/deploy_processor_version_sample.py new file mode 100644 index 00000000..217a23b3 --- /dev/null +++ b/samples/snippets/deploy_processor_version_sample.py @@ -0,0 +1,56 @@ +# Copyright 2022 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_deploy_processor_version] + +from google.api_core.client_options import ClientOptions +from google.api_core.exceptions import FailedPrecondition +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' + + +def deploy_processor_version_sample( + project_id: str, location: str, processor_id: str, processor_version_id: str +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor version + # e.g.: projects/project_id/locations/location/processors/processor_id/processorVersions/processor_version_id + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + # Make DeployProcessorVersion request + try: + operation = client.deploy_processor_version(name=name) + # Print operation details + print(operation.operation.name) + # Wait for operation to complete + operation.result() + # Deploy request will fail if the + # processor version is already deployed + except FailedPrecondition as e: + print(e.message) + + +# [END documentai_deploy_processor_version] diff --git a/samples/snippets/deploy_processor_version_sample_test.py b/samples/snippets/deploy_processor_version_sample_test.py new file mode 100644 index 00000000..31f96925 --- /dev/null +++ b/samples/snippets/deploy_processor_version_sample_test.py @@ -0,0 +1,48 @@ +# Copyright 2022 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 mock +from samples.snippets import deploy_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "aaaaaaaaa" +processor_version_id = "xxxxxxxxxx" + + +# TODO: Switch to Real Endpoint when Deployable Versions are Available +@mock.patch( + "google.cloud.documentai.DocumentProcessorServiceClient.deploy_processor_version" +) +@mock.patch("google.api_core.operation.Operation") +def test_deploy_processor_version( + operation_mock, deploy_processor_version_mock, capsys +): + deploy_processor_version_mock.return_value = operation_mock + + deploy_processor_version_sample.deploy_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + ) + + deploy_processor_version_mock.assert_called_once() + + out, _ = capsys.readouterr() + + assert "operation" in out diff --git a/samples/snippets/disable_processor_sample_test.py b/samples/snippets/disable_processor_sample_test.py index 0420ee48..0a816723 100644 --- a/samples/snippets/disable_processor_sample_test.py +++ b/samples/snippets/disable_processor_sample_test.py @@ -28,9 +28,7 @@ def test_disable_processor(capsys): ) out, _ = capsys.readouterr() - assert "projects" in out - assert "locations" in out - assert "operations" in out + assert "projects" in out or "DISABLED" in out # Re-Enable Processor enable_processor_sample.enable_processor_sample( diff --git a/samples/snippets/enable_processor_sample_test.py b/samples/snippets/enable_processor_sample_test.py index 0a238cd0..a4ccc370 100644 --- a/samples/snippets/enable_processor_sample_test.py +++ b/samples/snippets/enable_processor_sample_test.py @@ -28,9 +28,7 @@ def test_enable_processor(capsys): ) out, _ = capsys.readouterr() - assert "projects" in out - assert "locations" in out - assert "operations" in out + assert "projects" in out or "ENABLED" in out # Re-Disable Processor disable_processor_sample.disable_processor_sample( diff --git a/samples/snippets/get_processor_sample_test.py b/samples/snippets/get_processor_sample_test.py index b1848422..f8a37bea 100644 --- a/samples/snippets/get_processor_sample_test.py +++ b/samples/snippets/get_processor_sample_test.py @@ -19,7 +19,7 @@ location = "us" project_id = os.environ["GOOGLE_CLOUD_PROJECT"] -processor_id = "91e072f8626a76b7" +processor_id = "52a38e080c1a7296" def test_get_processor(capsys): diff --git a/samples/snippets/get_processor_version_sample.py b/samples/snippets/get_processor_version_sample.py new file mode 100644 index 00000000..5dc0babd --- /dev/null +++ b/samples/snippets/get_processor_version_sample.py @@ -0,0 +1,51 @@ +# Copyright 2022 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_get_processor_version] + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' + + +def get_processor_version_sample( + project_id: str, location: str, processor_id: str, processor_version_id: str +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor version + # e.g.: projects/project_id/locations/location/processors/processor_id/processorVersions/processor_version_id + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + # Make GetProcessorVersion request + processor_version = client.get_processor_version(name=name) + + # Print the processor version information + print(f"Processor Version: {processor_version_id}") + print(f"Display Name: {processor_version.display_name}") + print(processor_version.state) + + +# [END documentai_get_processor_version] diff --git a/samples/snippets/get_processor_version_sample_test.py b/samples/snippets/get_processor_version_sample_test.py new file mode 100644 index 00000000..071d740a --- /dev/null +++ b/samples/snippets/get_processor_version_sample_test.py @@ -0,0 +1,37 @@ +# Copyright 2022 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 + +from samples.snippets import get_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "52a38e080c1a7296" +processor_version_id = "pretrained-ocr-v1.0-2020-09-23" + + +def test_get_processor_version(capsys): + get_processor_version_sample.get_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + ) + out, _ = capsys.readouterr() + + assert "Processor Version: pretrained-ocr" in out + assert "Display Name: Google Stable" in out + assert "DEPLOYED" in out diff --git a/samples/snippets/list_processor_versions_sample.py b/samples/snippets/list_processor_versions_sample.py new file mode 100644 index 00000000..f615c1bb --- /dev/null +++ b/samples/snippets/list_processor_versions_sample.py @@ -0,0 +1,52 @@ +# Copyright 2022 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_list_processor_versions] + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample + + +def list_processor_versions_sample(project_id: str, location: str, processor_id: str): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor + # e.g.: projects/project_id/locations/location/processors/processor_id + parent = client.processor_path(project_id, location, processor_id) + + # Make ListProcessorVersions request + processor_versions = client.list_processor_versions(parent=parent) + + # Print the processor version information + for processor_version in processor_versions: + processor_version_id = client.parse_processor_version_path( + processor_version.name + )["processor_version"] + + print(f"Processor Version: {processor_version_id}") + print(f"Display Name: {processor_version.display_name}") + print(processor_version.state) + print("") + + +# [END documentai_list_processor_versions] diff --git a/samples/snippets/list_processor_versions_sample_test.py b/samples/snippets/list_processor_versions_sample_test.py new file mode 100644 index 00000000..16219795 --- /dev/null +++ b/samples/snippets/list_processor_versions_sample_test.py @@ -0,0 +1,34 @@ +# Copyright 2022 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 + +from samples.snippets import list_processor_versions_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "52a38e080c1a7296" + + +def test_list_processor_versions(capsys): + list_processor_versions_sample.list_processor_versions_sample( + project_id=project_id, location=location, processor_id=processor_id + ) + out, _ = capsys.readouterr() + + assert "Processor Version: pretrained-ocr" in out + assert "Display Name: Google Stable" in out + assert "Display Name: Google Release Candidate" in out + assert "DEPLOYED" in out diff --git a/samples/snippets/process_document_form_sample.py b/samples/snippets/process_document_form_sample.py index 7922490f..c113d6a8 100644 --- a/samples/snippets/process_document_form_sample.py +++ b/samples/snippets/process_document_form_sample.py @@ -81,7 +81,6 @@ def process_document( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/process_document_ocr_sample_test.py b/samples/snippets/process_document_ocr_sample_test.py index ba46ca17..d6cceb46 100644 --- a/samples/snippets/process_document_ocr_sample_test.py +++ b/samples/snippets/process_document_ocr_sample_test.py @@ -19,7 +19,7 @@ location = "us" project_id = os.environ["GOOGLE_CLOUD_PROJECT"] -processor_id = "91e072f8626a76b7" +processor_id = "52a38e080c1a7296" file_path = "resources/handwritten_form.pdf" mime_type = "application/pdf" diff --git a/samples/snippets/process_document_processor_version_sample.py b/samples/snippets/process_document_processor_version_sample.py new file mode 100644 index 00000000..39db461e --- /dev/null +++ b/samples/snippets/process_document_processor_version_sample.py @@ -0,0 +1,74 @@ +# Copyright 2020 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_process_document_processor_version] + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' # Processor version to use +# file_path = '/path/to/local/pdf' +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types +# field_mask = "text,entities,pages.pageNumber" # Optional. The fields to return in the Document object. + + +def process_document_processor_version_sample( + project_id: str, + location: str, + processor_id: str, + processor_version_id: str, + file_path: str, + mime_type: str, + field_mask: str = None, +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor version + # e.g. projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id} + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + # Read the file into memory + with open(file_path, "rb") as image: + image_content = image.read() + + # Load Binary Data into Document AI RawDocument Object + raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type) + + # Configure the process request + request = documentai.ProcessRequest( + name=name, raw_document=raw_document, field_mask=field_mask + ) + + result = client.process_document(request=request) + + # For a full list of Document object attributes, please reference this page: + # https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.Document + document = result.document + + # Read the text recognition output from the processor + print("The document contains the following text:") + print(document.text) + + +# [END documentai_process_document_processor_version] diff --git a/samples/snippets/process_document_processor_version_sample_test.py b/samples/snippets/process_document_processor_version_sample_test.py new file mode 100644 index 00000000..50d8bc63 --- /dev/null +++ b/samples/snippets/process_document_processor_version_sample_test.py @@ -0,0 +1,42 @@ +# # Copyright 2020 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 + +from samples.snippets import process_document_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "90484cfdedb024f6" +processor_version_id = "stable" +file_path = "resources/invoice.pdf" +mime_type = "application/pdf" +field_mask = "text,pages.pageNumber" + + +def test_process_document_processor_versions(capsys): + process_document_processor_version_sample.process_document_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + file_path=file_path, + mime_type=mime_type, + field_mask=field_mask, + ) + out, _ = capsys.readouterr() + + assert "text:" in out + assert "Invoice" in out diff --git a/samples/snippets/process_document_quality_sample.py b/samples/snippets/process_document_quality_sample.py index 0bbca75d..79e88977 100644 --- a/samples/snippets/process_document_quality_sample.py +++ b/samples/snippets/process_document_quality_sample.py @@ -60,7 +60,6 @@ def process_document( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/process_document_sample.py b/samples/snippets/process_document_sample.py index 13d7850e..2cd93538 100644 --- a/samples/snippets/process_document_sample.py +++ b/samples/snippets/process_document_sample.py @@ -21,8 +21,7 @@ # TODO(developer): Uncomment these variables before running the sample. # project_id = 'YOUR_PROJECT_ID' # location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' -# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample -# processor_version = "pretrained" # Optional. Processor version to use +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample # file_path = '/path/to/local/pdf' # mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types # field_mask = "text,entities,pages.pageNumber" # Optional. The fields to return in the Document object. @@ -43,17 +42,8 @@ def process_document_sample( # The full resource name of the processor, e.g.: # projects/{project_id}/locations/{location}/processors/{processor_id} - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) - # NOTE: Alternatively, specify the processor_version to specify a particular version of the processor to use - # projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processorVersion} - # - # name = client.processor_version_path( - # project_id, location, processor_id, processor_version - # ) - # - # Read the file into memory with open(file_path, "rb") as image: image_content = image.read() diff --git a/samples/snippets/process_document_sample_test.py b/samples/snippets/process_document_sample_test.py index bf7bcd63..6da4ae36 100644 --- a/samples/snippets/process_document_sample_test.py +++ b/samples/snippets/process_document_sample_test.py @@ -22,6 +22,7 @@ processor_id = "90484cfdedb024f6" file_path = "resources/invoice.pdf" mime_type = "application/pdf" +field_mask = "text,pages.pageNumber" def test_process_documents(capsys): @@ -31,6 +32,7 @@ def test_process_documents(capsys): processor_id=processor_id, file_path=file_path, mime_type=mime_type, + field_mask=field_mask, ) out, _ = capsys.readouterr() diff --git a/samples/snippets/process_document_specialized_sample.py b/samples/snippets/process_document_specialized_sample.py index c78ca704..272d8cdc 100644 --- a/samples/snippets/process_document_specialized_sample.py +++ b/samples/snippets/process_document_specialized_sample.py @@ -78,7 +78,6 @@ def process_document( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/process_document_splitter_sample.py b/samples/snippets/process_document_splitter_sample.py index b78c557c..e49e8ff1 100644 --- a/samples/snippets/process_document_splitter_sample.py +++ b/samples/snippets/process_document_splitter_sample.py @@ -67,7 +67,6 @@ def process_document( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/quickstart_sample.py b/samples/snippets/quickstart_sample.py index 23dcf084..e830acdf 100644 --- a/samples/snippets/quickstart_sample.py +++ b/samples/snippets/quickstart_sample.py @@ -38,7 +38,6 @@ def quickstart( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/review_document_sample.py b/samples/snippets/review_document_sample.py index 2bfa4dcd..969cf59a 100644 --- a/samples/snippets/review_document_sample.py +++ b/samples/snippets/review_document_sample.py @@ -74,7 +74,6 @@ def process_document( # The full resource name of the processor, e.g.: # projects/project_id/locations/location/processor/processor_id - # You must create new processors in the Cloud Console first name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/set_default_processor_version_sample.py b/samples/snippets/set_default_processor_version_sample.py new file mode 100644 index 00000000..2c7c3797 --- /dev/null +++ b/samples/snippets/set_default_processor_version_sample.py @@ -0,0 +1,62 @@ +# Copyright 2022 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_set_default_processor_version] + +from google.api_core.client_options import ClientOptions +from google.api_core.exceptions import NotFound +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' + + +def set_default_processor_version_sample( + project_id: str, location: str, processor_id: str, processor_version_id: str +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor + # e.g.: projects/project_id/locations/location/processors/processor_id + processor = client.processor_path(project_id, location, processor_id) + + # The full resource name of the processor version + # e.g.: projects/project_id/locations/location/processors/processor_id/processorVersions/processor_version_id + processor_version = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + request = documentai.SetDefaultProcessorVersionRequest( + processor=processor, default_processor_version=processor_version + ) + + # Make SetDefaultProcessorVersion request + try: + operation = client.set_default_processor_version(request) + # Print operation details + print(operation.operation.name) + # Wait for operation to complete + operation.result() + except NotFound as e: + print(e.message) + + +# [END documentai_set_default_processor_version] diff --git a/samples/snippets/set_default_processor_version_sample_test.py b/samples/snippets/set_default_processor_version_sample_test.py new file mode 100644 index 00000000..5750344d --- /dev/null +++ b/samples/snippets/set_default_processor_version_sample_test.py @@ -0,0 +1,44 @@ +# Copyright 2022 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 + +from samples.snippets import set_default_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "aeb8cea219b7c272" +current_default_processor_version = "pretrained-ocr-v1.0-2020-09-23" +new_default_processor_version = "pretrained-ocr-v1.1-2022-09-12" + + +def test_set_default_processor_version(capsys): + set_default_processor_version_sample.set_default_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=new_default_processor_version, + ) + out, _ = capsys.readouterr() + + assert "operation" in out + + # Set back to previous default + set_default_processor_version_sample.set_default_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=current_default_processor_version, + ) diff --git a/samples/snippets/undeploy_processor_version_sample.py b/samples/snippets/undeploy_processor_version_sample.py new file mode 100644 index 00000000..0ad19276 --- /dev/null +++ b/samples/snippets/undeploy_processor_version_sample.py @@ -0,0 +1,57 @@ +# Copyright 2022 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_undeploy_processor_version] + +from google.api_core.client_options import ClientOptions +from google.api_core.exceptions import FailedPrecondition, InvalidArgument +from google.cloud import documentai + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID' + + +def undeploy_processor_version_sample( + project_id: str, location: str, processor_id: str, processor_version_id: str +): + # You must set the api_endpoint if you use a location other than 'us', e.g.: + opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com") + + client = documentai.DocumentProcessorServiceClient(client_options=opts) + + # The full resource name of the processor version + # e.g.: projects/project_id/locations/location/processors/processor_id/processorVersions/processor_version_id + name = client.processor_version_path( + project_id, location, processor_id, processor_version_id + ) + + # Make UndeployProcessorVersion request + try: + operation = client.undeploy_processor_version(name=name) + # Print operation details + print(operation.operation.name) + # Wait for operation to complete + operation.result() + # Undeploy request will fail if the + # processor version is already undeployed + # or if a request is made on a pretrained processor version + except (FailedPrecondition, InvalidArgument) as e: + print(e.message) + + +# [END documentai_undeploy_processor_version] diff --git a/samples/snippets/undeploy_processor_version_sample_test.py b/samples/snippets/undeploy_processor_version_sample_test.py new file mode 100644 index 00000000..36f27438 --- /dev/null +++ b/samples/snippets/undeploy_processor_version_sample_test.py @@ -0,0 +1,48 @@ +# Copyright 2022 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 mock +from samples.snippets import undeploy_processor_version_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "aaaaaaaaa" +processor_version_id = "xxxxxxxxxx" + + +# TODO: Switch to Real Endpoint when Deployable Versions are Available +@mock.patch( + "google.cloud.documentai.DocumentProcessorServiceClient.undeploy_processor_version" +) +@mock.patch("google.api_core.operation.Operation") +def test_undeploy_processor_version( + operation_mock, undeploy_processor_version_mock, capsys +): + undeploy_processor_version_mock.return_value = operation_mock + + undeploy_processor_version_sample.undeploy_processor_version_sample( + project_id=project_id, + location=location, + processor_id=processor_id, + processor_version_id=processor_version_id, + ) + + undeploy_processor_version_mock.assert_called_once() + + out, _ = capsys.readouterr() + + assert "operation" in out From d65a0c76a60d47fb022eeee5427e4fa517d6a363 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Mon, 3 Oct 2022 11:07:05 -0400 Subject: [PATCH 5/6] fix(deps): require protobuf >= 3.20.2 (#385) * chore: exclude requirements.txt file from renovate-bot Source-Link: https://github.com/googleapis/synthtool/commit/f58d3135a2fab20e225d98741dbc06d57459b816 Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:7a40313731a7cb1454eef6b33d3446ebb121836738dc3ab3d2d3ded5268c35b6 * update constraints files * fix(deps): require protobuf 3.20.2 Co-authored-by: Owl Bot Co-authored-by: Anthonios Partheniou --- .github/.OwlBot.lock.yaml | 2 +- .kokoro/requirements.txt | 49 ++++++++++++++++++------------------- setup.py | 2 +- testing/constraints-3.7.txt | 2 +- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.github/.OwlBot.lock.yaml b/.github/.OwlBot.lock.yaml index aa547962..3815c983 100644 --- a/.github/.OwlBot.lock.yaml +++ b/.github/.OwlBot.lock.yaml @@ -13,4 +13,4 @@ # limitations under the License. docker: image: gcr.io/cloud-devrel-public-resources/owlbot-python:latest - digest: sha256:e09366bdf0fd9c8976592988390b24d53583dd9f002d476934da43725adbb978 + digest: sha256:7a40313731a7cb1454eef6b33d3446ebb121836738dc3ab3d2d3ded5268c35b6 diff --git a/.kokoro/requirements.txt b/.kokoro/requirements.txt index 385f2d4d..d15994ba 100644 --- a/.kokoro/requirements.txt +++ b/.kokoro/requirements.txt @@ -325,31 +325,30 @@ platformdirs==2.5.2 \ --hash=sha256:027d8e83a2d7de06bbac4e5ef7e023c02b863d7ea5d079477e722bb41ab25788 \ --hash=sha256:58c8abb07dcb441e6ee4b11d8df0ac856038f944ab98b7be6b27b2a3c7feef19 # via virtualenv -protobuf==3.20.1 \ - --hash=sha256:06059eb6953ff01e56a25cd02cca1a9649a75a7e65397b5b9b4e929ed71d10cf \ - --hash=sha256:097c5d8a9808302fb0da7e20edf0b8d4703274d140fd25c5edabddcde43e081f \ - --hash=sha256:284f86a6207c897542d7e956eb243a36bb8f9564c1742b253462386e96c6b78f \ - --hash=sha256:32ca378605b41fd180dfe4e14d3226386d8d1b002ab31c969c366549e66a2bb7 \ - --hash=sha256:3cc797c9d15d7689ed507b165cd05913acb992d78b379f6014e013f9ecb20996 \ - --hash=sha256:62f1b5c4cd6c5402b4e2d63804ba49a327e0c386c99b1675c8a0fefda23b2067 \ - --hash=sha256:69ccfdf3657ba59569c64295b7d51325f91af586f8d5793b734260dfe2e94e2c \ - --hash=sha256:6f50601512a3d23625d8a85b1638d914a0970f17920ff39cec63aaef80a93fb7 \ - --hash=sha256:7403941f6d0992d40161aa8bb23e12575637008a5a02283a930addc0508982f9 \ - --hash=sha256:755f3aee41354ae395e104d62119cb223339a8f3276a0cd009ffabfcdd46bb0c \ - --hash=sha256:77053d28427a29987ca9caf7b72ccafee011257561259faba8dd308fda9a8739 \ - --hash=sha256:7e371f10abe57cee5021797126c93479f59fccc9693dafd6bd5633ab67808a91 \ - --hash=sha256:9016d01c91e8e625141d24ec1b20fed584703e527d28512aa8c8707f105a683c \ - --hash=sha256:9be73ad47579abc26c12024239d3540e6b765182a91dbc88e23658ab71767153 \ - --hash=sha256:adc31566d027f45efe3f44eeb5b1f329da43891634d61c75a5944e9be6dd42c9 \ - --hash=sha256:adfc6cf69c7f8c50fd24c793964eef18f0ac321315439d94945820612849c388 \ - --hash=sha256:af0ebadc74e281a517141daad9d0f2c5d93ab78e9d455113719a45a49da9db4e \ - --hash=sha256:cb29edb9eab15742d791e1025dd7b6a8f6fcb53802ad2f6e3adcb102051063ab \ - --hash=sha256:cd68be2559e2a3b84f517fb029ee611546f7812b1fdd0aa2ecc9bc6ec0e4fdde \ - --hash=sha256:cdee09140e1cd184ba9324ec1df410e7147242b94b5f8b0c64fc89e38a8ba531 \ - --hash=sha256:db977c4ca738dd9ce508557d4fce0f5aebd105e158c725beec86feb1f6bc20d8 \ - --hash=sha256:dd5789b2948ca702c17027c84c2accb552fc30f4622a98ab5c51fcfe8c50d3e7 \ - --hash=sha256:e250a42f15bf9d5b09fe1b293bdba2801cd520a9f5ea2d7fb7536d4441811d20 \ - --hash=sha256:ff8d8fa42675249bb456f5db06c00de6c2f4c27a065955917b28c4f15978b9c3 +protobuf==3.20.2 \ + --hash=sha256:03d76b7bd42ac4a6e109742a4edf81ffe26ffd87c5993126d894fe48a120396a \ + --hash=sha256:09e25909c4297d71d97612f04f41cea8fa8510096864f2835ad2f3b3df5a5559 \ + --hash=sha256:18e34a10ae10d458b027d7638a599c964b030c1739ebd035a1dfc0e22baa3bfe \ + --hash=sha256:291fb4307094bf5ccc29f424b42268640e00d5240bf0d9b86bf3079f7576474d \ + --hash=sha256:2c0b040d0b5d5d207936ca2d02f00f765906622c07d3fa19c23a16a8ca71873f \ + --hash=sha256:384164994727f274cc34b8abd41a9e7e0562801361ee77437099ff6dfedd024b \ + --hash=sha256:3cb608e5a0eb61b8e00fe641d9f0282cd0eedb603be372f91f163cbfbca0ded0 \ + --hash=sha256:5d9402bf27d11e37801d1743eada54372f986a372ec9679673bfcc5c60441151 \ + --hash=sha256:712dca319eee507a1e7df3591e639a2b112a2f4a62d40fe7832a16fd19151750 \ + --hash=sha256:7a5037af4e76c975b88c3becdf53922b5ffa3f2cddf657574a4920a3b33b80f3 \ + --hash=sha256:8228e56a865c27163d5d1d1771d94b98194aa6917bcfb6ce139cbfa8e3c27334 \ + --hash=sha256:84a1544252a933ef07bb0b5ef13afe7c36232a774affa673fc3636f7cee1db6c \ + --hash=sha256:84fe5953b18a383fd4495d375fe16e1e55e0a3afe7b4f7b4d01a3a0649fcda9d \ + --hash=sha256:9c673c8bfdf52f903081816b9e0e612186684f4eb4c17eeb729133022d6032e3 \ + --hash=sha256:9f876a69ca55aed879b43c295a328970306e8e80a263ec91cf6e9189243c613b \ + --hash=sha256:a9e5ae5a8e8985c67e8944c23035a0dff2c26b0f5070b2f55b217a1c33bbe8b1 \ + --hash=sha256:b4fdb29c5a7406e3f7ef176b2a7079baa68b5b854f364c21abe327bbeec01cdb \ + --hash=sha256:c184485e0dfba4dfd451c3bd348c2e685d6523543a0f91b9fd4ae90eb09e8422 \ + --hash=sha256:c9cdf251c582c16fd6a9f5e95836c90828d51b0069ad22f463761d27c6c19019 \ + --hash=sha256:e39cf61bb8582bda88cdfebc0db163b774e7e03364bbf9ce1ead13863e81e359 \ + --hash=sha256:e8fbc522303e09036c752a0afcc5c0603e917222d8bedc02813fd73b4b4ed804 \ + --hash=sha256:f34464ab1207114e73bba0794d1257c150a2b89b7a9faf504e00af7c9fd58978 \ + --hash=sha256:f52dabc96ca99ebd2169dadbe018824ebda08a795c7684a0b7d203a290f3adb0 # via # gcp-docuploader # gcp-releasetool diff --git a/setup.py b/setup.py index 11a7fe50..c6d66ac6 100644 --- a/setup.py +++ b/setup.py @@ -48,7 +48,7 @@ install_requires=( "google-api-core[grpc] >= 1.32.0, <3.0.0dev,!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.*,!=2.4.*,!=2.5.*,!=2.6.*,!=2.7.*", "proto-plus >= 1.22.0, <2.0.0dev", - "protobuf >= 3.19.0, <5.0.0dev", + "protobuf >= 3.20.2, <5.0.0dev", ), python_requires=">=3.7", classifiers=[ diff --git a/testing/constraints-3.7.txt b/testing/constraints-3.7.txt index 51901fbc..260e08b9 100644 --- a/testing/constraints-3.7.txt +++ b/testing/constraints-3.7.txt @@ -7,4 +7,4 @@ # Then this file should have foo==1.14.0 google-api-core==1.32.0 proto-plus==1.22.0 -protobuf==3.19.0 +protobuf==3.20.2 From ef40f07384ac13346733fa8bec2723fa68bff203 Mon Sep 17 00:00:00 2001 From: "release-please[bot]" <55107282+release-please[bot]@users.noreply.github.com> Date: Mon, 3 Oct 2022 21:02:55 -0400 Subject: [PATCH 6/6] chore(main): release 2.0.2 (#384) Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com> --- CHANGELOG.md | 12 ++++++++++++ setup.py | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e56c357e..0dbe4cc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## [2.0.2](https://github.com/googleapis/python-documentai/compare/v2.0.1...v2.0.2) (2022-10-03) + + +### Bug Fixes + +* **deps:** Require protobuf >= 3.20.2 ([#385](https://github.com/googleapis/python-documentai/issues/385)) ([d65a0c7](https://github.com/googleapis/python-documentai/commit/d65a0c76a60d47fb022eeee5427e4fa517d6a363)) + + +### Documentation + +* **samples:** Added Processor Version Samples ([#382](https://github.com/googleapis/python-documentai/issues/382)) ([f9ce801](https://github.com/googleapis/python-documentai/commit/f9ce801119f96b632fddb73a4d314be1bb188639)) + ## [2.0.1](https://github.com/googleapis/python-documentai/compare/v2.0.0...v2.0.1) (2022-09-13) diff --git a/setup.py b/setup.py index c6d66ac6..cdda178e 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ import setuptools # type: ignore -version = "2.0.1" +version = "2.0.2" package_root = os.path.abspath(os.path.dirname(__file__)) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy