import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.automl.v1beta1.BatchPredictInputConfig;
import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig;
import com.google.cloud.automl.v1beta1.BatchPredictRequest;
import com.google.cloud.automl.v1beta1.BatchPredictResult;
import com.google.cloud.automl.v1beta1.GcsDestination;
import com.google.cloud.automl.v1beta1.GcsSource;
import com.google.cloud.automl.v1beta1.ModelName;
import com.google.cloud.automl.v1beta1.OperationMetadata;
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
class BatchPredict {
static void batchPredict() throws IOException, ExecutionException, InterruptedException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "YOUR_PROJECT_ID";
String modelId = "YOUR_MODEL_ID";
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl";
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
batchPredict(projectId, modelId, inputUri, outputUri);
}
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
throws IOException, ExecutionException, InterruptedException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests. After completing all of your requests, call
// the "close" method on the client to safely clean up any remaining background resources.
try (PredictionServiceClient client = PredictionServiceClient.create()) {
// Get the full path of the model.
ModelName name = ModelName.of(projectId, "us-central1", modelId);
// Configure the source of the file from a GCS bucket
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
BatchPredictInputConfig inputConfig =
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
// Configure where to store the output in a GCS bucket
GcsDestination gcsDestination =
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
BatchPredictOutputConfig outputConfig =
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
// Build the request that will be sent to the API
BatchPredictRequest request =
BatchPredictRequest.newBuilder()
.setName(name.toString())
.setInputConfig(inputConfig)
.setOutputConfig(outputConfig)
.build();
// Start an asynchronous request
OperationFuture<BatchPredictResult, OperationMetadata> 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.");
}
}
}
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# model_display_name = 'MODEL_DISPLAY_NAME_HERE'
# gcs_input_uri = 'gs://YOUR_BUCKET_ID/path_to_your_input_csv'
# gcs_output_uri = 'gs://YOUR_BUCKET_ID/path_to_save_results/'
# params = {}
from google.cloud import automl_v1beta1 as automl
client = automl.TablesClient(project=project_id, region=compute_region)
# Query model
response = client.batch_predict(
gcs_input_uris=gcs_input_uri,
gcs_output_uri_prefix=gcs_output_uri,
model_display_name=model_display_name,
params=params
)
print("Making batch prediction... ")
# `response` is a async operation descriptor,
# you can register a callback for the operation to complete via `add_done_callback`:
# def callback(operation_future):
# result = operation_future.result()
# response.add_done_callback(callback)
#
# or block the thread polling for the operation's results:
response.result()
print("Batch prediction complete.\n{}".format(response.metadata))