From f7b283b44db1605cc877661f090559eea0eaf2df Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 9 Oct 2019 22:08:47 -0600 Subject: [PATCH 1/8] Convert samples to new style guides and update to GA. Add missing samples --- translate/automl/pom.xml | 9 +- .../cloud/translate/automl/CreateDataset.java | 80 +++++++++++ .../cloud/translate/automl/CreateModel.java | 58 ++++++++ .../cloud/translate/automl/DeleteDataset.java | 47 ++++++ .../cloud/translate/automl/DeleteModel.java | 51 +++++++ .../cloud/translate/automl/DeployModel.java | 55 +++++++ .../cloud/translate/automl/ExportDataset.java | 55 +++++++ .../cloud/translate/automl/GetDataset.java | 62 ++++++++ .../cloud/translate/automl/GetModel.java | 55 +++++++ .../translate/automl/GetModelEvaluation.java | 57 ++++++++ .../translate/automl/GetOperationStatus.java | 62 ++++++++ .../cloud/translate/automl/ImportDataset.java | 62 ++++++++ .../cloud/translate/automl/ListDatasets.java | 70 +++++++++ .../automl/ListModelEvaluations.java | 65 +++++++++ .../cloud/translate/automl/ListModels.java | 66 +++++++++ .../translate/automl/ListOperationStatus.java | 74 ++++++++++ .../cloud/translate/automl/Prediction.java | 63 ++++++++ .../cloud/translate/automl/UndeployModel.java | 56 ++++++++ .../cloud/translate/automl/DatasetIT.java | 126 ++++++++++++++++ .../cloud/translate/automl/ModelIT.java | 134 ++++++++++++++++++ .../translate/automl/PredictionApiIT.java | 11 +- 21 files changed, 1315 insertions(+), 3 deletions(-) create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java create mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java create mode 100644 translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java create mode 100644 translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java diff --git a/translate/automl/pom.xml b/translate/automl/pom.xml index 221014744c8..c46d22991fd 100644 --- a/translate/automl/pom.xml +++ b/translate/automl/pom.xml @@ -40,14 +40,19 @@ com.google.cloud google-cloud-automl - 0.55.0-beta + 0.113.0-beta + + + + com.google.cloud + google-cloud-storage + 1.83.0 net.sourceforge.argparse4j argparse4j 0.8.1 - diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java new file mode 100644 index 00000000000..f39dbee6e7b --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java @@ -0,0 +1,80 @@ +/* + * 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 future = client.createDataset(projectLocation, dataset); + + Dataset createdDataset = future.get(); + + // Display the dataset information. + System.out.format("Dataset name: %s\n", createdDataset.getName()); + System.out.format( + "Dataset id: %s\n", + createdDataset.getName().split("/")[createdDataset.getName().split("/").length - 1]); + 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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java new file mode 100644 index 00000000000..d8b959f8661 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java @@ -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_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 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"); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture 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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java new file mode 100644 index 00000000000..34bea337bfb --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java @@ -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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java new file mode 100644 index 00000000000..0790f81ac05 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java @@ -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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java new file mode 100644 index 00000000000..54649a63c92 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java @@ -0,0 +1,55 @@ +/* + * 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_deploy_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.protobuf.Empty; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeployModel { + + // Deploy a model for prediction + static void deployModel(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); + DeployModelRequest request = DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .build(); + OperationFuture future = client.deployModelAsync(request); + + future.get(); + System.out.println("Model deployment finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_deploy_model] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java new file mode 100644 index 00000000000..72fb0b363c0 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java @@ -0,0 +1,55 @@ +/* + * 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://"; + + // 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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java new file mode 100644 index 00000000000..9cfbb49d739 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java @@ -0,0 +1,62 @@ +/* + * 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()); + System.out.format( + "Dataset id: %s\n", + dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + 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] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java new file mode 100644 index 00000000000..dd52f5995f2 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java @@ -0,0 +1,55 @@ +/* + * 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_model] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; + +import java.io.IOException; + +class GetModel { + + // Get a model + static void getModel(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); + Model model = client.getModel(modelFullId); + + // Display the model information. + System.out.format("Model name: %s\n", model.getName()); + System.out.format( + "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); + System.out.format("Model display name: %s\n", model.getDisplayName()); + System.out.println("Model create time:"); + System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos()); + System.out.format("Model deployment state: %s\n", model.getDeploymentState()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_get_model] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java new file mode 100644 index 00000000000..859cf0e79e8 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java @@ -0,0 +1,57 @@ +/* + * 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_model_evaluation] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ModelEvaluation; +import com.google.cloud.automl.v1.ModelEvaluationName; + +import java.io.IOException; + +class GetModelEvaluation { + + // Get a model evaluation + static void getModelEvaluation(String projectId, String modelId, String modelEvaluationId) { + // String projectId = "YOUR_PROJECT_ID"; + // String modelId = "YOUR_MODEL_ID"; + // String modelEvaluationId = "YOUR_MODEL_EVALUATION_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 evaluation. + ModelEvaluationName modelEvaluationFullId = + ModelEvaluationName.of(projectId, "us-central1", modelId, modelEvaluationId); + + // Get complete detail of the model evaluation. + ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId); + + System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName()); + System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId()); + System.out.println("Create Time:"); + System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9); + System.out.format("Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount()); + System.out.format("Model Evaluation Metrics: %s\n", modelEvaluation.getTranslationEvaluationMetrics()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_get_model_evaluation] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java new file mode 100644 index 00000000000..6b22560585b --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java @@ -0,0 +1,62 @@ +/* + * 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_operation_status] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.longrunning.Operation; + +import java.io.IOException; + +class GetOperationStatus { + + // Get the status of an operation + static void getOperationStatus(String operationFullId) { + // String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]"; + + // 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 latest state of a long-running operation. + Operation operation = client.getOperationsClient().getOperation(operationFullId); + + // Display operation details. + System.out.println("Operation details:"); + System.out.format("\tName: %s\n", operation.getName()); + System.out.println("\tMetadata:"); + System.out.format("\t\tType Url: %s\n", operation.getMetadata().getTypeUrl()); + System.out.format( + "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tDone: %s\n", operation.getDone()); + if (operation.hasResponse()) { + System.out.println("\tResponse:"); + System.out.format("\t\tType Url: %s\n", operation.getResponse().getTypeUrl()); + System.out.format( + "\t\tValue: %s\n", operation.getResponse().getValue().toStringUtf8().replace("\n", "")); + } + if (operation.hasError()) { + System.out.println("\tResponse:"); + System.out.format("\t\tError code: %s\n", operation.getError().getCode()); + System.out.format("\t\tError message: %s\n", operation.getError().getMessage()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_get_operation_status] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java new file mode 100644 index 00000000000..1eba27125f1 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java @@ -0,0 +1,62 @@ +/* + * 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_import_dataset] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DatasetName; +import com.google.cloud.automl.v1.GcsSource; +import com.google.cloud.automl.v1.InputConfig; +import com.google.protobuf.Empty; + +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +class ImportDataset { + + // 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. + static void importDataset(String projectId, String datasetId, String path) { + // String projectId = "YOUR_PROJECT_ID"; + // String datasetId = "YOUR_DATASET_ID"; + // String path = "gs://BUCKET_ID/path_to_training_data.csv"; + + // Instantiates a client + try (AutoMlClient client = AutoMlClient.create()) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + + // Get multiple Google Cloud Storage URIs to import data from + GcsSource gcsSource = GcsSource.newBuilder() + .addAllInputUris( + Arrays.asList(path.split(","))) + .build(); + + // Import data from the input URI + InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); + System.out.println("Processing import..."); + + Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); + System.out.format("Dataset imported. %s\n", response); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_import_dataset] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java new file mode 100644 index 00000000000..173f75a56ab --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java @@ -0,0 +1,70 @@ +/* + * 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_list_dataset] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.Dataset; +import com.google.cloud.automl.v1.ListDatasetsRequest; +import com.google.cloud.automl.v1.LocationName; + +import java.io.IOException; + +class ListDatasets { + + // List the datasets + static void listDatasets(String projectId) { + // String projectId = "YOUR_PROJECT_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()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("translation_dataset_metadata:*") + .build(); + + // List all the datasets available in the region by applying filter. + System.out.println("List of datasets:"); + for (Dataset dataset : client.listDatasets(request).iterateAll()) { + // Display the dataset information + System.out.format("\nDataset name: %s\n", dataset.getName()); + System.out.format( + "Dataset id: %s\n", + dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + 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_list_dataset] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java new file mode 100644 index 00000000000..a4d32c4942b --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java @@ -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_list_model_evaluation] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ListModelEvaluationsRequest; +import com.google.cloud.automl.v1.ModelEvaluation; +import com.google.cloud.automl.v1.ModelName; + +import java.io.IOException; + +class ListModelEvaluations { + + // List model evaluations + static void listModelEvaluations(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); + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder() + .setParent(modelFullId.toString()) + .build(); + + // List all the model evaluations in the model by applying filter. + System.out.println("List of model evaluations:"); + for (ModelEvaluation modelEvaluation : + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + + System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName()); + System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId()); + System.out.println("Create Time:"); + System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9); + System.out.format( + "Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount()); + System.out.format( + "Model Evaluation Metrics: %s\n\n", modelEvaluation.getTranslationEvaluationMetrics()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_list_model_evaluation] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java new file mode 100644 index 00000000000..ee937bc03a8 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java @@ -0,0 +1,66 @@ +/* + * 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_list_model] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ListModelsRequest; +import com.google.cloud.automl.v1.LocationName; +import com.google.cloud.automl.v1.Model; + +import java.io.IOException; + +class ListModels { + + // List models + static void listModels(String projectId) { + // String projectId = "YOUR_PROJECT_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()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + + // Create list models request. + ListModelsRequest listModlesRequest = + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("") + .build(); + + // List all the models available in the region by applying filter. + System.out.println("List of models:"); + for (Model model : client.listModels(listModlesRequest).iterateAll()) { + // Display the model information. + System.out.format("Model name: %s\n", model.getName()); + System.out.format( + "Model id: %s\n", + model.getName().split("/")[model.getName().split("/").length - 1]); + System.out.format("Model display name: %s\n", model.getDisplayName()); + System.out.println("Model create time:"); + System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", model.getCreateTime().getNanos()); + System.out.format("Model deployment state: %s\n", model.getDeploymentState()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_list_model] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java new file mode 100644 index 00000000000..262a689bed2 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -0,0 +1,74 @@ +/* + * 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_list_operation_status] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.LocationName; +import com.google.longrunning.ListOperationsRequest; +import com.google.longrunning.Operation; + +import java.io.IOException; + +class ListOperationStatus { + + // Get the status of an operation + static void listOperationStatus(String projectId) { + // String projectId = "YOUR_PROJECT_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()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + + // Create list operations request. + ListOperationsRequest listrequest = + ListOperationsRequest.newBuilder() + .setName(projectLocation.toString()) + .build(); + + // List all the operations names available in the region by applying filter. + for (Operation operation : + client.getOperationsClient().listOperations(listrequest).iterateAll()) { + System.out.println("Operation details:"); + System.out.format("\tName: %s\n", operation.getName()); + System.out.println("\tMetadata:"); + System.out.format("\t\tType Url: %s\n", operation.getMetadata().getTypeUrl()); + System.out.format( + "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tDone: %s\n", operation.getDone()); + if (operation.hasResponse()) { + System.out.println("\tResponse:"); + System.out.format("\t\tType Url: %s\n", operation.getResponse().getTypeUrl()); + System.out.format( + "\t\tValue: %s\n\n", + operation.getResponse().getValue().toStringUtf8().replace("\n", "")); + } + if (operation.hasError()) { + System.out.println("\tResponse:"); + System.out.format("\t\tError code: %s\n", operation.getError().getCode()); + System.out.format("\t\tError message: %s\n\n", operation.getError().getMessage()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_list_operation_status] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java new file mode 100644 index 00000000000..6bab2d159c8 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java @@ -0,0 +1,63 @@ +/* + * 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_predict] +import com.google.cloud.automl.v1.ExamplePayload; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.PredictRequest; +import com.google.cloud.automl.v1.PredictResponse; +import com.google.cloud.automl.v1.PredictionServiceClient; +import com.google.cloud.automl.v1.TextSnippet; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +class Prediction { + + // Deploy a model for prediction + static void predict(String projectId, String modelId, String filePath) { + // String projectId = "YOUR_PROJECT_ID"; + // String modelId = "YOUR_MODEL_ID"; + // String filePath = "path_to_local_file.txt"; + + // 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 (PredictionServiceClient client = PredictionServiceClient.create()) { + // Get the full path of the model. + ModelName name = ModelName.of(projectId, "us-central1", modelId); + + String content = new String(Files.readAllBytes(Paths.get(filePath))); + + TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + PredictRequest predictRequest = PredictRequest.newBuilder() + .setName(name.toString()) + .setPayload(payload) + .build(); + + PredictResponse response = client.predict(predictRequest); + TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); + System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_predict] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java new file mode 100644 index 00000000000..18d1530e878 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java @@ -0,0 +1,56 @@ +/* + * 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_undeploy_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.UndeployModelRequest; +import com.google.protobuf.Empty; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class UndeployModel { + + // Undeploy a model from prediction + static void undeployModel(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); + UndeployModelRequest undeployModelRequest = + UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + OperationFuture future = client.undeployModelAsync(undeployModelRequest); + + // Display the undeployment details of model. + future.get() + System.out.println("Model undeploy finished"); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_undeploy_model] diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java new file mode 100644 index 00000000000..77b83fb5041 --- /dev/null +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java @@ -0,0 +1,126 @@ +/* + * Copyright 2019 Google 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; + +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Automl translation datasets. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DatasetIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET = PROJECT_ID + "-vcm"; + private ByteArrayOutputStream bout; + private PrintStream out; + private String datasetId; + private String getdatasetId = "TRL3946265060617537378"; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + CreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + datasetId = + bout.toString() + .split("\n")[0] + .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; + assertThat(got).contains("Dataset id:"); + + // Act + ImportDataset.importDataset(PROJECT_ID, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/output"); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + + Page blobs = storage.list(BUCKET + "/output", Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(BUCKET + "/output/")); + for (Blob blob : blobs.iterateAll()) { +// blob.delete(); + } + } +} \ No newline at end of file diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java new file mode 100644 index 00000000000..fea62e167bc --- /dev/null +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java @@ -0,0 +1,134 @@ +/* + * Copyright 2019 Google 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Automl translation models. */ +@RunWith(JUnit4.class) +public class ModelIT { + // TODO(developer): Change PROJECT_ID, COMPUTE_REGION, DATASET_ID, DEPLOY_MODEL_ID and + // UNDEPLOY_MODEL_ID before running the test cases. + //e PrintStream out;private static final String PROJECT_ID = "java-docs-samples-testing"; + //// private static final String DATASET_ID = "IOD7155850371984785408"; + //// private static final String MODEL_NAME = "test_vision_model"; + //// private static final String DEPLOY_MODEL_ID = "IOD1728502647608049664"; + //// private static final String UNDEPLOY_MODEL_ID = "IOD3348109663601164288"; + //// private ByteArrayOutputStream bout; + //// private PrintStream out; + private static final String PROJECT_ID = "java-docs-samples-testing"; + private ByteArrayOutputStream bout; + private PrintStream out; + private ModelApi app; + private String modelId; + private String modelEvaluationId; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testModelApi() { + // Act + ListModels.listModels(PROJECT_ID); + + // Assert + String got = bout.toString(); + modelId = got.split("\n")[1].split("/")[got.split("\n")[1].split("/").length - 1]; + assertThat(got).contains("Model id:"); + + // Act + GetModel.getModel(PROJECT_ID, modelId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Model name:"); + + // Act + ListModelEvaluations.listModelEvaluations(PROJECT_ID, modelId); + + // Assert + got = bout.toString(); + modelEvaluationId = got.split("List of model evaluations:")[1].split("\"")[1].split("/")[7]; + assertThat(got).contains("name:"); + + // Act + GetModelEvaluation.getModelEvaluation(PROJECT_ID, modelId, modelEvaluationId); + + // Assert + got = bout.toString(); + assertThat(got).contains("name:"); + } + + @Test + public void testDeployModel() { + // Act + DeployModel.deployModel(PROJECT_ID, DEPLOY_MODEL_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Name:"); + } + + @Test + public void testUndeployModel() { + // Act + UndeployModel.undeployModel(PROJECT_ID, UNDEPLOY_MODEL_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Name:"); + } + + @Test + public void testOperationStatus() { + // Act + ListOperationStatus.listOperationStatus(PROJECT_ID); + + // Assert + String got = bout.toString(); + String operationId = got.split("\n")[1].split(":")[1].trim(); + assertThat(got).contains("Operation details:"); + + // Act + bout.reset(); + GetOperationStatus.getOperationStatus(operationId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Operation details:"); + } +} \ No newline at end of file diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java index 8f839493ce3..f60f61f3084 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java @@ -37,7 +37,6 @@ public class PredictionApiIT { private static final String filePath = "./resources/input.txt"; private ByteArrayOutputStream bout; private PrintStream out; - private PredictionApi app; @Before public void setUp() { @@ -60,4 +59,14 @@ public void testPredict() throws Exception { String got = bout.toString(); assertThat(got).contains("Translated Content"); } + + @Test + public void testPrediction() { + // Act + Prediction.predict(PROJECT_ID, modelId, filePath); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Translated Content"); + } } From da7863655bef214628e9bebf277e1a85876f0b06 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 10 Oct 2019 12:10:21 -0600 Subject: [PATCH 2/8] Update samples and tests --- translate/automl/pom.xml | 2 +- .../cloud/translate/automl/CreateDataset.java | 31 +++---- .../cloud/translate/automl/CreateModel.java | 16 ++-- .../cloud/translate/automl/DeleteDataset.java | 2 +- .../cloud/translate/automl/DeployModel.java | 55 ------------- .../cloud/translate/automl/ExportDataset.java | 9 ++- .../cloud/translate/automl/GetDataset.java | 12 +-- .../cloud/translate/automl/GetModel.java | 2 +- .../translate/automl/GetModelEvaluation.java | 8 +- .../translate/automl/GetOperationStatus.java | 7 +- .../cloud/translate/automl/ImportDataset.java | 6 +- .../cloud/translate/automl/ListDatasets.java | 12 +-- .../automl/ListModelEvaluations.java | 6 +- .../cloud/translate/automl/ListModels.java | 11 ++- .../translate/automl/ListOperationStatus.java | 4 +- .../cloud/translate/automl/Prediction.java | 11 ++- .../cloud/translate/automl/UndeployModel.java | 56 ------------- .../cloud/translate/automl/DatasetIT.java | 39 ++++++--- .../cloud/translate/automl/ModelIT.java | 81 ++++++++----------- .../translate/automl/PredictionApiIT.java | 2 +- 20 files changed, 135 insertions(+), 237 deletions(-) delete mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java delete mode 100644 translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java diff --git a/translate/automl/pom.xml b/translate/automl/pom.xml index c46d22991fd..9f4b449085a 100644 --- a/translate/automl/pom.xml +++ b/translate/automl/pom.xml @@ -40,7 +40,7 @@ com.google.cloud google-cloud-automl - 0.113.0-beta + 0.114.0-beta diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java index f39dbee6e7b..bc28c339dd3 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java @@ -43,32 +43,33 @@ static void createDataset(String projectId, String displayName) { // Specify the source and target language. TranslationDatasetMetadata translationDatasetMetadata = - TranslationDatasetMetadata.newBuilder() - .setSourceLanguageCode("en") - .setTargetLanguageCode("ja") - .build(); + TranslationDatasetMetadata.newBuilder() + .setSourceLanguageCode("en") + .setTargetLanguageCode("ja") + .build(); Dataset dataset = - Dataset.newBuilder() - .setDisplayName(displayName) - .setTranslationDatasetMetadata(translationDatasetMetadata) - .build(); - OperationFuture future = client.createDataset(projectLocation, dataset); + Dataset.newBuilder() + .setDisplayName(displayName) + .setTranslationDatasetMetadata(translationDatasetMetadata) + .build(); + OperationFuture future = + client.createDatasetAsync(projectLocation, dataset); Dataset createdDataset = future.get(); // Display the dataset information. System.out.format("Dataset name: %s\n", createdDataset.getName()); System.out.format( - "Dataset id: %s\n", - createdDataset.getName().split("/")[createdDataset.getName().split("/").length - 1]); + "Dataset id: %s\n", + createdDataset.getName().split("/")[createdDataset.getName().split("/").length - 1]); 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()); + "\tSource language code: %s\n", + createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode()); System.out.format( - "\tTarget language code: %s\n", - createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + "\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()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java index d8b959f8661..4c5aa966713 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java @@ -22,6 +22,7 @@ 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; @@ -40,14 +41,19 @@ static void createModel(String projectId, String datasetId, String displayName) 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) - .build(); + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTranslationModelMetadata(translationModelMetadata) + .build(); // Create a model with the model metadata in the region. - OperationFuture future = client.createModelAsync(projectLocation, model); + OperationFuture 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) { diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java index 34bea337bfb..304961f87bc 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java @@ -39,7 +39,7 @@ static void deleteDataset(String projectId, String datasetId) { 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) { + } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); } } diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java deleted file mode 100644 index 54649a63c92..00000000000 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeployModel.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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_deploy_model] -import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.automl.v1.AutoMlClient; -import com.google.cloud.automl.v1.ModelName; -import com.google.cloud.automl.v1.OperationMetadata; -import com.google.cloud.automl.v1.DeployModelRequest; -import com.google.protobuf.Empty; - -import java.io.IOException; -import java.util.concurrent.ExecutionException; - -class DeployModel { - - // Deploy a model for prediction - static void deployModel(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); - DeployModelRequest request = DeployModelRequest.newBuilder() - .setName(modelFullId.toString()) - .build(); - OperationFuture future = client.deployModelAsync(request); - - future.get(); - System.out.println("Model deployment finished"); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } -} -// [END automl_translate_deploy_model] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java index 72fb0b363c0..5510e68c66a 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java @@ -40,14 +40,17 @@ static void exportDataset(String projectId, String datasetId, String gcsUri) { 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(); + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build(); // Export the dataset to the output URI. - OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + 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) { + } catch (IOException | InterruptedException | ExecutionException e) { e.printStackTrace(); } } diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java index 9cfbb49d739..206cf79bd20 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java @@ -41,16 +41,16 @@ static void getDataset(String projectId, String datasetId) { // Display the dataset information System.out.format("Dataset name: %s\n", dataset.getName()); System.out.format( - "Dataset id: %s\n", - dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + "Dataset id: %s\n", + dataset.getName().split("/")[dataset.getName().split("/").length - 1]); 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()); + "\tSource language code: %s\n", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode()); System.out.format( - "\tTarget language code: %s\n", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + "\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()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java index dd52f5995f2..af0e164bcba 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java @@ -41,7 +41,7 @@ static void getModel(String projectId, String modelId) { // Display the model information. System.out.format("Model name: %s\n", model.getName()); System.out.format( - "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); + "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); System.out.format("Model display name: %s\n", model.getDisplayName()); System.out.println("Model create time:"); System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java index 859cf0e79e8..d17b9b1b53e 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java @@ -37,7 +37,7 @@ static void getModelEvaluation(String projectId, String modelId, String modelEva try (AutoMlClient client = AutoMlClient.create()) { // Get the full path of the model evaluation. ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, "us-central1", modelId, modelEvaluationId); + ModelEvaluationName.of(projectId, "us-central1", modelId, modelEvaluationId); // Get complete detail of the model evaluation. ModelEvaluation modelEvaluation = client.getModelEvaluation(modelEvaluationFullId); @@ -47,8 +47,10 @@ static void getModelEvaluation(String projectId, String modelId, String modelEva System.out.println("Create Time:"); System.out.format("\tseconds: %s\n", modelEvaluation.getCreateTime().getSeconds()); System.out.format("\tnanos: %s", modelEvaluation.getCreateTime().getNanos() / 1e9); - System.out.format("Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount()); - System.out.format("Model Evaluation Metrics: %s\n", modelEvaluation.getTranslationEvaluationMetrics()); + System.out.format( + "Evalution Example Count: %d\n", modelEvaluation.getEvaluatedExampleCount()); + System.out.format( + "Model Evaluation Metrics: %s\n", modelEvaluation.getTranslationEvaluationMetrics()); } catch (IOException e) { e.printStackTrace(); } diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java index 6b22560585b..8c0be18177d 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java @@ -26,7 +26,8 @@ class GetOperationStatus { // Get the status of an operation static void getOperationStatus(String operationFullId) { - // String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]"; + // String operationFullId = + // "projects/[projectId]/locations/us-central1/operations/[operationId]"; // 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 @@ -41,13 +42,13 @@ static void getOperationStatus(String operationFullId) { System.out.println("\tMetadata:"); System.out.format("\t\tType Url: %s\n", operation.getMetadata().getTypeUrl()); System.out.format( - "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); + "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); System.out.format("\tDone: %s\n", operation.getDone()); if (operation.hasResponse()) { System.out.println("\tResponse:"); System.out.format("\t\tType Url: %s\n", operation.getResponse().getTypeUrl()); System.out.format( - "\t\tValue: %s\n", operation.getResponse().getValue().toStringUtf8().replace("\n", "")); + "\t\tValue: %s\n", operation.getResponse().getValue().toStringUtf8().replace("\n", "")); } if (operation.hasError()) { System.out.println("\tResponse:"); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java index 1eba27125f1..0825467b7f2 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java @@ -43,10 +43,8 @@ static void importDataset(String projectId, String datasetId, String path) { DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); // Get multiple Google Cloud Storage URIs to import data from - GcsSource gcsSource = GcsSource.newBuilder() - .addAllInputUris( - Arrays.asList(path.split(","))) - .build(); + GcsSource gcsSource = + GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build(); // Import data from the input URI InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java index 173f75a56ab..df641942448 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java @@ -48,16 +48,16 @@ static void listDatasets(String projectId) { // Display the dataset information System.out.format("\nDataset name: %s\n", dataset.getName()); System.out.format( - "Dataset id: %s\n", - dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + "Dataset id: %s\n", + dataset.getName().split("/")[dataset.getName().split("/").length - 1]); 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()); + "\tSource language code: %s\n", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode()); System.out.format( - "\tTarget language code: %s\n", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + "\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()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java index a4d32c4942b..51f40d80203 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java @@ -38,14 +38,12 @@ static void listModelEvaluations(String projectId, String modelId) { // Get the full path of the model. ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .build(); + ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build(); // List all the model evaluations in the model by applying filter. System.out.println("List of model evaluations:"); for (ModelEvaluation modelEvaluation : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { System.out.format("Model Evaluation Name: %s\n", modelEvaluation.getName()); System.out.format("Model Annotation Spec Id: %s", modelEvaluation.getAnnotationSpecId()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java index ee937bc03a8..65c0e3f2a61 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java @@ -39,10 +39,10 @@ static void listModels(String projectId) { // Create list models request. ListModelsRequest listModlesRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter("") - .build(); + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("") + .build(); // List all the models available in the region by applying filter. System.out.println("List of models:"); @@ -50,8 +50,7 @@ static void listModels(String projectId) { // Display the model information. System.out.format("Model name: %s\n", model.getName()); System.out.format( - "Model id: %s\n", - model.getName().split("/")[model.getName().split("/").length - 1]); + "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); System.out.format("Model display name: %s\n", model.getDisplayName()); System.out.println("Model create time:"); System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java index 262a689bed2..2ee9cec41fe 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -39,9 +39,7 @@ static void listOperationStatus(String projectId) { // Create list operations request. ListOperationsRequest listrequest = - ListOperationsRequest.newBuilder() - .setName(projectLocation.toString()) - .build(); + ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build(); // List all the operations names available in the region by applying filter. for (Operation operation : diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java index 6bab2d159c8..66870c6c68b 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java @@ -30,7 +30,7 @@ class Prediction { - // Deploy a model for prediction + // Deploy a model for predictio static void predict(String projectId, String modelId, String filePath) { // String projectId = "YOUR_PROJECT_ID"; // String modelId = "YOUR_MODEL_ID"; @@ -47,13 +47,12 @@ static void predict(String projectId, String modelId, String filePath) { TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); - PredictRequest predictRequest = PredictRequest.newBuilder() - .setName(name.toString()) - .setPayload(payload) - .build(); + PredictRequest predictRequest = + PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build(); PredictResponse response = client.predict(predictRequest); - TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); + TextSnippet translatedContent = + response.getPayload(0).getTranslation().getTranslatedContent(); System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); } catch (IOException e) { e.printStackTrace(); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java deleted file mode 100644 index 18d1530e878..00000000000 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/UndeployModel.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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_undeploy_model] -import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.automl.v1.AutoMlClient; -import com.google.cloud.automl.v1.ModelName; -import com.google.cloud.automl.v1.OperationMetadata; -import com.google.cloud.automl.v1.DeployModelRequest; -import com.google.cloud.automl.v1.UndeployModelRequest; -import com.google.protobuf.Empty; - -import java.io.IOException; -import java.util.concurrent.ExecutionException; - -class UndeployModel { - - // Undeploy a model from prediction - static void undeployModel(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); - UndeployModelRequest undeployModelRequest = - UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); - OperationFuture future = client.undeployModelAsync(undeployModelRequest); - - // Display the undeployment details of model. - future.get() - System.out.println("Model undeploy finished"); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); - } - } -} -// [END automl_translate_undeploy_model] diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java index 77b83fb5041..d25397f6081 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java @@ -38,7 +38,7 @@ public class DatasetIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); - private static final String BUCKET = PROJECT_ID + "-vcm"; + private static final String BUCKET = "gs://" + PROJECT_ID + "-vcm"; private ByteArrayOutputStream bout; private PrintStream out; private String datasetId; @@ -62,7 +62,7 @@ public void testCreateImportDeleteDataset() { // To prevent name collisions when running tests in multiple java versions at once. // AutoML doesn't allow "-", but accepts "_" String datasetName = - String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); // Act CreateDataset.createDataset(PROJECT_ID, datasetName); @@ -70,13 +70,13 @@ public void testCreateImportDeleteDataset() { // Assert String got = bout.toString(); datasetId = - bout.toString() - .split("\n")[0] - .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; + bout.toString() + .split("\n")[0] + .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; assertThat(got).contains("Dataset id:"); // Act - ImportDataset.importDataset(PROJECT_ID, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); + ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/en-ja-short.csv"); // Assert got = bout.toString(); @@ -113,14 +113,31 @@ public void testGetDataset() { @Test public void testExportDataset() { - ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/output"); + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); Storage storage = StorageOptions.getDefaultInstance().getService(); - Page blobs = storage.list(BUCKET + "/output", Storage.BlobListOption.currentDirectory(), - Storage.BlobListOption.prefix(BUCKET + "/output/")); + String got = bout.toString(); + + assertThat(got).contains("Dataset exported."); + + Page blobs = + storage.list( + PROJECT_ID + "-vcm", + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + for (Blob blob : blobs.iterateAll()) { -// blob.delete(); + Page fileBlobs = + storage.list( + PROJECT_ID + "-vcm", + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } } } -} \ No newline at end of file +} diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java index fea62e167bc..ab8f21846a3 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java @@ -21,8 +21,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import java.util.concurrent.ExecutionException; +import com.google.cloud.automl.v1.AutoMlClient; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -32,19 +32,11 @@ /** Tests for Automl translation models. */ @RunWith(JUnit4.class) public class ModelIT { - // TODO(developer): Change PROJECT_ID, COMPUTE_REGION, DATASET_ID, DEPLOY_MODEL_ID and - // UNDEPLOY_MODEL_ID before running the test cases. - //e PrintStream out;private static final String PROJECT_ID = "java-docs-samples-testing"; - //// private static final String DATASET_ID = "IOD7155850371984785408"; - //// private static final String MODEL_NAME = "test_vision_model"; - //// private static final String DEPLOY_MODEL_ID = "IOD1728502647608049664"; - //// private static final String UNDEPLOY_MODEL_ID = "IOD3348109663601164288"; - //// private ByteArrayOutputStream bout; - //// private PrintStream out; - private static final String PROJECT_ID = "java-docs-samples-testing"; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "TRL3946265060617537378"; + private static final String MODEL_NAME = "translation_test_create_model"; private ByteArrayOutputStream bout; private PrintStream out; - private ModelApi app; private String modelId; private String modelEvaluationId; @@ -62,55 +54,36 @@ public void tearDown() { @Test public void testModelApi() { - // Act + // LIST MODELS ListModels.listModels(PROJECT_ID); - - // Assert String got = bout.toString(); modelId = got.split("\n")[1].split("/")[got.split("\n")[1].split("/").length - 1]; assertThat(got).contains("Model id:"); - // Act + // GET MODEL + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); GetModel.getModel(PROJECT_ID, modelId); - - // Assert got = bout.toString(); - assertThat(got).contains("Model name:"); + assertThat(got).contains("Model id: " + modelId); - // Act + // LIST MODEL EVALUATIONS + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); ListModelEvaluations.listModelEvaluations(PROJECT_ID, modelId); - - // Assert got = bout.toString(); - modelEvaluationId = got.split("List of model evaluations:")[1].split("\"")[1].split("/")[7]; - assertThat(got).contains("name:"); + modelEvaluationId = got.split(modelId + "/modelEvaluations/")[1].split("\n")[0]; + assertThat(got).contains("Model Evaluation Name:"); // Act + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); GetModelEvaluation.getModelEvaluation(PROJECT_ID, modelId, modelEvaluationId); - - // Assert got = bout.toString(); - assertThat(got).contains("name:"); - } - - @Test - public void testDeployModel() { - // Act - DeployModel.deployModel(PROJECT_ID, DEPLOY_MODEL_ID); - - // Assert - String got = bout.toString(); - assertThat(got).contains("Name:"); - } - - @Test - public void testUndeployModel() { - // Act - UndeployModel.undeployModel(PROJECT_ID, UNDEPLOY_MODEL_ID); - - // Assert - String got = bout.toString(); - assertThat(got).contains("Name:"); + assertThat(got).contains("Model Evaluation Name:"); } @Test @@ -131,4 +104,18 @@ public void testOperationStatus() { got = bout.toString(); assertThat(got).contains("Operation details:"); } + + @Test + public void testCreateModel() throws IOException { + CreateModel.createModel(PROJECT_ID, DATASET_ID, MODEL_NAME); + + String got = bout.toString(); + assertThat(got).contains("Training started"); + + String operationId = got.split("Training operation name: ")[1].split("\n")[0]; + + try (AutoMlClient client = AutoMlClient.create()) { + client.getOperationsClient().cancelOperation(operationId); + } + } } \ No newline at end of file diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java index f60f61f3084..c07839041e4 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java @@ -33,7 +33,7 @@ public class PredictionApiIT { private static final String COMPUTE_REGION = "us-central1"; private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String modelId = "2188848820815848149"; + private static final String modelId = "TRL2188848820815848149"; private static final String filePath = "./resources/input.txt"; private ByteArrayOutputStream bout; private PrintStream out; From a30231c6a532f8871d5352775e143b4b01a2b5e7 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 10 Oct 2019 12:58:29 -0600 Subject: [PATCH 3/8] Update ExportDataset.java --- .../java/com/google/cloud/translate/automl/ExportDataset.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java index 5510e68c66a..121bc6c4bf8 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java @@ -32,7 +32,7 @@ class ExportDataset { static void exportDataset(String projectId, String datasetId, String gcsUri) { // String projectId = "YOUR_PROJECT_ID"; // String datasetId = "YOUR_DATASET_ID"; - // String gcsUri = "gs://"; + // 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 From 0d6bd4ea0b782f7a5de7cd96e88d34833f9bbdae Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 10 Oct 2019 14:36:36 -0600 Subject: [PATCH 4/8] Update Prediction.java --- .../main/java/com/google/cloud/translate/automl/Prediction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java index 66870c6c68b..0b5e14bbc81 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java @@ -30,7 +30,7 @@ class Prediction { - // Deploy a model for predictio + // Predict static void predict(String projectId, String modelId, String filePath) { // String projectId = "YOUR_PROJECT_ID"; // String modelId = "YOUR_MODEL_ID"; From 3609abf8a3233e922cfa5d515a6f89297122326b Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 16 Oct 2019 09:58:38 -0600 Subject: [PATCH 5/8] Update based on feedback --- .../cloud/translate/automl/CreateDataset.java | 9 ++++++--- .../cloud/translate/automl/GetDataset.java | 9 ++++++--- .../google/cloud/translate/automl/GetModel.java | 8 ++++++-- .../translate/automl/GetOperationStatus.java | 10 ++-------- .../cloud/translate/automl/ListDatasets.java | 9 ++++++--- .../cloud/translate/automl/ListModels.java | 8 ++++++-- .../translate/automl/ListOperationStatus.java | 16 +++++++--------- .../google/cloud/translate/automl/DatasetIT.java | 5 +---- .../google/cloud/translate/automl/ModelIT.java | 2 +- 9 files changed, 41 insertions(+), 35 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java index bc28c339dd3..feb9257f143 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java @@ -59,9 +59,12 @@ static void createDataset(String projectId, String displayName) { // Display the dataset information. System.out.format("Dataset name: %s\n", createdDataset.getName()); - System.out.format( - "Dataset id: %s\n", - createdDataset.getName().split("/")[createdDataset.getName().split("/").length - 1]); + // 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( diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java index 206cf79bd20..2fbe9f5bbfd 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java @@ -40,9 +40,12 @@ static void getDataset(String projectId, String datasetId) { // Display the dataset information System.out.format("Dataset name: %s\n", dataset.getName()); - System.out.format( - "Dataset id: %s\n", - dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + // 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( diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java index af0e164bcba..1ff21250031 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java @@ -40,8 +40,12 @@ static void getModel(String projectId, String modelId) { // Display the model information. System.out.format("Model name: %s\n", model.getName()); - System.out.format( - "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); + // To get the model id, you have to parse it out of the `name` field. As models Ids are + // required for other methods. + // Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}` + String[] names = model.getName().split("/"); + String retrievedModelId = names[names.length - 1]; + System.out.format("Model id: %s\n", retrievedModelId); System.out.format("Model display name: %s\n", model.getDisplayName()); System.out.println("Model create time:"); System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java index 8c0be18177d..4592f00d7b8 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java @@ -39,16 +39,10 @@ static void getOperationStatus(String operationFullId) { // Display operation details. System.out.println("Operation details:"); System.out.format("\tName: %s\n", operation.getName()); - System.out.println("\tMetadata:"); - System.out.format("\t\tType Url: %s\n", operation.getMetadata().getTypeUrl()); - System.out.format( - "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); System.out.format("\tDone: %s\n", operation.getDone()); if (operation.hasResponse()) { - System.out.println("\tResponse:"); - System.out.format("\t\tType Url: %s\n", operation.getResponse().getTypeUrl()); - System.out.format( - "\t\tValue: %s\n", operation.getResponse().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); } if (operation.hasError()) { System.out.println("\tResponse:"); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java index df641942448..68f438e1f05 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java @@ -47,9 +47,12 @@ static void listDatasets(String projectId) { for (Dataset dataset : client.listDatasets(request).iterateAll()) { // Display the dataset information System.out.format("\nDataset name: %s\n", dataset.getName()); - System.out.format( - "Dataset id: %s\n", - dataset.getName().split("/")[dataset.getName().split("/").length - 1]); + // 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( diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java index 65c0e3f2a61..f5536ccd2f2 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java @@ -49,8 +49,12 @@ static void listModels(String projectId) { for (Model model : client.listModels(listModlesRequest).iterateAll()) { // Display the model information. System.out.format("Model name: %s\n", model.getName()); - System.out.format( - "Model id: %s\n", model.getName().split("/")[model.getName().split("/").length - 1]); + // To get the model id, you have to parse it out of the `name` field. As models Ids are + // required for other methods. + // Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}` + String[] names = model.getName().split("/"); + String retrievedModelId = names[names.length - 1]; + System.out.format("Model id: %s\n", retrievedModelId); System.out.format("Model display name: %s\n", model.getDisplayName()); System.out.println("Model create time:"); System.out.format("\tseconds: %s\n", model.getCreateTime().getSeconds()); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java index 2ee9cec41fe..8664778f8c8 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -46,17 +46,10 @@ static void listOperationStatus(String projectId) { client.getOperationsClient().listOperations(listrequest).iterateAll()) { System.out.println("Operation details:"); System.out.format("\tName: %s\n", operation.getName()); - System.out.println("\tMetadata:"); - System.out.format("\t\tType Url: %s\n", operation.getMetadata().getTypeUrl()); - System.out.format( - "\t\tValue: %s\n", operation.getMetadata().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); System.out.format("\tDone: %s\n", operation.getDone()); if (operation.hasResponse()) { - System.out.println("\tResponse:"); - System.out.format("\t\tType Url: %s\n", operation.getResponse().getTypeUrl()); - System.out.format( - "\t\tValue: %s\n\n", - operation.getResponse().getValue().toStringUtf8().replace("\n", "")); + System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); } if (operation.hasError()) { System.out.println("\tResponse:"); @@ -68,5 +61,10 @@ static void listOperationStatus(String projectId) { e.printStackTrace(); } } + + + public static void main(String[] args) { + listOperationStatus("java-docs-samples-testing"); + } } // [END automl_translate_list_operation_status] diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java index d25397f6081..f81552452de 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java @@ -69,10 +69,7 @@ public void testCreateImportDeleteDataset() { // Assert String got = bout.toString(); - datasetId = - bout.toString() - .split("\n")[0] - .split("/")[(bout.toString().split("\n")[0]).split("/").length - 1]; + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; assertThat(got).contains("Dataset id:"); // Act diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java index ab8f21846a3..27c17348820 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java @@ -57,7 +57,7 @@ public void testModelApi() { // LIST MODELS ListModels.listModels(PROJECT_ID); String got = bout.toString(); - modelId = got.split("\n")[1].split("/")[got.split("\n")[1].split("/").length - 1]; + modelId = got.split("Model id: ")[1].split("\n")[0]; assertThat(got).contains("Model id:"); // GET MODEL From e0c5971e5941713a523c4a3179c5bbbb7529d8ae Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Wed, 16 Oct 2019 09:59:51 -0600 Subject: [PATCH 6/8] Update ListOperationStatus.java --- .../google/cloud/translate/automl/ListOperationStatus.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java index 8664778f8c8..cb2074d78cf 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -61,10 +61,5 @@ static void listOperationStatus(String projectId) { e.printStackTrace(); } } - - - public static void main(String[] args) { - listOperationStatus("java-docs-samples-testing"); - } } // [END automl_translate_list_operation_status] From 15f8778a6da8a9ecd984038976c29656bdd466b5 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 16 Oct 2019 15:14:12 -0600 Subject: [PATCH 7/8] Update ID of test dataset --- .../java/com/google/cloud/translate/automl/DatasetApiIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 2f47e55968a..f77de7369a7 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -40,7 +40,7 @@ public class DatasetApiIT { private PrintStream out; private DatasetApi app; private String datasetId; - private String getdatasetId = "3946265060617537378"; + private String getdatasetId = "TRL3946265060617537378"; @Before public void setUp() { From 142fc96d235f5aab20cba335c718813ab8ebd858 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Wed, 16 Oct 2019 15:56:51 -0600 Subject: [PATCH 8/8] Lint: Update License header and import order --- .../com/google/cloud/translate/automl/CreateDataset.java | 2 +- .../com/google/cloud/translate/automl/CreateModel.java | 2 +- .../com/google/cloud/translate/automl/DeleteDataset.java | 2 +- .../com/google/cloud/translate/automl/DeleteModel.java | 2 +- .../com/google/cloud/translate/automl/ExportDataset.java | 2 +- .../com/google/cloud/translate/automl/GetDataset.java | 2 +- .../java/com/google/cloud/translate/automl/GetModel.java | 2 +- .../cloud/translate/automl/GetModelEvaluation.java | 2 +- .../cloud/translate/automl/GetOperationStatus.java | 2 +- .../com/google/cloud/translate/automl/ImportDataset.java | 2 +- .../com/google/cloud/translate/automl/ListDatasets.java | 2 +- .../cloud/translate/automl/ListModelEvaluations.java | 2 +- .../com/google/cloud/translate/automl/ListModels.java | 2 +- .../cloud/translate/automl/ListOperationStatus.java | 2 +- .../com/google/cloud/translate/automl/Prediction.java | 2 +- .../com/google/cloud/translate/automl/DatasetIT.java | 9 +++++---- .../java/com/google/cloud/translate/automl/ModelIT.java | 3 ++- 17 files changed, 22 insertions(+), 20 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java index feb9257f143..4b24c85d51e 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java index 4c5aa966713..b317d0dd801 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java index 304961f87bc..e284e9ef4ff 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java index 0790f81ac05..bddfe867fc0 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java index 121bc6c4bf8..9f7ab5b3095 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java index 2fbe9f5bbfd..8c362494b92 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java index 1ff21250031..b604a379f26 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java index d17b9b1b53e..6713d51f212 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java index 4592f00d7b8..92970c24e4c 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java index 0825467b7f2..e98b5aa38ae 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java index 68f438e1f05..651c0ec0b99 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java index 51f40d80203..7cf38b1318b 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java index f5536ccd2f2..ef18a11eec8 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java index cb2074d78cf..4a455d47e40 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java index 0b5e14bbc81..0b9e3e77ee7 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java @@ -1,5 +1,5 @@ /* - * Copyright 2019 LLC + * Copyright 2019 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java index f81552452de..2793fe81343 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java @@ -18,14 +18,15 @@ import static com.google.common.truth.Truth.assertThat; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.UUID; - import com.google.api.gax.paging.Page; import com.google.cloud.storage.Blob; import com.google.cloud.storage.Storage; import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.UUID; + import org.junit.After; import org.junit.Before; import org.junit.Test; diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java index 27c17348820..fde5d9ab529 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java @@ -18,11 +18,12 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.cloud.automl.v1.AutoMlClient; + import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; -import com.google.cloud.automl.v1.AutoMlClient; import org.junit.After; import org.junit.Before; import org.junit.Test;