문서에 대한 사람의 검토 요청

Google Cloud 계정을 설정하고, 프로세서를 만들고, 사람의 검토를 사용 설정한 후에는 프로세서에 검토 요청을 보낼 수 있습니다. 이 페이지에서는 문서에 대해 임시로 사람의 검토를 요청하는 방법을 자세히 설명합니다.

사람의 검토 요청

웹 UI

  1. Google Cloud 콘솔에서 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