diff --git a/automl/cloud-client/README.md b/automl/cloud-client/README.md index 0e4f93100c1..b7d8d39298c 100644 --- a/automl/cloud-client/README.md +++ b/automl/cloud-client/README.md @@ -1,25 +1,25 @@ # AutoML Samples - + Open in Cloud Shell This directory contains samples for the [Google Cloud AutoML APIs](https://cloud.google.com/automl/) - [docs](https://cloud.google.com/automl/docs/) We highly reccommend that you refer to the official documentation pages: - + * [Sentiment Analysis](https://cloud.google.com/natural-language/automl/sentiment/docs) * [AutoML Translation](https://cloud.google.com/translate/automl/docs) * AutoML Vision * [Classification](https://cloud.google.com/vision/automl/docs) - * [Edge](https://cloud.google.com/vision/automl/docs/edge-quickstart) + * [Object Detection](https://cloud.google.com/vision/automl/object-detection/docs) -* [AutoML Tables](https://cloud.google.com/automl-tables/docs)--> + This API is part of the larger collection of Cloud Machine Learning APIs. @@ -60,6 +60,9 @@ small section of code to print out the `metadata` field. * [Get Model](src/main/java/com/example/automl/) * [Get Model Evaluation](src/main/java/com/example/automl/GetModelEvaluation.java) * [Delete Model](src/main/java/com/example/automl/DeleteModel.java) +* [Deploy Model](src/main/java/com/example/automl/DeployModel.java) - Not supported by Translation +* [Uneploy Model](src/main/java/com/example/automl/UndeployModel.java) - Not supported by Translation + ### Operation Management * [List Operation Statuses](src/main/java/com/example/automl/ListOperationStatus.java) @@ -69,4 +72,34 @@ small section of code to print out the `metadata` field. ### Translation * [Translate Create Dataset](src/main/java/com/example/automl/TranslateCreateDataset.java) * [Translate Create Model](src/main/java/com/example/automl/TranslateCreateModel.java) -* [Translate Predict](src/main/java/com/example/automl/TranslatePredict.java) \ No newline at end of file +* [Translate Predict](src/main/java/com/example/automl/TranslatePredict.java) + +### Natural Language Entity Extraction +* [Entity Extraction Create Dataset](src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java) +* [Entity Extraction Create Model](src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java) +* [Entity Extraction Predict](src/main/java/com/example/automl/LanguageEntityExtractionPredict.java) +* [Entity Extraction Batch Predict](src/main/java/com/example/automl/LanguageBatchPredict.java) + +### Natural Language Sentiment Analysis +* [Sentiment Analysis Create Dataset](src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java) +* [Sentiment Analysis Create Model](src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java) +* [Sentiment Analysis Predict](src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java) + +### Natural Language Text Classification +* [Text Classification Create Dataset](src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java) +* [Text Classification Create Model](src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java) +* [Text Classification Predict](src/main/java/com/example/automl/LanguageTextClassificationPredict.java) + +### Vision Classification +* [Classification Create Dataset](src/main/java/com/example/automl/VisionClassificationCreateDataset.java) +* [Classification Create Model](src/main/java/com/example/automl/VisionClassificationCreateModel.java) +* [Classification Predict](src/main/java/com/example/automl/VisionClassificationPredict.java) +* [Classification Batch Predict](src/main/java/com/example/automl/VisionBatchPredict.java) +* [Deploy Node Count](src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java) + +### Vision Object Detection +* [Object Detection Create Dataset](src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java) +* [Object Detection Create Model](src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java) +* [Object Detection Predict](src/main/java/com/example/automl/VisionObjectDetectionPredict.java) +* [Object Detection Batch Predict](src/main/java/com/example/automl/VisionBatchPredict.java) +* [Deploy Node Count](src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java) diff --git a/automl/cloud-client/pom.xml b/automl/cloud-client/pom.xml index 209bc226f58..8176664f6b4 100644 --- a/automl/cloud-client/pom.xml +++ b/automl/cloud-client/pom.xml @@ -37,7 +37,7 @@ com.google.cloud google-cloud-automl - 0.114.1-beta + 0.115.0-beta diff --git a/automl/cloud-client/resources/salad.jpg b/automl/cloud-client/resources/salad.jpg new file mode 100644 index 00000000000..a7f960b5030 Binary files /dev/null and b/automl/cloud-client/resources/salad.jpg differ diff --git a/automl/cloud-client/resources/test.png b/automl/cloud-client/resources/test.png new file mode 100644 index 00000000000..653342a46e5 Binary files /dev/null and b/automl/cloud-client/resources/test.png differ diff --git a/automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java b/automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java index e885db9dc91..524042e63c8 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java +++ b/automl/cloud-client/src/main/java/com/example/automl/DeleteModel.java @@ -33,7 +33,7 @@ static void deleteModel() throws IOException, ExecutionException, InterruptedExc deleteModel(projectId, modelId); } - // Get a model + // 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 diff --git a/automl/cloud-client/src/main/java/com/example/automl/DeployModel.java b/automl/cloud-client/src/main/java/com/example/automl/DeployModel.java new file mode 100644 index 00000000000..8d1533b1440 --- /dev/null +++ b/automl/cloud-client/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 { + + static void deployModel() 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/cloud-client/src/main/java/com/example/automl/ExportDataset.java b/automl/cloud-client/src/main/java/com/example/automl/ExportDataset.java index ae8818d97ad..fa7767de1ab 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/ExportDataset.java +++ b/automl/cloud-client/src/main/java/com/example/automl/ExportDataset.java @@ -36,7 +36,7 @@ static void exportDataset() throws IOException, ExecutionException, InterruptedE exportDataset(projectId, datasetId, gcsUri); } - // Export a dataset + // 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 diff --git a/automl/cloud-client/src/main/java/com/example/automl/GetDataset.java b/automl/cloud-client/src/main/java/com/example/automl/GetDataset.java index 4e8d36117b7..a4d83f4e2d9 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/GetDataset.java +++ b/automl/cloud-client/src/main/java/com/example/automl/GetDataset.java @@ -16,8 +16,12 @@ 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; @@ -52,6 +56,30 @@ static void getDataset(String projectId, String datasetId) throws IOException { 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", @@ -59,10 +87,29 @@ static void getDataset(String projectId, String datasetId) throws IOException { System.out.format( "\tTarget language code: %s\n", dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); - System.out.println("Dataset create time:"); - System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); - System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + // [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/cloud-client/src/main/java/com/example/automl/LanguageBatchPredict.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageBatchPredict.java new file mode 100644 index 00000000000..3e7a2b9c041 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageBatchPredict.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_language_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 LanguageBatchPredict { + + 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_file.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_language_batch_predict] diff --git a/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.java new file mode 100644 index 00000000000..937d995ba7c --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateDataset.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_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/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.java new file mode 100644 index 00000000000..b2d191365ea --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionCreateModel.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_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/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java new file mode 100644 index 00000000000..87e0a647ced --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageEntityExtractionPredict.java @@ -0,0 +1,67 @@ +/* + * 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); + TextSnippet textSnippet = + TextSnippet.newBuilder().setContent(content).setMimeType("text/plain").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/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.java new file mode 100644 index 00000000000..69c2bee685c --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateDataset.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.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/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisCreateModel.java new file mode 100644 index 00000000000..e3af238e5ba --- /dev/null +++ b/automl/cloud-client/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. + 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/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.java new file mode 100644 index 00000000000..e12db8f7703 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageSentimentAnalysisPredict.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_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); + 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/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationCreateDataset.java new file mode 100644 index 00000000000..dd78d669121 --- /dev/null +++ b/automl/cloud-client/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 { + + 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 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/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationCreateModel.java new file mode 100644 index 00000000000..6ab0b62da34 --- /dev/null +++ b/automl/cloud-client/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 { + + 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. + 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/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationPredict.java b/automl/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationPredict.java new file mode 100644 index 00000000000..37ca2804dd0 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/LanguageTextClassificationPredict.java @@ -0,0 +1,67 @@ +/* + * 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 { + + 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); + 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/cloud-client/src/main/java/com/example/automl/ListDatasets.java b/automl/cloud-client/src/main/java/com/example/automl/ListDatasets.java index aca86c1c7f1..52607e6ce22 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/ListDatasets.java +++ b/automl/cloud-client/src/main/java/com/example/automl/ListDatasets.java @@ -16,7 +16,12 @@ 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; @@ -58,6 +63,30 @@ static void listDatasets(String projectId) throws IOException { 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", @@ -65,11 +94,30 @@ static void listDatasets(String projectId) throws IOException { System.out.format( "\tTarget language code: %s\n", dataset.getTranslationDatasetMetadata().getTargetLanguageCode()); - System.out.println("Dataset create time:"); - System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds()); - System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos()); + // [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/cloud-client/src/main/java/com/example/automl/ListModels.java b/automl/cloud-client/src/main/java/com/example/automl/ListModels.java index 67644664397..b534e8d8c23 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/ListModels.java +++ b/automl/cloud-client/src/main/java/com/example/automl/ListModels.java @@ -32,7 +32,7 @@ static void listModels() throws IOException { listModels(projectId); } - // List models + // 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 diff --git a/automl/cloud-client/src/main/java/com/example/automl/TranslateCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/TranslateCreateModel.java index 2fca0c38905..73219eae57c 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/TranslateCreateModel.java +++ b/automl/cloud-client/src/main/java/com/example/automl/TranslateCreateModel.java @@ -59,6 +59,9 @@ static void createModel(String projectId, String datasetId, String displayName) // 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..."); } diff --git a/automl/cloud-client/src/main/java/com/example/automl/TranslatePredict.java b/automl/cloud-client/src/main/java/com/example/automl/TranslatePredict.java index ccd2aaa1a4e..55a666cbd77 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/TranslatePredict.java +++ b/automl/cloud-client/src/main/java/com/example/automl/TranslatePredict.java @@ -38,7 +38,6 @@ static void predict() throws IOException { predict(projectId, modelId, filePath); } - // Predict 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 @@ -57,7 +56,7 @@ static void predict(String projectId, String modelId, String filePath) throws IO PredictResponse response = client.predict(predictRequest); TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); - System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + System.out.format("Translated Content: %s\n", translatedContent.getContent()); } } } diff --git a/automl/cloud-client/src/main/java/com/example/automl/UndeployModel.java b/automl/cloud-client/src/main/java/com/example/automl/UndeployModel.java new file mode 100644 index 00000000000..765d0a23316 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/UndeployModel.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_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/cloud-client/src/main/java/com/example/automl/VisionBatchPredict.java b/automl/cloud-client/src/main/java/com/example/automl/VisionBatchPredict.java new file mode 100644 index 00000000000..8546da72e23 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionBatchPredict.java @@ -0,0 +1,78 @@ +/* + * 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_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 VisionBatchPredict { + + 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_file.csv"; + 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) + // [0.0-1.0] Only produce results higher than this value + .putParams("score_threshold", "0.8") + .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_vision_batch_predict] diff --git a/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationCreateDataset.java b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationCreateDataset.java new file mode 100644 index 00000000000..0cf627d5a43 --- /dev/null +++ b/automl/cloud-client/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 { + + 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 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/cloud-client/src/main/java/com/example/automl/VisionClassificationCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationCreateModel.java new file mode 100644 index 00000000000..b6d3d163283 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationCreateModel.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_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 { + + 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. + ImageClassificationModelMetadata metadata = + ImageClassificationModelMetadata.newBuilder() + .setTrainBudgetMilliNodeHours( + 8) // The train budget of creating this model, expressed in hours. + .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/cloud-client/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java new file mode 100644 index 00000000000..32cfd97f79c --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationDeployModelNodeCount.java @@ -0,0 +1,64 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.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/cloud-client/src/main/java/com/example/automl/VisionClassificationPredict.java b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationPredict.java new file mode 100644 index 00000000000..6089c8e81a0 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionClassificationPredict.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_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 { + + 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.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/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.java new file mode 100644 index 00000000000..40270ca9525 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateDataset.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_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/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.java new file mode 100644 index 00000000000..14145a2f563 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionCreateModel.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_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/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java new file mode 100644 index 00000000000..603ec3be4d9 --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionDeployModelNodeCount.java @@ -0,0 +1,64 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.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/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionPredict.java b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionPredict.java new file mode 100644 index 00000000000..09483d9453d --- /dev/null +++ b/automl/cloud-client/src/main/java/com/example/automl/VisionObjectDetectionPredict.java @@ -0,0 +1,78 @@ +/* + * 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/cloud-client/src/test/java/com/example/automl/GenericModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/GenericModelManagementIT.java new file mode 100644 index 00000000000..6813cf04bd9 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/GenericModelManagementIT.java @@ -0,0 +1,134 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.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; + +// Tests for Automl models. +@RunWith(JUnit4.class) +public class GenericModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private String modelId; + private String modelEvaluationId; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testModelApi() throws IOException { + // LIST MODELS + ListModels.listModels(PROJECT_ID); + String got = bout.toString(); + modelId = got.split("Model id: ")[1].split("\n")[0]; + assertThat(got).contains("Model id:"); + + // GET MODEL + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + GetModel.getModel(PROJECT_ID, modelId); + got = bout.toString(); + assertThat(got).contains("Model id: " + modelId); + + // LIST MODEL EVALUATIONS + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + ListModelEvaluations.listModelEvaluations(PROJECT_ID, modelId); + got = bout.toString(); + modelEvaluationId = got.split(modelId + "/modelEvaluations/")[1].split("\n")[0]; + assertThat(got).contains("Model Evaluation Name:"); + + // GET MODEL EVALUATION + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + GetModelEvaluation.getModelEvaluation(PROJECT_ID, modelId, modelEvaluationId); + got = bout.toString(); + assertThat(got).contains("Model Evaluation Name:"); + } + + @Test + public void testOperationStatus() throws IOException { + // Act + ListOperationStatus.listOperationStatus(PROJECT_ID); + + // Assert + String got = bout.toString(); + String operationId = got.split("\n")[1].split(":")[1].trim(); + assertThat(got).contains("Operation details:"); + + // Act + bout.reset(); + GetOperationStatus.getOperationStatus(operationId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Operation details:"); + } + + @Test + public void 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/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionDatasetManagementIT.java new file mode 100644 index 00000000000..550ff61e31e --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionDatasetManagementIT.java @@ -0,0 +1,156 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.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 natural language entity extraction datasets +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageEntityExtractionDatasetManagementIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private String getdatasetId = "TEN477786180780294144"; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + LanguageEntityExtractionCreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Act + ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity_extraction/dataset.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() throws IOException { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() throws IOException { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); + + String got = bout.toString(); + assertThat(got).contains("Dataset exported."); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionModelManagementIT.java new file mode 100644 index 00000000000..3c460343346 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionModelManagementIT.java @@ -0,0 +1,78 @@ +/* + * 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; + +// Tests for Automl natural language entity extraction models. +@RunWith(JUnit4.class) +public class LanguageEntityExtractionModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TEN1974951581904273408"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployUndeployModel() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionPredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionPredictIT.java new file mode 100644 index 00000000000..8b5400d1a2c --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageEntityExtractionPredictIT.java @@ -0,0 +1,115 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.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; + +// Tests for automl natural language entity extraction "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageEntityExtractionPredictIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String modelId = "TEN1974951581904273408"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPredict() throws IOException { + String text = "Constitutional mutations in the WT1 gene in patients with Denys-Drash syndrome."; + // Act + LanguageEntityExtractionPredict.predict(PROJECT_ID, modelId, text); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Text Extract Entity Types:"); + } + + @Test + public void testBatchPredict() throws IOException, ExecutionException, InterruptedException { + String inputUri = String.format("gs://%s/entity_extraction/input.jsonl", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + // Act + LanguageBatchPredict.batchPredict(PROJECT_ID, modelId, inputUri, outputUri); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Batch Prediction results saved to specified Cloud Storage bucket"); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_BATCH_PREDICT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisDatasetManagementIT.java new file mode 100644 index 00000000000..64e07c3b349 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisDatasetManagementIT.java @@ -0,0 +1,157 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.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 natural language sentiment analysis datasets. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageSentimentAnalysisDatasetManagementIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private String getdatasetId = "TST3960250460385409610"; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + LanguageSentimentAnalysisCreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Act + ImportDataset.importDataset( + PROJECT_ID, datasetId, BUCKET + "/automl-sentiment/sentiment_dataset.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() throws IOException { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() throws IOException { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); + + String got = bout.toString(); + assertThat(got).contains("Dataset exported."); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisModelManagementIT.java new file mode 100644 index 00000000000..0bc7f68f6c7 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisModelManagementIT.java @@ -0,0 +1,78 @@ +/* + * 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; + +// Tests for Automl natural language sentiment analysis models. +@RunWith(JUnit4.class) +public class LanguageSentimentAnalysisModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TST864310464894223026"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployUndeployModel() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictIT.java new file mode 100644 index 00000000000..d0613385ffb --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageSentimentAnalysisPredictIT.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; + +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 natural language sentiment analysis "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageSentimentAnalysisPredictIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String modelId = "TST864310464894223026"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPredict() throws IOException { + String text = "Hopefully this Claritin kicks in soon"; + // Act + LanguageSentimentAnalysisPredict.predict(PROJECT_ID, modelId, text); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Predicted sentiment score:"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationDatasetManagementIT.java new file mode 100644 index 00000000000..54dcaacc429 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationDatasetManagementIT.java @@ -0,0 +1,156 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.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 natural language text classification datasets. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageTextClassificationDatasetManagementIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-lcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private String getdatasetId = "TCN2551826603472450019"; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + LanguageTextClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Act + ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/happiness.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() throws IOException { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() throws IOException { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); + + String got = bout.toString(); + assertThat(got).contains("Dataset exported."); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationModelManagementIT.java new file mode 100644 index 00000000000..2d630da6b6a --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationModelManagementIT.java @@ -0,0 +1,78 @@ +/* + * 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; + +// Tests for Automl natural language text classification models. +@RunWith(JUnit4.class) +public class LanguageTextClassificationModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "TCN6871084728972835631"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployUndeployModel() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationPredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationPredictIT.java new file mode 100644 index 00000000000..5034047f9c1 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/LanguageTextClassificationPredictIT.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; + +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 natural language text classification "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class LanguageTextClassificationPredictIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String modelId = "TCN6871084728972835631"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPredict() throws IOException { + String text = "Fruit and nut flavour"; + // Act + LanguageTextClassificationPredict.predict(PROJECT_ID, modelId, text); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Predicted class name:"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/TranslateDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/TranslateDatasetManagementIT.java index d7589a6cbde..62d8ea87e08 100644 --- a/automl/cloud-client/src/test/java/com/example/automl/TranslateDatasetManagementIT.java +++ b/automl/cloud-client/src/test/java/com/example/automl/TranslateDatasetManagementIT.java @@ -17,6 +17,7 @@ package com.example.automl; import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; import com.google.api.gax.paging.Page; import com.google.cloud.storage.Blob; @@ -31,11 +32,12 @@ 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 datasets. */ +// Tests for Automl translation datasets @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class TranslateDatasetManagementIT { @@ -47,6 +49,19 @@ public class TranslateDatasetManagementIT { private String datasetId; private String getdatasetId = "TRL3946265060617537378"; + 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"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/automl/cloud-client/src/test/java/com/example/automl/TranslateModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/TranslateModelManagementIT.java index fe3ae302ac8..fb416e42f10 100644 --- a/automl/cloud-client/src/test/java/com/example/automl/TranslateModelManagementIT.java +++ b/automl/cloud-client/src/test/java/com/example/automl/TranslateModelManagementIT.java @@ -17,6 +17,7 @@ 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; @@ -27,11 +28,12 @@ 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. */ +// Tests for Automl translation models. @RunWith(JUnit4.class) public class TranslateModelManagementIT { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); @@ -42,6 +44,19 @@ public class TranslateModelManagementIT { private String modelId; private String modelEvaluationId; + 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"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/automl/cloud-client/src/test/java/com/example/automl/TranslatePredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/TranslatePredictIT.java index e56ec6d6dec..24a0ff952f7 100644 --- a/automl/cloud-client/src/test/java/com/example/automl/TranslatePredictIT.java +++ b/automl/cloud-client/src/test/java/com/example/automl/TranslatePredictIT.java @@ -17,6 +17,7 @@ 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; @@ -25,11 +26,12 @@ 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. */ +// Tests for translation "Predict" sample. @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class TranslatePredictIT { @@ -39,6 +41,19 @@ public class TranslatePredictIT { private ByteArrayOutputStream bout; private PrintStream out; + 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"); + } + @Before public void setUp() { bout = new ByteArrayOutputStream(); diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationDatasetManagementIT.java new file mode 100644 index 00000000000..90873692297 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationDatasetManagementIT.java @@ -0,0 +1,156 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.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 vision image classification datasets. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionClassificationDatasetManagementIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-vcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private String getdatasetId = "ICN3876092572857648864"; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + VisionClassificationCreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Act + ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/flower_traindata.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() throws IOException { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() throws IOException { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); + + String got = bout.toString(); + assertThat(got).contains("Dataset exported."); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationModelManagementIT.java new file mode 100644 index 00000000000..b7f053f6e18 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationModelManagementIT.java @@ -0,0 +1,91 @@ +/* + * 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; + +// Tests for Automl vision image classification models. +@RunWith(JUnit4.class) +public class VisionClassificationModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "ICN6418888056864606028"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployUndeployModel() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } + + @Test + public void testDeployUndeployModelWithNodeCount() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + VisionClassificationDeployModelNodeCount.visionClassificationDeployModelNodeCount( + PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationPredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationPredictIT.java new file mode 100644 index 00000000000..532fe2ae258 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionClassificationPredictIT.java @@ -0,0 +1,115 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.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; + +// Tests for automl vision image classification "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionClassificationPredictIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-vcm"; + private static final String modelId = "ICN6418888056864606028"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPredict() throws IOException { + String filePath = "resources/test.png"; + // Act + VisionClassificationPredict.predict(PROJECT_ID, modelId, filePath); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Predicted class name:"); + } + + @Test + public void testBatchPredict() throws IOException, ExecutionException, InterruptedException { + String inputUri = String.format("gs://%s/batch_predict_test.csv", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + // Act + VisionBatchPredict.batchPredict(PROJECT_ID, modelId, inputUri, outputUri); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Batch Prediction results saved to specified Cloud Storage bucket"); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_BATCH_PREDICT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionDatasetManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionDatasetManagementIT.java new file mode 100644 index 00000000000..0feb2187a6f --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionDatasetManagementIT.java @@ -0,0 +1,157 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; + +import java.io.ByteArrayOutputStream; +import java.io.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 vision object detection datasets. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionObjectDetectionDatasetManagementIT { + + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-vcm"; + private static final String BUCKET = "gs://" + BUCKET_ID; + private ByteArrayOutputStream bout; + private PrintStream out; + private String getdatasetId = "IOD2036031651850485760"; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + + // Act + VisionObjectDetectionCreateDataset.createDataset(PROJECT_ID, datasetName); + + // Assert + String got = bout.toString(); + String datasetId = got.split("Dataset id: ")[1].split("\n")[0]; + + // Act + ImportDataset.importDataset( + PROJECT_ID, datasetId, "gs://cloud-ml-data/img/openimage/csv/salads_ml_use.csv"); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset id:"); + + // Act + DeleteDataset.deleteDataset(PROJECT_ID, datasetId); + + // Assert + got = bout.toString(); + assertThat(got).contains("Dataset deleted."); + } + + @Test + public void testListDataset() throws IOException { + // Act + ListDatasets.listDatasets(PROJECT_ID); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testGetDataset() throws IOException { + // Act + GetDataset.getDataset(PROJECT_ID, getdatasetId); + + // Assert + String got = bout.toString(); + + assertThat(got).contains("Dataset id:"); + } + + @Test + public void testExportDataset() throws IOException, ExecutionException, InterruptedException { + ExportDataset.exportDataset(PROJECT_ID, getdatasetId, BUCKET + "/TEST_EXPORT_OUTPUT/"); + + String got = bout.toString(); + assertThat(got).contains("Dataset exported."); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_EXPORT_OUTPUT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionModelManagementIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionModelManagementIT.java new file mode 100644 index 00000000000..e77efa8d319 --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionModelManagementIT.java @@ -0,0 +1,91 @@ +/* + * 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; + +// Tests for Automl vision object detection models. +@RunWith(JUnit4.class) +public class VisionObjectDetectionModelManagementIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String MODEL_ID = "IOD1854128448151224320"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testDeployUndeployModel() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + DeployModel.deployModel(PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } + + @Test + public void testDeployUndeployModelWithNodeCount() + throws IOException, ExecutionException, InterruptedException { + UndeployModel.undeployModel(PROJECT_ID, MODEL_ID); + String got = bout.toString(); + assertThat(got).contains("Model undeployment finished"); + + VisionObjectDetectionDeployModelNodeCount.visionObjectDetectionDeployModelNodeCount( + PROJECT_ID, MODEL_ID); + got = bout.toString(); + assertThat(got).contains("Model deployment finished"); + } +} diff --git a/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionPredictIT.java b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionPredictIT.java new file mode 100644 index 00000000000..394947580ae --- /dev/null +++ b/automl/cloud-client/src/test/java/com/example/automl/VisionObjectDetectionPredictIT.java @@ -0,0 +1,117 @@ +/* + * 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.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.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; + +// Tests for automl vision object detection "Predict" sample. +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class VisionObjectDetectionPredictIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String BUCKET_ID = PROJECT_ID + "-vcm"; + private static final String modelId = "IOD1854128448151224320"; + private ByteArrayOutputStream bout; + private PrintStream out; + + 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"); + } + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testPredict() throws IOException { + String filePath = "resources/salad.jpg"; + // Act + VisionObjectDetectionPredict.predict(PROJECT_ID, modelId, filePath); + + // Assert + String got = bout.toString(); + assertThat(got).contains("X:"); + assertThat(got).contains("Y:"); + } + + @Test + public void testBatchPredict() throws IOException, ExecutionException, InterruptedException { + String inputUri = + String.format("gs://%s/vision_object_detection_batch_predict_test.csv", BUCKET_ID); + String outputUri = String.format("gs://%s/TEST_BATCH_PREDICT/", BUCKET_ID); + // Act + VisionBatchPredict.batchPredict(PROJECT_ID, modelId, inputUri, outputUri); + + // Assert + String got = bout.toString(); + assertThat(got).contains("Batch Prediction results saved to specified Cloud Storage bucket"); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + Page blobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix("TEST_BATCH_PREDICT/")); + + for (Blob blob : blobs.iterateAll()) { + Page fileBlobs = + storage.list( + BUCKET_ID, + Storage.BlobListOption.currentDirectory(), + Storage.BlobListOption.prefix(blob.getName())); + for (Blob fileBlob : fileBlobs.iterateAll()) { + if (!fileBlob.isDirectory()) { + fileBlob.delete(); + } + } + } + } +}