|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.beta.automl; |
| 18 | + |
| 19 | +// [START automl_batch_predict_beta] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; |
| 22 | +import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; |
| 23 | +import com.google.cloud.automl.v1beta1.BatchPredictRequest; |
| 24 | +import com.google.cloud.automl.v1beta1.BatchPredictResult; |
| 25 | +import com.google.cloud.automl.v1beta1.GcsDestination; |
| 26 | +import com.google.cloud.automl.v1beta1.GcsSource; |
| 27 | +import com.google.cloud.automl.v1beta1.ModelName; |
| 28 | +import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 29 | +import com.google.cloud.automl.v1beta1.PredictionServiceClient; |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.concurrent.ExecutionException; |
| 32 | + |
| 33 | +class BatchPredict { |
| 34 | + |
| 35 | + static void batchPredict() throws IOException, ExecutionException, InterruptedException { |
| 36 | + // TODO(developer): Replace these variables before running the sample. |
| 37 | + String projectId = "YOUR_PROJECT_ID"; |
| 38 | + String modelId = "YOUR_MODEL_ID"; |
| 39 | + String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl"; |
| 40 | + String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; |
| 41 | + batchPredict(projectId, modelId, inputUri, outputUri); |
| 42 | + } |
| 43 | + |
| 44 | + static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) |
| 45 | + throws IOException, ExecutionException, InterruptedException { |
| 46 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 47 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 48 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 49 | + try (PredictionServiceClient client = PredictionServiceClient.create()) { |
| 50 | + // Get the full path of the model. |
| 51 | + ModelName name = ModelName.of(projectId, "us-central1", modelId); |
| 52 | + |
| 53 | + // Configure the source of the file from a GCS bucket |
| 54 | + GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); |
| 55 | + BatchPredictInputConfig inputConfig = |
| 56 | + BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); |
| 57 | + |
| 58 | + // Configure where to store the output in a GCS bucket |
| 59 | + GcsDestination gcsDestination = |
| 60 | + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); |
| 61 | + BatchPredictOutputConfig outputConfig = |
| 62 | + BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); |
| 63 | + |
| 64 | + // Build the request that will be sent to the API |
| 65 | + BatchPredictRequest request = |
| 66 | + BatchPredictRequest.newBuilder() |
| 67 | + .setName(name.toString()) |
| 68 | + .setInputConfig(inputConfig) |
| 69 | + .setOutputConfig(outputConfig) |
| 70 | + .build(); |
| 71 | + |
| 72 | + // Start an asynchronous request |
| 73 | + OperationFuture<BatchPredictResult, OperationMetadata> future = |
| 74 | + client.batchPredictAsync(request); |
| 75 | + |
| 76 | + System.out.println("Waiting for operation to complete..."); |
| 77 | + BatchPredictResult response = future.get(); |
| 78 | + System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +// [END automl_batch_predict_beta] |
0 commit comments