diff --git a/translate/automl/pom.xml b/translate/automl/pom.xml index 221014744c8..9f4b449085a 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.114.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..4b24c85d51e --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateDataset.java @@ -0,0 +1,84 @@ +/* + * 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; + +// [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.createDatasetAsync(projectLocation, dataset); + + Dataset createdDataset = future.get(); + + // Display the dataset information. + System.out.format("Dataset name: %s\n", createdDataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = createdDataset.getName().split("/"); + String datasetId = names[names.length - 1]; + System.out.format("Dataset id: %s\n", datasetId); + System.out.format("Dataset display name: %s\n", createdDataset.getDisplayName()); + System.out.println("Translation dataset Metadata:"); + System.out.format( + "\tSource language code: %s\n", + createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode()); + System.out.format( + "\tTarget language code: %s\n", + createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + System.out.println("Dataset create time:"); + System.out.format("\tseconds: %s\n", createdDataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", createdDataset.getCreateTime().getNanos()); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_create_dataset] 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..b317d0dd801 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/CreateModel.java @@ -0,0 +1,64 @@ +/* + * 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; + +// [START automl_translate_create_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.LocationName; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.cloud.automl.v1.TranslationModelMetadata; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class CreateModel { + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) { + // String projectId = "YOUR_PROJECT_ID"; + // String datasetId = "YOUR_DATASET_ID"; + // String displayName = "YOUR_DATASET_NAME"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "us-central1"); + // Leave model unset to use the default base model provided by Google + TranslationModelMetadata translationModelMetadata = + TranslationModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTranslationModelMetadata(translationModelMetadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture 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..e284e9ef4ff --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteDataset.java @@ -0,0 +1,47 @@ +/* + * 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; + +// [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..bddfe867fc0 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DeleteModel.java @@ -0,0 +1,51 @@ +/* + * 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; + +// [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/ExportDataset.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java new file mode 100644 index 00000000000..9f7ab5b3095 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ExportDataset.java @@ -0,0 +1,58 @@ +/* + * 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; + +// [START automl_translate_export_dataset] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DatasetName; +import com.google.cloud.automl.v1.GcsDestination; +import com.google.cloud.automl.v1.OutputConfig; +import com.google.protobuf.Empty; + +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class ExportDataset { + + // Export a dataset + static void exportDataset(String projectId, String datasetId, String gcsUri) { + // String projectId = "YOUR_PROJECT_ID"; + // String datasetId = "YOUR_DATASET_ID"; + // String gcsUri = "gs://BUCKET_ID/path_to_export/"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build(); + + // Export the dataset to the output URI. + OutputConfig outputConfig = + OutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + + System.out.println("Processing export..."); + Empty response = client.exportDataAsync(datasetFullId, outputConfig).get(); + System.out.format("Dataset exported. %s\n", response); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_export_dataset] 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..8c362494b92 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetDataset.java @@ -0,0 +1,65 @@ +/* + * 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; + +// [START automl_translate_get_dataset] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.Dataset; +import com.google.cloud.automl.v1.DatasetName; + +import java.io.IOException; + +class GetDataset { + + // Get a dataset + static void getDataset(String projectId, String datasetId) { + // String projectId = "YOUR_PROJECT_ID"; + // String datasetId = "YOUR_DATASET_ID"; + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (AutoMlClient client = AutoMlClient.create()) { + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); + Dataset dataset = client.getDataset(datasetFullId); + + // Display the dataset information + System.out.format("Dataset name: %s\n", dataset.getName()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = dataset.getName().split("/"); + String retrievedDatasetId = names[names.length - 1]; + System.out.format("Dataset id: %s\n", retrievedDatasetId); + System.out.format("Dataset display name: %s\n", dataset.getDisplayName()); + System.out.println("Translation dataset metadata:"); + System.out.format( + "\tSource language code: %s\n", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode()); + System.out.format( + "\tTarget language code: %s\n", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + System.out.println("Dataset create time:"); + System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_get_dataset] 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..b604a379f26 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModel.java @@ -0,0 +1,59 @@ +/* + * 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; + +// [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()); + // 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()); + 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..6713d51f212 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetModelEvaluation.java @@ -0,0 +1,59 @@ +/* + * 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; + +// [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..92970c24e4c --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/GetOperationStatus.java @@ -0,0 +1,57 @@ +/* + * 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; + +// [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.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); + System.out.format("\tDone: %s\n", operation.getDone()); + if (operation.hasResponse()) { + System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); + } + 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..e98b5aa38ae --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ImportDataset.java @@ -0,0 +1,60 @@ +/* + * 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; + +// [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..651c0ec0b99 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListDatasets.java @@ -0,0 +1,73 @@ +/* + * 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; + +// [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()); + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are + // required for other methods. + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` + String[] names = dataset.getName().split("/"); + String retrievedDatasetId = names[names.length - 1]; + System.out.format("Dataset id: %s\n", retrievedDatasetId); + System.out.format("Dataset display name: %s\n", dataset.getDisplayName()); + System.out.println("Translation dataset metadata:"); + System.out.format( + "\tSource language code: %s\n", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode()); + System.out.format( + "\tTarget language code: %s\n", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); + System.out.println("Dataset create time:"); + System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END automl_translate_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..7cf38b1318b --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModelEvaluations.java @@ -0,0 +1,63 @@ +/* + * 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; + +// [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..ef18a11eec8 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListModels.java @@ -0,0 +1,69 @@ +/* + * 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; + +// [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()); + // 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()); + 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..4a455d47e40 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ListOperationStatus.java @@ -0,0 +1,65 @@ +/* + * 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; + +// [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.format("\tMetadata Type Url: %s\n", operation.getMetadata().getTypeUrl()); + System.out.format("\tDone: %s\n", operation.getDone()); + if (operation.hasResponse()) { + System.out.format("\tResponse Type Url: %s\n", operation.getResponse().getTypeUrl()); + } + 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..0b9e3e77ee7 --- /dev/null +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/Prediction.java @@ -0,0 +1,62 @@ +/* + * 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; + +// [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 { + + // Predict + 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/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() { 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..2793fe81343 --- /dev/null +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetIT.java @@ -0,0 +1,141 @@ +/* + * 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 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; +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 = "gs://" + 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 = got.split("Dataset id: ")[1].split("\n")[0]; + assertThat(got).contains("Dataset id:"); + + // Act + ImportDataset.importDataset(PROJECT_ID, datasetId, 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 + "/TEST_EXPORT_OUTPUT/"); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + + 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()) { + 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(); + } + } + } + } +} 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..fde5d9ab529 --- /dev/null +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelIT.java @@ -0,0 +1,122 @@ +/* + * 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 com.google.cloud.automl.v1.AutoMlClient; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +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 { + 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 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() { + // LIST MODELS + ListModels.listModels(PROJECT_ID); + String got = bout.toString(); + modelId = got.split("Model id: ")[1].split("\n")[0]; + assertThat(got).contains("Model id:"); + + // GET MODEL + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + GetModel.getModel(PROJECT_ID, modelId); + got = bout.toString(); + assertThat(got).contains("Model id: " + modelId); + + // LIST MODEL EVALUATIONS + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + ListModelEvaluations.listModelEvaluations(PROJECT_ID, modelId); + got = bout.toString(); + 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); + got = bout.toString(); + assertThat(got).contains("Model Evaluation 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:"); + } + + @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 8f839493ce3..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,11 +33,10 @@ 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; - 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"); + } }