From d2e9b4ee1e9aecd78515ba8498681a44767fdbc9 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Thu, 21 Nov 2019 14:37:23 -0700 Subject: [PATCH] samples: Translate automl test fixes (#1606) * Update auto ml tests * Update style format, slightly to see if that fixes tests * Update to latest library and fix some syntax to fix the tests * Address review comments --- .../cloud/translate/automl/DatasetApi.java | 247 +++++++++--------- .../cloud/translate/automl/ModelApi.java | 214 +++++++-------- .../cloud/translate/automl/PredictionApi.java | 32 +-- .../cloud/translate/automl/DatasetApiIT.java | 22 +- .../cloud/translate/automl/ModelApiIT.java | 2 - 5 files changed, 267 insertions(+), 250 deletions(-) diff --git a/automl/snippets/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/automl/snippets/src/main/java/com/google/cloud/translate/automl/DatasetApi.java index af569360b8f..8616e0c1bae 100644 --- a/automl/snippets/src/main/java/com/google/cloud/translate/automl/DatasetApi.java +++ b/automl/snippets/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -21,7 +21,6 @@ import com.google.cloud.automl.v1beta1.Dataset; import com.google.cloud.automl.v1beta1.DatasetName; import com.google.cloud.automl.v1beta1.GcsSource; -import com.google.cloud.automl.v1beta1.GcsSource.Builder; import com.google.cloud.automl.v1beta1.InputConfig; import com.google.cloud.automl.v1beta1.ListDatasetsRequest; import com.google.cloud.automl.v1beta1.LocationName; @@ -30,6 +29,7 @@ import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.ExecutionException; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -54,53 +54,53 @@ public class DatasetApi { * @param datasetName the name of the dataset to be created. * @param source the Source language * @param target the Target language - * @throws IOException on Input/Output errors. */ public static void createDataset( String projectId, String computeRegion, String datasetName, String source, String target) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Specify the source and target language. - TranslationDatasetMetadata translationDatasetMetadata = - TranslationDatasetMetadata.newBuilder() - .setSourceLanguageCode(source) - .setTargetLanguageCode(target) - .build(); - - // Set dataset name and dataset metadata. - Dataset myDataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setTranslationDatasetMetadata(translationDatasetMetadata) - .build(); - - // Create a dataset with the dataset metadata in the region. - Dataset dataset = client.createDataset(projectLocation, myDataset); - - // Display the dataset information. - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Translation dataset Metadata:"); - System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); - System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Specify the source and target language. + TranslationDatasetMetadata translationDatasetMetadata = + TranslationDatasetMetadata.newBuilder() + .setSourceLanguageCode(source) + .setTargetLanguageCode(target) + .build(); + + // Set dataset name and dataset metadata. + Dataset myDataset = + Dataset.newBuilder() + .setDisplayName(datasetName) + .setTranslationDatasetMetadata(translationDatasetMetadata) + .build(); + + // Create a dataset with the dataset metadata in the region. + Dataset dataset = client.createDataset(projectLocation, myDataset); + + // Display the dataset information. + System.out.println(String.format("Dataset name: %s", dataset.getName())); + System.out.println( + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); + System.out.println("Translation dataset Metadata:"); + System.out.println( + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + System.out.println( + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + System.out.println("Dataset create time:"); + System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } } // [END automl_translate_create_dataset] @@ -111,27 +111,69 @@ public static void createDataset( * @param projectId the Google Cloud Project ID. * @param computeRegion the Region name. (e.g., "us-central1"). * @param filter the Filter expression. - * @throws Exception on AutoML Client errors */ public static void listDatasets(String projectId, String computeRegion, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .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.println(String.format("\nDataset name: %s", dataset.getName())); + System.out.println( + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); + System.out.println("Translation dataset metadata:"); + System.out.println( + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + System.out.println( + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + System.out.println("Dataset create time:"); + System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } + } + } + // [END automl_translate_list_datasets] + + // [START automl_translate_get_dataset] + /** + * Demonstrates using the AutoML client to get a dataset by ID. + * + * @param projectId the Google Cloud Project ID. + * @param computeRegion the Region name. (e.g., "us-central1"). + * @param datasetId the Id of the dataset. + */ + public static void getDataset(String projectId, String computeRegion, String datasetId) + throws IOException { + // Instantiates a client + try (AutoMlClient client = AutoMlClient.create()) { - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - ListDatasetsRequest request = - ListDatasetsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); + // Get all the information about a given dataset. + Dataset dataset = client.getDataset(datasetFullId); - // 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.println(String.format("\nDataset name: %s", dataset.getName())); + System.out.println(String.format("Dataset name: %s", dataset.getName())); System.out.println( String.format( "Dataset id: %s", @@ -151,48 +193,6 @@ public static void listDatasets(String projectId, String computeRegion, String f System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); } } - // [END automl_translate_list_datasets] - - // [START automl_translate_get_dataset] - /** - * Demonstrates using the AutoML client to get a dataset by ID. - * - * @param projectId the Google Cloud Project ID. - * @param computeRegion the Region name. (e.g., "us-central1"). - * @param datasetId the Id of the dataset. - * @throws Exception on AutoML Client errors - */ - public static void getDataset(String projectId, String computeRegion, String datasetId) - throws Exception { - // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Get all the information about a given dataset. - Dataset dataset = client.getDataset(datasetFullId); - - // Display the dataset information - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Translation dataset metadata:"); - System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); - System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } // [END automl_translate_get_dataset] // [START automl_translate_import_data] @@ -203,30 +203,31 @@ public static void getDataset(String projectId, String computeRegion, String dat * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. * @param path the remote Path of the training data csv file. - * @throws Exception on AutoML Client errors */ public static void importData( - String projectId, String computeRegion, String datasetId, String path) throws Exception { + String projectId, String computeRegion, String datasetId, String path) + throws IOException, InterruptedException, ExecutionException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - Builder gcsSource = GcsSource.newBuilder(); + GcsSource.Builder gcsSource = GcsSource.newBuilder(); - // Get multiple Google Cloud Storage URIs to import data from - String[] inputUris = path.split(","); - for (String inputUri : inputUris) { - gcsSource.addInputUris(inputUri); - } + // Get multiple Google Cloud Storage URIs to import data from + String[] inputUris = path.split(","); + for (String inputUri : inputUris) { + gcsSource.addInputUris(inputUri); + } - // Import data from the input URI - InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); - System.out.println("Processing import..."); + // 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.println(String.format("Dataset imported. %s", response)); + Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); + System.out.println(String.format("Dataset imported. %s", response)); + } } // [END automl_translate_import_data] @@ -237,20 +238,20 @@ public static void importData( * @param projectId the Google Cloud Project ID. * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. - * @throws Exception on AutoML Client errors */ public static void deleteDataset(String projectId, String computeRegion, String datasetId) - throws Exception { + throws IOException, InterruptedException, ExecutionException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - // Delete a dataset. - Empty response = client.deleteDatasetAsync(datasetFullId).get(); + // Delete a dataset. + Empty response = client.deleteDatasetAsync(datasetFullId).get(); - System.out.println(String.format("Dataset deleted. %s", response)); + System.out.println(String.format("Dataset deleted. %s", response)); + } } // [END automl_translate_delete_dataset] @@ -284,7 +285,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); - Namespace ns = null; + Namespace ns; try { ns = parser.parseArgs(args); if (ns.get("command").equals("create_dataset")) { diff --git a/automl/snippets/src/main/java/com/google/cloud/translate/automl/ModelApi.java b/automl/snippets/src/main/java/com/google/cloud/translate/automl/ModelApi.java index 1769d81bcc0..805aa0ab5f7 100644 --- a/automl/snippets/src/main/java/com/google/cloud/translate/automl/ModelApi.java +++ b/automl/snippets/src/main/java/com/google/cloud/translate/automl/ModelApi.java @@ -57,35 +57,37 @@ public class ModelApi { * @param computeRegion the Region name. * @param dataSetId the Id of the dataset to which model is created. * @param modelName the Name of the model. - * @throws Exception on AutoML Client errors */ public static void createModel( - String projectId, String computeRegion, String dataSetId, String modelName) throws Exception { + String projectId, String computeRegion, String dataSetId, String modelName) + throws IOException, InterruptedException, ExecutionException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Set model metadata. - TranslationModelMetadata translationModelMetadata = - TranslationModelMetadata.newBuilder().setBaseModel("").build(); - - // Set model name, dataset and metadata. - Model myModel = - Model.newBuilder() - .setDisplayName(modelName) - .setDatasetId(dataSetId) - .setTranslationModelMetadata(translationModelMetadata) - .build(); - - // Create a model with the model metadata in the region. - OperationFuture response = - client.createModelAsync(projectLocation, myModel); - - System.out.println( - String.format("Training operation name: %s", response.getInitialFuture().get().getName())); - System.out.println("Training started..."); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Set model metadata. + TranslationModelMetadata translationModelMetadata = + TranslationModelMetadata.newBuilder().setBaseModel("").build(); + + // Set model name, dataset and metadata. + Model myModel = + Model.newBuilder() + .setDisplayName(modelName) + .setDatasetId(dataSetId) + .setTranslationModelMetadata(translationModelMetadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture response = + client.createModelAsync(projectLocation, myModel); + + System.out.println( + String.format( + "Training operation name: %s", response.getInitialFuture().get().getName())); + System.out.println("Training started..."); + } } // [END automl_translate_create_model] @@ -101,31 +103,32 @@ public static void createModel( public static void listModels(String projectId, String computeRegion, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Create list models request. - ListModelsRequest listModlesRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .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.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Create list models request. + ListModelsRequest listModlesRequest = + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .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.println(String.format("Model name: %s", model.getName())); + System.out.println( + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + System.out.println(String.format("Model display name: %s", model.getDisplayName())); + System.out.println("Model create time:"); + System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); + System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + } } } // [END automl_translate_list_models] @@ -142,24 +145,25 @@ public static void listModels(String projectId, String computeRegion, String fil public static void getModel(String projectId, String computeRegion, String modelId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Get complete detail of the model. - Model model = client.getModel(modelFullId); - - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + + // Get complete detail of the model. + Model model = client.getModel(modelFullId); + + // Display the model information. + System.out.println(String.format("Model name: %s", model.getName())); + System.out.println( + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + System.out.println(String.format("Model display name: %s", model.getDisplayName())); + System.out.println("Model create time:"); + System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); + System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + } } // [END automl_translate_get_model] @@ -176,23 +180,24 @@ public static void getModel(String projectId, String computeRegion, String model public static void listModelEvaluations( String projectId, String computeRegion, String modelId, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Create list model evaluations request - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - // List all the model evaluations in the model by applying filter. - System.out.println("List of model evaluations:"); - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - System.out.println(element); + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + + // Create list model evaluations request + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder() + .setParent(modelFullId.toString()) + .setFilter(filter) + .build(); + + // List all the model evaluations in the model by applying filter. + System.out.println("List of model evaluations:"); + for (ModelEvaluation element : + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + System.out.println(element); + } } } // [END automl_translate_list_model_evaluations] @@ -211,16 +216,17 @@ public static void getModelEvaluation( String projectId, String computeRegion, String modelId, String modelEvaluationId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); + // Get the full path of the model evaluation. + ModelEvaluationName modelEvaluationFullId = + ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - // Get complete detail of the model evaluation. - ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); + // Get complete detail of the model evaluation. + ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); - System.out.println(response); + System.out.println(response); + } } // [END automl_translate_get_model_evaluation] @@ -236,15 +242,16 @@ public static void getModelEvaluation( public static void deleteModel(String projectId, String computeRegion, String modelId) throws InterruptedException, ExecutionException, IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - // Delete a model. - Empty response = client.deleteModelAsync(modelFullId).get(); + // Delete a model. + Empty response = client.deleteModelAsync(modelFullId).get(); - System.out.println("Model deletion started..."); + System.out.println("Model deletion started..."); + } } // [END automl_translate_delete_model] @@ -258,12 +265,13 @@ public static void deleteModel(String projectId, String computeRegion, String mo */ private static void getOperationStatus(String operationFullId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the latest state of a long-running operation. - Operation response = client.getOperationsClient().getOperation(operationFullId); + // Get the latest state of a long-running operation. + Operation response = client.getOperationsClient().getOperation(operationFullId); - System.out.println(String.format("Operation status: %s", response)); + System.out.println(String.format("Operation status: %s", response)); + } } // [END automl_translate_get_operation_status] diff --git a/automl/snippets/src/main/java/com/google/cloud/translate/automl/PredictionApi.java b/automl/snippets/src/main/java/com/google/cloud/translate/automl/PredictionApi.java index 647e17ff94a..98bbbc03b40 100644 --- a/automl/snippets/src/main/java/com/google/cloud/translate/automl/PredictionApi.java +++ b/automl/snippets/src/main/java/com/google/cloud/translate/automl/PredictionApi.java @@ -29,7 +29,6 @@ import com.google.cloud.automl.v1beta1.ModelName; import com.google.cloud.automl.v1beta1.PredictResponse; import com.google.cloud.automl.v1beta1.PredictionServiceClient; - import com.google.cloud.automl.v1beta1.TextSnippet; import java.io.IOException; import java.io.PrintStream; @@ -53,7 +52,6 @@ public class PredictionApi { // [START automl_translate_predict] - /** * Demonstrates using the AutoML client to predict an image. * @@ -67,26 +65,29 @@ public static void predict( String projectId, String computeRegion, String modelId, String filePath) throws IOException { // Instantiate client for prediction service. - PredictionServiceClient predictionClient = PredictionServiceClient.create(); + PredictResponse response; + try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) { - // Get the full path of the model. - ModelName name = ModelName.of(projectId, computeRegion, modelId); + // Get the full path of the model. + ModelName name = ModelName.of(projectId, computeRegion, modelId); - // Read the file content for translation. - String content = new String(Files.readAllBytes(Paths.get(filePath))); + // Read the file content for translation. + String content = new String(Files.readAllBytes(Paths.get(filePath))); - TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); + TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); - // Set the payload by giving the content of the file. - ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + // Set the payload by giving the content of the file. + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); - // Additional parameters that can be provided for prediction - Map params = new HashMap<>(); + // Additional parameters that can be provided for prediction + Map params = new HashMap<>(); - PredictResponse response = predictionClient.predict(name, payload, params); - TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); + response = predictionClient.predict(name, payload, params); + TextSnippet translatedContent = + response.getPayload(0).getTranslation().getTranslatedContent(); - System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + } } // [END automl_translate_predict] @@ -116,7 +117,6 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException ns = parser.parseArgs(args); if (ns.get("command").equals("predict")) { predict(projectId, computeRegion, ns.getString("modelId"), ns.getString("filePath")); - } } catch (ArgumentParserException e) { parser.handleError(e); diff --git a/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index f77de7369a7..2bb697faa02 100644 --- a/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/automl/snippets/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -19,7 +19,10 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; +import java.util.UUID; +import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Before; @@ -35,7 +38,6 @@ public class DatasetApiIT { private static final String PROJECT_ID = "java-docs-samples-testing"; private static final String BUCKET = PROJECT_ID + "-vcm"; private static final String COMPUTE_REGION = "us-central1"; - private static final String DATASET_NAME = "test_translate_dataset"; private ByteArrayOutputStream bout; private PrintStream out; private DatasetApi app; @@ -55,9 +57,16 @@ public void tearDown() { } @Test - public void testCreateImportDeleteDataset() throws Exception { + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // 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 - DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, DATASET_NAME, "en", "ja"); + DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, datasetName, "en", "ja"); // Assert String got = bout.toString(); @@ -68,7 +77,8 @@ public void testCreateImportDeleteDataset() throws Exception { assertThat(got).contains("Dataset id:"); // Act - DatasetApi.importData(PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja.csv"); + DatasetApi.importData( + PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); // Assert got = bout.toString(); @@ -83,7 +93,7 @@ public void testCreateImportDeleteDataset() throws Exception { } @Test - public void testListDataset() throws Exception { + public void testListDataset() throws IOException { // Act DatasetApi.listDatasets(PROJECT_ID, COMPUTE_REGION, "translation_dataset_metadata:*"); @@ -93,7 +103,7 @@ public void testListDataset() throws Exception { } @Test - public void testGetDataset() throws Exception { + public void testGetDataset() throws IOException { // Act DatasetApi.getDataset(PROJECT_ID, COMPUTE_REGION, getdatasetId); diff --git a/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java b/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java index 0ebfeed339b..fff8a1a1561 100644 --- a/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java +++ b/automl/snippets/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java @@ -83,7 +83,5 @@ public void testModelApi() throws Exception { // Assert got = bout.toString(); assertThat(got).contains("name:"); - } } -