Skip to content

Commit 81ff240

Browse files
authored
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 9b6008c commit 81ff240

File tree

51 files changed

+3506
-23
lines changed

Some content is hidden

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

51 files changed

+3506
-23
lines changed

automl/cloud-client/README.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# AutoML Samples
22

3-
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=vision/beta/cloud-client/README.md">
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=automl/cloud-client/README.md">
44
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
55

66

77
This directory contains samples for the [Google Cloud AutoML APIs](https://cloud.google.com/automl/) - [docs](https://cloud.google.com/automl/docs/)
88

99
We highly reccommend that you refer to the official documentation pages:
10-
<!--* AutoML Natural Language
10+
* AutoML Natural Language
1111
* [Classification](https://cloud.google.com/natural-language/automl/docs)
1212
* [Entity Extraction](https://cloud.google.com/natural-language/automl/entity-analysis/docs)
13-
* [Sentiment Analysis](https://cloud.google.com/natural-language/automl/sentiment/docs) -->
13+
* [Sentiment Analysis](https://cloud.google.com/natural-language/automl/sentiment/docs)
1414
* [AutoML Translation](https://cloud.google.com/translate/automl/docs)
1515
<!--* AutoML Video Intelligence
1616
* [Classification](https://cloud.google.com/video-intelligence/automl/docs)
17-
* [Object Tracking](https://cloud.google.com/video-intelligence/automl/object-tracking/docs)
17+
* [Object Tracking](https://cloud.google.com/video-intelligence/automl/object-tracking/docs) -->
1818
* AutoML Vision
1919
* [Classification](https://cloud.google.com/vision/automl/docs)
20-
* [Edge](https://cloud.google.com/vision/automl/docs/edge-quickstart)
20+
<!--* [Edge](https://cloud.google.com/vision/automl/docs/edge-quickstart) -->
2121
* [Object Detection](https://cloud.google.com/vision/automl/object-detection/docs)
22-
* [AutoML Tables](https://cloud.google.com/automl-tables/docs)-->
22+
<!--* [AutoML Tables](https://cloud.google.com/automl-tables/docs)-->
2323

2424
This API is part of the larger collection of Cloud Machine Learning APIs.
2525

@@ -60,6 +60,9 @@ small section of code to print out the `metadata` field.
6060
* [Get Model](src/main/java/com/example/automl/)
6161
* [Get Model Evaluation](src/main/java/com/example/automl/GetModelEvaluation.java)
6262
* [Delete Model](src/main/java/com/example/automl/DeleteModel.java)
63+
* [Deploy Model](src/main/java/com/example/automl/DeployModel.java) - Not supported by Translation
64+
* [Uneploy Model](src/main/java/com/example/automl/UndeployModel.java) - Not supported by Translation
65+
6366

6467
### Operation Management
6568
* [List Operation Statuses](src/main/java/com/example/automl/ListOperationStatus.java)
@@ -69,4 +72,34 @@ small section of code to print out the `metadata` field.
6972
### Translation
7073
* [Translate Create Dataset](src/main/java/com/example/automl/TranslateCreateDataset.java)
7174
* [Translate Create Model](src/main/java/com/example/automl/TranslateCreateModel.java)
72-
* [Translate Predict](src/main/java/com/example/automl/TranslatePredict.java)
75+
* [Translate Predict](src/main/java/com/example/automl/TranslatePredict.java)
76+
77+
### Natural Language Entity Extraction
78+
* [Entity Extraction Create Dataset](src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java)
79+
* [Entity Extraction Create Model](src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java)
80+
* [Entity Extraction Predict](src/main/java/com/example/automl/LanguageEntityExtractionPredict.java)
81+
* [Entity Extraction Batch Predict](src/main/java/com/example/automl/LanguageBatchPredict.java)
82+
83+
### Natural Language Sentiment Analysis
84+
* [Sentiment Analysis Create Dataset](src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java)
85+
* [Sentiment Analysis Create Model](src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java)
86+
* [Sentiment Analysis Predict](src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java)
87+
88+
### Natural Language Text Classification
89+
* [Text Classification Create Dataset](src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java)
90+
* [Text Classification Create Model](src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java)
91+
* [Text Classification Predict](src/main/java/com/example/automl/LanguageTextClassificationPredict.java)
92+
93+
### Vision Classification
94+
* [Classification Create Dataset](src/main/java/com/example/automl/VisionClassificationCreateDataset.java)
95+
* [Classification Create Model](src/main/java/com/example/automl/VisionClassificationCreateModel.java)
96+
* [Classification Predict](src/main/java/com/example/automl/VisionClassificationPredict.java)
97+
* [Classification Batch Predict](src/main/java/com/example/automl/VisionBatchPredict.java)
98+
* [Deploy Node Count](src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java)
99+
100+
### Vision Object Detection
101+
* [Object Detection Create Dataset](src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java)
102+
* [Object Detection Create Model](src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java)
103+
* [Object Detection Predict](src/main/java/com/example/automl/VisionObjectDetectionPredict.java)
104+
* [Object Detection Batch Predict](src/main/java/com/example/automl/VisionBatchPredict.java)
105+
* [Deploy Node Count](src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java)

automl/cloud-client/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<dependency>
3838
<groupId>com.google.cloud</groupId>
3939
<artifactId>google-cloud-automl</artifactId>
40-
<version>0.114.1-beta</version>
40+
<version>0.115.0-beta</version>
4141
</dependency>
4242
<!-- [END automl_java_dependencies] -->
4343
<dependency>
2.25 MB
Loading
1.97 MB
Loading

automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java

Lines changed: 1 addition & 1 deletion
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
Lines changed: 57 additions & 0 deletions
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/cloud-client/src/main/java/com/example/automl/ExportDataset.java

Lines changed: 1 addition & 1 deletion
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/cloud-client/src/main/java/com/example/automl/GetDataset.java

Lines changed: 51 additions & 4 deletions
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]
Lines changed: 76 additions & 0 deletions
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]
Lines changed: 71 additions & 0 deletions
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]

0 commit comments

Comments
 (0)