Skip to content

Commit 648993b

Browse files
holtskinnertelpirion
authored andcommitted
docs(samples): Document AI - Added Test & Evaluation Samples for v1beta3 2.3.0 (#8876)
- Replacement for https://github.com/googleapis/python-documentai/pull/417 ## Checklist - [X] I have followed [Sample Guidelines from AUTHORING_GUIDE.MD](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md) - [X] README is updated to include [all relevant information](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#readme-file) - [X] **Tests** pass: `nox -s py-3.9` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [X] **Lint** pass: `nox -s lint` (see [Test Environment Setup](https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md#test-environment-setup)) - [X] Please **merge** this PR for me once it is approved.
1 parent 375e9b0 commit 648993b

9 files changed

+483
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_evaluate_processor_version]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import documentai_v1beta3 as documentai
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_id = 'YOUR_PROJECT_ID'
23+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
24+
# processor_id = 'YOUR_PROCESSOR_ID'
25+
# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID'
26+
# gcs_input_uri = # Format: gs://bucket/directory/
27+
28+
29+
def evaluate_processor_version_sample(
30+
project_id: str,
31+
location: str,
32+
processor_id: str,
33+
processor_version_id: str,
34+
gcs_input_uri: str,
35+
):
36+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
37+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
38+
39+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
40+
41+
# The full resource name of the processor version
42+
# e.g. `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`
43+
name = client.processor_version_path(
44+
project_id, location, processor_id, processor_version_id
45+
)
46+
47+
evaluation_documents = documentai.BatchDocumentsInputConfig(
48+
gcs_prefix=documentai.GcsPrefix(gcs_uri_prefix=gcs_input_uri)
49+
)
50+
51+
# NOTE: Alternatively, specify a list of GCS Documents
52+
#
53+
# gcs_input_uri = "gs://bucket/directory/file.pdf"
54+
# input_mime_type = "application/pdf"
55+
#
56+
# gcs_document = documentai.GcsDocument(
57+
# gcs_uri=gcs_input_uri, mime_type=input_mime_type
58+
# )
59+
# gcs_documents = [gcs_document]
60+
# evaluation_documents = documentai.BatchDocumentsInputConfig(
61+
# gcs_documents=documentai.GcsDocuments(documents=gcs_documents)
62+
# )
63+
#
64+
65+
request = documentai.EvaluateProcessorVersionRequest(
66+
processor_version=name,
67+
evaluation_documents=evaluation_documents,
68+
)
69+
70+
# Make EvaluateProcessorVersion request
71+
# Continually polls the operation until it is complete.
72+
# This could take some time for larger files
73+
operation = client.evaluate_processor_version(request=request)
74+
# Print operation details
75+
# Format: projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_ID
76+
print(f"Waiting for operation {operation.operation.name} to complete...")
77+
# Wait for operation to complete
78+
response = documentai.EvaluateProcessorVersionResponse(operation.result())
79+
80+
# Once the operation is complete,
81+
# Print evaluation ID from operation response
82+
print(f"Evaluation Complete: {response.evaluation}")
83+
84+
85+
# [END documentai_evaluate_processor_version]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
18+
from documentai.snippets import evaluate_processor_version_sample
19+
20+
import mock
21+
22+
location = "us"
23+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
processor_id = "aaaaaaaaa"
25+
processor_version_id = "xxxxxxxxxx"
26+
gcs_input_uri = "gs://bucket/directory/"
27+
28+
29+
# Mocking request as evaluation can take a long time
30+
@mock.patch(
31+
"google.cloud.documentai_v1beta3.DocumentProcessorServiceClient.evaluate_processor_version"
32+
)
33+
@mock.patch("google.cloud.documentai_v1beta3.EvaluateProcessorVersionResponse")
34+
@mock.patch("google.api_core.operation.Operation")
35+
def test_evaluate_processor_version(
36+
operation_mock,
37+
evaluate_processor_version_response_mock,
38+
evaluate_processor_version_mock,
39+
capsys,
40+
):
41+
operation_mock.result.return_value = evaluate_processor_version_response_mock
42+
evaluate_processor_version_mock.return_value = operation_mock
43+
44+
evaluate_processor_version_sample.evaluate_processor_version_sample(
45+
project_id=project_id,
46+
location=location,
47+
processor_id=processor_id,
48+
processor_version_id=processor_version_id,
49+
gcs_input_uri=gcs_input_uri,
50+
)
51+
52+
evaluate_processor_version_mock.assert_called_once()
53+
54+
out, _ = capsys.readouterr()
55+
56+
assert "operation" in out
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_get_evaluation]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import documentai_v1beta3 as documentai
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_id = 'YOUR_PROJECT_ID'
23+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
24+
# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample
25+
# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID'
26+
# evaluation_id = 'YOUR_EVALUATION_ID'
27+
28+
29+
def get_evaluation_sample(
30+
project_id: str,
31+
location: str,
32+
processor_id: str,
33+
processor_version_id: str,
34+
evaluation_id: str,
35+
):
36+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
37+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
38+
39+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
40+
41+
# The full resource name of the evaluation
42+
# e.g. `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`
43+
evaluation_name = client.evaluation_path(
44+
project_id, location, processor_id, processor_version_id, evaluation_id
45+
)
46+
# Make GetEvaluation request
47+
evaluation = client.get_evaluation(name=evaluation_name)
48+
49+
create_time = evaluation.create_time
50+
document_counters = evaluation.document_counters
51+
52+
# Print the Evaluation Information
53+
# Refer to https://cloud.google.com/document-ai/docs/reference/rest/v1beta3/projects.locations.processors.processorVersions.evaluations
54+
# for more information on the available evaluation data
55+
print(f"Create Time: {create_time}")
56+
print(f"Input Documents: {document_counters.input_documents_count}")
57+
print(f"\tInvalid Documents: {document_counters.invalid_documents_count}")
58+
print(f"\tFailed Documents: {document_counters.failed_documents_count}")
59+
print(f"\tEvaluated Documents: {document_counters.evaluated_documents_count}")
60+
61+
62+
# [END documentai_get_evaluation]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
18+
from documentai.snippets import get_evaluation_sample
19+
20+
import mock
21+
22+
location = "us"
23+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
24+
processor_id = "a35310a144a6e4f8"
25+
processor_version_id = "2af620b2fd4d1fcf"
26+
evaluation_id = "55cdab6206095055"
27+
28+
29+
# Mocking request
30+
@mock.patch(
31+
"google.cloud.documentai_v1beta3.DocumentProcessorServiceClient.get_evaluation"
32+
)
33+
@mock.patch("google.cloud.documentai_v1beta3.Evaluation")
34+
def test_get_evaluation(evaluation_mock, get_evaluation_mock, capsys):
35+
36+
get_evaluation_mock.return_value = evaluation_mock
37+
38+
get_evaluation_sample.get_evaluation_sample(
39+
project_id=project_id,
40+
location=location,
41+
processor_id=processor_id,
42+
processor_version_id=processor_version_id,
43+
evaluation_id=evaluation_id,
44+
)
45+
46+
get_evaluation_mock.assert_called_once()
47+
48+
out, _ = capsys.readouterr()
49+
50+
assert "Create Time" in out
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
# [START documentai_list_evaluations]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import documentai_v1beta3 as documentai
20+
21+
# TODO(developer): Uncomment these variables before running the sample.
22+
# project_id = 'YOUR_PROJECT_ID'
23+
# location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'
24+
# processor_id = 'YOUR_PROCESSOR_ID' # Create processor before running sample
25+
# processor_version_id = 'YOUR_PROCESSOR_VERSION_ID'
26+
27+
28+
def list_evaluations_sample(
29+
project_id: str, location: str, processor_id: str, processor_version_id: str
30+
):
31+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
32+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
33+
34+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
35+
36+
# The full resource name of the processor version
37+
# e.g. `projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}`
38+
parent = client.processor_version_path(
39+
project_id, location, processor_id, processor_version_id
40+
)
41+
42+
evaluations = client.list_evaluations(parent=parent)
43+
44+
# Print the Evaluation Information
45+
# Refer to https://cloud.google.com/document-ai/docs/reference/rest/v1beta3/projects.locations.processors.processorVersions.evaluations
46+
# for more information on the available evaluation data
47+
print(f"Evaluations for Processor Version {parent}")
48+
49+
for evaluation in evaluations:
50+
print(f"Name: {evaluation.name}")
51+
print(f"\tCreate Time: {evaluation.create_time}\n")
52+
53+
54+
# [END documentai_list_evaluations]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
16+
import os
17+
18+
from documentai.snippets import list_evaluations_sample
19+
20+
location = "us"
21+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
processor_id = "feacd98c28866ede"
23+
processor_version_id = "stable"
24+
25+
26+
def test_list_evaluations(capsys):
27+
28+
list_evaluations_sample.list_evaluations_sample(
29+
project_id=project_id,
30+
location=location,
31+
processor_id=processor_id,
32+
processor_version_id=processor_version_id,
33+
)
34+
35+
out, _ = capsys.readouterr()
36+
assert "Evaluation" in out

documentai/snippets/noxfile_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
TEST_CONFIG_OVERRIDE = {
2424
# You can opt out from the test for specific Python versions.
25-
"ignored_versions": ["2.7", "3.6"],
25+
"ignored_versions": ["2.7", "3.6", "3.8", "3.9", "3.10", "3.11"],
2626
# Old samples are opted out of enforcing Python type hints
2727
# All new samples should feature them
2828
"enforce_type_hints": False,

0 commit comments

Comments
 (0)