Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.

Commit 24d31ba

Browse files
feat: revert "feat: remove automl beta samples" (#3024)
Reverts GoogleCloudPlatform/java-docs-samples#3019
1 parent 14ffd9d commit 24d31ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3451
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 com.example.automl;
18+
19+
// [START automl_batch_predict_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.BatchPredictInputConfig;
22+
import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig;
23+
import com.google.cloud.automl.v1beta1.BatchPredictRequest;
24+
import com.google.cloud.automl.v1beta1.BatchPredictResult;
25+
import com.google.cloud.automl.v1beta1.GcsDestination;
26+
import com.google.cloud.automl.v1beta1.GcsSource;
27+
import com.google.cloud.automl.v1beta1.ModelName;
28+
import com.google.cloud.automl.v1beta1.OperationMetadata;
29+
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
30+
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
32+
33+
class BatchPredict {
34+
35+
static void batchPredict() throws IOException, ExecutionException, InterruptedException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "YOUR_PROJECT_ID";
38+
String modelId = "YOUR_MODEL_ID";
39+
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl";
40+
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
41+
batchPredict(projectId, modelId, inputUri, outputUri);
42+
}
43+
44+
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
45+
throws IOException, ExecutionException, InterruptedException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (PredictionServiceClient client = PredictionServiceClient.create()) {
50+
// Get the full path of the model.
51+
ModelName name = ModelName.of(projectId, "us-central1", modelId);
52+
53+
// Configure the source of the file from a GCS bucket
54+
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
55+
BatchPredictInputConfig inputConfig =
56+
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
57+
58+
// Configure where to store the output in a GCS bucket
59+
GcsDestination gcsDestination =
60+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
61+
BatchPredictOutputConfig outputConfig =
62+
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
63+
64+
// Build the request that will be sent to the API
65+
BatchPredictRequest request =
66+
BatchPredictRequest.newBuilder()
67+
.setName(name.toString())
68+
.setInputConfig(inputConfig)
69+
.setOutputConfig(outputConfig)
70+
.build();
71+
72+
// Start an asynchronous request
73+
OperationFuture<BatchPredictResult, OperationMetadata> future =
74+
client.batchPredictAsync(request);
75+
76+
System.out.println("Waiting for operation to complete...");
77+
BatchPredictResult response = future.get();
78+
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
79+
}
80+
}
81+
}
82+
// [END automl_batch_predict_beta]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 com.example.automl;
18+
19+
// [START automl_cancel_operation_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import java.io.IOException;
22+
23+
class CancelOperation {
24+
25+
static void cancelOperation() throws IOException {
26+
// TODO(developer): Replace these variables before running the sample.
27+
String projectId = "YOUR_PROJECT_ID";
28+
String location = "us-central1";
29+
String operationId = "YOUR_OPERATION_ID";
30+
String operationFullId =
31+
String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId);
32+
cancelOperation(operationFullId);
33+
}
34+
35+
static void cancelOperation(String operationFullId) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (AutoMlClient client = AutoMlClient.create()) {
40+
client.getOperationsClient().cancelOperation(operationFullId);
41+
System.out.println("Operation cancelled");
42+
}
43+
}
44+
}
45+
// [END automl_cancel_operation_beta]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 com.example.automl;
18+
19+
// [START automl_delete_dataset_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.DatasetName;
22+
import com.google.protobuf.Empty;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
class DeleteDataset {
27+
28+
static void deleteDataset() throws IOException, ExecutionException, InterruptedException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String datasetId = "YOUR_DATASET_ID";
32+
deleteDataset(projectId, datasetId);
33+
}
34+
35+
// Delete a dataset
36+
static void deleteDataset(String projectId, String datasetId)
37+
throws IOException, ExecutionException, InterruptedException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// Get the full path of the dataset.
43+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
44+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
45+
System.out.format("Dataset deleted. %s%n", response);
46+
}
47+
}
48+
}
49+
// [END automl_delete_dataset_beta]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 com.example.automl;
18+
19+
// [START automl_delete_model_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.ModelName;
22+
import com.google.protobuf.Empty;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
class DeleteModel {
27+
28+
public static void main(String[] args)
29+
throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String modelId = "YOUR_MODEL_ID";
33+
deleteModel(projectId, modelId);
34+
}
35+
36+
// Delete a model
37+
static void deleteModel(String projectId, String modelId)
38+
throws IOException, ExecutionException, InterruptedException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
// Get the full path of the model.
44+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
45+
46+
// Delete a model.
47+
Empty response = client.deleteModelAsync(modelFullId).get();
48+
49+
System.out.println("Model deletion started...");
50+
System.out.println(String.format("Model deleted. %s", response));
51+
}
52+
}
53+
}
54+
// [END automl_delete_model_beta]
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 com.example.automl;
18+
19+
// [START automl_deploy_model_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23+
import com.google.cloud.automl.v1beta1.ModelName;
24+
import com.google.cloud.automl.v1beta1.OperationMetadata;
25+
import com.google.protobuf.Empty;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
class DeployModel {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR_PROJECT_ID";
35+
String modelId = "YOUR_MODEL_ID";
36+
deployModel(projectId, modelId);
37+
}
38+
39+
// Deploy a model for prediction
40+
static void deployModel(String projectId, String modelId)
41+
throws IOException, ExecutionException, InterruptedException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (AutoMlClient client = AutoMlClient.create()) {
46+
// Get the full path of the model.
47+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
48+
DeployModelRequest request =
49+
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
50+
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
51+
52+
future.get();
53+
System.out.println("Model deployment finished");
54+
}
55+
}
56+
}
57+
// [END automl_deploy_model_beta]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 com.example.automl;
18+
19+
// [START automl_get_model_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.Model;
22+
import com.google.cloud.automl.v1beta1.ModelName;
23+
import java.io.IOException;
24+
25+
class GetModel {
26+
27+
static void getModel() throws IOException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "YOUR_PROJECT_ID";
30+
String modelId = "YOUR_MODEL_ID";
31+
getModel(projectId, modelId);
32+
}
33+
34+
// Get a model
35+
static void getModel(String projectId, String modelId) throws IOException {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
try (AutoMlClient client = AutoMlClient.create()) {
40+
// Get the full path of the model.
41+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
42+
Model model = client.getModel(modelFullId);
43+
44+
// Display the model information.
45+
System.out.format("Model name: %s%n", model.getName());
46+
// To get the model id, you have to parse it out of the `name` field. As models Ids are
47+
// required for other methods.
48+
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
49+
String[] names = model.getName().split("/");
50+
String retrievedModelId = names[names.length - 1];
51+
System.out.format("Model id: %s%n", retrievedModelId);
52+
System.out.format("Model display name: %s%n", model.getDisplayName());
53+
System.out.println("Model create time:");
54+
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds());
55+
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos());
56+
System.out.format("Model deployment state: %s%n", model.getDeploymentState());
57+
}
58+
}
59+
}
60+
// [END automl_get_model_beta]

0 commit comments

Comments
 (0)