Skip to content

Commit 4838cff

Browse files
nnegreylesv
authored andcommitted
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 a886284 commit 4838cff

22 files changed

+399
-176
lines changed

automl/cloud-client/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# AutoML Samples
2+
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">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
7+
This directory contains samples for the [Google Cloud AutoML APIs](https://cloud.google.com/automl/) - [docs](https://cloud.google.com/automl/docs/)
8+
9+
We highly reccommend that you refer to the official documentation pages:
10+
<!--* AutoML Natural Language
11+
* [Classification](https://cloud.google.com/natural-language/automl/docs)
12+
* [Entity Extraction](https://cloud.google.com/natural-language/automl/entity-analysis/docs)
13+
* [Sentiment Analysis](https://cloud.google.com/natural-language/automl/sentiment/docs) -->
14+
* [AutoML Translation](https://cloud.google.com/translate/automl/docs)
15+
<!--* AutoML Video Intelligence
16+
* [Classification](https://cloud.google.com/video-intelligence/automl/docs)
17+
* [Object Tracking](https://cloud.google.com/video-intelligence/automl/object-tracking/docs)
18+
* AutoML Vision
19+
* [Classification](https://cloud.google.com/vision/automl/docs)
20+
* [Edge](https://cloud.google.com/vision/automl/docs/edge-quickstart)
21+
* [Object Detection](https://cloud.google.com/vision/automl/object-detection/docs)
22+
* [AutoML Tables](https://cloud.google.com/automl-tables/docs)-->
23+
24+
This API is part of the larger collection of Cloud Machine Learning APIs.
25+
26+
These Java samples demonstrates how to access the Cloud AutoML API
27+
using the [Google Cloud Client Library for Java][google-cloud-java].
28+
29+
[google-cloud-java]: https://github.com/GoogleCloudPlatform/google-cloud-java
30+
31+
## Build the samples
32+
33+
Install [Maven](http://maven.apache.org/).
34+
35+
Build your project with:
36+
37+
```
38+
mvn clean package
39+
```
40+
41+
## Sample Types
42+
There are two types of samples: Base and API Specific
43+
44+
The base samples make up a set of samples that have code that
45+
is identical or nearly identical for each AutoML Type. Meaning that for "Base" samples you can use them with any AutoML
46+
Type. However, for API Specific samples, there will be a unique sample for each AutoML type. See the below list for more info.
47+
48+
## Base Samples
49+
### Dataset Management
50+
* [Import Dataset](src/main/java/com/example/automl/ImportDataset.java)
51+
* [List Datasets](src/main/java/com/example/automl/ListDatasets.java) - For each AutoML Type the `metadata` field inside the dataset is unique, therefore each AutoML Type will have a
52+
small section of code to print out the `metadata` field.
53+
* [Get Dataset](src/main/java/com/example/automl/GetDataset.java) - For each AutoML Type the `metadata` field inside the dataset is unique, therefore each AutoML Type will have a
54+
small section of code to print out the `metadata` field.
55+
* [Export Dataset](src/main/java/com/example/automl/ExportDataset.java)
56+
* [Delete Dataset](src/main/java/com/example/automl/DeleteDataset.java)
57+
### Model Management
58+
* [List Models](src/main/java/com/example/automl/ListModels.java)
59+
* [List Model Evaluation](src/main/java/com/example/automl/ListModelEvaluations.java)
60+
* [Get Model](src/main/java/com/example/automl/)
61+
* [Get Model Evaluation](src/main/java/com/example/automl/GetModelEvaluation.java)
62+
* [Delete Model](src/main/java/com/example/automl/DeleteModel.java)
63+
64+
### Operation Management
65+
* [List Operation Statuses](src/main/java/com/example/automl/ListOperationStatus.java)
66+
* [Get Operation Status](src/main/java/com/example/automl/GetOperationStatus.java)
67+
68+
## AutoML Type Specific Samples
69+
### Translation
70+
* [Translate Create Dataset](src/main/java/com/example/automl/TranslateCreateDataset.java)
71+
* [Translate Create Model](src/main/java/com/example/automl/TranslateCreateModel.java)
72+
* [Translate Predict](src/main/java/com/example/automl/TranslatePredict.java)

automl/cloud-client/pom.xml

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+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<project>
14+
<modelVersion>4.0.0</modelVersion>
15+
<groupId>com.example.automl</groupId>
16+
<artifactId>automl-google-cloud-samples</artifactId>
17+
<packaging>jar</packaging>
18+
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
23+
<parent>
24+
<groupId>com.google.cloud.samples</groupId>
25+
<artifactId>shared-configuration</artifactId>
26+
<version>1.0.11</version>
27+
</parent>
28+
29+
<properties>
30+
<maven.compiler.target>1.11</maven.compiler.target>
31+
<maven.compiler.source>1.11</maven.compiler.source>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
</properties>
34+
35+
<dependencies>
36+
<!-- [START automl_java_dependencies] -->
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-automl</artifactId>
40+
<version>0.114.0-beta</version>
41+
</dependency>
42+
<!-- [END automl_java_dependencies] -->
43+
<dependency>
44+
<groupId>com.google.cloud</groupId>
45+
<artifactId>google-cloud-storage</artifactId>
46+
<version>1.83.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>net.sourceforge.argparse4j</groupId>
50+
<artifactId>argparse4j</artifactId>
51+
<version>0.8.1</version>
52+
</dependency>
53+
54+
<!-- Test dependencies -->
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.12</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.truth</groupId>
63+
<artifactId>truth</artifactId>
64+
<version>0.41</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tell me how this ends

translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java renamed to automl/cloud-client/src/main/java/com/example/automl/DeleteDataset.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.translate.automl;
17+
package com.example.automl;
1818

19-
// [START automl_translate_delete_dataset]
19+
// [START automl_delete_dataset]
2020
import com.google.cloud.automl.v1.AutoMlClient;
2121
import com.google.cloud.automl.v1.DatasetName;
2222
import com.google.protobuf.Empty;
@@ -26,11 +26,16 @@
2626

2727
class DeleteDataset {
2828

29-
// Delete a dataset
30-
static void deleteDataset(String projectId, String datasetId) {
31-
// String projectId = "YOUR_PROJECT_ID";
32-
// String datasetId = "YOUR_DATASET_ID";
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+
}
3335

36+
// Delete a dataset
37+
static void deleteDataset(String projectId, String datasetId)
38+
throws IOException, ExecutionException, InterruptedException {
3439
// Initialize client that will be used to send requests. This client only needs to be created
3540
// once, and can be reused for multiple requests. After completing all of your requests, call
3641
// the "close" method on the client to safely clean up any remaining background resources.
@@ -39,9 +44,7 @@ static void deleteDataset(String projectId, String datasetId) {
3944
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
4045
Empty response = client.deleteDatasetAsync(datasetFullId).get();
4146
System.out.format("Dataset deleted. %s\n", response);
42-
} catch (IOException | InterruptedException | ExecutionException e) {
43-
e.printStackTrace();
4447
}
4548
}
4649
}
47-
// [END automl_translate_delete_dataset]
50+
// [END automl_delete_dataset]

translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java renamed to automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.translate.automl;
17+
package com.example.automl;
1818

19-
// [START automl_translate_delete_model]
19+
// [START automl_delete_model]
2020
import com.google.cloud.automl.v1.AutoMlClient;
2121
import com.google.cloud.automl.v1.ModelName;
2222
import com.google.protobuf.Empty;
@@ -26,11 +26,16 @@
2626

2727
class DeleteModel {
2828

29-
// Get a model
30-
static void deleteModel(String projectId, String modelId) {
31-
// String projectId = "YOUR_PROJECT_ID";
32-
// String modelId = "YOUR_MODEL_ID";
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+
}
3335

36+
// Get a model
37+
static void deleteModel(String projectId, String modelId)
38+
throws IOException, ExecutionException, InterruptedException {
3439
// Initialize client that will be used to send requests. This client only needs to be created
3540
// once, and can be reused for multiple requests. After completing all of your requests, call
3641
// the "close" method on the client to safely clean up any remaining background resources.
@@ -43,9 +48,7 @@ static void deleteModel(String projectId, String modelId) {
4348

4449
System.out.println("Model deletion started...");
4550
System.out.println(String.format("Model deleted. %s", response));
46-
} catch (IOException | InterruptedException | ExecutionException e) {
47-
e.printStackTrace();
4851
}
4952
}
5053
}
51-
// [END automl_translate_delete_model]
54+
// [END automl_delete_model]

translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java renamed to automl/cloud-client/src/main/java/com/example/automl/ExportDataset.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.translate.automl;
17+
package com.example.automl;
1818

19-
// [START automl_translate_export_dataset]
19+
// [START automl_export_dataset]
2020
import com.google.cloud.automl.v1.AutoMlClient;
2121
import com.google.cloud.automl.v1.DatasetName;
2222
import com.google.cloud.automl.v1.GcsDestination;
@@ -28,12 +28,17 @@
2828

2929
class ExportDataset {
3030

31-
// Export a dataset
32-
static void exportDataset(String projectId, String datasetId, String gcsUri) {
33-
// String projectId = "YOUR_PROJECT_ID";
34-
// String datasetId = "YOUR_DATASET_ID";
35-
// String gcsUri = "gs://BUCKET_ID/path_to_export/";
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+
}
3638

39+
// Export a dataset
40+
static void exportDataset(String projectId, String datasetId, String gcsUri)
41+
throws IOException, ExecutionException, InterruptedException {
3742
// Initialize client that will be used to send requests. This client only needs to be created
3843
// once, and can be reused for multiple requests. After completing all of your requests, call
3944
// the "close" method on the client to safely clean up any remaining background resources.
@@ -50,9 +55,7 @@ static void exportDataset(String projectId, String datasetId, String gcsUri) {
5055
System.out.println("Processing export...");
5156
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
5257
System.out.format("Dataset exported. %s\n", response);
53-
} catch (IOException | InterruptedException | ExecutionException e) {
54-
e.printStackTrace();
5558
}
5659
}
5760
}
58-
// [END automl_translate_export_dataset]
61+
// [END automl_export_dataset]

translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java renamed to automl/cloud-client/src/main/java/com/example/automl/GetDataset.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.translate.automl;
17+
package com.example.automl;
1818

1919
// [START automl_translate_get_dataset]
20+
2021
import com.google.cloud.automl.v1.AutoMlClient;
2122
import com.google.cloud.automl.v1.Dataset;
2223
import com.google.cloud.automl.v1.DatasetName;
@@ -25,11 +26,15 @@
2526

2627
class GetDataset {
2728

28-
// Get a dataset
29-
static void getDataset(String projectId, String datasetId) {
30-
// String projectId = "YOUR_PROJECT_ID";
31-
// String datasetId = "YOUR_DATASET_ID";
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+
}
3235

36+
// Get a dataset
37+
static void getDataset(String projectId, String datasetId) throws IOException {
3338
// Initialize client that will be used to send requests. This client only needs to be created
3439
// once, and can be reused for multiple requests. After completing all of your requests, call
3540
// the "close" method on the client to safely clean up any remaining background resources.
@@ -57,8 +62,6 @@ static void getDataset(String projectId, String datasetId) {
5762
System.out.println("Dataset create time:");
5863
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
5964
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
60-
} catch (IOException e) {
61-
e.printStackTrace();
6265
}
6366
}
6467
}

translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java renamed to automl/cloud-client/src/main/java/com/example/automl/GetModel.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud.translate.automl;
17+
package com.example.automl;
1818

19-
// [START automl_translate_get_model]
19+
// [START automl_get_model]
2020
import com.google.cloud.automl.v1.AutoMlClient;
2121
import com.google.cloud.automl.v1.Model;
2222
import com.google.cloud.automl.v1.ModelName;
@@ -25,11 +25,15 @@
2525

2626
class GetModel {
2727

28-
// Get a model
29-
static void getModel(String projectId, String modelId) {
30-
// String projectId = "YOUR_PROJECT_ID";
31-
// String modelId = "YOUR_MODEL_ID";
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+
}
3234

35+
// Get a model
36+
static void getModel(String projectId, String modelId) throws IOException {
3337
// Initialize client that will be used to send requests. This client only needs to be created
3438
// once, and can be reused for multiple requests. After completing all of your requests, call
3539
// the "close" method on the client to safely clean up any remaining background resources.
@@ -51,9 +55,7 @@ static void getModel(String projectId, String modelId) {
5155
System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds());
5256
System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos());
5357
System.out.format("Model deployment state: %s\n", model.getDeploymentState());
54-
} catch (IOException e) {
55-
e.printStackTrace();
5658
}
5759
}
5860
}
59-
// [END automl_translate_get_model]
61+
// [END automl_get_model]

0 commit comments

Comments
 (0)