Train a processor version
Stay organized with collections
Save and categorize content based on your preferences.
Start a new training job for a processor
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis code sample demonstrates how to initiate a new training job for a Document AI processor using the Python API.\u003c/p\u003e\n"],["\u003cp\u003eThe code utilizes the \u003ccode\u003edocumentai\u003c/code\u003e library to set up and train a new processor version, specifying project, location, and processor identifiers.\u003c/p\u003e\n"],["\u003cp\u003eYou can optionally configure the training and test data by providing URIs for Google Cloud Storage (GCS) buckets containing your documents, otherwise the Cloud Console defaults will be used.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication with Document AI is required, using Application Default Credentials as detailed in the provided link.\u003c/p\u003e\n"],["\u003cp\u003eUpon successful execution, the code will output the operation details, the new processor version, and the validation results for both training and test datasets.\u003c/p\u003e\n"]]],[],null,["# Train a processor version\n\nStart a new training job for a processor\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Train and evaluate](/document-ai/docs/training-overview)\n\nCode sample\n-----------\n\n### Python\n\n\nFor more information, see the\n[Document AI Python API\nreference documentation](/python/docs/reference/documentai/latest).\n\n\nTo authenticate to Document AI, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n from typing import Optional\n\n from google.api_core.client_options import ClientOptions\n from google.cloud import documentai # type: ignore\n\n # TODO(developer): Uncomment these variables before running the sample.\n # project_id = 'YOUR_PROJECT_ID'\n # location = 'YOUR_PROCESSOR_LOCATION' # Format is 'us' or 'eu'\n # processor_id = 'YOUR_PROCESSOR_ID'\n # processor_version_display_name = 'new-processor-version'\n # train_data_uri = 'gs://bucket/directory/' # (Optional)\n # test_data_uri = 'gs://bucket/directory/' # (Optional)\n\n\n def train_processor_version_sample(\n project_id: str,\n location: str,\n processor_id: str,\n processor_version_display_name: str,\n train_data_uri: Optional[str] = None,\n test_data_uri: Optional[str] = None,\n ) -\u003e None:\n # You must set the api_endpoint if you use a location other than 'us', e.g.:\n opts = ClientOptions(api_endpoint=f\"{location}-documentai.googleapis.com\")\n\n client = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.services.document_processor_service.DocumentProcessorServiceClient.html(client_options=opts)\n\n # The full resource name of the processor\n # e.g. `projects/{project_id}/locations/{location}/processors/{processor_id}\n parent = client.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.services.document_processor_service.DocumentProcessorServiceClient.html#google_cloud_documentai_v1_services_document_processor_service_DocumentProcessorServiceClient_processor_path(project_id, location, processor_id)\n\n processor_version = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.ProcessorVersion.html(\n display_name=processor_version_display_name\n )\n\n # If train/test data is not supplied, the default sets in the Cloud Console will be used\n input_data = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.TrainProcessorVersionRequest.html.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.TrainProcessorVersionRequest.InputData.html(\n training_documents=documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.BatchDocumentsInputConfig.html(\n gcs_prefix=documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.GcsPrefix.html(gcs_uri_prefix=train_data_uri)\n ),\n test_documents=documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.BatchDocumentsInputConfig.html(\n gcs_prefix=documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.GcsPrefix.html(gcs_uri_prefix=test_data_uri)\n ),\n )\n\n request = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.TrainProcessorVersionRequest.html(\n parent=parent, processor_version=processor_version, input_data=input_data\n )\n\n operation = client.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.services.document_processor_service.DocumentProcessorServiceClient.html#google_cloud_documentai_v1_services_document_processor_service_DocumentProcessorServiceClient_train_processor_version(request=request)\n # Print operation details\n print(operation.operation.name)\n # Wait for operation to complete\n response = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.TrainProcessorVersionResponse.html(operation.result())\n\n metadata = documentai.https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.TrainProcessorVersionMetadata.html(operation.metadata)\n\n print(f\"New Processor Version:{response.processor_version}\")\n print(f\"Training Set Validation: {metadata.training_dataset_validation}\")\n print(f\"Test Set Validation: {metadata.test_dataset_validation}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=documentai)."]]