Skip to content

Commit 89e7ce1

Browse files
holtskinnerparthea
andcommitted
docs(samples): Added Human Review Request Sample (#357)
- Illustrates how to call `review_document()` to send a processed document to Human in the Loop (HITL) Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent c00fd29 commit 89e7ce1

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

review_document_sample.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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_review_document]
17+
18+
from google.api_core.client_options import ClientOptions
19+
from google.cloud import documentai_v1 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 in Cloud Console
25+
# file_path = '/path/to/local/pdf'
26+
# mime_type = 'application/pdf' # https://cloud.google.com/document-ai/docs/file-types
27+
28+
29+
def review_document_sample(
30+
project_id: str, location: str, processor_id: str, file_path: str, mime_type: str
31+
):
32+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
33+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
34+
35+
# Create a client
36+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
37+
38+
# Make Processing Request
39+
inline_document = process_document(
40+
project_id, location, processor_id, file_path, mime_type
41+
)
42+
43+
# Get the full resource name of the human review config, e.g.:
44+
# projects/project_id/locations/location/processor/processor_id/humanReviewConfig
45+
human_review_config = client.human_review_config_path(
46+
project_id, location, processor_id
47+
)
48+
49+
# Options are DEFAULT, URGENT
50+
priority = documentai.ReviewDocumentRequest.Priority.DEFAULT
51+
52+
# Configure the human review request
53+
request = documentai.ReviewDocumentRequest(
54+
inline_document=inline_document,
55+
human_review_config=human_review_config,
56+
enable_schema_validation=False,
57+
priority=priority,
58+
)
59+
60+
# Make a request for human review of the processed document
61+
operation = client.review_document(request=request)
62+
63+
# Print operation name, can be used to check status of the request
64+
print(operation.operation.name)
65+
66+
67+
def process_document(
68+
project_id: str, location: str, processor_id: str, file_path: str, mime_type: str
69+
) -> documentai.Document:
70+
# You must set the api_endpoint if you use a location other than 'us', e.g.:
71+
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
72+
73+
client = documentai.DocumentProcessorServiceClient(client_options=opts)
74+
75+
# The full resource name of the processor, e.g.:
76+
# projects/project_id/locations/location/processor/processor_id
77+
# You must create new processors in the Cloud Console first
78+
name = client.processor_path(project_id, location, processor_id)
79+
80+
# Read the file into memory
81+
with open(file_path, "rb") as image:
82+
image_content = image.read()
83+
84+
# Load Binary Data into Document AI RawDocument Object
85+
raw_document = documentai.RawDocument(content=image_content, mime_type=mime_type)
86+
87+
# Configure the process request
88+
request = documentai.ProcessRequest(name=name, raw_document=raw_document)
89+
90+
result = client.process_document(request=request)
91+
92+
return result.document
93+
94+
95+
# [END documentai_review_document]

review_document_sample_test.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 samples.snippets import review_document_sample
19+
20+
location = "us"
21+
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
processor_id = "b7054d67d76c39f1"
23+
file_path = "resources/invoice.pdf"
24+
mime_type = "application/pdf"
25+
26+
27+
def test_process_documents(capsys):
28+
review_document_sample.review_document_sample(
29+
project_id=project_id,
30+
location=location,
31+
processor_id=processor_id,
32+
file_path=file_path,
33+
mime_type=mime_type,
34+
)
35+
out, _ = capsys.readouterr()
36+
37+
assert "projects/" in out
38+
assert "locations/" in out
39+
assert "operations/" in out

0 commit comments

Comments
 (0)