|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2019 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +import os |
| 19 | + |
| 20 | +from google.api_core.client_options import ClientOptions |
| 21 | + |
| 22 | + |
| 23 | +# [START datalabeling_create_instruction_beta] |
| 24 | +def create_instruction(project_id, data_type, instruction_gcs_uri): |
| 25 | + """ Creates a data labeling PDF instruction for the given Google Cloud |
| 26 | + project. The PDF file should be uploaded to the project in |
| 27 | + Google Cloud Storage. |
| 28 | + """ |
| 29 | + from google.cloud import datalabeling_v1beta1 as datalabeling |
| 30 | + client = datalabeling.DataLabelingServiceClient() |
| 31 | + # [END datalabeling_create_instruction_beta] |
| 32 | + # If provided, use a provided test endpoint - this will prevent tests on |
| 33 | + # this snippet from triggering any action by a real human |
| 34 | + if 'DATALABELING_ENDPOINT' in os.environ: |
| 35 | + opts = ClientOptions(api_endpoint=os.getenv('DATALABELING_ENDPOINT')) |
| 36 | + client = datalabeling.DataLabelingServiceClient(client_options=opts) |
| 37 | + # [START datalabeling_create_instruction_beta] |
| 38 | + |
| 39 | + project_path = client.project_path(project_id) |
| 40 | + |
| 41 | + pdf_instruction = datalabeling.types.PdfInstruction( |
| 42 | + gcs_file_uri=instruction_gcs_uri) |
| 43 | + |
| 44 | + instruction = datalabeling.types.Instruction( |
| 45 | + display_name='YOUR_INSTRUCTION_DISPLAY_NAME', |
| 46 | + description='YOUR_DESCRIPTION', |
| 47 | + data_type=data_type, |
| 48 | + pdf_instruction=pdf_instruction |
| 49 | + ) |
| 50 | + |
| 51 | + operation = client.create_instruction(project_path, instruction) |
| 52 | + |
| 53 | + result = operation.result() |
| 54 | + |
| 55 | + # The format of the resource name: |
| 56 | + # project_id/{project_id}/instruction/{instruction_id} |
| 57 | + print('The instruction resource name: {}'.format(result.name)) |
| 58 | + print('Display name: {}'.format(result.display_name)) |
| 59 | + print('Description: {}'.format(result.description)) |
| 60 | + print('Create time:') |
| 61 | + print('\tseconds: {}'.format(result.create_time.seconds)) |
| 62 | + print('\tnanos: {}'.format(result.create_time.nanos)) |
| 63 | + print('Data type: {}'.format( |
| 64 | + datalabeling.enums.DataType(result.data_type).name)) |
| 65 | + print('Pdf instruction:') |
| 66 | + print('\tGcs file uri: {}\n'.format( |
| 67 | + result.pdf_instruction.gcs_file_uri)) |
| 68 | + |
| 69 | + return result |
| 70 | +# [END datalabeling_create_instruction_beta] |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + parser = argparse.ArgumentParser( |
| 75 | + description=__doc__, |
| 76 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 77 | + ) |
| 78 | + |
| 79 | + parser.add_argument( |
| 80 | + '--project-id', |
| 81 | + help='Project ID. Required.', |
| 82 | + required=True |
| 83 | + ) |
| 84 | + |
| 85 | + parser.add_argument( |
| 86 | + '--data-type', |
| 87 | + help='Data type. Only support IMAGE, VIDEO, TEXT and AUDIO. Required.', |
| 88 | + required=True |
| 89 | + ) |
| 90 | + |
| 91 | + parser.add_argument( |
| 92 | + '--instruction-gcs-uri', |
| 93 | + help='The URI of Google Cloud Storage of the instruction. Required.', |
| 94 | + required=True |
| 95 | + ) |
| 96 | + |
| 97 | + args = parser.parse_args() |
| 98 | + |
| 99 | + create_instruction( |
| 100 | + args.project_id, |
| 101 | + args.data_type, |
| 102 | + args.instruction_gcs_uri |
| 103 | + ) |
0 commit comments