Skip to content

Commit 3cd29aa

Browse files
authored
Add new sample for deploying a model with a node count (GoogleCloudPlatform#1601)
1 parent bcec16b commit 3cd29aa

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

vision/automl/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-automl</artifactId>
43-
<version>0.55.0-beta</version>
43+
<version>0.112.0-beta</version>
4444
</dependency>
4545
<dependency>
4646
<groupId>net.sourceforge.argparse4j</groupId>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2019 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.google.cloud.vision.samples.automl;
18+
19+
// [START automl_vision_object_detection_deploy_model_node_count]
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.ImageObjectDetectionModelDeploymentMetadata;
24+
import com.google.cloud.automl.v1beta1.ModelName;
25+
import com.google.cloud.automl.v1beta1.OperationMetadata;
26+
import com.google.protobuf.Empty;
27+
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
31+
class DeployModelNodeCount {
32+
33+
static void deployModelNodeCount(String projectId, String modelId) {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String modelId = "YOUR_MODEL_ID";
36+
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests. After completing all of your requests, call
39+
// the "close" method on the client to safely clean up any remaining background resources.
40+
try (AutoMlClient client = AutoMlClient.create()) {
41+
// Get the full path of the model.
42+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
43+
44+
// Set how many nodes the model is deployed on
45+
ImageObjectDetectionModelDeploymentMetadata deploymentMetadata =
46+
ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build();
47+
48+
DeployModelRequest request = DeployModelRequest.newBuilder()
49+
.setName(modelFullId.toString())
50+
.setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata)
51+
.build();
52+
// Deploy the model
53+
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
54+
future.get();
55+
System.out.println("Model deployment on 2 nodes finished");
56+
} catch (IOException | InterruptedException | ExecutionException e) {
57+
e.printStackTrace();
58+
}
59+
}
60+
}
61+
// [END automl_vision_object_detection_deploy_model_node_count]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2019 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.google.cloud.vision.samples.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.automl.v1beta1.AutoMlClient;
23+
import com.google.cloud.automl.v1beta1.ModelName;
24+
import com.google.cloud.automl.v1beta1.OperationMetadata;
25+
import com.google.protobuf.Empty;
26+
27+
import java.io.ByteArrayOutputStream;
28+
import java.io.IOException;
29+
import java.io.PrintStream;
30+
import java.util.concurrent.ExecutionException;
31+
32+
import org.junit.After;
33+
import org.junit.Before;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
/** Tests for vision "Deploy Model Node Count" sample. */
39+
@RunWith(JUnit4.class)
40+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
41+
public class DeployModelNodeCountIT {
42+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
43+
private static final String MODEL_ID = "IOD1854128448151224320";
44+
private ByteArrayOutputStream bout;
45+
private PrintStream out;
46+
47+
@Before
48+
public void setUp() {
49+
bout = new ByteArrayOutputStream();
50+
out = new PrintStream(bout);
51+
System.setOut(out);
52+
}
53+
54+
@After
55+
public void tearDown() throws IOException, InterruptedException, ExecutionException {
56+
System.setOut(null);
57+
58+
try (AutoMlClient client = AutoMlClient.create()) {
59+
OperationFuture<Empty, OperationMetadata> future = client.undeployModelAsync(
60+
ModelName.of(PROJECT_ID, "us-central1", MODEL_ID).toString());
61+
62+
future.get();
63+
}
64+
}
65+
66+
@Test
67+
public void testModelApi() {
68+
DeployModelNodeCount.deployModelNodeCount(PROJECT_ID, MODEL_ID);
69+
70+
String got = bout.toString();
71+
assertThat(got).contains("Model deployment on 2 nodes finished");
72+
}
73+
}

0 commit comments

Comments
 (0)