Skip to content

Automl translation ga #1614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 17, 2019
9 changes: 7 additions & 2 deletions translate/automl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-automl</artifactId>
<version>0.55.0-beta</version>
<version>0.114.0-beta</version>
</dependency>
<!-- [END automl_translate_java_dependencies] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.83.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.argparse4j</groupId>
<artifactId>argparse4j</artifactId>
<version>0.8.1</version>
</dependency>
<!-- [END automl_translate_java_dependencies] -->

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_create_dataset]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.Dataset;
import com.google.cloud.automl.v1.LocationName;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.cloud.automl.v1.TranslationDatasetMetadata;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class CreateDataset {

// Create a dataset
static void createDataset(String projectId, String displayName) {
// String projectId = "YOUR_PROJECT_ID";
// String displayName = "YOUR_DATASET_NAME";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");

// Specify the source and target language.
TranslationDatasetMetadata translationDatasetMetadata =
TranslationDatasetMetadata.newBuilder()
.setSourceLanguageCode("en")
.setTargetLanguageCode("ja")
.build();
Dataset dataset =
Dataset.newBuilder()
.setDisplayName(displayName)
.setTranslationDatasetMetadata(translationDatasetMetadata)
.build();
OperationFuture<Dataset, OperationMetadata> future =
client.createDatasetAsync(projectLocation, dataset);

Dataset createdDataset = future.get();

// Display the dataset information.
System.out.format("Dataset name: %s\n", createdDataset.getName());
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
// required for other methods.
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
String[] names = createdDataset.getName().split("/");
String datasetId = names[names.length - 1];
System.out.format("Dataset id: %s\n", datasetId);
System.out.format("Dataset display name: %s\n", createdDataset.getDisplayName());
System.out.println("Translation dataset Metadata:");
System.out.format(
"\tSource language code: %s\n",
createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode());
System.out.format(
"\tTarget language code: %s\n",
createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode());
System.out.println("Dataset create time:");
System.out.format("\tseconds: %s\n", createdDataset.getCreateTime().getSeconds());
System.out.format("\tnanos: %s\n", createdDataset.getCreateTime().getNanos());
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_create_dataset]
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_create_model]
import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.LocationName;
import com.google.cloud.automl.v1.Model;
import com.google.cloud.automl.v1.OperationMetadata;
import com.google.cloud.automl.v1.TranslationModelMetadata;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class CreateModel {

// Create a model
static void createModel(String projectId, String datasetId, String displayName) {
// String projectId = "YOUR_PROJECT_ID";
// String datasetId = "YOUR_DATASET_ID";
// String displayName = "YOUR_DATASET_NAME";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// A resource that represents Google Cloud Platform location.
LocationName projectLocation = LocationName.of(projectId, "us-central1");
// Leave model unset to use the default base model provided by Google
TranslationModelMetadata translationModelMetadata =
TranslationModelMetadata.newBuilder().build();
Model model =
Model.newBuilder()
.setDisplayName(displayName)
.setDatasetId(datasetId)
.setTranslationModelMetadata(translationModelMetadata)
.build();

// Create a model with the model metadata in the region.
OperationFuture<Model, OperationMetadata> future =
client.createModelAsync(projectLocation, model);
System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName());
System.out.println("Training started...");
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_create_model]
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_delete_dataset]
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DatasetName;
import com.google.protobuf.Empty;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeleteDataset {

// Delete a dataset
static void deleteDataset(String projectId, String datasetId) {
// String projectId = "YOUR_PROJECT_ID";
// String datasetId = "YOUR_DATASET_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
Empty response = client.deleteDatasetAsync(datasetFullId).get();
System.out.format("Dataset deleted. %s\n", response);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_delete_dataset]
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_delete_model]
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.ModelName;
import com.google.protobuf.Empty;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class DeleteModel {

// Get a model
static void deleteModel(String projectId, String modelId) {
// String projectId = "YOUR_PROJECT_ID";
// String modelId = "YOUR_MODEL_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the full path of the model.
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);

// Delete a model.
Empty response = client.deleteModelAsync(modelFullId).get();

System.out.println("Model deletion started...");
System.out.println(String.format("Model deleted. %s", response));
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_delete_model]
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_export_dataset]
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.DatasetName;
import com.google.cloud.automl.v1.GcsDestination;
import com.google.cloud.automl.v1.OutputConfig;
import com.google.protobuf.Empty;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

class ExportDataset {

// Export a dataset
static void exportDataset(String projectId, String datasetId, String gcsUri) {
// String projectId = "YOUR_PROJECT_ID";
// String datasetId = "YOUR_DATASET_ID";
// String gcsUri = "gs://BUCKET_ID/path_to_export/";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the complete path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
GcsDestination gcsDestination =
GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();

// Export the dataset to the output URI.
OutputConfig outputConfig =
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();

System.out.println("Processing export...");
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
System.out.format("Dataset exported. %s\n", response);
} catch (IOException | InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_export_dataset]
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2019 LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.translate.automl;

// [START automl_translate_get_dataset]
import com.google.cloud.automl.v1.AutoMlClient;
import com.google.cloud.automl.v1.Dataset;
import com.google.cloud.automl.v1.DatasetName;

import java.io.IOException;

class GetDataset {

// Get a dataset
static void getDataset(String projectId, String datasetId) {
// String projectId = "YOUR_PROJECT_ID";
// String datasetId = "YOUR_DATASET_ID";

// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (AutoMlClient client = AutoMlClient.create()) {
// Get the complete path of the dataset.
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
Dataset dataset = client.getDataset(datasetFullId);

// Display the dataset information
System.out.format("Dataset name: %s\n", dataset.getName());
// To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
// required for other methods.
// Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
String[] names = dataset.getName().split("/");
String retrievedDatasetId = names[names.length - 1];
System.out.format("Dataset id: %s\n", retrievedDatasetId);
System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
System.out.println("Translation dataset metadata:");
System.out.format(
"\tSource language code: %s\n",
dataset.getTranslationDatasetMetadata().getSourceLanguageCode());
System.out.format(
"\tTarget language code: %s\n",
dataset.getTranslationDatasetMetadata().getTargetLanguageCode());
System.out.println("Dataset create time:");
System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
} catch (IOException e) {
e.printStackTrace();
}
}
}
// [END automl_translate_get_dataset]
Loading