Skip to content

Commit 596aba6

Browse files
munkhuushmglShabirmean
authored andcommitted
chore: version bump and clean up automl vision samples (#385)
1 parent 4f09527 commit 596aba6

File tree

8 files changed

+32
-207
lines changed

8 files changed

+32
-207
lines changed

automl/snippets/src/main/java/com/google/cloud/vision/ClassificationDeployModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
// [START automl_vision_classification_deploy_model]
2020
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;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.DeployModelRequest;
23+
import com.google.cloud.automl.v1.ModelName;
24+
import com.google.cloud.automl.v1.OperationMetadata;
2525
import com.google.protobuf.Empty;
2626
import java.io.IOException;
2727
import java.util.concurrent.ExecutionException;

automl/snippets/src/main/java/com/google/cloud/vision/ClassificationDeployModelNodeCount.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

automl/snippets/src/main/java/com/google/cloud/vision/ClassificationUndeployModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
// [START automl_vision_classification_undeploy_model]
2020
import com.google.api.gax.longrunning.OperationFuture;
21-
import com.google.cloud.automl.v1beta1.AutoMlClient;
22-
import com.google.cloud.automl.v1beta1.ModelName;
23-
import com.google.cloud.automl.v1beta1.OperationMetadata;
24-
import com.google.cloud.automl.v1beta1.UndeployModelRequest;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.ModelName;
23+
import com.google.cloud.automl.v1.OperationMetadata;
24+
import com.google.cloud.automl.v1.UndeployModelRequest;
2525
import com.google.protobuf.Empty;
2626
import java.io.IOException;
2727
import java.util.concurrent.ExecutionException;

automl/snippets/src/main/java/com/google/cloud/vision/ModelApi.java

Lines changed: 7 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,13 @@
1818

1919
// Imports the Google Cloud client library
2020
import com.google.api.gax.longrunning.OperationFuture;
21-
import com.google.cloud.automl.v1beta1.AutoMlClient;
22-
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics;
23-
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationEvaluationMetrics.ConfidenceMetricsEntry;
24-
import com.google.cloud.automl.v1beta1.ImageClassificationModelMetadata;
25-
import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest;
26-
import com.google.cloud.automl.v1beta1.ListModelsRequest;
27-
import com.google.cloud.automl.v1beta1.LocationName;
28-
import com.google.cloud.automl.v1beta1.Model;
29-
import com.google.cloud.automl.v1beta1.ModelEvaluation;
30-
import com.google.cloud.automl.v1beta1.ModelEvaluationName;
31-
import com.google.cloud.automl.v1beta1.ModelName;
32-
import com.google.cloud.automl.v1beta1.OperationMetadata;
33-
import com.google.longrunning.Operation;
34-
import com.google.protobuf.Empty;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.ImageClassificationModelMetadata;
23+
import com.google.cloud.automl.v1.LocationName;
24+
import com.google.cloud.automl.v1.Model;
25+
import com.google.cloud.automl.v1.OperationMetadata;
3526
import java.io.IOException;
36-
import java.util.List;
3727
import java.util.concurrent.ExecutionException;
38-
import net.sourceforge.argparse4j.ArgumentParsers;
39-
import net.sourceforge.argparse4j.inf.ArgumentParser;
40-
import net.sourceforge.argparse4j.inf.ArgumentParserException;
41-
import net.sourceforge.argparse4j.inf.Namespace;
42-
import net.sourceforge.argparse4j.inf.Subparser;
43-
import net.sourceforge.argparse4j.inf.Subparsers;
4428

4529
/**
4630
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java
@@ -64,7 +48,7 @@ static void createModel(
6448
String computeRegion,
6549
String dataSetId,
6650
String modelName,
67-
String trainBudget) {
51+
String trainBudget) throws IOException, ExecutionException, InterruptedException {
6852
// Instantiates a client
6953
try (AutoMlClient client = AutoMlClient.create()) {
7054

@@ -76,7 +60,7 @@ static void createModel(
7660
Long.valueOf(trainBudget) == 0
7761
? ImageClassificationModelMetadata.newBuilder().build()
7862
: ImageClassificationModelMetadata.newBuilder()
79-
.setTrainBudget(Long.valueOf(trainBudget))
63+
.setTrainBudgetMilliNodeHours(Long.valueOf(trainBudget))
8064
.build();
8165

8266
// Set model name and model metadata for the image dataset.
@@ -95,49 +79,7 @@ static void createModel(
9579
String.format(
9680
"Training operation name: %s", response.getInitialFuture().get().getName()));
9781
System.out.println("Training started...");
98-
} catch (IOException | ExecutionException | InterruptedException e) {
99-
e.printStackTrace();
10082
}
10183
}
10284
// [END automl_vision_create_model]
103-
104-
public static void main(String[] args) {
105-
argsHelper(args);
106-
}
107-
108-
static void argsHelper(String[] args) {
109-
ArgumentParser parser =
110-
ArgumentParsers.newFor("ModelApi")
111-
.build()
112-
.defaultHelp(true)
113-
.description("Model API operations.");
114-
Subparsers subparsers = parser.addSubparsers().dest("command");
115-
116-
Subparser createModelParser = subparsers.addParser("create_model");
117-
createModelParser.addArgument("datasetId");
118-
createModelParser.addArgument("modelName");
119-
createModelParser.addArgument("trainBudget");
120-
121-
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
122-
String computeRegion = System.getenv("REGION_NAME");
123-
124-
if (projectId == null || computeRegion == null) {
125-
System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README");
126-
System.exit(-1);
127-
}
128-
129-
try {
130-
Namespace ns = parser.parseArgs(args);
131-
if (ns.get("command").equals("create_model")) {
132-
createModel(
133-
projectId,
134-
computeRegion,
135-
ns.getString("datasetId"),
136-
ns.getString("modelName"),
137-
ns.getString("trainBudget"));
138-
}
139-
} catch (ArgumentParserException e) {
140-
parser.handleError(e);
141-
}
142-
}
14385
}

automl/snippets/src/main/java/com/google/cloud/vision/ObjectDetectionDeployModelNodeCount.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
// [START automl_vision_object_detection_deploy_model_node_count]
2020
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;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.DeployModelRequest;
23+
import com.google.cloud.automl.v1.ImageObjectDetectionModelDeploymentMetadata;
24+
import com.google.cloud.automl.v1.ModelName;
25+
import com.google.cloud.automl.v1.OperationMetadata;
2626
import com.google.protobuf.Empty;
2727
import java.io.IOException;
2828
import java.util.concurrent.ExecutionException;

automl/snippets/src/main/java/com/google/cloud/vision/PredictionApi.java

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,18 @@
2525
package com.google.cloud.vision;
2626

2727
// Imports the Google Cloud client library
28-
import com.google.cloud.automl.v1beta1.AnnotationPayload;
29-
import com.google.cloud.automl.v1beta1.ExamplePayload;
30-
import com.google.cloud.automl.v1beta1.Image;
31-
import com.google.cloud.automl.v1beta1.ModelName;
32-
import com.google.cloud.automl.v1beta1.PredictResponse;
33-
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
28+
import com.google.cloud.automl.v1.AnnotationPayload;
29+
import com.google.cloud.automl.v1.ExamplePayload;
30+
import com.google.cloud.automl.v1.Image;
31+
import com.google.cloud.automl.v1.ModelName;
32+
import com.google.cloud.automl.v1.PredictResponse;
33+
import com.google.cloud.automl.v1.PredictionServiceClient;
3434
import com.google.protobuf.ByteString;
3535
import java.io.IOException;
3636
import java.nio.file.Files;
3737
import java.nio.file.Paths;
3838
import java.util.HashMap;
3939
import java.util.Map;
40-
import net.sourceforge.argparse4j.ArgumentParsers;
41-
import net.sourceforge.argparse4j.inf.ArgumentParser;
42-
import net.sourceforge.argparse4j.inf.ArgumentParserException;
43-
import net.sourceforge.argparse4j.inf.Namespace;
4440

4541
/**
4642
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java
@@ -65,7 +61,7 @@ static void predict(
6561
String computeRegion,
6662
String modelId,
6763
String filePath,
68-
String scoreThreshold) {
64+
String scoreThreshold) throws IOException {
6965

7066
// Instantiate client for prediction service.
7167
try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) {
@@ -92,45 +88,7 @@ static void predict(
9288
System.out.println(
9389
"Predicted class score :" + annotationPayload.getClassification().getScore());
9490
}
95-
} catch (IOException e) {
96-
e.printStackTrace();
9791
}
9892
}
9993
// [END automl_vision_predict]
100-
101-
public static void main(String[] args) {
102-
argsHelper(args);
103-
}
104-
105-
static void argsHelper(String[] args) {
106-
ArgumentParser parser =
107-
ArgumentParsers.newFor("PredictionApi")
108-
.build()
109-
.defaultHelp(true)
110-
.description("Prediction API Operation");
111-
112-
parser.addArgument("modelId").required(true);
113-
parser.addArgument("filePath").required(true);
114-
parser.addArgument("scoreThreshold").nargs("?").type(String.class).setDefault("");
115-
116-
String projectId = System.getenv("GOOGLE_CLOUD_PROJECT");
117-
String computeRegion = System.getenv("REGION_NAME");
118-
119-
if (projectId == null || computeRegion == null) {
120-
System.out.println("Set `GOOGLE_CLOUD_PROJECT` and `REGION_NAME` as specified in the README");
121-
System.exit(-1);
122-
}
123-
124-
try {
125-
Namespace ns = parser.parseArgs(args);
126-
predict(
127-
projectId,
128-
computeRegion,
129-
ns.getString("modelId"),
130-
ns.getString("filePath"),
131-
ns.getString("scoreThreshold"));
132-
} catch (ArgumentParserException e) {
133-
parser.handleError(e);
134-
}
135-
}
13694
}

automl/snippets/src/test/java/com/google/cloud/vision/ClassificationDeployModelIT.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,4 @@ public void testClassificationUndeployModelApi() {
7575
assertThat(e.getMessage()).contains("The model does not exist");
7676
}
7777
}
78-
79-
@Test
80-
public void testClassificationDeployModelNodeCountApi() {
81-
// As model deployment can take a long time, instead try to deploy a
82-
// nonexistent model and confirm that the model was not found, but other
83-
// elements of the request were valid.
84-
try {
85-
ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID);
86-
String got = bout.toString();
87-
assertThat(got).contains("The model does not exist");
88-
} catch (IOException | ExecutionException | InterruptedException e) {
89-
assertThat(e.getMessage()).contains("The model does not exist");
90-
}
91-
}
9278
}

automl/snippets/src/test/java/com/google/cloud/vision/PredictionApiIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21-
import com.google.cloud.automl.v1beta1.AutoMlClient;
22-
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23-
import com.google.cloud.automl.v1beta1.Model;
24-
import com.google.cloud.automl.v1beta1.ModelName;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.DeployModelRequest;
23+
import com.google.cloud.automl.v1.Model;
24+
import com.google.cloud.automl.v1.ModelName;
2525
import java.io.ByteArrayOutputStream;
2626
import java.io.IOException;
2727
import java.io.PrintStream;
@@ -77,7 +77,7 @@ public void tearDown() {
7777
}
7878

7979
@Test
80-
public void testPredict() {
80+
public void testPredict() throws IOException {
8181
PredictionApi.predict(PROJECT_ID, COMPUTE_REGION, modelId, filePath, scoreThreshold);
8282
String got = bout.toString();
8383
assertThat(got).contains("dandelion");

0 commit comments

Comments
 (0)