ドキュメントの人間による確認をリクエストする

Google Cloud アカウントを設定し、プロセッサを作成して、人間による審査を有効にしたら、プロセッサに審査リクエストを送信できます。このページでは、ドキュメントの人間による確認をアドホックにリクエストする方法について説明します。

人間による審査をリクエストする

ウェブ UI

  1. Google Cloud コンソールで [Human-in-the-Loop] ページを開きます。

    Human-in-the-Loop ダッシュボードに移動

  2. 手動レビューに使用するプロセッサをクリックします。

  3. [ドキュメントをアップロード] を選択し、審査に送るローカル ファイルをアップロードします。ドキュメントが審査キューに表示されるまで 1 ~ 2 分かかります。

REST

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 実際の Google Cloud プロジェクト ID。
  • LOCATION: プロセッサのロケーション(例:
    • us - 米国
    • eu - 欧州連合
  • PROCESSOR_ID: カスタム プロセッサの ID。
  • ENABLE_SCHEMA_VALIDATION: アドホック レビュー リクエストでスキーマ検証を実行するかどうか。
    • true - アドホック レビュー リクエストでスキーマ検証を有効にします。
    • false - アドホック審査リクエストのスキーマ検証を無効にします。
  • PRIORITY: 人間による審査タスクの優先度
    • DEFAULT - デフォルトの優先度。
    • URGENT - 緊急の優先度。ラベル付けマネージャーは、この優先度を尊重するために、ラベル付け担当者のリソースを緊急タスクキューに割り当てる必要があります。
  • DOCUMENT: 審査用に構成されたプロセッサによってすでに処理されている Document オブジェクト。

HTTP メソッドと URL:

POST https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/humanReviewConfig:reviewDocument

リクエストの本文(JSON):

{
  "enableSchemaValidation": ENABLE_SCHEMA_VALIDATION,
  "priority": "PRIORITY",
  "inlineDocument": "DOCUMENT"
}

リクエストを送信するには、次のいずれかのオプションを選択します。

curl

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/humanReviewConfig:reviewDocument"

PowerShell

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-documentai.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/humanReviewConfig:reviewDocument" | Select-Object -Expand Content

リクエストが成功した場合、レスポンスには Operation のインスタンスが含まれます。長時間実行オペレーションの詳細については、ドキュメントをご覧ください。

{
  "name": "projects/PROJECT_ID/locations/LOCATION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.documentai.v1.ReviewDocumentOperationMetadata",
    "commonMetadata": {
      "state": "RUNNING",
      "createTime": "TIMESTAMP",
      "updateTime": "TIMESTAMP",
      "resource": "projects/PROJECT_ID/locations/LOCATION/processors/PROCESSOR_ID/humanReviewConfig"
    }
  }
}

Python

詳細については、Document AI Python API リファレンス ドキュメントをご覧ください。

Document AI に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。


from google.api_core.client_options import ClientOptions
from google.cloud import documentai  # type: ignore

# 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
# file_path = '/path/to/local/pdf'
# mime_type = 'application/pdf'  # https://cloud.google.com/document-ai/docs/file-types


def review_document_sample(
    project_id: str, location: str, processor_id: str, file_path: str, mime_type: str
) -> None:
    # You must set the api_endpoint if you use a location other than 'us'.
    opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")

    # Create a client
    client = documentai.DocumentProcessorServiceClient(client_options=opts)

    # Make Processing Request
    inline_document = process_document(
        project_id, location, processor_id, file_path, mime_type
    )

    # Get the full resource name of the human review config, e.g.:
    # projects/project_id/locations/location/processor/processor_id/humanReviewConfig
    human_review_config = client.human_review_config_path(
        project_id, location, processor_id
    )

    # Options are DEFAULT, URGENT
    priority = documentai.ReviewDocumentRequest.Priority.DEFAULT

    # Configure the human review request
    request = documentai.ReviewDocumentRequest(
        inline_document=inline_document,
        human_review_config=human_review_config,
        enable_schema_validation=False,
        priority=priority,
    )

    # Make a request for human review of the processed document
    operation = client.review_document(request=request)

    # Print operation name, can be used to check status of the request
    print(operation.operation.name)


def process_document(
    project_id: str, location: str, processor_id: str, file_path: str, mime_type: str
) -> documentai.Document:
    # You must set the api_endpoint if you use a location other than 'us'.
    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)

    # 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)

    result = client.process_document(request=request)

    return result.document