Skip to content

Commit 0c8edf6

Browse files
authored
samples: updates samples to v1 (1 of 8) (#206)
* samples: updates samples to v1 * samples: adds NLP batch jobs * samples: update copyright, added some env vars * fix: test teardowns
1 parent af5228b commit 0c8edf6

File tree

36 files changed

+892
-285
lines changed

36 files changed

+892
-285
lines changed

aiplatform/snippets/src/main/java/aiplatform/CancelBatchPredictionJobSample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,9 @@
1818

1919
// [START aiplatform_cancel_batch_prediction_job_sample]
2020

21-
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJobName;
22-
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
23-
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
21+
import com.google.cloud.aiplatform.v1.BatchPredictionJobName;
22+
import com.google.cloud.aiplatform.v1.JobServiceClient;
23+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
2424
import java.io.IOException;
2525

2626
public class CancelBatchPredictionJobSample {

aiplatform/snippets/src/main/java/aiplatform/CancelDataLabelingJobSample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,9 @@
1818

1919
// [START aiplatform_cancel_data_labeling_job_sample]
2020

21-
import com.google.cloud.aiplatform.v1beta1.DataLabelingJobName;
22-
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
23-
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
21+
import com.google.cloud.aiplatform.v1.DataLabelingJobName;
22+
import com.google.cloud.aiplatform.v1.JobServiceClient;
23+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
2424
import java.io.IOException;
2525

2626
public class CancelDataLabelingJobSample {

aiplatform/snippets/src/main/java/aiplatform/CancelTrainingPipelineSample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,9 +18,9 @@
1818

1919
// [START aiplatform_cancel_training_pipeline_sample]
2020

21-
import com.google.cloud.aiplatform.v1beta1.PipelineServiceClient;
22-
import com.google.cloud.aiplatform.v1beta1.PipelineServiceSettings;
23-
import com.google.cloud.aiplatform.v1beta1.TrainingPipelineName;
21+
import com.google.cloud.aiplatform.v1.PipelineServiceClient;
22+
import com.google.cloud.aiplatform.v1.PipelineServiceSettings;
23+
import com.google.cloud.aiplatform.v1.TrainingPipelineName;
2424
import java.io.IOException;
2525

2626
public class CancelTrainingPipelineSample {

aiplatform/snippets/src/main/java/aiplatform/CreateBatchPredictionJobBigquerySample.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,13 +17,13 @@
1717
package aiplatform;
1818

1919
// [START aiplatform_create_batch_prediction_job_bigquery_sample]
20-
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob;
21-
import com.google.cloud.aiplatform.v1beta1.BigQueryDestination;
22-
import com.google.cloud.aiplatform.v1beta1.BigQuerySource;
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.cloud.aiplatform.v1beta1.ModelName;
20+
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
21+
import com.google.cloud.aiplatform.v1.BigQueryDestination;
22+
import com.google.cloud.aiplatform.v1.BigQuerySource;
23+
import com.google.cloud.aiplatform.v1.JobServiceClient;
24+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
25+
import com.google.cloud.aiplatform.v1.LocationName;
26+
import com.google.cloud.aiplatform.v1.ModelName;
2727
import com.google.gson.JsonObject;
2828
import com.google.protobuf.Value;
2929
import com.google.protobuf.util.JsonFormat;
@@ -95,8 +95,6 @@ static void createBatchPredictionJobBigquerySample(
9595
.setModelParameters(modelParameters)
9696
.setInputConfig(inputConfig)
9797
.setOutputConfig(outputConfig)
98-
// optional
99-
.setGenerateExplanation(true)
10098
.build();
10199
LocationName parent = LocationName.of(project, location);
102100
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);

aiplatform/snippets/src/main/java/aiplatform/CreateBatchPredictionJobSample.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 Google LLC
2+
* Copyright 2021 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,19 +17,18 @@
1717
package aiplatform;
1818

1919
// [START aiplatform_create_batch_prediction_job_sample]
20-
import com.google.cloud.aiplatform.v1beta1.AcceleratorType;
21-
import com.google.cloud.aiplatform.v1beta1.BatchDedicatedResources;
22-
import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob;
23-
import com.google.cloud.aiplatform.v1beta1.GcsDestination;
24-
import com.google.cloud.aiplatform.v1beta1.GcsSource;
25-
import com.google.cloud.aiplatform.v1beta1.JobServiceClient;
26-
import com.google.cloud.aiplatform.v1beta1.JobServiceSettings;
27-
import com.google.cloud.aiplatform.v1beta1.LocationName;
28-
import com.google.cloud.aiplatform.v1beta1.MachineSpec;
29-
import com.google.cloud.aiplatform.v1beta1.ModelName;
30-
import com.google.gson.JsonObject;
20+
import com.google.cloud.aiplatform.util.ValueConverter;
21+
import com.google.cloud.aiplatform.v1.AcceleratorType;
22+
import com.google.cloud.aiplatform.v1.BatchDedicatedResources;
23+
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
24+
import com.google.cloud.aiplatform.v1.GcsDestination;
25+
import com.google.cloud.aiplatform.v1.GcsSource;
26+
import com.google.cloud.aiplatform.v1.JobServiceClient;
27+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
28+
import com.google.cloud.aiplatform.v1.LocationName;
29+
import com.google.cloud.aiplatform.v1.MachineSpec;
30+
import com.google.cloud.aiplatform.v1.ModelName;
3131
import com.google.protobuf.Value;
32-
import com.google.protobuf.util.JsonFormat;
3332
import java.io.IOException;
3433

3534
public class CreateBatchPredictionJobSample {
@@ -74,10 +73,7 @@ static void createBatchPredictionJobSample(
7473
try (JobServiceClient client = JobServiceClient.create(settings)) {
7574

7675
// Passing in an empty Value object for model parameters
77-
JsonObject jsonModelParameters = new JsonObject();
78-
Value.Builder modelParametersBuilder = Value.newBuilder();
79-
JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder);
80-
Value modelParameters = modelParametersBuilder.build();
76+
Value modelParameters = ValueConverter.EMPTY_VALUE;
8177

8278
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
8379
BatchPredictionJob.InputConfig inputConfig =
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2021 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_batch_prediction_job_text_classification_sample]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
22+
import com.google.cloud.aiplatform.v1.GcsDestination;
23+
import com.google.cloud.aiplatform.v1.GcsSource;
24+
import com.google.cloud.aiplatform.v1.JobServiceClient;
25+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
26+
import com.google.cloud.aiplatform.v1.LocationName;
27+
import com.google.cloud.aiplatform.v1.ModelName;
28+
import java.io.IOException;
29+
30+
public class CreateBatchPredictionJobTextClassificationSample {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String project = "PROJECT";
35+
String location = "us-central1";
36+
String displayName = "DISPLAY_NAME";
37+
String modelId = "MODEL_ID";
38+
String gcsSourceUri = "GCS_SOURCE_URI";
39+
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
40+
createBatchPredictionJobTextClassificationSample(
41+
project, location, displayName, modelId, gcsSourceUri, gcsDestinationOutputUriPrefix);
42+
}
43+
44+
static void createBatchPredictionJobTextClassificationSample(
45+
String project,
46+
String location,
47+
String displayName,
48+
String modelId,
49+
String gcsSourceUri,
50+
String gcsDestinationOutputUriPrefix)
51+
throws IOException {
52+
// The AI Platform services require regional API endpoints.
53+
JobServiceSettings settings =
54+
JobServiceSettings.newBuilder()
55+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
56+
.build();
57+
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (JobServiceClient client = JobServiceClient.create(settings)) {
62+
try {
63+
String modelName = ModelName.of(project, location, modelId).toString();
64+
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
65+
BatchPredictionJob.InputConfig inputConfig =
66+
BatchPredictionJob.InputConfig.newBuilder()
67+
.setInstancesFormat("jsonl")
68+
.setGcsSource(gcsSource)
69+
.build();
70+
GcsDestination gcsDestination =
71+
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
72+
BatchPredictionJob.OutputConfig outputConfig =
73+
BatchPredictionJob.OutputConfig.newBuilder()
74+
.setPredictionsFormat("jsonl")
75+
.setGcsDestination(gcsDestination)
76+
.build();
77+
BatchPredictionJob batchPredictionJob =
78+
BatchPredictionJob.newBuilder()
79+
.setDisplayName(displayName)
80+
.setModel(modelName)
81+
.setInputConfig(inputConfig)
82+
.setOutputConfig(outputConfig)
83+
.build();
84+
LocationName parent = LocationName.of(project, location);
85+
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
86+
System.out.format("response: %s\n", response);
87+
} catch (ApiException ex) {
88+
System.out.format("Exception: %s\n", ex.getLocalizedMessage());
89+
}
90+
}
91+
}
92+
}
93+
94+
// [END aiplatform_create_batch_prediction_job_text_classification_sample]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2021 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_batch_prediction_job_text_entity_extraction_sample]
20+
import com.google.api.gax.rpc.ApiException;
21+
import com.google.cloud.aiplatform.v1.BatchPredictionJob;
22+
import com.google.cloud.aiplatform.v1.GcsDestination;
23+
import com.google.cloud.aiplatform.v1.GcsSource;
24+
import com.google.cloud.aiplatform.v1.JobServiceClient;
25+
import com.google.cloud.aiplatform.v1.JobServiceSettings;
26+
import com.google.cloud.aiplatform.v1.LocationName;
27+
import com.google.cloud.aiplatform.v1.ModelName;
28+
import java.io.IOException;
29+
30+
public class CreateBatchPredictionJobTextEntityExtractionSample {
31+
32+
public static void main(String[] args) throws IOException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String project = "PROJECT";
35+
String location = "us-central1";
36+
String displayName = "DISPLAY_NAME";
37+
String modelId = "MODEL_ID";
38+
String gcsSourceUri = "GCS_SOURCE_URI";
39+
String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX";
40+
createBatchPredictionJobTextEntityExtractionSample(
41+
project, location, displayName, modelId, gcsSourceUri, gcsDestinationOutputUriPrefix);
42+
}
43+
44+
static void createBatchPredictionJobTextEntityExtractionSample(
45+
String project,
46+
String location,
47+
String displayName,
48+
String modelId,
49+
String gcsSourceUri,
50+
String gcsDestinationOutputUriPrefix)
51+
throws IOException {
52+
// The AI Platform services require regional API endpoints.
53+
JobServiceSettings settings =
54+
JobServiceSettings.newBuilder()
55+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
56+
.build();
57+
58+
// Initialize client that will be used to send requests. This client only needs to be created
59+
// once, and can be reused for multiple requests. After completing all of your requests, call
60+
// the "close" method on the client to safely clean up any remaining background resources.
61+
try (JobServiceClient client = JobServiceClient.create(settings)) {
62+
try {
63+
String modelName = ModelName.of(project, location, modelId).toString();
64+
GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
65+
BatchPredictionJob.InputConfig inputConfig =
66+
BatchPredictionJob.InputConfig.newBuilder()
67+
.setInstancesFormat("jsonl")
68+
.setGcsSource(gcsSource)
69+
.build();
70+
GcsDestination gcsDestination =
71+
GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
72+
BatchPredictionJob.OutputConfig outputConfig =
73+
BatchPredictionJob.OutputConfig.newBuilder()
74+
.setPredictionsFormat("jsonl")
75+
.setGcsDestination(gcsDestination)
76+
.build();
77+
BatchPredictionJob batchPredictionJob =
78+
BatchPredictionJob.newBuilder()
79+
.setDisplayName(displayName)
80+
.setModel(modelName)
81+
.setInputConfig(inputConfig)
82+
.setOutputConfig(outputConfig)
83+
.build();
84+
LocationName parent = LocationName.of(project, location);
85+
BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
86+
System.out.format("response: %s\n", response);
87+
System.out.format("\tname:%s\n", response.getName());
88+
} catch (ApiException ex) {
89+
System.out.format("Exception: %s\n", ex.getLocalizedMessage());
90+
}
91+
}
92+
}
93+
}
94+
95+
// [END aiplatform_create_batch_prediction_job_text_entity_extraction_sample]

0 commit comments

Comments
 (0)