Skip to content

Commit

Permalink
samples: migrate samples from GoogleCloudPlatform/java-docs-samples /…
Browse files Browse the repository at this point in the history
…beta/automl (#290)

* samples: added missing beta samples

* made requested changes

* formatted the code

* fixed all lint issues

* refactored all create model tests and made requested changes

* hardcoded table model id
  • Loading branch information
munkhuushmgl authored and Shabirmean committed Nov 18, 2022
1 parent d19bb96 commit 186e893
Show file tree
Hide file tree
Showing 88 changed files with 4,067 additions and 211 deletions.
8 changes: 8 additions & 0 deletions automl/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-automl</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-bigquery</artifactId>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
</dependency>
<!-- [START_EXCLUDE] -->
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
Expand Down
82 changes: 82 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/BatchPredict.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_batch_predict_beta]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.BatchPredictInputConfig;
import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig;
import com.google.cloud.automl.v1beta1.BatchPredictRequest;
import com.google.cloud.automl.v1beta1.BatchPredictResult;
import com.google.cloud.automl.v1beta1.GcsDestination;
import com.google.cloud.automl.v1beta1.GcsSource;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class BatchPredict {

static void batchPredict() throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl";
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
batchPredict(projectId, modelId, inputUri, outputUri);
}

static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);

// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
BatchPredictInputConfig inputConfig =
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();

// Configure where to store the output in a GCS bucket
GcsDestination gcsDestination =
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchPredictOutputConfig outputConfig =
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();

// Build the request that will be sent to the API
BatchPredictRequest request =
BatchPredictRequest.newBuilder()
.setName(name.toString())
.setInputConfig(inputConfig)
.setOutputConfig(outputConfig)
.build();

// Start an asynchronous request
OperationFuture<BatchPredictResult, OperationMetadata> future =
client.batchPredictAsync(request);

System.out.println("Waiting for operation to complete...");
BatchPredictResult response = future.get();
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
}
}
}
// [END automl_batch_predict_beta]
48 changes: 48 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/CancelOperation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_cancel_operation_beta]

import com.google.cloud.automl.v1beta1.AutoMlClient;
import io.grpc.StatusRuntimeException;
import java.io.IOException;

class CancelOperation {

static void cancelOperation() throws IOException, InterruptedException, StatusRuntimeException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String location = "us-central1";
String operationId = "YOUR_OPERATION_ID";
String operationFullId =
String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId);
cancelOperation(operationFullId);
}

static void cancelOperation(String operationFullId)
throws IOException, InterruptedException, StatusRuntimeException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
client.getOperationsClient().cancelOperation(operationFullId);
System.out.println("Operation cancelled");
}
}
}
// [END automl_cancel_operation_beta]
49 changes: 49 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/DeleteDataset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_delete_dataset_beta]
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DatasetName;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeleteDataset {

static void deleteDataset() throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String datasetId = "YOUR_DATASET_ID";
deleteDataset(projectId, datasetId);
}

// Delete a dataset
static void deleteDataset(String projectId, String datasetId)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
Empty response = client.deleteDatasetAsync(datasetFullId).get();
System.out.format("Dataset deleted. %s%n", response);
}
}
}
// [END automl_delete_dataset_beta]
54 changes: 54 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/DeleteModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_delete_model_beta]
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeleteModel {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
deleteModel(projectId, modelId);
}

// Delete a model
static void deleteModel(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Delete a model.
Empty response = client.deleteModelAsync(modelFullId).get();

System.out.println("Model deletion started...");
System.out.println(String.format("Model deleted. %s", response));
}
}
}
// [END automl_delete_model_beta]
57 changes: 57 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/DeployModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_deploy_model_beta]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.DeployModelRequest;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeployModel {

public static void main(String[] args)
throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
deployModel(projectId, modelId);
}

// Deploy a model for prediction
static void deployModel(String projectId, String modelId)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
DeployModelRequest request =
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);

future.get();
System.out.println("Model deployment finished");
}
}
}
// [END automl_deploy_model_beta]
62 changes: 62 additions & 0 deletions automl/snippets/src/main/java/com/beta/automl/GetModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.beta.automl;

// [START automl_get_model_beta]
import com.google.cloud.automl.v1beta1.AutoMlClient;
import com.google.cloud.automl.v1beta1.Model;
import com.google.cloud.automl.v1beta1.ModelName;
import io.grpc.StatusRuntimeException;
import java.io.IOException;

class GetModel {

static void getModel() throws IOException, StatusRuntimeException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
getModel(projectId, modelId);
}

// Get a model
static void getModel(String projectId, String modelId)
throws IOException, StatusRuntimeException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
Model model = client.getModel(modelFullId);

// Display the model information.
System.out.format("Model name: %s%n", model.getName());
// To get the model id, you have to parse it out of the `name` field. As models Ids are
// required for other methods.
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
String[] names = model.getName().split("/");
String retrievedModelId = names[names.length - 1];
System.out.format("Model id: %s%n", retrievedModelId);
System.out.format("Model display name: %s%n", model.getDisplayName());
System.out.println("Model create time:");
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds());
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos());
System.out.format("Model deployment state: %s%n", model.getDeploymentState());
}
}
}
// [END automl_get_model_beta]
Loading

0 comments on commit 186e893

Please sign in to comment.