Skip to content

Commit 5068fb4

Browse files
nnegreyanguillanneuf
authored andcommitted
samples: Automl cleanup (#1621)
* Move samples and refactor them for simplification * Add missing resource files * Update GetDataset.java * Update ListDatasets.java * Remove output that isn't returned with create * Update pom to Java 11 * Update samples to new format * Use throw exception instead of catch
1 parent 26d79b8 commit 5068fb4

19 files changed

+1292
-0
lines changed

automl/snippets/resources/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tell me how this ends
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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_delete_dataset]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.DatasetName;
22+
import com.google.protobuf.Empty;
23+
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
class DeleteDataset {
28+
29+
static void deleteDataset() throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String datasetId = "YOUR_DATASET_ID";
33+
deleteDataset(projectId, datasetId);
34+
}
35+
36+
// Delete a dataset
37+
static void deleteDataset(String projectId, String datasetId)
38+
throws IOException, ExecutionException, InterruptedException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
// Get the full path of the dataset.
44+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
45+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
46+
System.out.format("Dataset deleted. %s\n", response);
47+
}
48+
}
49+
}
50+
// [END automl_delete_dataset]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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_delete_model]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.ModelName;
22+
import com.google.protobuf.Empty;
23+
24+
import java.io.IOException;
25+
import java.util.concurrent.ExecutionException;
26+
27+
class DeleteModel {
28+
29+
static void deleteModel() throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String modelId = "YOUR_MODEL_ID";
33+
deleteModel(projectId, modelId);
34+
}
35+
36+
// Get a model
37+
static void deleteModel(String projectId, String modelId)
38+
throws IOException, ExecutionException, InterruptedException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
// Get the full path of the model.
44+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
45+
46+
// Delete a model.
47+
Empty response = client.deleteModelAsync(modelFullId).get();
48+
49+
System.out.println("Model deletion started...");
50+
System.out.println(String.format("Model deleted. %s", response));
51+
}
52+
}
53+
}
54+
// [END automl_delete_model]
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.example.automl;
18+
19+
// [START automl_export_dataset]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.DatasetName;
22+
import com.google.cloud.automl.v1.GcsDestination;
23+
import com.google.cloud.automl.v1.OutputConfig;
24+
import com.google.protobuf.Empty;
25+
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
class ExportDataset {
30+
31+
static void exportDataset() throws IOException, ExecutionException, InterruptedException {
32+
// TODO(developer): Replace these variables before running the sample.
33+
String projectId = "YOUR_PROJECT_ID";
34+
String datasetId = "YOUR_DATASET_ID";
35+
String gcsUri = "gs://BUCKET_ID/path_to_export/";
36+
exportDataset(projectId, datasetId, gcsUri);
37+
}
38+
39+
// Export a dataset
40+
static void exportDataset(String projectId, String datasetId, String gcsUri)
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 complete path of the dataset.
47+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
48+
GcsDestination gcsDestination =
49+
GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
50+
51+
// Export the dataset to the output URI.
52+
OutputConfig outputConfig =
53+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
54+
55+
System.out.println("Processing export...");
56+
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
57+
System.out.format("Dataset exported. %s\n", response);
58+
}
59+
}
60+
}
61+
// [END automl_export_dataset]
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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_translate_get_dataset]
20+
21+
import com.google.cloud.automl.v1.AutoMlClient;
22+
import com.google.cloud.automl.v1.Dataset;
23+
import com.google.cloud.automl.v1.DatasetName;
24+
25+
import java.io.IOException;
26+
27+
class GetDataset {
28+
29+
static void getDataset() throws IOException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String datasetId = "YOUR_DATASET_ID";
33+
getDataset(projectId, datasetId);
34+
}
35+
36+
// Get a dataset
37+
static void getDataset(String projectId, String datasetId) throws IOException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// Get the complete path of the dataset.
43+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
44+
Dataset dataset = client.getDataset(datasetFullId);
45+
46+
// Display the dataset information
47+
System.out.format("Dataset name: %s\n", dataset.getName());
48+
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
49+
// required for other methods.
50+
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
51+
String[] names = dataset.getName().split("/");
52+
String retrievedDatasetId = names[names.length - 1];
53+
System.out.format("Dataset id: %s\n", retrievedDatasetId);
54+
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
55+
System.out.println("Translation dataset metadata:");
56+
System.out.format(
57+
"\tSource language code: %s\n",
58+
dataset.getTranslationDatasetMetadata().getSourceLanguageCode());
59+
System.out.format(
60+
"\tTarget language code: %s\n",
61+
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());
65+
}
66+
}
67+
}
68+
// [END automl_translate_get_dataset]
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.example.automl;
18+
19+
// [START automl_get_model]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.Model;
22+
import com.google.cloud.automl.v1.ModelName;
23+
24+
import java.io.IOException;
25+
26+
class GetModel {
27+
28+
static void getModel() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String modelId = "YOUR_MODEL_ID";
32+
getModel(projectId, modelId);
33+
}
34+
35+
// Get a model
36+
static void getModel(String projectId, String modelId) throws IOException {
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+
Model model = client.getModel(modelFullId);
44+
45+
// Display the model information.
46+
System.out.format("Model name: %s\n", model.getName());
47+
// To get the model id, you have to parse it out of the `name` field. As models Ids are
48+
// required for other methods.
49+
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
50+
String[] names = model.getName().split("/");
51+
String retrievedModelId = names[names.length - 1];
52+
System.out.format("Model id: %s\n", retrievedModelId);
53+
System.out.format("Model display name: %s\n", model.getDisplayName());
54+
System.out.println("Model create time:");
55+
System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds());
56+
System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos());
57+
System.out.format("Model deployment state: %s\n", model.getDeploymentState());
58+
}
59+
}
60+
}
61+
// [END automl_get_model]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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_get_model_evaluation]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.ModelEvaluation;
22+
import com.google.cloud.automl.v1.ModelEvaluationName;
23+
24+
import java.io.IOException;
25+
26+
class GetModelEvaluation {
27+
28+
static void getModelEvaluation() throws IOException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String modelId = "YOUR_MODEL_ID";
32+
String modelEvaluationId = "YOUR_MODEL_EVALUATION_ID";
33+
getModelEvaluation(projectId, modelId, modelEvaluationId);
34+
}
35+
36+
// Get a model evaluation
37+
static void getModelEvaluation(String projectId, String modelId, String modelEvaluationId)
38+
throws IOException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
// Get the full path of the model evaluation.
44+
ModelEvaluationName modelEvaluationFullId =
45+
ModelEvaluationName.of(projectId, "us-central1", modelId, modelEvaluationId);
46+
47+
// Get complete detail of the model evaluation.
48+
ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId);
49+
50+
System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName());
51+
System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId());
52+
System.out.println("Create Time:");
53+
System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds());
54+
System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9);
55+
System.out.format(
56+
"Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount());
57+
System.out.format(
58+
"Model Evaluation Metrics: %s\n", modelEvaluation.getTranslationEvaluationMetrics());
59+
}
60+
}
61+
}
62+
// [END automl_get_model_evaluation]

0 commit comments

Comments
 (0)