Meminta Peninjauan Manual terhadap Dokumen

Setelah menyiapkan akun Google Cloud , membuat pemroses, dan mengaktifkan peninjauan manual, Anda dapat mengirim permintaan peninjauan ke pemroses. Halaman ini memberikan detail tentang cara meminta Peninjauan Manual ad-hoc terhadap dokumen.

Meminta Peninjauan Manual

UI Web

  1. Buka halaman Human-in-the-Loop di konsol Google Cloud.

    Buka dasbor Human-in-the-Loop

  2. Klik prosesor yang ingin Anda gunakan untuk peninjauan manual.

  3. Pilih Upload Dokumen dan upload file lokal yang akan dikirim untuk ditinjau. Tunggu 1-2 menit hingga dokumen muncul di antrean peninjauan.

REST

Sebelum menggunakan salah satu data permintaan, lakukan penggantian berikut:

  • PROJECT_ID: project ID Google Cloud Anda.
  • LOCATION: Lokasi pemroses Anda, misalnya:
    • us - Amerika Serikat
    • eu - Uni Eropa
  • PROCESSOR_ID: ID pemroses kustom Anda.
  • ENABLE_SCHEMA_VALIDATION: Apakah validasi skema harus dilakukan pada permintaan peninjauan ad-hoc.
    • true - Mengaktifkan validasi skema pada permintaan peninjauan ad-hoc.
    • false - Menonaktifkan validasi skema pada permintaan peninjauan ad hoc.
  • PRIORITY: Prioritas tugas peninjauan manual.
    • DEFAULT - Tingkat prioritas default.
    • URGENT - Tingkat prioritas mendesak. Pengelola pemberian label harus mengalokasikan resource pemberi label ke antrean tugas mendesak untuk mematuhi tingkat prioritas ini.
  • DOCUMENT: Objek Document yang telah diproses oleh pemroses yang dikonfigurasi untuk ditinjau.

Metode HTTP dan URL:

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

Isi JSON permintaan:

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

Untuk mengirim permintaan Anda, pilih salah satu opsi berikut:

curl

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

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

Simpan isi permintaan dalam file bernama request.json, dan jalankan perintah berikut:

$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

Jika permintaan berhasil, respons akan berisi instance Operation. Anda dapat membaca selengkapnya tentang Operasi Berjalan Lama dalam dokumentasi.

{
  "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

Untuk mengetahui informasi selengkapnya, lihat dokumentasi referensi API Python Document AI.

Untuk melakukan autentikasi ke Document AI, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


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