From 74f22495da86c16338ef229ccb5eebd81d36c498 Mon Sep 17 00:00:00 2001 From: Holt Skinner <13262395+holtskinner@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:57:01 -0500 Subject: [PATCH] docs(samples): Updated Samples for v2.0.0 Client Library (#365) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * docs(samples): Updated Samples for v2.0.0 Client Library - Added Examples for FetchProcessorTypes, CreateProcessor, ListProcessors, DeleteProcessor, DisableProcessor, EnableProcessor - Added example parameters for processorVersion and FieldMask - Removed references to 'v1' in imports (default is v1) - Changed File types Documentation link in comments - Corrected Human Review test method name * docs(samples): Added Sample Code for Operations Functions * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * docs(samples): Added GetProcessor Sample - Also removed extra test function call * docs(samples): Addressed unit test issues * docs(samples): Adjusted imports to resovle presubmit errors Co-authored-by: Anthonios Partheniou Co-authored-by: Owl Bot --- .../batch_process_documents_sample.py | 14 ++++- ...process_documents_sample_bad_input_test.py | 4 +- samples/snippets/cancel_operation_sample.py | 44 +++++++++++++++ .../snippets/cancel_operation_sample_test.py | 33 +++++++++++ samples/snippets/create_processor_sample.py | 54 ++++++++++++++++++ .../snippets/create_processor_sample_test.py | 47 ++++++++++++++++ samples/snippets/delete_processor_sample.py | 49 +++++++++++++++++ .../snippets/delete_processor_sample_test.py | 41 ++++++++++++++ samples/snippets/disable_processor_sample.py | 52 ++++++++++++++++++ .../snippets/disable_processor_sample_test.py | 38 +++++++++++++ samples/snippets/enable_processor_sample.py | 52 ++++++++++++++++++ .../snippets/enable_processor_sample_test.py | 38 +++++++++++++ .../snippets/fetch_processor_types_sample.py | 46 ++++++++++++++++ .../fetch_processor_types_sample_test.py | 31 +++++++++++ samples/snippets/get_operation_sample.py | 42 ++++++++++++++ samples/snippets/get_operation_sample_test.py | 32 +++++++++++ samples/snippets/get_processor_sample.py | 46 ++++++++++++++++ samples/snippets/get_processor_sample_test.py | 34 ++++++++++++ samples/snippets/list_operations_sample.py | 55 +++++++++++++++++++ .../snippets/list_operations_sample_test.py | 32 +++++++++++ samples/snippets/list_processors_sample.py | 47 ++++++++++++++++ .../snippets/list_processors_sample_test.py | 33 +++++++++++ samples/snippets/poll_operation_sample.py | 52 ++++++++++++++++++ .../snippets/poll_operation_sample_test.py | 32 +++++++++++ .../snippets/process_document_form_sample.py | 6 +- .../snippets/process_document_ocr_sample.py | 8 +-- .../process_document_quality_sample.py | 6 +- samples/snippets/process_document_sample.py | 29 ++++++++-- .../process_document_specialized_sample.py | 6 +- .../process_document_splitter_sample.py | 6 +- samples/snippets/quickstart_sample.py | 6 +- samples/snippets/requirements-test.txt | 1 + samples/snippets/review_document_sample.py | 4 +- .../snippets/review_document_sample_test.py | 2 +- 34 files changed, 989 insertions(+), 33 deletions(-) create mode 100644 samples/snippets/cancel_operation_sample.py create mode 100644 samples/snippets/cancel_operation_sample_test.py create mode 100644 samples/snippets/create_processor_sample.py create mode 100644 samples/snippets/create_processor_sample_test.py create mode 100644 samples/snippets/delete_processor_sample.py create mode 100644 samples/snippets/delete_processor_sample_test.py create mode 100644 samples/snippets/disable_processor_sample.py create mode 100644 samples/snippets/disable_processor_sample_test.py create mode 100644 samples/snippets/enable_processor_sample.py create mode 100644 samples/snippets/enable_processor_sample_test.py create mode 100644 samples/snippets/fetch_processor_types_sample.py create mode 100644 samples/snippets/fetch_processor_types_sample_test.py create mode 100644 samples/snippets/get_operation_sample.py create mode 100644 samples/snippets/get_operation_sample_test.py create mode 100644 samples/snippets/get_processor_sample.py create mode 100644 samples/snippets/get_processor_sample_test.py create mode 100644 samples/snippets/list_operations_sample.py create mode 100644 samples/snippets/list_operations_sample_test.py create mode 100644 samples/snippets/list_processors_sample.py create mode 100644 samples/snippets/list_processors_sample_test.py create mode 100644 samples/snippets/poll_operation_sample.py create mode 100644 samples/snippets/poll_operation_sample_test.py diff --git a/samples/snippets/batch_process_documents_sample.py b/samples/snippets/batch_process_documents_sample.py index ec335b81..f3e01936 100644 --- a/samples/snippets/batch_process_documents_sample.py +++ b/samples/snippets/batch_process_documents_sample.py @@ -17,13 +17,13 @@ import re from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai -from google.cloud import storage +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' # Create processor in Cloud Console +# 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 @@ -76,6 +76,14 @@ def batch_process_documents( # 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 20e1fb1c..7d2ffd75 100644 --- a/samples/snippets/batch_process_documents_sample_bad_input_test.py +++ b/samples/snippets/batch_process_documents_sample_bad_input_test.py @@ -42,6 +42,6 @@ def test_batch_process_documents_with_bad_input(capsys): timeout=450, ) out, _ = capsys.readouterr() - assert "Failed to process" in out + assert "Failed" in out except Exception as e: - assert "Failed to process" in e.message + assert "Internal error" in e.message diff --git a/samples/snippets/cancel_operation_sample.py b/samples/snippets/cancel_operation_sample.py new file mode 100644 index 00000000..c5797d6e --- /dev/null +++ b/samples/snippets/cancel_operation_sample.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. +# + +# [START documentai_cancel_operation] + +from google.api_core.client_options import ClientOptions +from google.api_core.exceptions import FailedPrecondition, NotFound +from google.cloud import documentai +from google.longrunning.operations_pb2 import CancelOperationRequest + +# TODO(developer): Uncomment these variables before running the sample. +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# operation_name = 'YOUR_OPERATION_NAME' # Format is 'projects/project_id/locations/location/operations/operation_id' + + +def cancel_operation_sample(location: str, operation_name: 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) + + request = CancelOperationRequest(name=operation_name) + + # Make CancelOperation request + try: + client.cancel_operation(request=request) + print(f"Operation {operation_name} cancelled") + except (FailedPrecondition, NotFound) as e: + print(e.message) + + +# [END documentai_cancel_operation] diff --git a/samples/snippets/cancel_operation_sample_test.py b/samples/snippets/cancel_operation_sample_test.py new file mode 100644 index 00000000..5d403be0 --- /dev/null +++ b/samples/snippets/cancel_operation_sample_test.py @@ -0,0 +1,33 @@ +# 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 cancel_operation_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +operation_id = "4311241022337572151" +operation_name = f"projects/{project_id}/locations/{location}/operations/{operation_id}" + + +def test_cancel_operation(capsys): + cancel_operation_sample.cancel_operation_sample( + location=location, operation_name=operation_name + ) + out, _ = capsys.readouterr() + + assert "Operation" in out + assert "cancelled" in out diff --git a/samples/snippets/create_processor_sample.py b/samples/snippets/create_processor_sample.py new file mode 100644 index 00000000..916dbe4a --- /dev/null +++ b/samples/snippets/create_processor_sample.py @@ -0,0 +1,54 @@ +# 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_create_processor] + +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_display_name = 'YOUR_PROCESSOR_DISPLAY_NAME' # Must be unique per project, e.g.: 'My Processor' +# processor_type = 'YOUR_PROCESSOR_TYPE' # Use fetch_processor_types to get available processor types + + +def create_processor_sample( + project_id: str, location: str, processor_display_name: str, processor_type: 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 location + # e.g.: projects/project_id/locations/location + parent = client.common_location_path(project_id, location) + + # Create a processor + processor = client.create_processor( + parent=parent, + processor=documentai.Processor( + display_name=processor_display_name, type_=processor_type + ), + ) + + # Print the processor information + print(f"Processor Name: {processor.name}") + print(f"Processor Display Name: {processor.display_name}") + print(f"Processor Type: {processor.type_}") + + +# [END documentai_create_processor] diff --git a/samples/snippets/create_processor_sample_test.py b/samples/snippets/create_processor_sample_test.py new file mode 100644 index 00000000..4c4fd7ff --- /dev/null +++ b/samples/snippets/create_processor_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 +from uuid import uuid4 + +import mock + +from samples.snippets import create_processor_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_display_name = f"test-processor-{uuid4()}" +processor_type = "OCR_PROCESSOR" + + +@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.create_processor") +@mock.patch("google.cloud.documentai.Processor") +def test_create_processor(create_processor_mock, processor_mock, capsys): + create_processor_mock.return_value = processor_mock + + create_processor_sample.create_processor_sample( + project_id=project_id, + location=location, + processor_display_name=processor_display_name, + processor_type=processor_type, + ) + + create_processor_mock.assert_called_once() + + out, _ = capsys.readouterr() + + assert "Processor Name:" in out + assert "Processor Display Name:" in out + assert "Processor Type:" in out diff --git a/samples/snippets/delete_processor_sample.py b/samples/snippets/delete_processor_sample.py new file mode 100644 index 00000000..fe86b57a --- /dev/null +++ b/samples/snippets/delete_processor_sample.py @@ -0,0 +1,49 @@ +# 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] + +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' + + +def delete_processor_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 + processor_name = client.processor_path(project_id, location, processor_id) + + # Delete a processor + try: + operation = client.delete_processor(name=processor_name) + # Print operation details + print(operation.operation.name) + # Wait for operation to complete + operation.result() + except NotFound as e: + print(e.message) + + +# [END documentai_delete_processor] diff --git a/samples/snippets/delete_processor_sample_test.py b/samples/snippets/delete_processor_sample_test.py new file mode 100644 index 00000000..1a2ed3f4 --- /dev/null +++ b/samples/snippets/delete_processor_sample_test.py @@ -0,0 +1,41 @@ +# 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_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "aaaaaaaaa" +parent = f"projects/{project_id}/locations/{location}/processors/{processor_id}" + + +@mock.patch("google.cloud.documentai.DocumentProcessorServiceClient.delete_processor") +@mock.patch("google.api_core.operation.Operation") +def test_delete_processor(operation_mock, delete_processor_mock, capsys): + delete_processor_mock.return_value = operation_mock + + delete_processor_sample.delete_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) + + delete_processor_mock.assert_called_once() + + out, _ = capsys.readouterr() + + assert "operation" in out diff --git a/samples/snippets/disable_processor_sample.py b/samples/snippets/disable_processor_sample.py new file mode 100644 index 00000000..2b99e7d8 --- /dev/null +++ b/samples/snippets/disable_processor_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_disable_processor] + +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' + + +def disable_processor_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 + processor_name = client.processor_path(project_id, location, processor_id) + request = documentai.DisableProcessorRequest(name=processor_name) + + # Make DisableProcessor request + try: + operation = client.disable_processor(request=request) + + # Print operation name + print(operation.operation.name) + # Wait for operation to complete + operation.result() + # Cannot disable a processor that is already disabled + except FailedPrecondition as e: + print(e.message) + + +# [END documentai_disable_processor] diff --git a/samples/snippets/disable_processor_sample_test.py b/samples/snippets/disable_processor_sample_test.py new file mode 100644 index 00000000..0420ee48 --- /dev/null +++ b/samples/snippets/disable_processor_sample_test.py @@ -0,0 +1,38 @@ +# 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 disable_processor_sample, enable_processor_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "5e32eb3e1d0237c0" + + +def test_disable_processor(capsys): + disable_processor_sample.disable_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) + out, _ = capsys.readouterr() + + assert "projects" in out + assert "locations" in out + assert "operations" in out + + # Re-Enable Processor + enable_processor_sample.enable_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) diff --git a/samples/snippets/enable_processor_sample.py b/samples/snippets/enable_processor_sample.py new file mode 100644 index 00000000..42fddf77 --- /dev/null +++ b/samples/snippets/enable_processor_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_enable_processor] + +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' + + +def enable_processor_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 location + # e.g.: projects/project_id/locations/location/processors/processor_id + processor_name = client.processor_path(project_id, location, processor_id) + request = documentai.EnableProcessorRequest(name=processor_name) + + # Make EnableProcessor request + try: + operation = client.enable_processor(request=request) + + # Print operation name + print(operation.operation.name) + # Wait for operation to complete + operation.result() + # Cannot enable a processor that is already enabled + except FailedPrecondition as e: + print(e.message) + + +# [END documentai_enable_processor] diff --git a/samples/snippets/enable_processor_sample_test.py b/samples/snippets/enable_processor_sample_test.py new file mode 100644 index 00000000..0a238cd0 --- /dev/null +++ b/samples/snippets/enable_processor_sample_test.py @@ -0,0 +1,38 @@ +# 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 disable_processor_sample, enable_processor_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "351535be16606fe3" + + +def test_enable_processor(capsys): + enable_processor_sample.enable_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) + out, _ = capsys.readouterr() + + assert "projects" in out + assert "locations" in out + assert "operations" in out + + # Re-Disable Processor + disable_processor_sample.disable_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) diff --git a/samples/snippets/fetch_processor_types_sample.py b/samples/snippets/fetch_processor_types_sample.py new file mode 100644 index 00000000..cd6ead86 --- /dev/null +++ b/samples/snippets/fetch_processor_types_sample.py @@ -0,0 +1,46 @@ +# 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_fetch_processor_types] + +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' + + +def fetch_processor_types_sample(project_id: str, location: 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 location + # e.g.: projects/project_id/locations/location + parent = client.common_location_path(project_id, location) + + # Fetch all processor types + response = client.fetch_processor_types(parent=parent) + + print("Processor types:") + # Print the available processor types + for processor_type in response.processor_types: + if processor_type.allow_creation: + print(processor_type.type_) + + +# [END documentai_fetch_processor_types] diff --git a/samples/snippets/fetch_processor_types_sample_test.py b/samples/snippets/fetch_processor_types_sample_test.py new file mode 100644 index 00000000..752e9045 --- /dev/null +++ b/samples/snippets/fetch_processor_types_sample_test.py @@ -0,0 +1,31 @@ +# 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 fetch_processor_types_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] + + +def test_fetch_processor_types(capsys): + fetch_processor_types_sample.fetch_processor_types_sample( + project_id=project_id, location=location + ) + out, _ = capsys.readouterr() + + assert "OCR_PROCESSOR" in out + assert "FORM_PARSER_PROCESSOR" in out diff --git a/samples/snippets/get_operation_sample.py b/samples/snippets/get_operation_sample.py new file mode 100644 index 00000000..a569e816 --- /dev/null +++ b/samples/snippets/get_operation_sample.py @@ -0,0 +1,42 @@ +# 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_operation] + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai +from google.longrunning.operations_pb2 import GetOperationRequest + +# TODO(developer): Uncomment these variables before running the sample. +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# operation_name = 'YOUR_OPERATION_NAME' # Format is 'projects/project_id/locations/location/operations/operation_id' + + +def get_operation_sample(location: str, operation_name: 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) + + request = GetOperationRequest(name=operation_name) + + # Make GetOperation request + operation = client.get_operation(request=request) + + # Print the Operation Information + print(operation) + + +# [END documentai_get_operation] diff --git a/samples/snippets/get_operation_sample_test.py b/samples/snippets/get_operation_sample_test.py new file mode 100644 index 00000000..77219f64 --- /dev/null +++ b/samples/snippets/get_operation_sample_test.py @@ -0,0 +1,32 @@ +# 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_operation_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +operation_id = "10828996427112056798" +operation_name = f"projects/{project_id}/locations/{location}/operations/{operation_id}" + + +def test_get_operation(capsys): + get_operation_sample.get_operation_sample( + location=location, operation_name=operation_name + ) + out, _ = capsys.readouterr() + + assert "operation" in out diff --git a/samples/snippets/get_processor_sample.py b/samples/snippets/get_processor_sample.py new file mode 100644 index 00000000..580b7e71 --- /dev/null +++ b/samples/snippets/get_processor_sample.py @@ -0,0 +1,46 @@ +# 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] + +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' + + +def get_processor_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/processor/processor_id + name = client.processor_path(project_id, location, processor_id) + + # Make GetProcessor request + processor = client.get_processor(name=name) + + # Print the processor information + print(f"Processor Name: {processor.name}") + print(f"Processor Display Name: {processor.display_name}") + print(f"Processor Type: {processor.type_}") + + +# [END documentai_get_processor] diff --git a/samples/snippets/get_processor_sample_test.py b/samples/snippets/get_processor_sample_test.py new file mode 100644 index 00000000..b1848422 --- /dev/null +++ b/samples/snippets/get_processor_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 get_processor_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +processor_id = "91e072f8626a76b7" + + +def test_get_processor(capsys): + get_processor_sample.get_processor_sample( + project_id=project_id, location=location, processor_id=processor_id + ) + out, _ = capsys.readouterr() + + assert "Processor Name:" in out + assert "Processor Display Name:" in out + assert "OCR_PROCESSOR" in out + assert processor_id in out diff --git a/samples/snippets/list_operations_sample.py b/samples/snippets/list_operations_sample.py new file mode 100644 index 00000000..5235e757 --- /dev/null +++ b/samples/snippets/list_operations_sample.py @@ -0,0 +1,55 @@ +# 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_operations] + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai +from google.longrunning.operations_pb2 import ListOperationsRequest + +# TODO(developer): Uncomment these variables before running the sample. +# project_id = 'YOUR_PROJECT_ID' +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' + +# Create filter in https://google.aip.dev/160 syntax +# For full options, refer to: +# https://cloud.google.com/document-ai/docs/long-running-operations#listing_long-running_operations +# operations_filter = 'YOUR_FILTER' + +# Example: +# operations_filter = "TYPE=BATCH_PROCESS_DOCUMENTS AND STATE=RUNNING" + + +def list_operations_sample(project_id: str, location: str, operations_filter: 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) + + # Format: projects/project_id/locations/location + name = client.common_location_path(project=project_id, location=location) + request = ListOperationsRequest( + name=f"{name}/operations", + filter=operations_filter, + ) + + # Make ListOperations request + operations = client.list_operations(request=request) + + # Print the Operation Information + print(operations) + + +# [END documentai_list_operations] diff --git a/samples/snippets/list_operations_sample_test.py b/samples/snippets/list_operations_sample_test.py new file mode 100644 index 00000000..e82e6dab --- /dev/null +++ b/samples/snippets/list_operations_sample_test.py @@ -0,0 +1,32 @@ +# 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_operations_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +operations_filter = "TYPE=BATCH_PROCESS_DOCUMENTS AND STATE=DONE" + + +def test_list_operations(capsys): + list_operations_sample.list_operations_sample( + project_id=project_id, location=location, operations_filter=operations_filter + ) + out, _ = capsys.readouterr() + + assert "operations" in out + assert "BatchProcessMetadata" in out diff --git a/samples/snippets/list_processors_sample.py b/samples/snippets/list_processors_sample.py new file mode 100644 index 00000000..06515171 --- /dev/null +++ b/samples/snippets/list_processors_sample.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. +# + +# [START documentai_list_processors] + +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' + + +def list_processors_sample(project_id: str, location: 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 location + # e.g.: projects/project_id/locations/location + parent = client.common_location_path(project_id, location) + + # Make ListProcessors request + processor_list = client.list_processors(parent=parent) + + # Print the processor information + for processor in processor_list: + print(f"Processor Name: {processor.name}") + print(f"Processor Display Name: {processor.display_name}") + print(f"Processor Type: {processor.type_}") + print("") + + +# [END documentai_list_processors] diff --git a/samples/snippets/list_processors_sample_test.py b/samples/snippets/list_processors_sample_test.py new file mode 100644 index 00000000..09da9262 --- /dev/null +++ b/samples/snippets/list_processors_sample_test.py @@ -0,0 +1,33 @@ +# 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_processors_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] + + +def test_list_processors(capsys): + list_processors_sample.list_processors_sample( + project_id=project_id, location=location + ) + out, _ = capsys.readouterr() + + assert "Processor Name:" in out + assert "Processor Display Name:" in out + assert "OCR_PROCESSOR" in out + assert "FORM_PARSER_PROCESSOR" in out diff --git a/samples/snippets/poll_operation_sample.py b/samples/snippets/poll_operation_sample.py new file mode 100644 index 00000000..c3e7cc60 --- /dev/null +++ b/samples/snippets/poll_operation_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_poll_operation] + +from time import sleep + +from google.api_core.client_options import ClientOptions +from google.cloud import documentai +from google.longrunning.operations_pb2 import GetOperationRequest + +# TODO(developer): Uncomment these variables before running the sample. +# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu' +# operation_name = 'YOUR_OPERATION_NAME' # Format is 'projects/project_id/locations/location/operations/operation_id' + + +def poll_operation_sample(location: str, operation_name: 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) + + request = GetOperationRequest(name=operation_name) + + while True: + # Make GetOperation request + operation = client.get_operation(request=request) + + # Print the Operation Information + print(operation) + + # Stop Polling when Operation is no longer running + if operation.done: + break + + # Wait 10 seconds before polling again + sleep(10) + + +# [END documentai_poll_operation] diff --git a/samples/snippets/poll_operation_sample_test.py b/samples/snippets/poll_operation_sample_test.py new file mode 100644 index 00000000..4f481b5c --- /dev/null +++ b/samples/snippets/poll_operation_sample_test.py @@ -0,0 +1,32 @@ +# 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 poll_operation_sample + +location = "us" +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] +operation_id = "10828996427112056798" +operation_name = f"projects/{project_id}/locations/{location}/operations/{operation_id}" + + +def test_poll_operation(capsys): + poll_operation_sample.poll_operation_sample( + location=location, operation_name=operation_name + ) + out, _ = capsys.readouterr() + + assert "operation" in out diff --git a/samples/snippets/process_document_form_sample.py b/samples/snippets/process_document_form_sample.py index 78e8bad9..7922490f 100644 --- a/samples/snippets/process_document_form_sample.py +++ b/samples/snippets/process_document_form_sample.py @@ -18,14 +18,14 @@ from typing import Sequence from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def process_document_form_sample( diff --git a/samples/snippets/process_document_ocr_sample.py b/samples/snippets/process_document_ocr_sample.py index 1966ebe3..a48d2178 100644 --- a/samples/snippets/process_document_ocr_sample.py +++ b/samples/snippets/process_document_ocr_sample.py @@ -18,14 +18,14 @@ from typing import Sequence from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def process_document_ocr_sample( @@ -63,7 +63,7 @@ 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 + # You must create processors before running sample code. name = client.processor_path(project_id, location, processor_id) # Read the file into memory diff --git a/samples/snippets/process_document_quality_sample.py b/samples/snippets/process_document_quality_sample.py index 46371886..0bbca75d 100644 --- a/samples/snippets/process_document_quality_sample.py +++ b/samples/snippets/process_document_quality_sample.py @@ -16,14 +16,14 @@ # [START documentai_process_quality_document] from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def process_document_quality_sample( diff --git a/samples/snippets/process_document_sample.py b/samples/snippets/process_document_sample.py index 0f804777..13d7850e 100644 --- a/samples/snippets/process_document_sample.py +++ b/samples/snippets/process_document_sample.py @@ -16,18 +16,25 @@ # [START documentai_process_document] from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample +# processor_version = "pretrained" # Optional. Processor version to use # file_path = '/path/to/local/pdf' -# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/processors-list for supported file types +# 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_sample( - project_id: str, location: str, processor_id: str, file_path: str, mime_type: str + project_id: str, + location: str, + processor_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") @@ -35,10 +42,18 @@ def process_document_sample( client = documentai.DocumentProcessorServiceClient(client_options=opts) # The full resource name of the processor, e.g.: - # projects/project_id/locations/location/processor/processor_id + # 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() @@ -47,7 +62,9 @@ def process_document_sample( raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type) # Configure the process request - request = documentai.ProcessRequest(name=name, raw_document=raw_document) + request = documentai.ProcessRequest( + name=name, raw_document=raw_document, field_mask=field_mask + ) result = client.process_document(request=request) diff --git a/samples/snippets/process_document_specialized_sample.py b/samples/snippets/process_document_specialized_sample.py index e2b3b0b0..c78ca704 100644 --- a/samples/snippets/process_document_specialized_sample.py +++ b/samples/snippets/process_document_specialized_sample.py @@ -17,14 +17,14 @@ from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def process_document_specialized_sample( diff --git a/samples/snippets/process_document_splitter_sample.py b/samples/snippets/process_document_splitter_sample.py index a726acd0..b78c557c 100644 --- a/samples/snippets/process_document_splitter_sample.py +++ b/samples/snippets/process_document_splitter_sample.py @@ -18,14 +18,14 @@ from typing import Sequence from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def process_document_splitter_sample( diff --git a/samples/snippets/quickstart_sample.py b/samples/snippets/quickstart_sample.py index a300f543..23dcf084 100644 --- a/samples/snippets/quickstart_sample.py +++ b/samples/snippets/quickstart_sample.py @@ -18,14 +18,14 @@ from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# 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/processors-list for supported file types +# mime_type = 'application/pdf' # Refer to https://cloud.google.com/document-ai/docs/file-types for supported file types def quickstart( diff --git a/samples/snippets/requirements-test.txt b/samples/snippets/requirements-test.txt index e0716850..6f722c66 100644 --- a/samples/snippets/requirements-test.txt +++ b/samples/snippets/requirements-test.txt @@ -1 +1,2 @@ pytest==7.1.3 +mock==4.0.3 diff --git a/samples/snippets/review_document_sample.py b/samples/snippets/review_document_sample.py index 6e96a1ea..2bfa4dcd 100644 --- a/samples/snippets/review_document_sample.py +++ b/samples/snippets/review_document_sample.py @@ -16,12 +16,12 @@ # [START documentai_review_document] from google.api_core.client_options import ClientOptions -from google.cloud import documentai_v1 as documentai +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 in Cloud Console +# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample # file_path = '/path/to/local/pdf' # mime_type = 'application/pdf' # https://cloud.google.com/document-ai/docs/file-types diff --git a/samples/snippets/review_document_sample_test.py b/samples/snippets/review_document_sample_test.py index 2ce0f478..78fcc8fa 100644 --- a/samples/snippets/review_document_sample_test.py +++ b/samples/snippets/review_document_sample_test.py @@ -24,7 +24,7 @@ mime_type = "application/pdf" -def test_process_documents(capsys): +def test_review_document(capsys): review_document_sample.review_document_sample( project_id=project_id, location=location,