diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java new file mode 100644 index 00000000000..4a0d2801b4d --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 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.google.cloud.vision.samples.automl; + +// [START automl_vision_classification_deploy_model] +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 ClassificationDeployModel { + + // Deploy a model + static void classificationDeployModel(String projectId, String modelId) { + // String projectId = "YOUR_PROJECT_ID"; + // String modelId = "YOUR_MODEL_ID"; + + // 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); + + // Build deploy model request. + DeployModelRequest deployModelRequest = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + + // Deploy a model with the deploy model request. + OperationFuture future = + client.deployModelAsync(deployModelRequest); + + future.get(); + + // Display the deployment details of model. + System.out.println("Model deployment finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_deploy_model] diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java new file mode 100644 index 00000000000..f41e68d3829 --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 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.google.cloud.vision.samples.automl; + +// [START automl_vision_classification_deploy_model_node_count] +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.ImageClassificationModelDeploymentMetadata; +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 ClassificationDeployModelNodeCount { + + // Deploy a model with a specified node count + static void classificationDeployModelNodeCount(String projectId, String modelId) { + // String projectId = "YOUR_PROJECT_ID"; + // String modelId = "YOUR_MODEL_ID"; + + // 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); + + // Set how many nodes the model is deployed on + ImageClassificationModelDeploymentMetadata deploymentMetadata = + ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); + + DeployModelRequest request = DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .setImageClassificationModelDeploymentMetadata(deploymentMetadata) + .build(); + // Deploy the model + OperationFuture future = client.deployModelAsync(request); + future.get(); + System.out.println("Model deployment on 2 nodes finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_deploy_model_node_count] \ No newline at end of file diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java new file mode 100644 index 00000000000..aeae7907038 --- /dev/null +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationUndeployModel.java @@ -0,0 +1,62 @@ +/* + * Copyright 2019 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.google.cloud.vision.samples.automl; + +// [START automl_vision_classification_undeploy_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.UndeployModelRequest; +import com.google.protobuf.Empty; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class ClassificationUndeployModel { + + // Deploy a model + static void classificationUndeployModel(String projectId, String modelId) { + // String projectId = "YOUR_PROJECT_ID"; + // String modelId = "YOUR_MODEL_ID"; + + // 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); + + // Build deploy model request. + UndeployModelRequest undeployModelRequest = + UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + + // Deploy a model with the deploy model request. + OperationFuture future = + client.undeployModelAsync(undeployModelRequest); + + future.get(); + + // Display the deployment details of model. + System.out.println("Model undeploy finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_vision_classification_undeploy_model] diff --git a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java similarity index 95% rename from vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java rename to vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java index f686fa7e055..f0fea3defb7 100644 --- a/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/DeployModelNodeCount.java +++ b/vision/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java @@ -28,9 +28,9 @@ import java.io.IOException; import java.util.concurrent.ExecutionException; -class DeployModelNodeCount { +class ObjectDetectionDeployModelNodeCount { - static void deployModelNodeCount(String projectId, String modelId) { + static void objectDetectionDeployModelNodeCount(String projectId, String modelId) { // String projectId = "YOUR_PROJECT_ID"; // String modelId = "YOUR_MODEL_ID"; diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java new file mode 100644 index 00000000000..524ab5a503f --- /dev/null +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java @@ -0,0 +1,71 @@ +/* + * Copyright 2019 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.google.cloud.vision.samples.automl; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class ClassificationDeployModelIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "ICN3125115511348658176"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testClassificationDeployModelApi() { + ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID); + + String got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + + ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID); + + got = bout.toString(); + assertThat(got).contains("Model undeploy finished"); + } + + @Test + public void testClassificationDeployModelNodeCountApi() { + ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID); + + String got = bout.toString(); + assertThat(got).contains("Model deployment on 2 nodes finished"); + + ClassificationUndeployModel.classificationUndeployModel(PROJECT_ID, MODEL_ID); + + got = bout.toString(); + assertThat(got).contains("Model undeploy finished"); + } +} diff --git a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java similarity index 91% rename from vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java rename to vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java index c0a3efd7269..17319e03a22 100644 --- a/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/DeployModelNodeCountIT.java +++ b/vision/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java @@ -38,7 +38,7 @@ /** Tests for vision "Deploy Model Node Count" sample. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") -public class DeployModelNodeCountIT { +public class ObjectDetectionDeployModelNodeCountIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String MODEL_ID = "IOD1854128448151224320"; private ByteArrayOutputStream bout; @@ -64,8 +64,8 @@ public void tearDown() throws IOException, InterruptedException, ExecutionExcept } @Test - public void testModelApi() { - DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID); + public void testObjectDetectionDeployModelNodeCountApi() { + ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID); String got = bout.toString(); assertThat(got).contains("Model deployment on 2 nodes finished");