Skip to content

Commit 69d7c85

Browse files
authored
samples: ucaip batch samples 5 of 6 (#20)
* samples: ucaip batch samples 5 of 6 * disabled the tests * applied changes from the feedbacj
1 parent a662f6b commit 69d7c85

9 files changed

+905
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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_cancel_data_labeling_job_sample]
20+
21+
import com.google.cloud.aiplatform.v1beta1.DataLabelingJobName;
22+
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
23+
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
24+
import java.io.IOException;
25+
26+
public class CancelDataLabelingJobSample {
27+
public static void main(String[] args) throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String project = "YOUR_PROJECT_ID";
30+
String dataLabelingJobId = "YOUR_DATA_LABELING_JOB_ID";
31+
cancelDataLabelingJob(project, dataLabelingJobId);
32+
}
33+
34+
static void cancelDataLabelingJob(String project, String dataLabelingJobId) throws IOException {
35+
JobServiceSettings jobServiceSettings =
36+
JobServiceSettings.newBuilder()
37+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
38+
.build();
39+
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the "close" method on the client to safely clean up any remaining background resources.
43+
try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) {
44+
String location = "us-central1";
45+
46+
DataLabelingJobName dataLabelingJobName =
47+
DataLabelingJobName.of(project, location, dataLabelingJobId);
48+
jobServiceClient.cancelDataLabelingJob(dataLabelingJobName);
49+
System.out.println("Cancelled Data labeling job");
50+
}
51+
}
52+
}
53+
// [END aiplatform_cancel_data_labeling_job_sample]
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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_image_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 CreateDataLabelingJobImageSample {
33+
public static void main(String[] args) throws IOException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
String project = "YOUR_PROJECT_ID";
36+
String displayName = "YOUR_DATA_LABELING_DISPLAY_NAME";
37+
String datasetId = "YOUR_DATASET_ID";
38+
String instructionUri =
39+
"gs://YOUR_GCS_SOURCE_BUCKET/path_to_your_data_labeling_source/file.pdf";
40+
String annotationSpec = "YOUR_ANNOTATION_SPEC";
41+
createDataLabelingJobImage(project, displayName, datasetId, instructionUri, annotationSpec);
42+
}
43+
44+
static void createDataLabelingJobImage(
45+
String project,
46+
String displayName,
47+
String datasetId,
48+
String instructionUri,
49+
String annotationSpec)
50+
throws IOException {
51+
JobServiceSettings jobServiceSettings =
52+
JobServiceSettings.newBuilder()
53+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
54+
.build();
55+
56+
// Initialize client that will be used to send requests. This client only needs to be created
57+
// once, and can be reused for multiple requests. After completing all of your requests, call
58+
// the "close" method on the client to safely clean up any remaining background resources.
59+
try (JobServiceClient jobServiceClient = JobServiceClient.create(jobServiceSettings)) {
60+
String location = "us-central1";
61+
LocationName locationName = LocationName.of(project, location);
62+
63+
String jsonString = "{\"annotation_specs\": [ " + annotationSpec + "]}";
64+
Value.Builder annotationSpecValue = Value.newBuilder();
65+
JsonFormat.parser().merge(jsonString, annotationSpecValue);
66+
67+
DatasetName datasetName = DatasetName.of(project, location, datasetId);
68+
DataLabelingJob dataLabelingJob =
69+
DataLabelingJob.newBuilder()
70+
.setDisplayName(displayName)
71+
.setLabelerCount(1)
72+
.setInstructionUri(instructionUri)
73+
.setInputsSchemaUri(
74+
"gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/"
75+
+ "image_classification.yaml")
76+
.addDatasets(datasetName.toString())
77+
.setInputs(annotationSpecValue)
78+
.putAnnotationLabels(
79+
"aiplatform.googleapis.com/annotation_set_name", "my_test_saved_query")
80+
.build();
81+
82+
DataLabelingJob dataLabelingJobResponse =
83+
jobServiceClient.createDataLabelingJob(locationName, dataLabelingJob);
84+
85+
System.out.println("Create Data Labeling Job Image Response");
86+
System.out.format("\tName: %s\n", dataLabelingJobResponse.getName());
87+
System.out.format("\tDisplay Name: %s\n", dataLabelingJobResponse.getDisplayName());
88+
System.out.format("\tDatasets: %s\n", dataLabelingJobResponse.getDatasetsList());
89+
System.out.format("\tLabeler Count: %s\n", dataLabelingJobResponse.getLabelerCount());
90+
System.out.format("\tInstruction Uri: %s\n", dataLabelingJobResponse.getInstructionUri());
91+
System.out.format("\tInputs Schema Uri: %s\n", dataLabelingJobResponse.getInputsSchemaUri());
92+
System.out.format("\tInputs: %s\n", dataLabelingJobResponse.getInputs());
93+
System.out.format("\tState: %s\n", dataLabelingJobResponse.getState());
94+
System.out.format("\tLabeling Progress: %s\n", dataLabelingJobResponse.getLabelingProgress());
95+
System.out.format("\tCreate Time: %s\n", dataLabelingJobResponse.getCreateTime());
96+
System.out.format("\tUpdate Time: %s\n", dataLabelingJobResponse.getUpdateTime());
97+
System.out.format("\tLabels: %s\n", dataLabelingJobResponse.getLabelsMap());
98+
System.out.format(
99+
"\tSpecialist Pools: %s\n", dataLabelingJobResponse.getSpecialistPoolsList());
100+
for (Map.Entry<String, String> annotationLabelMap :
101+
dataLabelingJobResponse.getAnnotationLabelsMap().entrySet()) {
102+
System.out.println("\tAnnotation Level");
103+
System.out.format("\t\tkey: %s\n", annotationLabelMap.getKey());
104+
System.out.format("\t\tvalue: %s\n", annotationLabelMap.getValue());
105+
}
106+
Money money = dataLabelingJobResponse.getCurrentSpend();
107+
108+
System.out.println("\tCurrent Spend");
109+
System.out.format("\t\tCurrency Code: %s\n", money.getCurrencyCode());
110+
System.out.format("\t\tUnits: %s\n", money.getUnits());
111+
System.out.format("\t\tNanos: %s\n", money.getNanos());
112+
}
113+
}
114+
}
115+
// [END aiplatform_create_data_labeling_job_image_sample]
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)