Skip to content

Commit 89435fa

Browse files
nnegreybradmiro
authored andcommitted
Automl translation ga (#1614)
* Convert samples to new style guides and update to GA. Add missing samples * Update samples and tests * Update ExportDataset.java * Update Prediction.java * Update based on feedback * Update ListOperationStatus.java * Update ID of test dataset * Lint: Update License header and import order
1 parent 061c3df commit 89435fa

20 files changed

+1218
-5
lines changed

translate/automl/pom.xml

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-automl</artifactId>
43-
<version>0.55.0-beta</version>
43+
<version>0.114.0-beta</version>
44+
</dependency>
45+
<!-- [END automl_translate_java_dependencies] -->
46+
<dependency>
47+
<groupId>com.google.cloud</groupId>
48+
<artifactId>google-cloud-storage</artifactId>
49+
<version>1.83.0</version>
4450
</dependency>
4551
<dependency>
4652
<groupId>net.sourceforge.argparse4j</groupId>
4753
<artifactId>argparse4j</artifactId>
4854
<version>0.8.1</version>
4955
</dependency>
50-
<!-- [END automl_translate_java_dependencies] -->
5156

5257
<!-- Test dependencies -->
5358
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_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.TranslationDatasetMetadata;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class CreateDataset {
31+
32+
// Create a dataset
33+
static void createDataset(String projectId, String displayName) {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String displayName = "YOUR_DATASET_NAME";
36+
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+
// A resource that represents Google Cloud Platform location.
42+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
43+
44+
// Specify the source and target language.
45+
TranslationDatasetMetadata translationDatasetMetadata =
46+
TranslationDatasetMetadata.newBuilder()
47+
.setSourceLanguageCode("en")
48+
.setTargetLanguageCode("ja")
49+
.build();
50+
Dataset dataset =
51+
Dataset.newBuilder()
52+
.setDisplayName(displayName)
53+
.setTranslationDatasetMetadata(translationDatasetMetadata)
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+
System.out.format("Dataset display name: %s\n", createdDataset.getDisplayName());
69+
System.out.println("Translation dataset Metadata:");
70+
System.out.format(
71+
"\tSource language code: %s\n",
72+
createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode());
73+
System.out.format(
74+
"\tTarget language code: %s\n",
75+
createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode());
76+
System.out.println("Dataset create time:");
77+
System.out.format("\tseconds: %s\n", createdDataset.getCreateTime().getSeconds());
78+
System.out.format("\tnanos: %s\n", createdDataset.getCreateTime().getNanos());
79+
} catch (IOException | InterruptedException | ExecutionException e) {
80+
e.printStackTrace();
81+
}
82+
}
83+
}
84+
// [END automl_translate_create_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_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.TranslationModelMetadata;
26+
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class CreateModel {
31+
32+
// Create a model
33+
static void createModel(String projectId, String datasetId, String displayName) {
34+
// String projectId = "YOUR_PROJECT_ID";
35+
// String datasetId = "YOUR_DATASET_ID";
36+
// String displayName = "YOUR_DATASET_NAME";
37+
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+
// A resource that represents Google Cloud Platform location.
43+
LocationName projectLocation = LocationName.of(projectId, "us-central1");
44+
// Leave model unset to use the default base model provided by Google
45+
TranslationModelMetadata translationModelMetadata =
46+
TranslationModelMetadata.newBuilder().build();
47+
Model model =
48+
Model.newBuilder()
49+
.setDisplayName(displayName)
50+
.setDatasetId(datasetId)
51+
.setTranslationModelMetadata(translationModelMetadata)
52+
.build();
53+
54+
// Create a model with the model metadata in the region.
55+
OperationFuture<Model, OperationMetadata> future =
56+
client.createModelAsync(projectLocation, model);
57+
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
58+
System.out.println("Training started...");
59+
} catch (IOException | InterruptedException | ExecutionException e) {
60+
e.printStackTrace();
61+
}
62+
}
63+
}
64+
// [END automl_translate_create_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_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+
// Delete a dataset
30+
static void deleteDataset(String projectId, String datasetId) {
31+
// String projectId = "YOUR_PROJECT_ID";
32+
// String datasetId = "YOUR_DATASET_ID";
33+
34+
// Initialize client that will be used to send requests. This client only needs to be created
35+
// once, and can be reused for multiple requests. After completing all of your requests, call
36+
// the "close" method on the client to safely clean up any remaining background resources.
37+
try (AutoMlClient client = AutoMlClient.create()) {
38+
// Get the full path of the dataset.
39+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
40+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
41+
System.out.format("Dataset deleted. %s\n", response);
42+
} catch (IOException | InterruptedException | ExecutionException e) {
43+
e.printStackTrace();
44+
}
45+
}
46+
}
47+
// [END automl_translate_delete_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_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+
// Get a model
30+
static void deleteModel(String projectId, String modelId) {
31+
// String projectId = "YOUR_PROJECT_ID";
32+
// String modelId = "YOUR_MODEL_ID";
33+
34+
// Initialize client that will be used to send requests. This client only needs to be created
35+
// once, and can be reused for multiple requests. After completing all of your requests, call
36+
// the "close" method on the client to safely clean up any remaining background resources.
37+
try (AutoMlClient client = AutoMlClient.create()) {
38+
// Get the full path of the model.
39+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
40+
41+
// Delete a model.
42+
Empty response = client.deleteModelAsync(modelFullId).get();
43+
44+
System.out.println("Model deletion started...");
45+
System.out.println(String.format("Model deleted. %s", response));
46+
} catch (IOException | InterruptedException | ExecutionException e) {
47+
e.printStackTrace();
48+
}
49+
}
50+
}
51+
// [END automl_translate_delete_model]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_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+
// 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/";
36+
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 complete path of the dataset.
42+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
43+
GcsDestination gcsDestination =
44+
GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
45+
46+
// Export the dataset to the output URI.
47+
OutputConfig outputConfig =
48+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
49+
50+
System.out.println("Processing export...");
51+
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
52+
System.out.format("Dataset exported. %s\n", response);
53+
} catch (IOException | InterruptedException | ExecutionException e) {
54+
e.printStackTrace();
55+
}
56+
}
57+
}
58+
// [END automl_translate_export_dataset]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.google.cloud.translate.automl;
18+
19+
// [START automl_translate_get_dataset]
20+
import com.google.cloud.automl.v1.AutoMlClient;
21+
import com.google.cloud.automl.v1.Dataset;
22+
import com.google.cloud.automl.v1.DatasetName;
23+
24+
import java.io.IOException;
25+
26+
class GetDataset {
27+
28+
// Get a dataset
29+
static void getDataset(String projectId, String datasetId) {
30+
// String projectId = "YOUR_PROJECT_ID";
31+
// String datasetId = "YOUR_DATASET_ID";
32+
33+
// Initialize client that will be used to send requests. This client only needs to be created
34+
// once, and can be reused for multiple requests. After completing all of your requests, call
35+
// the "close" method on the client to safely clean up any remaining background resources.
36+
try (AutoMlClient client = AutoMlClient.create()) {
37+
// Get the complete path of the dataset.
38+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
39+
Dataset dataset = client.getDataset(datasetFullId);
40+
41+
// Display the dataset information
42+
System.out.format("Dataset name: %s\n", dataset.getName());
43+
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
44+
// required for other methods.
45+
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
46+
String[] names = dataset.getName().split("/");
47+
String retrievedDatasetId = names[names.length - 1];
48+
System.out.format("Dataset id: %s\n", retrievedDatasetId);
49+
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
50+
System.out.println("Translation dataset metadata:");
51+
System.out.format(
52+
"\tSource language code: %s\n",
53+
dataset.getTranslationDatasetMetadata().getSourceLanguageCode());
54+
System.out.format(
55+
"\tTarget language code: %s\n",
56+
dataset.getTranslationDatasetMetadata().getTargetLanguageCode());
57+
System.out.println("Dataset create time:");
58+
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
59+
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
60+
} catch (IOException e) {
61+
e.printStackTrace();
62+
}
63+
}
64+
}
65+
// [END automl_translate_get_dataset]

0 commit comments

Comments
 (0)