Skip to content

Commit de1bebb

Browse files
nnegreyanguillanneuf
authored andcommitted
samples: Automl GA (#1729)
* Add Natural Language GA Samples * Add Undeploy Model * Add Vision Classification Samples * Add Vision Object Detection Samples * Lint * Update samples based on feedback: remove unhelpful descriptions, add wrapper to env variables in tests * Add comment to explain why the create model call doesn't wait for compeletion * Update comment wording * Update method comment, only check env var for tests before class, remove javadoc comments
1 parent 5068fb4 commit de1bebb

File tree

49 files changed

+3465
-15
lines changed

Some content is hidden

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

49 files changed

+3465
-15
lines changed

automl/snippets/resources/salad.jpg

2.25 MB
Loading

automl/snippets/resources/test.png

1.97 MB
Loading

automl/snippets/src/main/java/com/example/automl/DeleteModel.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static void deleteModel() throws IOException, ExecutionException, InterruptedExc
3333
deleteModel(projectId, modelId);
3434
}
3535

36-
// Get a model
36+
// Delete a model
3737
static void deleteModel(String projectId, String modelId)
3838
throws IOException, ExecutionException, InterruptedException {
3939
// Initialize client that will be used to send requests. This client only needs to be created
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.example.automl;
18+
19+
// [START automl_deploy_model]
20+
import com.google.api.gax.longrunning.OperationFuture;
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;
25+
import com.google.protobuf.Empty;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class DeployModel {
31+
32+
static void deployModel() 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]

automl/snippets/src/main/java/com/example/automl/ExportDataset.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static void exportDataset() throws IOException, ExecutionException, InterruptedE
3636
exportDataset(projectId, datasetId, gcsUri);
3737
}
3838

39-
// Export a dataset
39+
// Export a dataset to a GCS bucket
4040
static void exportDataset(String projectId, String datasetId, String gcsUri)
4141
throws IOException, ExecutionException, InterruptedException {
4242
// Initialize client that will be used to send requests. This client only needs to be created

automl/snippets/src/main/java/com/example/automl/GetDataset.java

+51-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616

1717
package com.example.automl;
1818

19+
// [START automl_language_entity_extraction_get_dataset]
20+
// [START automl_language_sentiment_analysis_get_dataset]
21+
// [START automl_language_text_classification_get_dataset]
1922
// [START automl_translate_get_dataset]
20-
23+
// [START automl_vision_classification_get_dataset]
24+
// [START automl_vision_object_detection_get_dataset]
2125
import com.google.cloud.automl.v1.AutoMlClient;
2226
import com.google.cloud.automl.v1.Dataset;
2327
import com.google.cloud.automl.v1.DatasetName;
@@ -52,17 +56,60 @@ static void getDataset(String projectId, String datasetId) throws IOException {
5256
String retrievedDatasetId = names[names.length - 1];
5357
System.out.format("Dataset id: %s\n", retrievedDatasetId);
5458
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
59+
System.out.println("Dataset create time:");
60+
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
61+
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
62+
// [END automl_language_sentiment_analysis_get_dataset]
63+
// [END automl_language_text_classification_get_dataset]
64+
// [END automl_translate_get_dataset]
65+
// [END automl_vision_classification_get_dataset]
66+
// [END automl_vision_object_detection_get_dataset]
67+
System.out.format(
68+
"Text extraction dataset metadata: %s\n", dataset.getTextExtractionDatasetMetadata());
69+
// [END automl_language_entity_extraction_get_dataset]
70+
71+
// [START automl_language_sentiment_analysis_get_dataset]
72+
System.out.format(
73+
"Text sentiment dataset metadata: %s\n", dataset.getTextSentimentDatasetMetadata());
74+
// [END automl_language_sentiment_analysis_get_dataset]
75+
76+
// [START automl_language_text_classification_get_dataset]
77+
System.out.format(
78+
"Text classification dataset metadata: %s\n",
79+
dataset.getTextClassificationDatasetMetadata());
80+
// [END automl_language_text_classification_get_dataset]
81+
82+
// [START automl_translate_get_dataset]
5583
System.out.println("Translation dataset metadata:");
5684
System.out.format(
5785
"\tSource language code: %s\n",
5886
dataset.getTranslationDatasetMetadata().getSourceLanguageCode());
5987
System.out.format(
6088
"\tTarget language code: %s\n",
6189
dataset.getTranslationDatasetMetadata().getTargetLanguageCode());
62-
System.out.println("Dataset create time:");
63-
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
64-
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
90+
// [END automl_translate_get_dataset]
91+
92+
// [START automl_vision_classification_get_dataset]
93+
System.out.format(
94+
"Image classification dataset metadata: %s\n",
95+
dataset.getImageClassificationDatasetMetadata());
96+
// [END automl_vision_classification_get_dataset]
97+
98+
// [START automl_vision_object_detection_get_dataset]
99+
System.out.format(
100+
"Image object detection dataset metadata: %s\n",
101+
dataset.getImageObjectDetectionDatasetMetadata());
102+
// [START automl_language_entity_extraction_get_dataset]
103+
// [START automl_language_sentiment_analysis_get_dataset]
104+
// [START automl_language_text_classification_get_dataset]
105+
// [START automl_translate_get_dataset]
106+
// [START automl_vision_classification_get_dataset]
65107
}
66108
}
67109
}
110+
// [END automl_language_entity_extraction_get_dataset]
111+
// [END automl_language_sentiment_analysis_get_dataset]
112+
// [END automl_language_text_classification_get_dataset]
68113
// [END automl_translate_get_dataset]
114+
// [END automl_vision_classification_get_dataset]
115+
// [END automl_vision_object_detection_get_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.example.automl;
18+
19+
// [START automl_language_batch_predict]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1.BatchPredictInputConfig;
22+
import com.google.cloud.automl.v1.BatchPredictOutputConfig;
23+
import com.google.cloud.automl.v1.BatchPredictRequest;
24+
import com.google.cloud.automl.v1.BatchPredictResult;
25+
import com.google.cloud.automl.v1.GcsDestination;
26+
import com.google.cloud.automl.v1.GcsSource;
27+
import com.google.cloud.automl.v1.ModelName;
28+
import com.google.cloud.automl.v1.OperationMetadata;
29+
import com.google.cloud.automl.v1.PredictionServiceClient;
30+
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
34+
class LanguageBatchPredict {
35+
36+
static void batchPredict() throws IOException, ExecutionException, InterruptedException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String projectId = "YOUR_PROJECT_ID";
39+
String modelId = "YOUR_MODEL_ID";
40+
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_file.jsonl";
41+
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
42+
batchPredict(projectId, modelId, inputUri, outputUri);
43+
}
44+
45+
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
46+
throws IOException, ExecutionException, InterruptedException {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests. After completing all of your requests, call
49+
// the "close" method on the client to safely clean up any remaining background resources.
50+
try (PredictionServiceClient client = PredictionServiceClient.create()) {
51+
// Get the full path of the model.
52+
ModelName name = ModelName.of(projectId, "us-central1", modelId);
53+
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
54+
BatchPredictInputConfig inputConfig =
55+
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
56+
GcsDestination gcsDestination =
57+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
58+
BatchPredictOutputConfig outputConfig =
59+
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
60+
BatchPredictRequest request =
61+
BatchPredictRequest.newBuilder()
62+
.setName(name.toString())
63+
.setInputConfig(inputConfig)
64+
.setOutputConfig(outputConfig)
65+
.build();
66+
67+
OperationFuture<BatchPredictResult, OperationMetadata> future =
68+
client.batchPredictAsync(request);
69+
70+
System.out.println("Waiting for operation to complete...");
71+
BatchPredictResult response = future.get();
72+
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
73+
}
74+
}
75+
}
76+
// [END automl_language_batch_predict]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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.example.automl;
18+
19+
// [START automl_language_entity_extraction_create_dataset]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.Dataset;
23+
import com.google.cloud.automl.v1.LocationName;
24+
import com.google.cloud.automl.v1.OperationMetadata;
25+
import com.google.cloud.automl.v1.TextExtractionDatasetMetadata;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class LanguageEntityExtractionCreateDataset {
31+
32+
static void createDataset() throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR_PROJECT_ID";
35+
String displayName = "YOUR_DATASET_NAME";
36+
createDataset(projectId, displayName);
37+
}
38+
39+
// Create a dataset
40+
static void createDataset(String projectId, String displayName)
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+
// A resource that represents Google Cloud Platform location.
47+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
48+
49+
TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build();
50+
Dataset dataset =
51+
Dataset.newBuilder()
52+
.setDisplayName(displayName)
53+
.setTextExtractionDatasetMetadata(metadata)
54+
.build();
55+
OperationFuture<Dataset, OperationMetadata> future =
56+
client.createDatasetAsync(projectLocation, dataset);
57+
58+
Dataset createdDataset = future.get();
59+
60+
// Display the dataset information.
61+
System.out.format("Dataset name: %s\n", createdDataset.getName());
62+
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
63+
// required for other methods.
64+
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
65+
String[] names = createdDataset.getName().split("/");
66+
String datasetId = names[names.length - 1];
67+
System.out.format("Dataset id: %s\n", datasetId);
68+
}
69+
}
70+
}
71+
// [END automl_language_entity_extraction_create_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.example.automl;
18+
19+
// [START automl_language_entity_extraction_create_model]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.LocationName;
23+
import com.google.cloud.automl.v1.Model;
24+
import com.google.cloud.automl.v1.OperationMetadata;
25+
import com.google.cloud.automl.v1.TextExtractionModelMetadata;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class LanguageEntityExtractionCreateModel {
31+
32+
static void createModel() throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR_PROJECT_ID";
35+
String datasetId = "YOUR_DATASET_ID";
36+
String displayName = "YOUR_DATASET_NAME";
37+
createModel(projectId, datasetId, displayName);
38+
}
39+
40+
// Create a model
41+
static void createModel(String projectId, String datasetId, String displayName)
42+
throws IOException, ExecutionException, InterruptedException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the "close" method on the client to safely clean up any remaining background resources.
46+
try (AutoMlClient client = AutoMlClient.create()) {
47+
// A resource that represents Google Cloud Platform location.
48+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
49+
// Set model metadata.
50+
TextExtractionModelMetadata metadata = TextExtractionModelMetadata.newBuilder().build();
51+
Model model =
52+
Model.newBuilder()
53+
.setDisplayName(displayName)
54+
.setDatasetId(datasetId)
55+
.setTextExtractionModelMetadata(metadata)
56+
.build();
57+
58+
// Create a model with the model metadata in the region.
59+
OperationFuture<Model, OperationMetadata> future =
60+
client.createModelAsync(projectLocation, model);
61+
// OperationFuture.get() will block until the model is created, which may take several hours.
62+
// You can use OperationFuture.getInitialFuture to get a future representing the initial
63+
// response to the request, which contains information while the operation is in progress.
64+
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
65+
System.out.println("Training started...");
66+
}
67+
}
68+
}
69+
// [END automl_language_entity_extraction_create_model]

0 commit comments

Comments
 (0)