diff --git a/automl/README.md b/automl/README.md deleted file mode 100644 index 9402335d279..00000000000 --- a/automl/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# AutoML Samples have been moved - -[https://github.com/googleapis/java-automl](https://github.com/googleapis/java-automl/tree/main/samples). diff --git a/automl/pom.xml b/automl/pom.xml new file mode 100644 index 00000000000..e7072ca199d --- /dev/null +++ b/automl/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + com.example.automl + automl-snippets + jar + Google Cloud Auto ML Snippets + https://github.com/GoogleCloudPlatform/java-docs-samples/tree/main/automl + + + + com.google.cloud.samples + shared-configuration + 1.2.0 + + + + 1.8 + 1.8 + UTF-8 + + + + + + + + com.google.cloud + libraries-bom + 26.1.4 + pom + import + + + + + + + com.google.cloud + google-cloud-automl + + + com.google.cloud + google-cloud-bigquery + + + com.google.cloud + google-cloud-storage + + + + net.sourceforge.argparse4j + argparse4j + 0.9.0 + + + junit + junit + 4.13.2 + test + + + com.google.truth + truth + 1.1.3 + test + + + com.google.cloud + google-cloud-core + 2.8.21 + test + tests + + + + + diff --git a/automl/resources/dandelion.jpg b/automl/resources/dandelion.jpg new file mode 100644 index 00000000000..326e4c1bf53 Binary files /dev/null and b/automl/resources/dandelion.jpg differ diff --git a/automl/resources/input.txt b/automl/resources/input.txt new file mode 100644 index 00000000000..5aecd6590fc --- /dev/null +++ b/automl/resources/input.txt @@ -0,0 +1 @@ +Tell me how this ends \ No newline at end of file diff --git a/automl/resources/salad.jpg b/automl/resources/salad.jpg new file mode 100644 index 00000000000..a7f960b5030 Binary files /dev/null and b/automl/resources/salad.jpg differ diff --git a/automl/resources/test.png b/automl/resources/test.png new file mode 100644 index 00000000000..653342a46e5 Binary files /dev/null and b/automl/resources/test.png differ diff --git a/automl/src/main/java/beta/automl/BatchPredict.java b/automl/src/main/java/beta/automl/BatchPredict.java new file mode 100644 index 00000000000..4684f7079e2 --- /dev/null +++ b/automl/src/main/java/beta/automl/BatchPredict.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_batch_predict] +// [START automl_batch_predict_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictRequest; +import com.google.cloud.automl.v1beta1.BatchPredictResult; +import com.google.cloud.automl.v1beta1.GcsDestination; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class BatchPredict { + + static void batchPredict() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl"; + String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; + batchPredict(projectId, modelId, inputUri, outputUri); + } + + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) + throws IOException, ExecutionException, InterruptedException { + // 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); + + // Configure the source of the file from a GCS bucket + GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); + BatchPredictInputConfig inputConfig = + BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); + + // Configure where to store the output in a GCS bucket + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); + BatchPredictOutputConfig outputConfig = + BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + + // Build the request that will be sent to the API + BatchPredictRequest request = + BatchPredictRequest.newBuilder() + .setName(name.toString()) + .setInputConfig(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + // Start an asynchronous request + OperationFuture future = + client.batchPredictAsync(request); + + System.out.println("Waiting for operation to complete..."); + BatchPredictResult response = future.get(); + System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); + } + } +} +// [END automl_batch_predict_beta] +// [END automl_tables_batch_predict] diff --git a/automl/src/main/java/beta/automl/CancelOperation.java b/automl/src/main/java/beta/automl/CancelOperation.java new file mode 100644 index 00000000000..1e0e4fd2a71 --- /dev/null +++ b/automl/src/main/java/beta/automl/CancelOperation.java @@ -0,0 +1,48 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_cancel_operation_beta] + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import io.grpc.StatusRuntimeException; +import java.io.IOException; + +class CancelOperation { + + static void cancelOperation() throws IOException, InterruptedException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String location = "us-central1"; + String operationId = "YOUR_OPERATION_ID"; + String operationFullId = + String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId); + cancelOperation(operationFullId); + } + + static void cancelOperation(String operationFullId) + throws IOException, InterruptedException, StatusRuntimeException { + // 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()) { + client.getOperationsClient().cancelOperation(operationFullId); + System.out.println("Operation cancelled"); + } + } +} +// [END automl_cancel_operation_beta] diff --git a/automl/src/main/java/beta/automl/DeleteDataset.java b/automl/src/main/java/beta/automl/DeleteDataset.java new file mode 100644 index 00000000000..9ef47c5c2fa --- /dev/null +++ b/automl/src/main/java/beta/automl/DeleteDataset.java @@ -0,0 +1,51 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_delete_dataset_beta] +// [START automl_tables_delete_dataset] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeleteDataset { + + static void deleteDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + deleteDataset(projectId, datasetId); + } + + // Delete a dataset + static void deleteDataset(String projectId, String datasetId) + throws IOException, ExecutionException, InterruptedException { + // 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); + } + } +} +// [END automl_tables_delete_dataset] +// [END automl_delete_dataset_beta] diff --git a/automl/src/main/java/beta/automl/DeleteModel.java b/automl/src/main/java/beta/automl/DeleteModel.java new file mode 100644 index 00000000000..77cc99bb2c9 --- /dev/null +++ b/automl/src/main/java/beta/automl/DeleteModel.java @@ -0,0 +1,56 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_delete_model] +// [START automl_delete_model_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeleteModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deleteModel(projectId, modelId); + } + + // Delete a model + static void deleteModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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)); + } + } +} +// [END automl_delete_model_beta] +// [END automl_tables_delete_model] diff --git a/automl/src/main/java/beta/automl/DeployModel.java b/automl/src/main/java/beta/automl/DeployModel.java new file mode 100644 index 00000000000..55a3105dd1c --- /dev/null +++ b/automl/src/main/java/beta/automl/DeployModel.java @@ -0,0 +1,59 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_deploy_model] +// [START automl_deploy_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DeployModelRequest; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeployModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deployModel(projectId, modelId); + } + + // Deploy a model for prediction + static void deployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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"); + } + } +} +// [END automl_deploy_model_beta] +// [END automl_tables_deploy_model] diff --git a/automl/src/main/java/beta/automl/GetModel.java b/automl/src/main/java/beta/automl/GetModel.java new file mode 100644 index 00000000000..710b78053eb --- /dev/null +++ b/automl/src/main/java/beta/automl/GetModel.java @@ -0,0 +1,62 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_get_model_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.ModelName; +import io.grpc.StatusRuntimeException; +import java.io.IOException; + +class GetModel { + + static void getModel() throws IOException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + getModel(projectId, modelId); + } + + // Get a model + static void getModel(String projectId, String modelId) + throws IOException, StatusRuntimeException { + // 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()); + } + } +} +// [END automl_get_model_beta] diff --git a/automl/src/main/java/beta/automl/GetModelEvaluation.java b/automl/src/main/java/beta/automl/GetModelEvaluation.java new file mode 100644 index 00000000000..74f35f5a74e --- /dev/null +++ b/automl/src/main/java/beta/automl/GetModelEvaluation.java @@ -0,0 +1,75 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_classification_get_model_evaluation_beta] +// [START automl_video_object_tracking_get_model_evaluation_beta] +// [START automl_tables_get_model_evaluation] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelEvaluation; +import com.google.cloud.automl.v1beta1.ModelEvaluationName; +import java.io.IOException; + +class GetModelEvaluation { + + static void getModelEvaluation() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String modelEvaluationId = "YOUR_MODEL_EVALUATION_ID"; + getModelEvaluation(projectId, modelId, modelEvaluationId); + } + + // Get a model evaluation + static void getModelEvaluation(String projectId, String modelId, String modelEvaluationId) + throws IOException { + // 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()); + + // [END automl_video_object_tracking_get_model_evaluation_beta] + System.out.format( + "Classification Model Evaluation Metrics: %s%n", + modelEvaluation.getClassificationEvaluationMetrics()); + // [END automl_video_classification_get_model_evaluation_beta] + + // [START automl_video_object_tracking_get_model_evaluation_beta] + System.out.format( + "Video Object Tracking Evaluation Metrics: %s%n", + modelEvaluation.getVideoObjectTrackingEvaluationMetrics()); + // [START automl_video_classification_get_model_evaluation_beta] + } + } +} +// [END automl_tables_get_model_evaluation] +// [END automl_video_object_tracking_get_model_evaluation_beta] +// [END automl_video_classification_get_model_evaluation_beta] diff --git a/automl/src/main/java/beta/automl/GetOperationStatus.java b/automl/src/main/java/beta/automl/GetOperationStatus.java new file mode 100644 index 00000000000..77dc703407e --- /dev/null +++ b/automl/src/main/java/beta/automl/GetOperationStatus.java @@ -0,0 +1,59 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_get_operation_status_beta] +// [START automl_tables_get_operation_status] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.longrunning.Operation; +import java.io.IOException; + +class GetOperationStatus { + + static void getOperationStatus() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]"; + getOperationStatus(operationFullId); + } + + // Get the status of an operation + static void getOperationStatus(String operationFullId) throws IOException { + // 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()); + } + } + } +} +// [END automl_tables_get_operation_status] +// [END automl_get_operation_status_beta] diff --git a/automl/src/main/java/beta/automl/ImportDataset.java b/automl/src/main/java/beta/automl/ImportDataset.java new file mode 100644 index 00000000000..72f19b19233 --- /dev/null +++ b/automl/src/main/java/beta/automl/ImportDataset.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_import_dataset_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.api.gax.retrying.RetrySettings; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.threeten.bp.Duration; + +class ImportDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String path = "gs://BUCKET_ID/path_to_training_data.csv"; + importDataset(projectId, datasetId, path); + } + + // Import a dataset + static void importDataset(String projectId, String datasetId, String path) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Duration totalTimeout = Duration.ofMinutes(45); + RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build(); + AutoMlSettings.Builder builder = AutoMlSettings.newBuilder(); + builder.importDataSettings().setRetrySettings(retrySettings).build(); + AutoMlSettings settings = builder.build(); + + // 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(settings)) { + // 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..."); + + // Start the import job + OperationFuture operation = + client.importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); + System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; + } + } +} +// [END automl_import_dataset_beta] diff --git a/automl/src/main/java/beta/automl/ListDatasets.java b/automl/src/main/java/beta/automl/ListDatasets.java new file mode 100644 index 00000000000..7e624aa6fef --- /dev/null +++ b/automl/src/main/java/beta/automl/ListDatasets.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_classification_list_datasets_beta] +// [START automl_video_object_tracking_list_datasets_beta] +// [START automl_tables_list_datasets] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.ListDatasetsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import java.io.IOException; + +class ListDatasets { + + static void listDatasets() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listDatasets(projectId); + } + + // List the datasets + static void listDatasets(String projectId) throws IOException { + // 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()).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("Dataset create time:"); + System.out.format("\tseconds: %s%n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s%n", dataset.getCreateTime().getNanos()); + + // [END automl_video_object_tracking_list_datasets_beta] + // [END automl_tables_list_datasets] + System.out.format( + "Video classification dataset metadata: %s%n", + dataset.getVideoClassificationDatasetMetadata()); + // [END automl_video_classification_list_datasets_beta] + + // [START automl_video_object_tracking_list_datasets_beta] + System.out.format( + "Video object tracking dataset metadata: %s%n", + dataset.getVideoObjectTrackingDatasetMetadata()); + // [END automl_video_object_tracking_list_datasets_beta] + + // [START automl_tables_list_datasets] + System.out.format("Tables dataset metadata: %s%n", dataset.getTablesDatasetMetadata()); + + // [START automl_video_classification_list_datasets_beta] + // [START automl_video_object_tracking_list_datasets_beta] + } + } + } +} +// [END automl_video_classification_list_datasets_beta] +// [END automl_video_object_tracking_list_datasets_beta] +// [END automl_tables_list_datasets] diff --git a/automl/src/main/java/beta/automl/ListModelEvaluations.java b/automl/src/main/java/beta/automl/ListModelEvaluations.java new file mode 100644 index 00000000000..2fad4ea8e76 --- /dev/null +++ b/automl/src/main/java/beta/automl/ListModelEvaluations.java @@ -0,0 +1,66 @@ +/* + * 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 beta.automl; + +// [START automl_tables_list_model_evaluations] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ListModelEvaluationsRequest; +import com.google.cloud.automl.v1beta1.ModelEvaluation; +import com.google.cloud.automl.v1beta1.ModelName; +import java.io.IOException; + +class ListModelEvaluations { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + listModelEvaluations(projectId, modelId); + } + + // List model evaluations + static void listModelEvaluations(String projectId, String modelId) throws IOException { + // 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( + "Tables Model Evaluation Metrics: %s%n", + modelEvaluation.getClassificationEvaluationMetrics()); + } + } + } +} +// [END automl_tables_list_model_evaluations] diff --git a/automl/src/main/java/beta/automl/ListModels.java b/automl/src/main/java/beta/automl/ListModels.java new file mode 100644 index 00000000000..87d1b0a7f10 --- /dev/null +++ b/automl/src/main/java/beta/automl/ListModels.java @@ -0,0 +1,72 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_list_models] +// [START automl_list_models_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ListModelsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import java.io.IOException; + +class ListModels { + + static void listModels() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listModels(projectId); + } + + // List the models available in the specified location + static void listModels(String projectId) throws IOException { + // 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 listModelsRequest = + 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(listModelsRequest).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()); + } + } + } +} +// [END automl_list_models_beta] +// [END automl_tables_list_models] diff --git a/automl/src/main/java/beta/automl/SetEndpoint.java b/automl/src/main/java/beta/automl/SetEndpoint.java new file mode 100644 index 00000000000..4718b220a23 --- /dev/null +++ b/automl/src/main/java/beta/automl/SetEndpoint.java @@ -0,0 +1,55 @@ +/* + * 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 beta.automl; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.ListDatasetsRequest; +import com.google.cloud.automl.v1beta1.LocationName; +import java.io.IOException; + +class SetEndpoint { + + // Change your endpoint + static void setEndpoint(String projectId) throws IOException { + // [START automl_set_endpoint] + AutoMlSettings settings = + AutoMlSettings.newBuilder().setEndpoint("eu-automl.googleapis.com:443").build(); + + // 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. + AutoMlClient client = AutoMlClient.create(settings); + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, "eu"); + // [END automl_set_endpoint] + + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter("translation_dataset_metadata:*") + .build(); + // List all the datasets available + System.out.println("List of datasets:"); + for (Dataset dataset : client.listDatasets(request).iterateAll()) { + System.out.println(dataset); + } + client.close(); + } +} diff --git a/automl/src/main/java/beta/automl/TablesBatchPredictBigQuery.java b/automl/src/main/java/beta/automl/TablesBatchPredictBigQuery.java new file mode 100644 index 00000000000..73e964da461 --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesBatchPredictBigQuery.java @@ -0,0 +1,82 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_batch_predict_bq] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; +import com.google.cloud.automl.v1beta1.BatchPredictRequest; +import com.google.cloud.automl.v1beta1.BatchPredictResult; +import com.google.cloud.automl.v1beta1.BigQueryDestination; +import com.google.cloud.automl.v1beta1.BigQuerySource; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class TablesBatchPredictBigQuery { + + static void batchPredict() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String inputUri = "bq://YOUR_PROJECT_ID.bqDatasetID.bqTableId"; + String outputUri = "bq://YOUR_PROJECT_ID"; + batchPredict(projectId, modelId, inputUri, outputUri); + } + + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) + throws IOException, ExecutionException, InterruptedException { + // 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); + + // Configure the source of the file from BigQuery + BigQuerySource bigQuerySource = BigQuerySource.newBuilder().setInputUri(inputUri).build(); + BatchPredictInputConfig inputConfig = + BatchPredictInputConfig.newBuilder().setBigquerySource(bigQuerySource).build(); + + // Configure where to store the output in BigQuery + BigQueryDestination bigQueryDestination = + BigQueryDestination.newBuilder().setOutputUri(outputUri).build(); + BatchPredictOutputConfig outputConfig = + BatchPredictOutputConfig.newBuilder().setBigqueryDestination(bigQueryDestination).build(); + + // Build the request that will be sent to the API + BatchPredictRequest request = + BatchPredictRequest.newBuilder() + .setName(name.toString()) + .setInputConfig(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + // Start an asynchronous request + OperationFuture future = + client.batchPredictAsync(request); + + System.out.println("Waiting for operation to complete..."); + BatchPredictResult response = future.get(); + System.out.println("Batch Prediction results saved to BigQuery."); + } + } +} +// [END automl_tables_batch_predict_bq] diff --git a/automl/src/main/java/beta/automl/TablesCreateDataset.java b/automl/src/main/java/beta/automl/TablesCreateDataset.java new file mode 100644 index 00000000000..b5390f33705 --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesCreateDataset.java @@ -0,0 +1,63 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_create_dataset] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.TablesDatasetMetadata; +import java.io.IOException; + +class TablesCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // 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"); + TablesDatasetMetadata metadata = TablesDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setTablesDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // 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); + } + } +} +// [END automl_tables_create_dataset] diff --git a/automl/src/main/java/beta/automl/TablesCreateModel.java b/automl/src/main/java/beta/automl/TablesCreateModel.java new file mode 100644 index 00000000000..9c95119e17a --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesCreateModel.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_create_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ColumnSpec; +import com.google.cloud.automl.v1beta1.ColumnSpecName; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.TablesModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class TablesCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String tableSpecId = "YOUR_TABLE_SPEC_ID"; + String columnSpecId = "YOUR_COLUMN_SPEC_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, tableSpecId, columnSpecId, displayName); + } + + // Create a model + static void createModel( + String projectId, + String datasetId, + String tableSpecId, + String columnSpecId, + String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + + // Get the complete path of the column. + ColumnSpecName columnSpecName = + ColumnSpecName.of(projectId, "us-central1", datasetId, tableSpecId, columnSpecId); + + // Build the get column spec. + ColumnSpec targetColumnSpec = + ColumnSpec.newBuilder().setName(columnSpecName.toString()).build(); + + // Set model metadata. + TablesModelMetadata metadata = + TablesModelMetadata.newBuilder() + .setTargetColumnSpec(targetColumnSpec) + .setTrainBudgetMilliNodeHours(24000) + .build(); + + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTablesModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_tables_create_model] diff --git a/automl/src/main/java/beta/automl/TablesGetModel.java b/automl/src/main/java/beta/automl/TablesGetModel.java new file mode 100644 index 00000000000..443491252b5 --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesGetModel.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_get_model] + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.TablesModelColumnInfo; +import io.grpc.StatusRuntimeException; +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; + +public class TablesGetModel { + + public static void main(String[] args) throws IOException, StatusRuntimeException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String region = "YOUR_REGION"; + String modelId = "YOUR_MODEL_ID"; + getModel(projectId, region, modelId); + } + + // Demonstrates using the AutoML client to get model details. + public static void getModel(String projectId, String computeRegion, String modelId) + throws IOException, StatusRuntimeException { + // 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, computeRegion, modelId); + + // Get complete detail of the model. + 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.format("Dataset Id: %s%n", model.getDatasetId()); + System.out.println("Tables Model Metadata: "); + System.out.format( + "\tTraining budget: %s%n", model.getTablesModelMetadata().getTrainBudgetMilliNodeHours()); + System.out.format( + "\tTraining cost: %s%n", model.getTablesModelMetadata().getTrainBudgetMilliNodeHours()); + + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + String createTime = + dateFormat.format(new java.util.Date(model.getCreateTime().getSeconds() * 1000)); + System.out.format("Model create time: %s%n", createTime); + + System.out.format("Model deployment state: %s%n", model.getDeploymentState()); + + // Get features of top importance + for (TablesModelColumnInfo info : + model.getTablesModelMetadata().getTablesModelColumnInfoList()) { + System.out.format( + "Column: %s - Importance: %.2f%n", + info.getColumnDisplayName(), info.getFeatureImportance()); + } + } + } +} +// [END automl_tables_get_model] diff --git a/automl/src/main/java/beta/automl/TablesImportDataset.java b/automl/src/main/java/beta/automl/TablesImportDataset.java new file mode 100644 index 00000000000..ce0e5fc16f0 --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesImportDataset.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_import_data] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.BigQuerySource; +import com.google.cloud.automl.v1beta1.DatasetName; +import com.google.cloud.automl.v1beta1.GcsSource; +import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; + +class TablesImportDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String path = "gs://BUCKET_ID/path/to//data.csv or bq://project_id.dataset_id.table_id"; + importDataset(projectId, datasetId, path); + } + + // Import a dataset via BigQuery or Google Cloud Storage + static void importDataset(String projectId, String datasetId, String path) + throws IOException, ExecutionException, InterruptedException { + // 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); + + InputConfig.Builder inputConfigBuilder = InputConfig.newBuilder(); + + // Determine which source type was used for the input path (BigQuery or GCS) + if (path.startsWith("bq")) { + // Get training data file to be imported from a BigQuery source. + BigQuerySource.Builder bigQuerySource = BigQuerySource.newBuilder(); + bigQuerySource.setInputUri(path); + inputConfigBuilder.setBigquerySource(bigQuerySource); + } else { + // Get multiple Google Cloud Storage URIs to import data from + GcsSource gcsSource = + GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build(); + inputConfigBuilder.setGcsSource(gcsSource); + } + + // Import data from the input URI + System.out.println("Processing import..."); + + Empty response = client.importDataAsync(datasetFullId, inputConfigBuilder.build()).get(); + System.out.format("Dataset imported. %s%n", response); + } + } +} +// [END automl_tables_import_data] diff --git a/automl/src/main/java/beta/automl/TablesPredict.java b/automl/src/main/java/beta/automl/TablesPredict.java new file mode 100644 index 00000000000..fd2257c899e --- /dev/null +++ b/automl/src/main/java/beta/automl/TablesPredict.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_predict] +import com.google.cloud.automl.v1beta1.AnnotationPayload; +import com.google.cloud.automl.v1beta1.ExamplePayload; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.PredictRequest; +import com.google.cloud.automl.v1beta1.PredictResponse; +import com.google.cloud.automl.v1beta1.PredictionServiceClient; +import com.google.cloud.automl.v1beta1.Row; +import com.google.cloud.automl.v1beta1.TablesAnnotation; +import com.google.protobuf.Value; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +class TablesPredict { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + // Values should match the input expected by your model. + List values = new ArrayList<>(); + // values.add(Value.newBuilder().setBoolValue(true).build()); + // values.add(Value.newBuilder().setNumberValue(10).build()); + // values.add(Value.newBuilder().setStringValue("YOUR_STRING").build()); + predict(projectId, modelId, values); + } + + static void predict(String projectId, String modelId, List values) throws IOException { + // 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); + Row row = Row.newBuilder().addAllValues(values).build(); + ExamplePayload payload = ExamplePayload.newBuilder().setRow(row).build(); + + // Feature importance gives you visibility into how the features in a specific prediction + // request informed the resulting prediction. For more info, see: + // https://cloud.google.com/automl-tables/docs/features#local + PredictRequest request = + PredictRequest.newBuilder() + .setName(name.toString()) + .setPayload(payload) + .putParams("feature_importance", "true") + .build(); + + PredictResponse response = client.predict(request); + + System.out.println("Prediction results:"); + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + TablesAnnotation tablesAnnotation = annotationPayload.getTables(); + System.out.format( + "Classification label: %s%n", tablesAnnotation.getValue().getStringValue()); + System.out.format("Classification score: %.3f%n", tablesAnnotation.getScore()); + // Get features of top importance + tablesAnnotation + .getTablesModelColumnInfoList() + .forEach( + info -> + System.out.format( + "\tColumn: %s - Importance: %.2f%n", + info.getColumnDisplayName(), info.getFeatureImportance())); + } + } + } +} +// [END automl_tables_predict] diff --git a/automl/src/main/java/beta/automl/UndeployModel.java b/automl/src/main/java/beta/automl/UndeployModel.java new file mode 100644 index 00000000000..7325f519dba --- /dev/null +++ b/automl/src/main/java/beta/automl/UndeployModel.java @@ -0,0 +1,58 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_tables_undeploy_model] +// [START automl_undeploy_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.UndeployModelRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class UndeployModel { + + static void undeployModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + undeployModel(projectId, modelId); + } + + // Undeploy a model from prediction + static void undeployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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 request = + UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + OperationFuture future = client.undeployModelAsync(request); + + future.get(); + System.out.println("Model undeployment finished"); + } + } +} +// [END automl_undeploy_model_beta] +// [END automl_tables_undeploy_model] diff --git a/automl/src/main/java/beta/automl/VideoClassificationCreateDataset.java b/automl/src/main/java/beta/automl/VideoClassificationCreateDataset.java new file mode 100644 index 00000000000..1fe70c9f2b2 --- /dev/null +++ b/automl/src/main/java/beta/automl/VideoClassificationCreateDataset.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_classification_create_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.VideoClassificationDatasetMetadata; +import java.io.IOException; + +class VideoClassificationCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // 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"); + VideoClassificationDatasetMetadata metadata = + VideoClassificationDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setVideoClassificationDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // 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); + } + } +} +// [END automl_video_classification_create_dataset_beta] diff --git a/automl/src/main/java/beta/automl/VideoClassificationCreateModel.java b/automl/src/main/java/beta/automl/VideoClassificationCreateModel.java new file mode 100644 index 00000000000..94f3407d442 --- /dev/null +++ b/automl/src/main/java/beta/automl/VideoClassificationCreateModel.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_classification_create_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.VideoClassificationModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VideoClassificationCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + VideoClassificationModelMetadata metadata = + VideoClassificationModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setVideoClassificationModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_video_classification_create_model_beta] diff --git a/automl/src/main/java/beta/automl/VideoObjectTrackingCreateDataset.java b/automl/src/main/java/beta/automl/VideoObjectTrackingCreateDataset.java new file mode 100644 index 00000000000..61b2a1ada3a --- /dev/null +++ b/automl/src/main/java/beta/automl/VideoObjectTrackingCreateDataset.java @@ -0,0 +1,64 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_object_tracking_create_dataset_beta] +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.VideoObjectTrackingDatasetMetadata; +import java.io.IOException; + +class VideoObjectTrackingCreateDataset { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) throws IOException { + // 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"); + VideoObjectTrackingDatasetMetadata metadata = + VideoObjectTrackingDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setVideoObjectTrackingDatasetMetadata(metadata) + .build(); + + Dataset createdDataset = client.createDataset(projectLocation, dataset); + + // 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); + } + } +} +// [END automl_video_object_tracking_create_dataset_beta] diff --git a/automl/src/main/java/beta/automl/VideoObjectTrackingCreateModel.java b/automl/src/main/java/beta/automl/VideoObjectTrackingCreateModel.java new file mode 100644 index 00000000000..29df7866613 --- /dev/null +++ b/automl/src/main/java/beta/automl/VideoObjectTrackingCreateModel.java @@ -0,0 +1,70 @@ +/* + * Copyright 2020 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 beta.automl; + +// [START automl_video_object_tracking_create_model_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.Model; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.cloud.automl.v1beta1.VideoObjectTrackingModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VideoObjectTrackingCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + VideoObjectTrackingModelMetadata metadata = + VideoObjectTrackingModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setVideoObjectTrackingModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_video_object_tracking_create_model_beta] diff --git a/automl/src/main/java/com/example/automl/BatchPredict.java b/automl/src/main/java/com/example/automl/BatchPredict.java new file mode 100644 index 00000000000..da1e0afe5dd --- /dev/null +++ b/automl/src/main/java/com/example/automl/BatchPredict.java @@ -0,0 +1,75 @@ +/* + * 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.example.automl; + +// [START automl_batch_predict] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.BatchPredictInputConfig; +import com.google.cloud.automl.v1.BatchPredictOutputConfig; +import com.google.cloud.automl.v1.BatchPredictRequest; +import com.google.cloud.automl.v1.BatchPredictResult; +import com.google.cloud.automl.v1.GcsDestination; +import com.google.cloud.automl.v1.GcsSource; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.cloud.automl.v1.PredictionServiceClient; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class BatchPredict { + + static void batchPredict() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl"; + String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; + batchPredict(projectId, modelId, inputUri, outputUri); + } + + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) + throws IOException, ExecutionException, InterruptedException { + // 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); + GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); + BatchPredictInputConfig inputConfig = + BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); + GcsDestination gcsDestination = + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); + BatchPredictOutputConfig outputConfig = + BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); + BatchPredictRequest request = + BatchPredictRequest.newBuilder() + .setName(name.toString()) + .setInputConfig(inputConfig) + .setOutputConfig(outputConfig) + .build(); + + OperationFuture future = + client.batchPredictAsync(request); + + System.out.println("Waiting for operation to complete..."); + BatchPredictResult response = future.get(); + System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); + } + } +} +// [END automl_batch_predict] diff --git a/automl/src/main/java/com/example/automl/DeleteDataset.java b/automl/src/main/java/com/example/automl/DeleteDataset.java new file mode 100644 index 00000000000..fcb6e2e913c --- /dev/null +++ b/automl/src/main/java/com/example/automl/DeleteDataset.java @@ -0,0 +1,49 @@ +/* + * 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.example.automl; + +// [START automl_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 { + + static void deleteDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + deleteDataset(projectId, datasetId); + } + + // Delete a dataset + static void deleteDataset(String projectId, String datasetId) + throws IOException, ExecutionException, InterruptedException { + // 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); + } + } +} +// [END automl_delete_dataset] diff --git a/automl/src/main/java/com/example/automl/DeleteModel.java b/automl/src/main/java/com/example/automl/DeleteModel.java new file mode 100644 index 00000000000..66969ff75d4 --- /dev/null +++ b/automl/src/main/java/com/example/automl/DeleteModel.java @@ -0,0 +1,54 @@ +/* + * 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.example.automl; + +// [START automl_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 { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deleteModel(projectId, modelId); + } + + // Delete a model + static void deleteModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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)); + } + } +} +// [END automl_delete_model] diff --git a/automl/src/main/java/com/example/automl/DeployModel.java b/automl/src/main/java/com/example/automl/DeployModel.java new file mode 100644 index 00000000000..ecc338cf37e --- /dev/null +++ b/automl/src/main/java/com/example/automl/DeployModel.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.example.automl; + +// [START automl_deploy_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class DeployModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + deployModel(projectId, modelId); + } + + // Deploy a model for prediction + static void deployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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"); + } + } +} +// [END automl_deploy_model] diff --git a/automl/src/main/java/com/example/automl/ExportDataset.java b/automl/src/main/java/com/example/automl/ExportDataset.java new file mode 100644 index 00000000000..812869b9a56 --- /dev/null +++ b/automl/src/main/java/com/example/automl/ExportDataset.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.example.automl; + +// [START automl_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 { + + static void exportDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String gcsUri = "gs://BUCKET_ID/path_to_export/"; + exportDataset(projectId, datasetId, gcsUri); + } + + // Export a dataset to a GCS bucket + static void exportDataset(String projectId, String datasetId, String gcsUri) + throws IOException, ExecutionException, InterruptedException { + // 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); + } + } +} +// [END automl_export_dataset] diff --git a/automl/src/main/java/com/example/automl/GetDataset.java b/automl/src/main/java/com/example/automl/GetDataset.java new file mode 100644 index 00000000000..e1028d7052d --- /dev/null +++ b/automl/src/main/java/com/example/automl/GetDataset.java @@ -0,0 +1,114 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_get_dataset] +// [START automl_language_sentiment_analysis_get_dataset] +// [START automl_language_text_classification_get_dataset] +// [START automl_translate_get_dataset] +// [START automl_vision_classification_get_dataset] +// [START automl_vision_object_detection_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 { + + static void getDataset() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + getDataset(projectId, datasetId); + } + + // Get a dataset + static void getDataset(String projectId, String datasetId) throws IOException { + // 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("Dataset create time:"); + System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + // [END automl_language_sentiment_analysis_get_dataset] + // [END automl_language_text_classification_get_dataset] + // [END automl_translate_get_dataset] + // [END automl_vision_classification_get_dataset] + // [END automl_vision_object_detection_get_dataset] + System.out.format( + "Text extraction dataset metadata: %s\n", dataset.getTextExtractionDatasetMetadata()); + // [END automl_language_entity_extraction_get_dataset] + + // [START automl_language_sentiment_analysis_get_dataset] + System.out.format( + "Text sentiment dataset metadata: %s\n", dataset.getTextSentimentDatasetMetadata()); + // [END automl_language_sentiment_analysis_get_dataset] + + // [START automl_language_text_classification_get_dataset] + System.out.format( + "Text classification dataset metadata: %s\n", + dataset.getTextClassificationDatasetMetadata()); + // [END automl_language_text_classification_get_dataset] + + // [START automl_translate_get_dataset] + 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()); + // [END automl_translate_get_dataset] + + // [START automl_vision_classification_get_dataset] + System.out.format( + "Image classification dataset metadata: %s\n", + dataset.getImageClassificationDatasetMetadata()); + // [END automl_vision_classification_get_dataset] + + // [START automl_vision_object_detection_get_dataset] + System.out.format( + "Image object detection dataset metadata: %s\n", + dataset.getImageObjectDetectionDatasetMetadata()); + // [START automl_language_entity_extraction_get_dataset] + // [START automl_language_sentiment_analysis_get_dataset] + // [START automl_language_text_classification_get_dataset] + // [START automl_translate_get_dataset] + // [START automl_vision_classification_get_dataset] + } + } +} +// [END automl_language_entity_extraction_get_dataset] +// [END automl_language_sentiment_analysis_get_dataset] +// [END automl_language_text_classification_get_dataset] +// [END automl_translate_get_dataset] +// [END automl_vision_classification_get_dataset] +// [END automl_vision_object_detection_get_dataset] diff --git a/automl/src/main/java/com/example/automl/GetModel.java b/automl/src/main/java/com/example/automl/GetModel.java new file mode 100644 index 00000000000..70b97905c2e --- /dev/null +++ b/automl/src/main/java/com/example/automl/GetModel.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.example.automl; + +// [START automl_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 { + + static void getModel() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + getModel(projectId, modelId); + } + + // Get a model + static void getModel(String projectId, String modelId) throws IOException { + // 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()); + } + } +} +// [END automl_get_model] diff --git a/automl/src/main/java/com/example/automl/GetModelEvaluation.java b/automl/src/main/java/com/example/automl/GetModelEvaluation.java new file mode 100644 index 00000000000..694888d53fa --- /dev/null +++ b/automl/src/main/java/com/example/automl/GetModelEvaluation.java @@ -0,0 +1,109 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_get_model_evaluation] +// [START automl_language_sentiment_analysis_get_model_evaluation] +// [START automl_language_text_classification_get_model_evaluation] +// [START automl_translate_get_model_evaluation] +// [START automl_vision_classification_get_model_evaluation] +// [START automl_vision_object_detection_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 { + + static void getModelEvaluation() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String modelEvaluationId = "YOUR_MODEL_EVALUATION_ID"; + getModelEvaluation(projectId, modelId, modelEvaluationId); + } + + // Get a model evaluation + static void getModelEvaluation(String projectId, String modelId, String modelEvaluationId) + throws IOException { + // 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()); + // [END automl_language_sentiment_analysis_get_model_evaluation] + // [END automl_language_text_classification_get_model_evaluation] + // [END automl_translate_get_model_evaluation] + // [END automl_vision_classification_get_model_evaluation] + // [END automl_vision_object_detection_get_model_evaluation] + System.out.format( + "Entity Extraction Model Evaluation Metrics: %s\n", + modelEvaluation.getTextExtractionEvaluationMetrics()); + // [END automl_language_entity_extraction_get_model_evaluation] + + // [START automl_language_sentiment_analysis_get_model_evaluation] + System.out.format( + "Sentiment Analysis Model Evaluation Metrics: %s\n", + modelEvaluation.getTextSentimentEvaluationMetrics()); + // [END automl_language_sentiment_analysis_get_model_evaluation] + + // [START automl_language_text_classification_get_model_evaluation] + // [START automl_vision_classification_get_model_evaluation] + System.out.format( + "Classification Model Evaluation Metrics: %s\n", + modelEvaluation.getClassificationEvaluationMetrics()); + // [END automl_language_text_classification_get_model_evaluation] + // [END automl_vision_classification_get_model_evaluation] + + // [START automl_translate_get_model_evaluation] + System.out.format( + "Translate Model Evaluation Metrics: %s\n", + modelEvaluation.getTranslationEvaluationMetrics()); + // [END automl_translate_get_model_evaluation] + + // [START automl_vision_object_detection_get_model_evaluation] + System.out.format( + "Object Detection Model Evaluation Metrics: %s\n", + modelEvaluation.getImageObjectDetectionEvaluationMetrics()); + // [START automl_language_entity_extraction_get_model_evaluation] + // [START automl_language_sentiment_analysis_get_model_evaluation] + // [START automl_language_text_classification_get_model_evaluation] + // [START automl_translate_get_model_evaluation] + // [START automl_vision_classification_get_model_evaluation] + } + } +} +// [END automl_language_entity_extraction_get_model_evaluation] +// [END automl_language_sentiment_analysis_get_model_evaluation] +// [END automl_language_text_classification_get_model_evaluation] +// [END automl_translate_get_model_evaluation] +// [END automl_vision_classification_get_model_evaluation] +// [END automl_vision_object_detection_get_model_evaluation] diff --git a/automl/src/main/java/com/example/automl/GetOperationStatus.java b/automl/src/main/java/com/example/automl/GetOperationStatus.java new file mode 100644 index 00000000000..07bfe02933d --- /dev/null +++ b/automl/src/main/java/com/example/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.example.automl; + +// [START automl_get_operation_status] +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.longrunning.Operation; +import java.io.IOException; + +class GetOperationStatus { + + static void getOperationStatus() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String operationFullId = "projects/[projectId]/locations/us-central1/operations/[operationId]"; + getOperationStatus(operationFullId); + } + + // Get the status of an operation + static void getOperationStatus(String operationFullId) throws IOException { + // 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()); + } + } + } +} +// [END automl_get_operation_status] diff --git a/automl/src/main/java/com/example/automl/ImportDataset.java b/automl/src/main/java/com/example/automl/ImportDataset.java new file mode 100644 index 00000000000..3ead88326b3 --- /dev/null +++ b/automl/src/main/java/com/example/automl/ImportDataset.java @@ -0,0 +1,81 @@ +/* + * 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.example.automl; + +// [START automl_import_dataset] +import com.google.api.gax.longrunning.OperationFuture; +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.cloud.automl.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.Arrays; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +class ImportDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String path = "gs://BUCKET_ID/path_to_training_data.csv"; + importDataset(projectId, datasetId, path); + } + + // Import a dataset + static void importDataset(String projectId, String datasetId, String path) + throws IOException, ExecutionException, InterruptedException, TimeoutException { + // 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); + + // 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..."); + + // Start the import job + OperationFuture operation = + client.importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); + System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; + } + } +} +// [END automl_import_dataset] diff --git a/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java b/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java new file mode 100644 index 00000000000..7adf18dc623 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java @@ -0,0 +1,70 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_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.TextExtractionDatasetMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageEntityExtractionCreateDataset { + + static void createDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + + TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setTextExtractionDatasetMetadata(metadata) + .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); + } + } +} +// [END automl_language_entity_extraction_create_dataset] diff --git a/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java b/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java new file mode 100644 index 00000000000..f2da97894df --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java @@ -0,0 +1,68 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_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.TextExtractionModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageEntityExtractionCreateModel { + + static void createModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + TextExtractionModelMetadata metadata = TextExtractionModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTextExtractionModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_language_entity_extraction_create_model] diff --git a/automl/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java b/automl/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java new file mode 100644 index 00000000000..065990613b3 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java @@ -0,0 +1,72 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_predict] +import com.google.cloud.automl.v1.AnnotationPayload; +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.TextSegment; +import com.google.cloud.automl.v1.TextSnippet; +import java.io.IOException; + +class LanguageEntityExtractionPredict { + + static void predict() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String content = "text to predict"; + predict(projectId, modelId, content); + } + + static void predict(String projectId, String modelId, String content) throws IOException { + // 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); + + // For available mime types, see: + // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet + TextSnippet textSnippet = + TextSnippet.newBuilder() + .setContent(content) + .setMimeType("text/plain") // Types: text/plain, text/html + .build(); + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + PredictRequest predictRequest = + PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build(); + + PredictResponse response = client.predict(predictRequest); + + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + System.out.format("Text Extract Entity Type: %s\n", annotationPayload.getDisplayName()); + System.out.format("Text score: %.2f\n", annotationPayload.getTextExtraction().getScore()); + TextSegment textSegment = annotationPayload.getTextExtraction().getTextSegment(); + System.out.format("Text Extract Entity Content: %s\n", textSegment.getContent()); + System.out.format("Text Start Offset: %s\n", textSegment.getStartOffset()); + System.out.format("Text End Offset: %s\n\n", textSegment.getEndOffset()); + } + } + } +} +// [END automl_language_entity_extraction_predict] diff --git a/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java new file mode 100644 index 00000000000..6ab0be85851 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.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.example.automl; + +// [START automl_language_sentiment_analysis_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.TextSentimentDatasetMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageSentimentAnalysisCreateDataset { + + static void createDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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 text classification type for the dataset. + TextSentimentDatasetMetadata metadata = + TextSentimentDatasetMetadata.newBuilder() + .setSentimentMax(4) // Possible max sentiment score: 1-10 + .build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setTextSentimentDatasetMetadata(metadata) + .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); + } + } +} +// [END automl_language_sentiment_analysis_create_dataset] diff --git a/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java new file mode 100644 index 00000000000..04af77592e5 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.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.example.automl; + +// [START automl_language_sentiment_analysis_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.TextSentimentModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageSentimentAnalysisCreateModel { + + static void createModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + System.out.println(datasetId); + TextSentimentModelMetadata metadata = TextSentimentModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTextSentimentModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_language_sentiment_analysis_create_model] diff --git a/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java new file mode 100644 index 00000000000..65945c26023 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java @@ -0,0 +1,68 @@ +/* + * 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.example.automl; + +// [START automl_language_sentiment_analysis_predict] +import com.google.cloud.automl.v1.AnnotationPayload; +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; + +class LanguageSentimentAnalysisPredict { + + static void predict() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String content = "text to predict"; + predict(projectId, modelId, content); + } + + static void predict(String projectId, String modelId, String content) throws IOException { + // 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); + + // For available mime types, see: + // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet + TextSnippet textSnippet = + TextSnippet.newBuilder() + .setContent(content) + .setMimeType("text/plain") // Types: text/plain, text/html + .build(); + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + PredictRequest predictRequest = + PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build(); + + PredictResponse response = client.predict(predictRequest); + + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName()); + System.out.format( + "Predicted sentiment score: %d\n", annotationPayload.getTextSentiment().getSentiment()); + } + } + } +} +// [END automl_language_sentiment_analysis_predict] diff --git a/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java b/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java new file mode 100644 index 00000000000..9332c06a246 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java @@ -0,0 +1,82 @@ +/* + * 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.example.automl; + +// [START automl_language_text_classification_create_dataset] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ClassificationType; +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.TextClassificationDatasetMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageTextClassificationCreateDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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 classification type + // Types: + // MultiLabel: Multiple labels are allowed for one example. + // MultiClass: At most one label is allowed per example. + ClassificationType classificationType = ClassificationType.MULTILABEL; + + // Specify the text classification type for the dataset. + TextClassificationDatasetMetadata metadata = + TextClassificationDatasetMetadata.newBuilder() + .setClassificationType(classificationType) + .build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setTextClassificationDatasetMetadata(metadata) + .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); + } + } +} +// [END automl_language_text_classification_create_dataset] diff --git a/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java b/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java new file mode 100644 index 00000000000..bf26db03101 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java @@ -0,0 +1,70 @@ +/* + * 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.example.automl; + +// [START automl_language_text_classification_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.TextClassificationModelMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class LanguageTextClassificationCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + TextClassificationModelMetadata metadata = + TextClassificationModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setTextClassificationModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_language_text_classification_create_model] diff --git a/automl/src/main/java/com/example/automl/LanguageTextClassificationPredict.java b/automl/src/main/java/com/example/automl/LanguageTextClassificationPredict.java new file mode 100644 index 00000000000..e2da94aa121 --- /dev/null +++ b/automl/src/main/java/com/example/automl/LanguageTextClassificationPredict.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.example.automl; + +// [START automl_language_text_classification_predict] +import com.google.cloud.automl.v1.AnnotationPayload; +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; + +class LanguageTextClassificationPredict { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String content = "text to predict"; + predict(projectId, modelId, content); + } + + static void predict(String projectId, String modelId, String content) throws IOException { + // 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); + + // For available mime types, see: + // https://cloud.google.com/automl/docs/reference/rest/v1/projects.locations.models/predict#textsnippet + TextSnippet textSnippet = + TextSnippet.newBuilder() + .setContent(content) + .setMimeType("text/plain") // Types: text/plain, text/html + .build(); + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + PredictRequest predictRequest = + PredictRequest.newBuilder().setName(name.toString()).setPayload(payload).build(); + + PredictResponse response = client.predict(predictRequest); + + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName()); + System.out.format( + "Predicted sentiment score: %.2f\n\n", + annotationPayload.getClassification().getScore()); + } + } + } +} +// [END automl_language_text_classification_predict] diff --git a/automl/src/main/java/com/example/automl/ListDatasets.java b/automl/src/main/java/com/example/automl/ListDatasets.java new file mode 100644 index 00000000000..b766a673ff4 --- /dev/null +++ b/automl/src/main/java/com/example/automl/ListDatasets.java @@ -0,0 +1,119 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_list_datasets] +// [START automl_language_sentiment_analysis_list_datasets] +// [START automl_language_text_classification_list_datasets] +// [START automl_translate_list_datasets] +// [START automl_vision_classification_list_datasets] +// [START automl_vision_object_detection_list_datasets] +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 { + + static void listDatasets() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listDatasets(projectId); + } + + // List the datasets + static void listDatasets(String projectId) throws IOException { + // 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()).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("Dataset create time:"); + System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); + System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + // [END automl_language_sentiment_analysis_list_datasets] + // [END automl_language_text_classification_list_datasets] + // [END automl_translate_list_datasets] + // [END automl_vision_classification_list_datasets] + // [END automl_vision_object_detection_list_datasets] + System.out.format( + "Text extraction dataset metadata: %s\n", dataset.getTextExtractionDatasetMetadata()); + // [END automl_language_entity_extraction_list_datasets] + + // [START automl_language_sentiment_analysis_list_datasets] + System.out.format( + "Text sentiment dataset metadata: %s\n", dataset.getTextSentimentDatasetMetadata()); + // [END automl_language_sentiment_analysis_list_datasets] + + // [START automl_language_text_classification_list_datasets] + System.out.format( + "Text classification dataset metadata: %s\n", + dataset.getTextClassificationDatasetMetadata()); + // [END automl_language_text_classification_list_datasets] + + // [START automl_translate_list_datasets] + 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()); + // [END automl_translate_list_datasets] + + // [START automl_vision_classification_list_datasets] + System.out.format( + "Image classification dataset metadata: %s\n", + dataset.getImageClassificationDatasetMetadata()); + // [END automl_vision_classification_list_datasets] + + // [START automl_vision_object_detection_list_datasets] + System.out.format( + "Image object detection dataset metadata: %s\n", + dataset.getImageObjectDetectionDatasetMetadata()); + // [START automl_language_entity_extraction_list_datasets] + // [START automl_language_sentiment_analysis_list_datasets] + // [START automl_language_text_classification_list_datasets] + // [START automl_translate_list_datasets] + // [START automl_vision_classification_list_datasets] + } + } + } +} +// [END automl_language_entity_extraction_list_datasets] +// [END automl_language_sentiment_analysis_list_datasets] +// [END automl_language_text_classification_list_datasets] +// [END automl_translate_list_datasets] +// [END automl_vision_classification_list_datasets] +// [END automl_vision_object_detection_list_datasets] diff --git a/automl/src/main/java/com/example/automl/ListModelEvaluations.java b/automl/src/main/java/com/example/automl/ListModelEvaluations.java new file mode 100644 index 00000000000..5b25ec08fe7 --- /dev/null +++ b/automl/src/main/java/com/example/automl/ListModelEvaluations.java @@ -0,0 +1,112 @@ +/* + * 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.example.automl; + +// [START automl_language_entity_extraction_list_model_evaluations] +// [START automl_language_sentiment_analysis_list_model_evaluations] +// [START automl_language_text_classification_list_model_evaluations] +// [START automl_translate_list_model_evaluations] +// [START automl_vision_classification_list_model_evaluations] +// [START automl_vision_object_detection_list_model_evaluations] + +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 { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + listModelEvaluations(projectId, modelId); + } + + // List model evaluations + static void listModelEvaluations(String projectId, String modelId) throws IOException { + // 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()); + // [END automl_language_sentiment_analysis_list_model_evaluations] + // [END automl_language_text_classification_list_model_evaluations] + // [END automl_translate_list_model_evaluations] + // [END automl_vision_classification_list_model_evaluations] + // [END automl_vision_object_detection_list_model_evaluations] + System.out.format( + "Entity Extraction Model Evaluation Metrics: %s\n", + modelEvaluation.getTextExtractionEvaluationMetrics()); + // [END automl_language_entity_extraction_list_model_evaluations] + + // [START automl_language_sentiment_analysis_list_model_evaluations] + System.out.format( + "Sentiment Analysis Model Evaluation Metrics: %s\n", + modelEvaluation.getTextSentimentEvaluationMetrics()); + // [END automl_language_sentiment_analysis_list_model_evaluations] + + // [START automl_language_text_classification_list_model_evaluations] + // [START automl_vision_classification_list_model_evaluations] + System.out.format( + "Classification Model Evaluation Metrics: %s\n", + modelEvaluation.getClassificationEvaluationMetrics()); + // [END automl_language_text_classification_list_model_evaluations] + // [END automl_vision_classification_list_model_evaluations] + + // [START automl_translate_list_model_evaluations] + System.out.format( + "Translate Model Evaluation Metrics: %s\n", + modelEvaluation.getTranslationEvaluationMetrics()); + // [END automl_translate_list_model_evaluations] + + // [START automl_vision_object_detection_list_model_evaluations] + System.out.format( + "Object Detection Model Evaluation Metrics: %s\n", + modelEvaluation.getImageObjectDetectionEvaluationMetrics()); + // [START automl_language_entity_extraction_list_model_evaluations] + // [START automl_language_sentiment_analysis_list_model_evaluations] + // [START automl_language_text_classification_list_model_evaluations] + // [START automl_translate_list_model_evaluations] + // [START automl_vision_classification_list_model_evaluations] + } + } + } +} +// [END automl_language_entity_extraction_list_model_evaluations] +// [END automl_language_sentiment_analysis_list_model_evaluations] +// [END automl_language_text_classification_list_model_evaluations] +// [END automl_translate_list_model_evaluations] +// [END automl_vision_classification_list_model_evaluations] +// [END automl_vision_object_detection_list_model_evaluations] diff --git a/automl/src/main/java/com/example/automl/ListModels.java b/automl/src/main/java/com/example/automl/ListModels.java new file mode 100644 index 00000000000..c24f09b28ae --- /dev/null +++ b/automl/src/main/java/com/example/automl/ListModels.java @@ -0,0 +1,70 @@ +/* + * 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.example.automl; + +// [START automl_list_models] +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 { + + static void listModels() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listModels(projectId); + } + + // List the models available in the specified location + static void listModels(String projectId) throws IOException { + // 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 listModelsRequest = + 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(listModelsRequest).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()); + } + } + } +} +// [END automl_list_models] diff --git a/automl/src/main/java/com/example/automl/ListOperationStatus.java b/automl/src/main/java/com/example/automl/ListOperationStatus.java new file mode 100644 index 00000000000..a980d41bad5 --- /dev/null +++ b/automl/src/main/java/com/example/automl/ListOperationStatus.java @@ -0,0 +1,66 @@ +/* + * 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.example.automl; + +// [START automl_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 { + + static void listOperationStatus() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + listOperationStatus(projectId); + } + + // Get the status of an operation + static void listOperationStatus(String projectId) throws IOException { + // 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()); + } + } + } + } +} +// [END automl_list_operation_status] diff --git a/automl/src/main/java/com/example/automl/TranslateCreateDataset.java b/automl/src/main/java/com/example/automl/TranslateCreateDataset.java new file mode 100644 index 00000000000..77769bdbec4 --- /dev/null +++ b/automl/src/main/java/com/example/automl/TranslateCreateDataset.java @@ -0,0 +1,76 @@ +/* + * 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.example.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 TranslateCreateDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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); + } + } +} +// [END automl_translate_create_dataset] diff --git a/automl/src/main/java/com/example/automl/TranslateCreateModel.java b/automl/src/main/java/com/example/automl/TranslateCreateModel.java new file mode 100644 index 00000000000..cddd597c6bb --- /dev/null +++ b/automl/src/main/java/com/example/automl/TranslateCreateModel.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.example.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 TranslateCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + 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); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_translate_create_model] diff --git a/automl/src/main/java/com/example/automl/TranslatePredict.java b/automl/src/main/java/com/example/automl/TranslatePredict.java new file mode 100644 index 00000000000..2385b831c44 --- /dev/null +++ b/automl/src/main/java/com/example/automl/TranslatePredict.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.example.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 TranslatePredict { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String filePath = "path_to_local_file.txt"; + predict(projectId, modelId, filePath); + } + + static void predict(String projectId, String modelId, String filePath) throws IOException { + // 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.format("Translated Content: %s\n", translatedContent.getContent()); + } + } +} +// [END automl_translate_predict] diff --git a/automl/src/main/java/com/example/automl/UndeployModel.java b/automl/src/main/java/com/example/automl/UndeployModel.java new file mode 100644 index 00000000000..e7ef532e94d --- /dev/null +++ b/automl/src/main/java/com/example/automl/UndeployModel.java @@ -0,0 +1,56 @@ +/* + * 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.example.automl; + +// [START automl_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.UndeployModelRequest; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class UndeployModel { + + static void undeployModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + undeployModel(projectId, modelId); + } + + // Undeploy a model from prediction + static void undeployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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 request = + UndeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + OperationFuture future = client.undeployModelAsync(request); + + future.get(); + System.out.println("Model undeployment finished"); + } + } +} +// [END automl_undeploy_model] diff --git a/automl/src/main/java/com/example/automl/VisionClassificationCreateDataset.java b/automl/src/main/java/com/example/automl/VisionClassificationCreateDataset.java new file mode 100644 index 00000000000..f3e2d951d01 --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionClassificationCreateDataset.java @@ -0,0 +1,80 @@ +/* + * 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.example.automl; + +// [START automl_vision_classification_create_dataset] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ClassificationType; +import com.google.cloud.automl.v1.Dataset; +import com.google.cloud.automl.v1.ImageClassificationDatasetMetadata; +import com.google.cloud.automl.v1.LocationName; +import com.google.cloud.automl.v1.OperationMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VisionClassificationCreateDataset { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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 classification type + // Types: + // MultiLabel: Multiple labels are allowed for one example. + // MultiClass: At most one label is allowed per example. + ClassificationType classificationType = ClassificationType.MULTILABEL; + ImageClassificationDatasetMetadata metadata = + ImageClassificationDatasetMetadata.newBuilder() + .setClassificationType(classificationType) + .build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setImageClassificationDatasetMetadata(metadata) + .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); + } + } +} +// [END automl_vision_classification_create_dataset] diff --git a/automl/src/main/java/com/example/automl/VisionClassificationCreateModel.java b/automl/src/main/java/com/example/automl/VisionClassificationCreateModel.java new file mode 100644 index 00000000000..ea4da40b1d6 --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionClassificationCreateModel.java @@ -0,0 +1,70 @@ +/* + * 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.example.automl; + +// [START automl_vision_classification_create_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ImageClassificationModelMetadata; +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 VisionClassificationCreateModel { + + public static void main(String[] args) + throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + ImageClassificationModelMetadata metadata = + ImageClassificationModelMetadata.newBuilder().setTrainBudgetMilliNodeHours(24000).build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setImageClassificationModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_vision_classification_create_model] diff --git a/automl/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java b/automl/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java new file mode 100644 index 00000000000..c2d27f3a132 --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.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.example.automl; + +// [START automl_vision_classification_deploy_model_node_count] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.ImageClassificationModelDeploymentMetadata; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VisionClassificationDeployModelNodeCount { + + static void visionClassificationDeployModelNodeCount() + throws InterruptedException, ExecutionException, IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + visionClassificationDeployModelNodeCount(projectId, modelId); + } + + // Deploy a model for prediction with a specified node count (can be used to redeploy a model) + static void visionClassificationDeployModelNodeCount(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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); + ImageClassificationModelDeploymentMetadata metadata = + ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); + DeployModelRequest request = + DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .setImageClassificationModelDeploymentMetadata(metadata) + .build(); + OperationFuture future = client.deployModelAsync(request); + + future.get(); + System.out.println("Model deployment finished"); + } + } +} +// [END automl_vision_classification_deploy_model_node_count] diff --git a/automl/src/main/java/com/example/automl/VisionClassificationPredict.java b/automl/src/main/java/com/example/automl/VisionClassificationPredict.java new file mode 100644 index 00000000000..6f110cf9eff --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionClassificationPredict.java @@ -0,0 +1,70 @@ +/* + * 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.example.automl; + +// [START automl_vision_classification_predict] +import com.google.cloud.automl.v1.AnnotationPayload; +import com.google.cloud.automl.v1.ExamplePayload; +import com.google.cloud.automl.v1.Image; +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.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +class VisionClassificationPredict { + + public static void main(String[] args) throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String filePath = "path_to_local_file.jpg"; + predict(projectId, modelId, filePath); + } + + static void predict(String projectId, String modelId, String filePath) throws IOException { + // 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); + ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath))); + Image image = Image.newBuilder().setImageBytes(content).build(); + ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build(); + PredictRequest predictRequest = + PredictRequest.newBuilder() + .setName(name.toString()) + .setPayload(payload) + .putParams( + "score_threshold", "0.8") // [0.0-1.0] Only produce results higher than this value + .build(); + + PredictResponse response = client.predict(predictRequest); + + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName()); + System.out.format( + "Predicted class score: %.2f\n", annotationPayload.getClassification().getScore()); + } + } + } +} +// [END automl_vision_classification_predict] diff --git a/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java b/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java new file mode 100644 index 00000000000..874099d537d --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java @@ -0,0 +1,71 @@ +/* + * 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.example.automl; + +// [START automl_vision_object_detection_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.ImageObjectDetectionDatasetMetadata; +import com.google.cloud.automl.v1.LocationName; +import com.google.cloud.automl.v1.OperationMetadata; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VisionObjectDetectionCreateDataset { + + static void createDataset() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String displayName = "YOUR_DATASET_NAME"; + createDataset(projectId, displayName); + } + + // Create a dataset + static void createDataset(String projectId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + + ImageObjectDetectionDatasetMetadata metadata = + ImageObjectDetectionDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(displayName) + .setImageObjectDetectionDatasetMetadata(metadata) + .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); + } + } +} +// [END automl_vision_object_detection_create_dataset] diff --git a/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java b/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java new file mode 100644 index 00000000000..2e0ccbc43d9 --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.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.example.automl; + +// [START automl_vision_object_detection_create_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.ImageObjectDetectionModelMetadata; +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 VisionObjectDetectionCreateModel { + + static void createModel() throws IOException, ExecutionException, InterruptedException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String datasetId = "YOUR_DATASET_ID"; + String displayName = "YOUR_DATASET_NAME"; + createModel(projectId, datasetId, displayName); + } + + // Create a model + static void createModel(String projectId, String datasetId, String displayName) + throws IOException, ExecutionException, InterruptedException { + // 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"); + // Set model metadata. + ImageObjectDetectionModelMetadata metadata = + ImageObjectDetectionModelMetadata.newBuilder().build(); + Model model = + Model.newBuilder() + .setDisplayName(displayName) + .setDatasetId(datasetId) + .setImageObjectDetectionModelMetadata(metadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture future = + client.createModelAsync(projectLocation, model); + // OperationFuture.get() will block until the model is created, which may take several hours. + // You can use OperationFuture.getInitialFuture to get a future representing the initial + // response to the request, which contains information while the operation is in progress. + System.out.format("Training operation name: %s\n", future.getInitialFuture().get().getName()); + System.out.println("Training started..."); + } + } +} +// [END automl_vision_object_detection_create_model] diff --git a/automl/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java b/automl/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java new file mode 100644 index 00000000000..42621d3e87f --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.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.example.automl; + +// [START automl_vision_object_detection_deploy_model_node_count] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.ImageObjectDetectionModelDeploymentMetadata; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class VisionObjectDetectionDeployModelNodeCount { + + static void visionObjectDetectionDeployModelNodeCount() + throws InterruptedException, ExecutionException, IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + visionObjectDetectionDeployModelNodeCount(projectId, modelId); + } + + // Deploy a model for prediction with a specified node count (can be used to redeploy a model) + static void visionObjectDetectionDeployModelNodeCount(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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); + ImageObjectDetectionModelDeploymentMetadata metadata = + ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); + DeployModelRequest request = + DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .setImageObjectDetectionModelDeploymentMetadata(metadata) + .build(); + OperationFuture future = client.deployModelAsync(request); + + future.get(); + System.out.println("Model deployment finished"); + } + } +} +// [END automl_vision_object_detection_deploy_model_node_count] diff --git a/automl/src/main/java/com/example/automl/VisionObjectDetectionPredict.java b/automl/src/main/java/com/example/automl/VisionObjectDetectionPredict.java new file mode 100644 index 00000000000..8d7dec6b357 --- /dev/null +++ b/automl/src/main/java/com/example/automl/VisionObjectDetectionPredict.java @@ -0,0 +1,77 @@ +/* + * 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.example.automl; + +// [START automl_vision_object_detection_predict] +import com.google.cloud.automl.v1.AnnotationPayload; +import com.google.cloud.automl.v1.BoundingPoly; +import com.google.cloud.automl.v1.ExamplePayload; +import com.google.cloud.automl.v1.Image; +import com.google.cloud.automl.v1.ModelName; +import com.google.cloud.automl.v1.NormalizedVertex; +import com.google.cloud.automl.v1.PredictRequest; +import com.google.cloud.automl.v1.PredictResponse; +import com.google.cloud.automl.v1.PredictionServiceClient; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +class VisionObjectDetectionPredict { + + static void predict() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "YOUR_PROJECT_ID"; + String modelId = "YOUR_MODEL_ID"; + String filePath = "path_to_local_file.jpg"; + predict(projectId, modelId, filePath); + } + + static void predict(String projectId, String modelId, String filePath) throws IOException { + // 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); + ByteString content = ByteString.copyFrom(Files.readAllBytes(Paths.get(filePath))); + Image image = Image.newBuilder().setImageBytes(content).build(); + ExamplePayload payload = ExamplePayload.newBuilder().setImage(image).build(); + PredictRequest predictRequest = + PredictRequest.newBuilder() + .setName(name.toString()) + .setPayload(payload) + .putParams( + "score_threshold", "0.5") // [0.0-1.0] Only produce results higher than this value + .build(); + + PredictResponse response = client.predict(predictRequest); + for (AnnotationPayload annotationPayload : response.getPayloadList()) { + System.out.format("Predicted class name: %s\n", annotationPayload.getDisplayName()); + System.out.format( + "Predicted class score: %.2f\n", + annotationPayload.getImageObjectDetection().getScore()); + BoundingPoly boundingPoly = annotationPayload.getImageObjectDetection().getBoundingBox(); + System.out.println("Normalized Vertices:"); + for (NormalizedVertex vertex : boundingPoly.getNormalizedVerticesList()) { + System.out.format("\tX: %.2f, Y: %.2f\n", vertex.getX(), vertex.getY()); + } + } + } + } +} +// [END automl_vision_object_detection_predict] diff --git a/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java new file mode 100644 index 00000000000..9853ae39b9c --- /dev/null +++ b/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -0,0 +1,104 @@ +/* + * Copyright 2018 Google Inc. + * + * 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; + +// Imports the Google Cloud client library +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.io.PrintStream; +import java.util.concurrent.ExecutionException; +import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.inf.ArgumentParser; +import net.sourceforge.argparse4j.inf.ArgumentParserException; +import net.sourceforge.argparse4j.inf.Namespace; +import net.sourceforge.argparse4j.inf.Subparser; +import net.sourceforge.argparse4j.inf.Subparsers; + +/** + * Google Cloud AutoML Translate API sample application. Example usage: mvn package exec:java + * -Dexec.mainClass ='com.google.cloud.translate.samples.DatasetAPI' -Dexec.args='create_dataset + * test_dataset' + */ +public class DatasetApi { + + // [START automl_translate_import_data] + /** + * Import sentence pairs to the dataset. + * + * @param projectId the Google Cloud Project ID. + * @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. + */ + public static void importData( + String projectId, String computeRegion, String datasetId, String path) + throws IOException, InterruptedException, ExecutionException { + // Instantiates a client + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + + 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); + } + + // 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)); + } + } + // [END automl_translate_import_data] + + public static void main(String[] args) throws Exception { + DatasetApi datasetApi = new DatasetApi(); + datasetApi.argsHelper(args, System.out); + } + + public static void argsHelper(String[] args, PrintStream out) throws Exception { + ArgumentParser parser = ArgumentParsers.newFor("").build(); + Subparsers subparsers = parser.addSubparsers().dest("command"); + + Subparser importDataParser = subparsers.addParser("import_data"); + importDataParser.addArgument("datasetId"); + importDataParser.addArgument("path"); + + String projectId = System.getenv("PROJECT_ID"); + String computeRegion = System.getenv("REGION_NAME"); + + Namespace ns; + try { + ns = parser.parseArgs(args); + if (ns.get("command").equals("import_data")) { + importData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("path")); + } + } catch (ArgumentParserException e) { + parser.handleError(e); + } + } +} diff --git a/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java b/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.java new file mode 100644 index 00000000000..63da52ead0d --- /dev/null +++ b/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModel.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.vision.samples.automl; + +// [START automl_vision_classification_deploy_model] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DeployModelRequest; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class ClassificationDeployModel { + + // Deploy a model + static void classificationDeployModel(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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); + + // Build deploy model request. + DeployModelRequest deployModelRequest = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + + // Deploy a model with the deploy model request. + OperationFuture future = + client.deployModelAsync(deployModelRequest); + + future.get(); + + // Display the deployment details of model. + System.out.println("Model deployment finished"); + } + } +} +// [END automl_vision_classification_deploy_model] diff --git a/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java b/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java new file mode 100644 index 00000000000..655cd7218c9 --- /dev/null +++ b/automl/src/main/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelNodeCount.java @@ -0,0 +1,61 @@ +/* + * 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.vision.samples.automl; + +// [START automl_vision_classification_deploy_model_node_count] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DeployModelRequest; +import com.google.cloud.automl.v1beta1.ImageClassificationModelDeploymentMetadata; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class ClassificationDeployModelNodeCount { + + // Deploy a model with a specified node count + static void classificationDeployModelNodeCount(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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); + + // Set how many nodes the model is deployed on + ImageClassificationModelDeploymentMetadata deploymentMetadata = + ImageClassificationModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); + + DeployModelRequest request = + DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .setImageClassificationModelDeploymentMetadata(deploymentMetadata) + .build(); + // Deploy the model + OperationFuture future = client.deployModelAsync(request); + future.get(); + System.out.println("Model deployment on 2 nodes finished"); + } + } +} +// [END automl_vision_classification_deploy_model_node_count] diff --git a/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java b/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.java new file mode 100644 index 00000000000..cd6de5c5bcc --- /dev/null +++ b/automl/src/main/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCount.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.vision.samples.automl; + +// [START automl_vision_object_detection_deploy_model_node_count] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DeployModelRequest; +import com.google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata; +import com.google.cloud.automl.v1beta1.ModelName; +import com.google.cloud.automl.v1beta1.OperationMetadata; +import com.google.protobuf.Empty; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class ObjectDetectionDeployModelNodeCount { + + static void objectDetectionDeployModelNodeCount(String projectId, String modelId) + throws IOException, ExecutionException, InterruptedException { + // 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); + + // Set how many nodes the model is deployed on + ImageObjectDetectionModelDeploymentMetadata deploymentMetadata = + ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); + + DeployModelRequest request = + DeployModelRequest.newBuilder() + .setName(modelFullId.toString()) + .setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata) + .build(); + // Deploy the model + OperationFuture future = client.deployModelAsync(request); + future.get(); + System.out.println("Model deployment on 2 nodes finished"); + } + } +} +// [END automl_vision_object_detection_deploy_model_node_count] diff --git a/automl/src/test/java/beta/automl/BatchPredictTest.java b/automl/src/test/java/beta/automl/BatchPredictTest.java new file mode 100644 index 00000000000..96c783b6562 --- /dev/null +++ b/automl/src/test/java/beta/automl/BatchPredictTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class BatchPredictTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String MODEL_ID = "VCN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testBatchPredict() { + // As batch prediction can take a long time. Try to batch predict on a model and confirm that + // the model was not found, but other elements of the request were valid. + try { + String inputUri = String.format("gs://%s/entity-extraction/input.csv", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + BatchPredict.batchPredict(PROJECT_ID, MODEL_ID, inputUri, outputUri); + String got = bout.toString(); + assertThat(got).contains("does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/CancelOperationTest.java b/automl/src/test/java/beta/automl/CancelOperationTest.java new file mode 100644 index 00000000000..2a3d9ab7088 --- /dev/null +++ b/automl/src/test/java/beta/automl/CancelOperationTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import io.grpc.StatusRuntimeException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CancelOperationTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testCancelOperation() throws IOException { + String operationFullPathId = + String.format( + "projects/%s/locations/%s/operations/%s", PROJECT_ID, "us-central1", "TCN0000000000"); + // Any cancelled operation on models or datasets will be hidden once the operations are flagged + // as failed operations + // which makes them hard to delete in the teardown. + try { + CancelOperation.cancelOperation(operationFullPathId); + String got = bout.toString(); + assertThat(got).contains("not found"); + } catch (NotFoundException | StatusRuntimeException | InterruptedException e) { + assertThat(e.getMessage()).contains("not found"); + } + } +} diff --git a/automl/src/test/java/beta/automl/DeleteDatasetTest.java b/automl/src/test/java/beta/automl/DeleteDatasetTest.java new file mode 100644 index 00000000000..5f98603ba2c --- /dev/null +++ b/automl/src/test/java/beta/automl/DeleteDatasetTest.java @@ -0,0 +1,102 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.Dataset; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.cloud.automl.v1beta1.TextExtractionDatasetMetadata; +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException { + // Create a fake dataset to be deleted + // 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)); + try (AutoMlClient client = AutoMlClient.create()) { + LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); + TextExtractionDatasetMetadata metadata = TextExtractionDatasetMetadata.newBuilder().build(); + Dataset dataset = + Dataset.newBuilder() + .setDisplayName(datasetName) + .setTextExtractionDatasetMetadata(metadata) + .build(); + Dataset createdDataset = client.createDataset(projectLocation, dataset); + String[] names = createdDataset.getName().split("/"); + datasetId = names[names.length - 1]; + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteDataset() throws IOException, ExecutionException, InterruptedException { + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + String got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } +} diff --git a/automl/src/test/java/beta/automl/DeleteModelTest.java b/automl/src/test/java/beta/automl/DeleteModelTest.java new file mode 100644 index 00000000000..fe03799dced --- /dev/null +++ b/automl/src/test/java/beta/automl/DeleteModelTest.java @@ -0,0 +1,83 @@ +/* + * 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeleteModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteModel() { + // As model creation can take many hours, instead try to delete a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeleteModel.deleteModel(PROJECT_ID, "TRL0000000000000000000"); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/DeployModelTest.java b/automl/src/test/java/beta/automl/DeployModelTest.java new file mode 100644 index 00000000000..144d6aa019e --- /dev/null +++ b/automl/src/test/java/beta/automl/DeployModelTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeployModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/GetModelEvaluationTest.java b/automl/src/test/java/beta/automl/GetModelEvaluationTest.java new file mode 100644 index 00000000000..42a00f2b192 --- /dev/null +++ b/automl/src/test/java/beta/automl/GetModelEvaluationTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetModelEvaluationTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private String modelEvaluationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException { + // Get a model evaluation ID from the List request first to be used in the Get call + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder().setParent(modelFullId.toString()).build(); + ModelEvaluation modelEvaluation = + client + .listModelEvaluations(modelEvaluationsrequest) + .getPage() + .getValues() + .iterator() + .next(); + modelEvaluationId = modelEvaluation.getName().split("/modelEvaluations/")[1]; + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModelEvaluation() throws IOException { + GetModelEvaluation.getModelEvaluation(PROJECT_ID, MODEL_ID, modelEvaluationId); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/src/test/java/beta/automl/GetModelTest.java b/automl/src/test/java/beta/automl/GetModelTest.java new file mode 100644 index 00000000000..7b80ec29106 --- /dev/null +++ b/automl/src/test/java/beta/automl/GetModelTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModel() throws IOException { + GetModel.getModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model id: " + MODEL_ID); + } +} diff --git a/automl/src/test/java/beta/automl/GetOperationStatusTest.java b/automl/src/test/java/beta/automl/GetOperationStatusTest.java new file mode 100644 index 00000000000..7f63f1ca26b --- /dev/null +++ b/automl/src/test/java/beta/automl/GetOperationStatusTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.LocationName; +import com.google.longrunning.ListOperationsRequest; +import com.google.longrunning.Operation; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetOperationStatusTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private String operationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException { + // Use list operations to get a single operation id for the get call. + try (AutoMlClient client = AutoMlClient.create()) { + LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); + ListOperationsRequest request = + ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build(); + Operation operation = + client.getOperationsClient().listOperations(request).iterateAll().iterator().next(); + operationId = operation.getName(); + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetOperationStatus() throws IOException { + GetOperationStatus.getOperationStatus(operationId); + String got = bout.toString(); + assertThat(got).contains("Operation details:"); + } +} diff --git a/automl/src/test/java/beta/automl/ImportDatasetTest.java b/automl/src/test/java/beta/automl/ImportDatasetTest.java new file mode 100644 index 00000000000..12683878543 --- /dev/null +++ b/automl/src/test/java/beta/automl/ImportDatasetTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ImportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private String datasetId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testImportDataset() throws TimeoutException { + try { + ImportDataset.importDataset( + PROJECT_ID, "TCN0000000000", BUCKET + "/entity-extraction/dataset.csv"); + String got = bout.toString(); + assertThat(got).contains("doesn't exist"); + } catch (NotFoundException | IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("doesn't exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/ListDatasetsTest.java b/automl/src/test/java/beta/automl/ListDatasetsTest.java new file mode 100644 index 00000000000..62bdf614439 --- /dev/null +++ b/automl/src/test/java/beta/automl/ListDatasetsTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ListDatasetsTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListDataset() throws IOException { + ListDatasets.listDatasets(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } +} diff --git a/automl/src/test/java/beta/automl/ListModelEvaluationsTest.java b/automl/src/test/java/beta/automl/ListModelEvaluationsTest.java new file mode 100644 index 00000000000..d8c4cb6ac38 --- /dev/null +++ b/automl/src/test/java/beta/automl/ListModelEvaluationsTest.java @@ -0,0 +1,77 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListModelEvaluationsTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListModelEvaluations() throws IOException { + ListModelEvaluations.listModelEvaluations(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/src/test/java/beta/automl/ListModelsTest.java b/automl/src/test/java/beta/automl/ListModelsTest.java new file mode 100644 index 00000000000..6b70440d600 --- /dev/null +++ b/automl/src/test/java/beta/automl/ListModelsTest.java @@ -0,0 +1,78 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListModelsTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + // Skipping this test until backend is cleaned up. + // https://github.com/googleapis/java-automl/issues/291 + @Test + @Ignore + public void testListModels() throws IOException { + ListModels.listModels(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Model id:"); + } +} diff --git a/automl/src/test/java/beta/automl/SetEndpointIT.java b/automl/src/test/java/beta/automl/SetEndpointIT.java new file mode 100644 index 00000000000..b74742e713e --- /dev/null +++ b/automl/src/test/java/beta/automl/SetEndpointIT.java @@ -0,0 +1,80 @@ +/* + * 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for Automl Set Endpoint */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class SetEndpointIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testSetEndpoint() throws IOException { + // Act + SetEndpoint.setEndpoint(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("display_name: \"do_not_delete_eu\""); + } +} diff --git a/automl/src/test/java/beta/automl/TablesBatchPredictBigQueryTest.java b/automl/src/test/java/beta/automl/TablesBatchPredictBigQueryTest.java new file mode 100644 index 00000000000..173564c13b9 --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesBatchPredictBigQueryTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesBatchPredictBigQueryTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TBL0000000000000000000"; + private static final String INPUT_URI = + String.format( + "bq://%s.automl_do_not_delete_predict_test.automl_predict_test_table", PROJECT_ID); + private static final String OUTPUT_URI = "bq://" + PROJECT_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesBigQueryBatchPredict() { + // As batch prediction can take a long time. Try to batch predict on a model and confirm that + // the model was not found, but other elements of the request were valid. + try { + TablesBatchPredictBigQuery.batchPredict(PROJECT_ID, MODEL_ID, INPUT_URI, OUTPUT_URI); + String got = bout.toString(); + assertThat(got).contains("does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/TablesCreateDatasetTest.java b/automl/src/test/java/beta/automl/TablesCreateDatasetTest.java new file mode 100644 index 00000000000..d5aab148f6a --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesCreateDatasetTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesCreateDataset() + 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)); + TablesCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/beta/automl/TablesCreateModelTest.java b/automl/src/test/java/beta/automl/TablesCreateModelTest.java new file mode 100644 index 00000000000..996daae231f --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesCreateModelTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "TBL00000000000000000000"; + private static final String TABLE_SPEC_ID = "3172574831249981440"; + private static final String COLUMN_SPEC_ID = "3224682886313541632"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesCreateModel() throws IOException, ExecutionException, InterruptedException { + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + TablesCreateModel.createModel( + PROJECT_ID, DATASET_ID, TABLE_SPEC_ID, COLUMN_SPEC_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/TablesGetModelTest.java b/automl/src/test/java/beta/automl/TablesGetModelTest.java new file mode 100644 index 00000000000..dd76b0646fe --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesGetModelTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class TablesGetModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TBL7473655411900416000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModel() throws IOException { + GetModel.getModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model id: " + MODEL_ID); + } +} diff --git a/automl/src/test/java/beta/automl/TablesImportDatasetTest.java b/automl/src/test/java/beta/automl/TablesImportDatasetTest.java new file mode 100644 index 00000000000..c6c52f37563 --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesImportDatasetTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesImportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesImportDataset() { + try { + TablesImportDataset.importDataset( + PROJECT_ID, "TEN0000000000000000000", "gs://cloud-ml-tables-data/bank-marketing.csv"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()) + .contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } + } +} diff --git a/automl/src/test/java/beta/automl/TablesPredictTest.java b/automl/src/test/java/beta/automl/TablesPredictTest.java new file mode 100644 index 00000000000..0abb04b2e8d --- /dev/null +++ b/automl/src/test/java/beta/automl/TablesPredictTest.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +import com.google.protobuf.Value; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TablesPredictTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TBL7972827093840953344"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testTablesPredict() throws IOException { + List values = new ArrayList<>(); + values.add(Value.newBuilder().setNumberValue(39).build()); // Age + values.add(Value.newBuilder().setStringValue("technician").build()); // Job + values.add(Value.newBuilder().setStringValue("married").build()); // MaritalStatus + values.add(Value.newBuilder().setStringValue("secondary").build()); // Education + values.add(Value.newBuilder().setStringValue("no").build()); // Default + values.add(Value.newBuilder().setNumberValue(52).build()); // Balance + values.add(Value.newBuilder().setStringValue("no").build()); // Housing + values.add(Value.newBuilder().setStringValue("no").build()); // Loan + values.add(Value.newBuilder().setStringValue("cellular").build()); // Contact + values.add(Value.newBuilder().setNumberValue(12).build()); // Day + values.add(Value.newBuilder().setStringValue("aug").build()); // Month + values.add(Value.newBuilder().setNumberValue(96).build()); // Duration + values.add(Value.newBuilder().setNumberValue(2).build()); // Campaign + values.add(Value.newBuilder().setNumberValue(-1).build()); // PDays + values.add(Value.newBuilder().setNumberValue(0).build()); // Previous + values.add(Value.newBuilder().setStringValue("unknown").build()); // POutcome + + TablesPredict.predict(PROJECT_ID, MODEL_ID, values); + + String got = bout.toString(); + assertThat(got).contains("Prediction results:"); + } +} diff --git a/automl/src/test/java/beta/automl/UndeployModelTest.java b/automl/src/test/java/beta/automl/UndeployModelTest.java new file mode 100644 index 00000000000..4ec06dabfd3 --- /dev/null +++ b/automl/src/test/java/beta/automl/UndeployModelTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class UndeployModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testUndeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/VideoClassificationCreateDatasetTest.java b/automl/src/test/java/beta/automl/VideoClassificationCreateDatasetTest.java new file mode 100644 index 00000000000..a29435f2846 --- /dev/null +++ b/automl/src/test/java/beta/automl/VideoClassificationCreateDatasetTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoClassificationCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId); + client.deleteDatasetAsync(datasetFullId).get(); + } + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + VideoClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/beta/automl/VideoClassificationCreateModelTest.java b/automl/src/test/java/beta/automl/VideoClassificationCreateModelTest.java new file mode 100644 index 00000000000..233a6af230c --- /dev/null +++ b/automl/src/test/java/beta/automl/VideoClassificationCreateModelTest.java @@ -0,0 +1,90 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoClassificationCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "VCN00000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testVisionClassificationCreateModel() + throws IOException, ExecutionException, InterruptedException { + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/beta/automl/VideoObjectTrackingCreateDatasetTest.java b/automl/src/test/java/beta/automl/VideoObjectTrackingCreateDatasetTest.java new file mode 100644 index 00000000000..db458e5cee8 --- /dev/null +++ b/automl/src/test/java/beta/automl/VideoObjectTrackingCreateDatasetTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.DatasetName; +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoObjectTrackingCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + try (AutoMlClient client = AutoMlClient.create()) { + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(PROJECT_ID, "us-central1", datasetId); + client.deleteDatasetAsync(datasetFullId).get(); + } + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + VideoObjectTrackingCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/beta/automl/VideoObjectTrackingCreateModelTest.java b/automl/src/test/java/beta/automl/VideoObjectTrackingCreateModelTest.java new file mode 100644 index 00000000000..04df91b367d --- /dev/null +++ b/automl/src/test/java/beta/automl/VideoObjectTrackingCreateModelTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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 beta.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VideoObjectTrackingCreateModelTest { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String DATASET_ID = "VOT0000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static String requireEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + "Environment variable " + varName + " is required to perform these tests.", + System.getenv(varName)); + return value; + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testVisionClassificationCreateModel() + throws IOException, ExecutionException, InterruptedException { + + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VideoObjectTrackingCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/BatchPredictTest.java b/automl/src/test/java/com/example/automl/BatchPredictTest.java new file mode 100644 index 00000000000..29dfa3305d6 --- /dev/null +++ b/automl/src/test/java/com/example/automl/BatchPredictTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class BatchPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testBatchPredict() { + // As batch prediction can take a long time. Try to batch predict on a model and confirm that + // the model was not found, but other elements of the request were valid. + try { + String inputUri = String.format("gs://%s/entity-extraction/input.jsonl", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + BatchPredict.batchPredict(PROJECT_ID, MODEL_ID, inputUri, outputUri); + String got = bout.toString(); + assertThat(got).contains("does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/DeleteDatasetTest.java b/automl/src/test/java/com/example/automl/DeleteDatasetTest.java new file mode 100644 index 00000000000..fd4f96171dd --- /dev/null +++ b/automl/src/test/java/com/example/automl/DeleteDatasetTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class DeleteDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws InterruptedException, ExecutionException, IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Create a fake dataset to be deleted + // 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)); + LanguageEntityExtractionCreateDataset.createDataset(PROJECT_ID, datasetName); + String got = bout.toString(); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteDataset() throws IOException, ExecutionException, InterruptedException { + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + String got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } +} diff --git a/automl/src/test/java/com/example/automl/DeleteModelTest.java b/automl/src/test/java/com/example/automl/DeleteModelTest.java new file mode 100644 index 00000000000..96b5c59fff3 --- /dev/null +++ b/automl/src/test/java/com/example/automl/DeleteModelTest.java @@ -0,0 +1,80 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeleteModelTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeleteModel() { + // As model creation can take many hours, instead try to delete a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeleteModel.deleteModel(PROJECT_ID, "TRL0000000000000000000"); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/DeployModelTest.java b/automl/src/test/java/com/example/automl/DeployModelTest.java new file mode 100644 index 00000000000..5ad7f560778 --- /dev/null +++ b/automl/src/test/java/com/example/automl/DeployModelTest.java @@ -0,0 +1,81 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeployModelTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/ExportDatasetTest.java b/automl/src/test/java/com/example/automl/ExportDatasetTest.java new file mode 100644 index 00000000000..6dc3a7ed65e --- /dev/null +++ b/automl/src/test/java/com/example/automl/ExportDatasetTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ExportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TEN0000000000000000000"; + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_DATASET_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + // As exporting a dataset can take a long time and only one operation can be run on a dataset + // at once. Try to export a nonexistent dataset and confirm that the dataset was not found, but + // other elements of the request were valid. + try { + ExportDataset.exportDataset(PROJECT_ID, DATASET_ID, BUCKET + "/TEST_EXPORT_OUTPUT/"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()) + .contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); + } + } +} diff --git a/automl/src/test/java/com/example/automl/GetDatasetTest.java b/automl/src/test/java/com/example/automl/GetDatasetTest.java new file mode 100644 index 00000000000..50629da4f48 --- /dev/null +++ b/automl/src/test/java/com/example/automl/GetDatasetTest.java @@ -0,0 +1,76 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class GetDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = System.getenv("ENTITY_EXTRACTION_DATASET_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_DATASET_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetDataset() throws IOException { + GetDataset.getDataset(PROJECT_ID, DATASET_ID); + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } +} diff --git a/automl/src/test/java/com/example/automl/GetModelEvaluationTest.java b/automl/src/test/java/com/example/automl/GetModelEvaluationTest.java new file mode 100644 index 00000000000..74e4f16781d --- /dev/null +++ b/automl/src/test/java/com/example/automl/GetModelEvaluationTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetModelEvaluationTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private String modelEvaluationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // Get a model evaluation ID from the List request first to be used in the Get call + ListModelEvaluations.listModelEvaluations(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + modelEvaluationId = got.split(MODEL_ID + "/modelEvaluations/")[1].split("\n")[0]; + assertThat(got).contains("Model Evaluation Name:"); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModelEvaluation() throws IOException { + GetModelEvaluation.getModelEvaluation(PROJECT_ID, MODEL_ID, modelEvaluationId); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/src/test/java/com/example/automl/GetModelTest.java b/automl/src/test/java/com/example/automl/GetModelTest.java new file mode 100644 index 00000000000..dc375d88ad3 --- /dev/null +++ b/automl/src/test/java/com/example/automl/GetModelTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetModelTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetModel() throws IOException { + GetModel.getModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model id: " + MODEL_ID); + } +} diff --git a/automl/src/test/java/com/example/automl/GetOperationStatusTest.java b/automl/src/test/java/com/example/automl/GetOperationStatusTest.java new file mode 100644 index 00000000000..bc233a9a602 --- /dev/null +++ b/automl/src/test/java/com/example/automl/GetOperationStatusTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetOperationStatusTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private String operationId; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + ListOperationStatus.listOperationStatus(PROJECT_ID); + String got = bout.toString(); + operationId = got.split("\n")[1].split(":")[1].trim(); + assertThat(got).contains("Operation details:"); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testGetOperationStatus() throws IOException { + GetOperationStatus.getOperationStatus(operationId); + String got = bout.toString(); + assertThat(got).contains("Operation details:"); + } +} diff --git a/automl/src/test/java/com/example/automl/ImportDatasetTest.java b/automl/src/test/java/com/example/automl/ImportDatasetTest.java new file mode 100644 index 00000000000..727df922dd7 --- /dev/null +++ b/automl/src/test/java/com/example/automl/ImportDatasetTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.rpc.NotFoundException; +import io.grpc.StatusRuntimeException; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ImportDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TEN0000000000000000000"; + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testImportDataset() throws InterruptedException, TimeoutException, IOException { + + try { + ImportDataset.importDataset( + PROJECT_ID, DATASET_ID, BUCKET + "/entity-extraction/dataset.csv"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist "); + } catch (NotFoundException | ExecutionException | StatusRuntimeException ex) { + assertThat(ex.getMessage()).contains("The Dataset doesn't exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java b/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java new file mode 100644 index 00000000000..decbfeb36d0 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateDatasetTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageEntityExtractionCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + LanguageEntityExtractionCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java b/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java new file mode 100644 index 00000000000..21baebcc927 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageEntityExtractionCreateModelTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageEntityExtractionCreateModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testLanguageEntityExtractionCreateModel() { + // As entity extraction does not let you cancel model creation, instead try to create a model + // from a nonexistent dataset, but other elements of the request were valid. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + LanguageEntityExtractionCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java b/automl/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java new file mode 100644 index 00000000000..bf553f4b3d5 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageEntityExtractionPredictTest.java @@ -0,0 +1,94 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageEntityExtractionPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("GOOGLE_CLOUD_PROJECT"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + String text = "Constitutional mutations in the WT1 gene in patients with Denys-Drash syndrome."; + LanguageEntityExtractionPredict.predict(PROJECT_ID, MODEL_ID, text); + String got = bout.toString(); + assertThat(got).contains("Text Extract Entity Type:"); + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java new file mode 100644 index 00000000000..7e065772946 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateDatasetTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageSentimentAnalysisCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + LanguageSentimentAnalysisCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java new file mode 100644 index 00000000000..8d28cf81076 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisCreateModelTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageSentimentAnalysisCreateModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TST00000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testLanguageSentimentAnalysisCreateModel() { + // Create a model from a nonexistent dataset. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + LanguageSentimentAnalysisCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java new file mode 100644 index 00000000000..665712719ce --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictTest.java @@ -0,0 +1,93 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageSentimentAnalysisPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("SENTIMENT_ANALYSIS_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("SENTIMENT_ANALYSIS_MODEL_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + String text = "Hopefully this Claritin kicks in soon"; + LanguageSentimentAnalysisPredict.predict(PROJECT_ID, MODEL_ID, text); + String got = bout.toString(); + assertThat(got).contains("Predicted sentiment score:"); + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java b/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java new file mode 100644 index 00000000000..b4c43c51956 --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateDatasetTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageTextClassificationCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + LanguageTextClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java b/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java new file mode 100644 index 00000000000..14bd5098d4c --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageTextClassificationCreateModelTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageTextClassificationCreateModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TCN00000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testLanguageTextClassificationCreateModel() { + // Create a model from a nonexistent dataset. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + LanguageTextClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java b/automl/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java new file mode 100644 index 00000000000..f27234e74dc --- /dev/null +++ b/automl/src/test/java/com/example/automl/LanguageTextClassificationPredictTest.java @@ -0,0 +1,93 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageTextClassificationPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("TEXT_CLASSIFICATION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("TEXT_CLASSIFICATION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + String text = "Fruit and nut flavour"; + LanguageTextClassificationPredict.predict(PROJECT_ID, MODEL_ID, text); + String got = bout.toString(); + assertThat(got).contains("Predicted class name:"); + } +} diff --git a/automl/src/test/java/com/example/automl/ListDatasetsTest.java b/automl/src/test/java/com/example/automl/ListDatasetsTest.java new file mode 100644 index 00000000000..df2a91892f4 --- /dev/null +++ b/automl/src/test/java/com/example/automl/ListDatasetsTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ListDatasetsTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListDataset() throws IOException { + ListDatasets.listDatasets(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } +} diff --git a/automl/src/test/java/com/example/automl/ListModelEvaluationsTest.java b/automl/src/test/java/com/example/automl/ListModelEvaluationsTest.java new file mode 100644 index 00000000000..7d7d08e31dd --- /dev/null +++ b/automl/src/test/java/com/example/automl/ListModelEvaluationsTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListModelEvaluationsTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("ENTITY_EXTRACTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("ENTITY_EXTRACTION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListModelEvaluations() throws IOException { + ListModelEvaluations.listModelEvaluations(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } +} diff --git a/automl/src/test/java/com/example/automl/ListModelsTest.java b/automl/src/test/java/com/example/automl/ListModelsTest.java new file mode 100644 index 00000000000..df6c986bf2c --- /dev/null +++ b/automl/src/test/java/com/example/automl/ListModelsTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListModelsTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testListModels() throws IOException { + ListModels.listModels(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Model id:"); + } +} diff --git a/automl/src/test/java/com/example/automl/ListOperationStatusTest.java b/automl/src/test/java/com/example/automl/ListOperationStatusTest.java new file mode 100644 index 00000000000..5a4779a42f8 --- /dev/null +++ b/automl/src/test/java/com/example/automl/ListOperationStatusTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.client.util.ExponentialBackOff; +import com.google.api.gax.rpc.ResourceExhaustedException; +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 com.google.longrunning.OperationsClient; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListOperationStatusTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() throws IOException, InterruptedException { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + + // if the LRO status count more than 300, delete half of operations. + try (AutoMlClient client = AutoMlClient.create()) { + OperationsClient operationsClient = client.getOperationsClient(); + LocationName projectLocation = LocationName.of(PROJECT_ID, "us-central1"); + ListOperationsRequest listRequest = + ListOperationsRequest.newBuilder().setName(projectLocation.toString()).build(); + List operationFullPathsToBeDeleted = new ArrayList<>(); + for (Operation operation : operationsClient.listOperations(listRequest).iterateAll()) { + // collect unused operation into the list. + // Filter: deleting already done operations. + if (operation.getDone() && !operation.hasError()) { + operationFullPathsToBeDeleted.add(operation.getName()); + } + } + + if (operationFullPathsToBeDeleted.size() > 300) { + System.out.println("Cleaning up..."); + + for (String operationFullPath : + operationFullPathsToBeDeleted.subList(0, operationFullPathsToBeDeleted.size() / 2)) { + // retry_interval * (random value in range [1 - rand_factor, 1 + rand_factor]) + ExponentialBackOff exponentialBackOff = + new ExponentialBackOff.Builder() + .setInitialIntervalMillis(60000) + .setMaxElapsedTimeMillis(300000) + .setRandomizationFactor(0.5) + .setMultiplier(1.1) + .setMaxIntervalMillis(80000) + .build(); + + // delete unused operations. + try { + operationsClient.deleteOperation(operationFullPath); + } catch (ResourceExhaustedException ex) { + // exponential back off and retry. + long backOffInMillis = exponentialBackOff.nextBackOffMillis(); + System.out.printf( + "Backing off for %d milliseconds " + "due to Resource exhaustion.\n", + backOffInMillis); + if (backOffInMillis < 0) { + break; + } + System.out.println("Backing off" + backOffInMillis); + TimeUnit.MILLISECONDS.sleep(backOffInMillis); + } catch (Exception ex) { + throw ex; + } + } + } else { + // Clear the list since we wont anything with the list. + operationFullPathsToBeDeleted.clear(); + } + } + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testOperationStatus() throws IOException { + ListOperationStatus.listOperationStatus(PROJECT_ID); + String got = bout.toString(); + assertThat(got).contains("Operation details:"); + } +} diff --git a/automl/src/test/java/com/example/automl/TranslateCreateDatasetTest.java b/automl/src/test/java/com/example/automl/TranslateCreateDatasetTest.java new file mode 100644 index 00000000000..6fd75eff945 --- /dev/null +++ b/automl/src/test/java/com/example/automl/TranslateCreateDatasetTest.java @@ -0,0 +1,89 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.testing.junit4.MultipleAttemptsRule; +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; +import org.junit.BeforeClass; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TranslateCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String got; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Rule public MultipleAttemptsRule multipleAttemptsRule = new MultipleAttemptsRule(3); + + @Test + public void testCreateDataset() 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)); + TranslateCreateDataset.createDataset(PROJECT_ID, datasetName); + + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } +} diff --git a/automl/src/test/java/com/example/automl/TranslateCreateModelTest.java b/automl/src/test/java/com/example/automl/TranslateCreateModelTest.java new file mode 100644 index 00000000000..65ce99331c7 --- /dev/null +++ b/automl/src/test/java/com/example/automl/TranslateCreateModelTest.java @@ -0,0 +1,88 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +// Tests for Automl translation models. +@RunWith(JUnit4.class) +public class TranslateCreateModelTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "TRL00000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testTranslateCreateModel() { + // Create a model from a nonexistent dataset. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + TranslateCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + // After setting DATASET_ID, change line below to + // assertThat(got).contains("Training started..."); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + // After setting DATASET_ID, change line below to + // assertThat(e.getMessage()).contains("Training started..."); + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/TranslatePredictTest.java b/automl/src/test/java/com/example/automl/TranslatePredictTest.java new file mode 100644 index 00000000000..8713ab99351 --- /dev/null +++ b/automl/src/test/java/com/example/automl/TranslatePredictTest.java @@ -0,0 +1,80 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +// Tests for translation "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class TranslatePredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String modelId = System.getenv("TRANSLATION_MODEL_ID"); + private static final String filePath = "./resources/input.txt"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("TRANSLATION_MODEL_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + // Act + TranslatePredict.predict(PROJECT_ID, modelId, filePath); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Translated Content"); + } +} diff --git a/automl/src/test/java/com/example/automl/UndeployModelTest.java b/automl/src/test/java/com/example/automl/UndeployModelTest.java new file mode 100644 index 00000000000..02d480a673a --- /dev/null +++ b/automl/src/test/java/com/example/automl/UndeployModelTest.java @@ -0,0 +1,81 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class UndeployModelTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "TEN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testUndeployModel() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java b/automl/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java new file mode 100644 index 00000000000..bf8b759cb2b --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionClassificationCreateDatasetTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionClassificationCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + VisionClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java b/automl/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java new file mode 100644 index 00000000000..1fac2cccb66 --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionClassificationCreateModelTest.java @@ -0,0 +1,86 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionClassificationCreateModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "ICN000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String operationId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testVisionClassificationCreateModel() { + // Create a model from a nonexistent dataset. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VisionClassificationCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java b/automl/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java new file mode 100644 index 00000000000..2414c00a51b --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionClassificationDeployModelNodeCountTest.java @@ -0,0 +1,82 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class VisionClassificationDeployModelNodeCountTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "ICN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeployModelWithNodeCount() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + VisionClassificationDeployModelNodeCount.visionClassificationDeployModelNodeCount( + PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/VisionClassificationPredictTest.java b/automl/src/test/java/com/example/automl/VisionClassificationPredictTest.java new file mode 100644 index 00000000000..e4c3af69a4f --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionClassificationPredictTest.java @@ -0,0 +1,93 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionClassificationPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("VISION_CLASSIFICATION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("VISION_CLASSIFICATION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + String filePath = "resources/test.png"; + VisionClassificationPredict.predict(PROJECT_ID, MODEL_ID, filePath); + String got = bout.toString(); + assertThat(got).contains("Predicted class name:"); + } +} diff --git a/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java b/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java new file mode 100644 index 00000000000..20e6e1c5a47 --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateDatasetTest.java @@ -0,0 +1,84 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionObjectDetectionCreateDatasetTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() throws InterruptedException, ExecutionException, IOException { + // Delete the created dataset + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateDataset() 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)); + VisionObjectDetectionCreateDataset.createDataset(PROJECT_ID, datasetName); + + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + } +} diff --git a/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java b/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java new file mode 100644 index 00000000000..3bd4228f8f8 --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionObjectDetectionCreateModelTest.java @@ -0,0 +1,85 @@ +/* + * Copyright 2020 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionObjectDetectionCreateModelTest { + + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String DATASET_ID = "IOD0000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(originalPrintStream); + } + + @Test + public void testVisionObjectDetectionCreateModel() { + // Create a model from a nonexistent dataset. + try { + // 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 modelName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + VisionObjectDetectionCreateModel.createModel(PROJECT_ID, DATASET_ID, modelName); + String got = bout.toString(); + assertThat(got).contains("Dataset does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("Dataset does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java b/automl/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java new file mode 100644 index 00000000000..b218d2e59fb --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionObjectDetectionDeployModelNodeCountTest.java @@ -0,0 +1,82 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class VisionObjectDetectionDeployModelNodeCountTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = "0000000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testDeployModelWithNodeCount() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + VisionObjectDetectionDeployModelNodeCount.visionObjectDetectionDeployModelNodeCount( + PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java b/automl/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java new file mode 100644 index 00000000000..1580c94b39b --- /dev/null +++ b/automl/src/test/java/com/example/automl/VisionObjectDetectionPredictTest.java @@ -0,0 +1,94 @@ +/* + * 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.example.automl; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.cloud.automl.v1.AutoMlClient; +import com.google.cloud.automl.v1.DeployModelRequest; +import com.google.cloud.automl.v1.Model; +import com.google.cloud.automl.v1.ModelName; +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.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionObjectDetectionPredictTest { + private static final String PROJECT_ID = System.getenv("AUTOML_PROJECT_ID"); + private static final String MODEL_ID = System.getenv("OBJECT_DETECTION_MODEL_ID"); + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + + private static void requireEnvVar(String varName) { + assertNotNull( + System.getenv(varName), + "Environment variable '%s' is required to perform these tests.".format(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("AUTOML_PROJECT_ID"); + requireEnvVar("OBJECT_DETECTION_MODEL_ID"); + } + + @Before + public void setUp() throws IOException, ExecutionException, InterruptedException { + // Verify that the model is deployed for prediction + try (AutoMlClient client = AutoMlClient.create()) { + ModelName modelFullId = ModelName.of(PROJECT_ID, "us-central1", MODEL_ID); + Model model = client.getModel(modelFullId); + if (model.getDeploymentState() == Model.DeploymentState.UNDEPLOYED) { + // Deploy the model if not deployed + DeployModelRequest request = + DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); + client.deployModelAsync(request).get(); + } + } + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testPredict() throws IOException { + String filePath = "resources/salad.jpg"; + VisionObjectDetectionPredict.predict(PROJECT_ID, MODEL_ID, filePath); + String got = bout.toString(); + assertThat(got).contains("X:"); + assertThat(got).contains("Y:"); + } +} diff --git a/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java new file mode 100644 index 00000000000..4b8f1428859 --- /dev/null +++ b/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -0,0 +1,72 @@ +/* + * Copyright 2018 Google Inc. + * + * 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.rpc.NotFoundException; +import io.grpc.StatusRuntimeException; +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 "Dataset API" sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +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 ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String datasetId = "TEN0000000000000000000"; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + @After + public void tearDown() { + // restores print statements in the original method + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testCreateImportDeleteDataset() throws IOException, InterruptedException { + try { + DatasetApi.importData( + PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); + String got = bout.toString(); + assertThat(got).contains("The Dataset doesn't exist "); + } catch (NotFoundException | ExecutionException | StatusRuntimeException ex) { + assertThat(ex.getMessage()).contains("The Dataset doesn't exist"); + } + } +} diff --git a/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java b/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java new file mode 100644 index 00000000000..6037ffb9b58 --- /dev/null +++ b/automl/src/test/java/com/google/cloud/vision/samples/automl/ClassificationDeployModelIT.java @@ -0,0 +1,74 @@ +/* + * 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.vision.samples.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; + +public class ClassificationDeployModelIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "ICN0000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testClassificationDeployModelApi() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + ClassificationDeployModel.classificationDeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } + + @Test + public void testClassificationDeployModelNodeCountApi() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + ClassificationDeployModelNodeCount.classificationDeployModelNodeCount(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +} diff --git a/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java b/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.java new file mode 100644 index 00000000000..54a7f48dd85 --- /dev/null +++ b/automl/src/test/java/com/google/cloud/vision/samples/automl/ObjectDetectionDeployModelNodeCountIT.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.vision.samples.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 vision "Deploy Model Node Count" sample. */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class ObjectDetectionDeployModelNodeCountIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "0000000000000000000000"; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testObjectDetectionDeployModelNodeCountApi() { + // As model deployment can take a long time, instead try to deploy a + // nonexistent model and confirm that the model was not found, but other + // elements of the request were valid. + try { + ObjectDetectionDeployModelNodeCount.objectDetectionDeployModelNodeCount(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("The model does not exist"); + } catch (IOException | ExecutionException | InterruptedException e) { + assertThat(e.getMessage()).contains("The model does not exist"); + } + } +}