|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +// [START aiplatform_create_data_labeling_job_sample] |
| 20 | + |
| 21 | +import com.google.cloud.aiplatform.v1beta1.DataLabelingJob; |
| 22 | +import com.google.cloud.aiplatform.v1beta1.DatasetName; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.JobServiceClient; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; |
| 25 | +import com.google.cloud.aiplatform.v1beta1.LocationName; |
| 26 | +import com.google.protobuf.Value; |
| 27 | +import com.google.protobuf.util.JsonFormat; |
| 28 | +import com.google.type.Money; |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +public class CreateDataLabelingJobSample { |
| 33 | + |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String project = "YOUR_PROJECT_ID"; |
| 37 | + String displayName = "YOUR_DATA_LABELING_DISPLAY_NAME"; |
| 38 | + String datasetId = "YOUR_DATASET_ID"; |
| 39 | + String instructionUri = |
| 40 | + "gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_data_labeling_source/file.pdf"; |
| 41 | + String inputsSchemaUri = "YOUR_INPUT_SCHEMA_URI"; |
| 42 | + String annotationSpec = "YOUR_ANNOTATION_SPEC"; |
| 43 | + createDataLabelingJob( |
| 44 | + project, displayName, datasetId, instructionUri, inputsSchemaUri, annotationSpec); |
| 45 | + } |
| 46 | + |
| 47 | + static void createDataLabelingJob( |
| 48 | + String project, |
| 49 | + String displayName, |
| 50 | + String datasetId, |
| 51 | + String instructionUri, |
| 52 | + String inputsSchemaUri, |
| 53 | + String annotationSpec) |
| 54 | + throws IOException { |
| 55 | + JobServiceSettings jobServiceSettings = |
| 56 | + JobServiceSettings.newBuilder() |
| 57 | + .setEndpoint("us-central1-aiplatform.googleapis.com:443") |
| 58 | + .build(); |
| 59 | + |
| 60 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 61 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 62 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 63 | + try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) { |
| 64 | + String location = "us-central1"; |
| 65 | + LocationName locationName = LocationName.of(project, location); |
| 66 | + |
| 67 | + String jsonString = "{\"annotation_specs\": [ " + annotationSpec + "]}"; |
| 68 | + Value.Builder annotationSpecValue = Value.newBuilder(); |
| 69 | + JsonFormat.parser().merge(jsonString, annotationSpecValue); |
| 70 | + |
| 71 | + DatasetName datasetName = DatasetName.of(project, location, datasetId); |
| 72 | + DataLabelingJob dataLabelingJob = |
| 73 | + DataLabelingJob.newBuilder() |
| 74 | + .setDisplayName(displayName) |
| 75 | + .setLabelerCount(1) |
| 76 | + .setInstructionUri(instructionUri) |
| 77 | + .setInputsSchemaUri(inputsSchemaUri) |
| 78 | + .addDatasets(datasetName.toString()) |
| 79 | + .setInputs(annotationSpecValue) |
| 80 | + .putAnnotationLabels( |
| 81 | + "aiplatform.googleapis.com/annotation_set_name", "my_test_saved_query") |
| 82 | + .build(); |
| 83 | + |
| 84 | + DataLabelingJob dataLabelingJobResponse = |
| 85 | + jobServiceClient.createDataLabelingJob(locationName, dataLabelingJob); |
| 86 | + |
| 87 | + System.out.println("Create Data Labeling Job Response"); |
| 88 | + System.out.format("\tName: %s\n", dataLabelingJobResponse.getName()); |
| 89 | + System.out.format("\tDisplay Name: %s\n", dataLabelingJobResponse.getDisplayName()); |
| 90 | + System.out.format("\tDatasets: %s\n", dataLabelingJobResponse.getDatasetsList()); |
| 91 | + System.out.format("\tLabeler Count: %s\n", dataLabelingJobResponse.getLabelerCount()); |
| 92 | + System.out.format("\tInstruction Uri: %s\n", dataLabelingJobResponse.getInstructionUri()); |
| 93 | + System.out.format("\tInputs Schema Uri: %s\n", dataLabelingJobResponse.getInputsSchemaUri()); |
| 94 | + System.out.format("\tInputs: %s\n", dataLabelingJobResponse.getInputs()); |
| 95 | + System.out.format("\tState: %s\n", dataLabelingJobResponse.getState()); |
| 96 | + System.out.format("\tLabeling Progress: %s\n", dataLabelingJobResponse.getLabelingProgress()); |
| 97 | + System.out.format("\tCreate Time: %s\n", dataLabelingJobResponse.getCreateTime()); |
| 98 | + System.out.format("\tUpdate Time: %s\n", dataLabelingJobResponse.getUpdateTime()); |
| 99 | + System.out.format("\tLabels: %s\n", dataLabelingJobResponse.getLabelsMap()); |
| 100 | + System.out.format( |
| 101 | + "\tSpecialist Pools: %s\n", dataLabelingJobResponse.getSpecialistPoolsList()); |
| 102 | + for (Map.Entry<String, String> annotationLabelMap : |
| 103 | + dataLabelingJobResponse.getAnnotationLabelsMap().entrySet()) { |
| 104 | + System.out.println("\tAnnotation Level"); |
| 105 | + System.out.format("\t\tkey: %s\n", annotationLabelMap.getKey()); |
| 106 | + System.out.format("\t\tvalue: %s\n", annotationLabelMap.getValue()); |
| 107 | + } |
| 108 | + Money money = dataLabelingJobResponse.getCurrentSpend(); |
| 109 | + |
| 110 | + System.out.println("\tCurrent Spend"); |
| 111 | + System.out.format("\t\tCurrency Code: %s\n", money.getCurrencyCode()); |
| 112 | + System.out.format("\t\tUnits: %s\n", money.getUnits()); |
| 113 | + System.out.format("\t\tNanos: %s\n", money.getNanos()); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | +// [END aiplatform_create_data_labeling_job_sample] |
0 commit comments