Skip to content

Commit 4787e35

Browse files
committed
docs(samples): Added Test & Evaluation Samples for v1beta3 2.3.0
- Replacement for googleapis/python-documentai#417
1 parent 66e069f commit 4787e35

8 files changed

+480
-0
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
import mock
19+
from documentai.snippets import evaluate_processor_version_sample
20+
21+
location = "us"
22+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
processor_id = "aaaaaaaaa"
24+
processor_version_id = "xxxxxxxxxx"
25+
gcs_input_uri = "gs://bucket/directory/"
26+
27+
28+
# Mocking request as evaluation can take a long time
29+
@mock.patch(
30+
"google.cloud.documentai_v1beta3.DocumentProcessorServiceClient.evaluate_processor_version"
31+
)
32+
@mock.patch("google.cloud.documentai_v1beta3.EvaluateProcessorVersionResponse")
33+
@mock.patch("google.api_core.operation.Operation")
34+
def test_evaluate_processor_version(
35+
operation_mock,
36+
evaluate_processor_version_response_mock,
37+
evaluate_processor_version_mock,
38+
capsys,
39+
):
40+
operation_mock.result.return_value = evaluate_processor_version_response_mock
41+
evaluate_processor_version_mock.return_value = operation_mock
42+
43+
evaluate_processor_version_sample.evaluate_processor_version_sample(
44+
project_id=project_id,
45+
location=location,
46+
processor_id=processor_id,
47+
processor_version_id=processor_version_id,
48+
gcs_input_uri=gcs_input_uri,
49+
)
50+
51+
evaluate_processor_version_mock.assert_called_once()
52+
53+
out, _ = capsys.readouterr()
54+
55+
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import mock
19+
from documentai.snippets import get_evaluation_sample
20+
21+
location = "us"
22+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
23+
processor_id = "a35310a144a6e4f8"
24+
processor_version_id = "2af620b2fd4d1fcf"
25+
evaluation_id = "55cdab6206095055"
26+
27+
28+
# Mocking request
29+
@mock.patch(
30+
"google.cloud.documentai_v1beta3.DocumentProcessorServiceClient.get_evaluation"
31+
)
32+
@mock.patch("google.cloud.documentai_v1beta3.Evaluation")
33+
def test_get_evaluation(evaluation_mock, get_evaluation_mock, capsys):
34+
35+
get_evaluation_mock.return_value = evaluation_mock
36+
37+
get_evaluation_sample.get_evaluation_sample(
38+
project_id=project_id,
39+
location=location,
40+
processor_id=processor_id,
41+
processor_version_id=processor_version_id,
42+
evaluation_id=evaluation_id,
43+
)
44+
45+
get_evaluation_mock.assert_called_once()
46+
47+
out, _ = capsys.readouterr()
48+
49+
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

0 commit comments

Comments
 (0)