请求对文档进行人工审核

设置 Google Cloud 账号、创建处理方并启用人工审核后,您就可以向处理方发送审核请求了。本页详细介绍了如何请求对文档进行临时人工审核。

申请人工审核

网页界面

  1. 在 Google Cloud 控制台中打开“人机协同”页面。

    前往“人机协同”信息中心

  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 方法和网址:

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