@BetaApi @Generated(value="by gapic-generator-java") public class PredictionServiceClient extends Object implements com.google.api.gax.core.BackgroundResource
On any input that is documented to expect a string parameter in snake_case or kebab-case, either of those cases is accepted.
This class provides the ability to make remote calls to the backing service through method calls that map to API methods. Sample code to get started:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name, payload, params);
}
Note: close() needs to be called on the PredictionServiceClient object to clean up resources such as threads. In the example above, try-with-resources is used, which automatically calls close().
The surface of this class includes several types of Java methods for each of the API's methods:
See the individual methods for example code.
Many parameters require resource names to be formatted in a particular way. To assist with these names, this class includes a format method for each type of name, and additionally a parse method to extract the individual identifiers contained within names that are returned.
This class can be customized by passing in a custom instance of PredictionServiceSettings to create(). For example:
To customize credentials:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To customize the endpoint:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over the wire:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setTransportChannelProvider(
PredictionServiceSettings.defaultHttpJsonTransportProviderBuilder().build())
.build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
Please refer to the GitHub repository's samples for more quickstart code snippets.
| Modifier | Constructor and Description |
|---|---|
protected |
PredictionServiceClient(PredictionServiceSettings settings)
Constructs an instance of PredictionServiceClient, using the given settings.
|
protected |
PredictionServiceClient(PredictionServiceStub stub) |
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long duration,
TimeUnit unit) |
com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> |
batchPredictAsync(BatchPredictRequest request)
Perform a batch prediction.
|
com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> |
batchPredictAsync(ModelName name,
BatchPredictInputConfig inputConfig,
BatchPredictOutputConfig outputConfig,
Map<String,String> params)
Perform a batch prediction.
|
com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> |
batchPredictAsync(String name,
BatchPredictInputConfig inputConfig,
BatchPredictOutputConfig outputConfig,
Map<String,String> params)
Perform a batch prediction.
|
com.google.api.gax.rpc.UnaryCallable<BatchPredictRequest,com.google.longrunning.Operation> |
batchPredictCallable()
Perform a batch prediction.
|
com.google.api.gax.rpc.OperationCallable<BatchPredictRequest,BatchPredictResult,OperationMetadata> |
batchPredictOperationCallable()
Perform a batch prediction.
|
void |
close() |
static PredictionServiceClient |
create()
Constructs an instance of PredictionServiceClient with default settings.
|
static PredictionServiceClient |
create(PredictionServiceSettings settings)
Constructs an instance of PredictionServiceClient, using the given settings.
|
static PredictionServiceClient |
create(PredictionServiceStub stub)
Constructs an instance of PredictionServiceClient, using the given stub for making calls.
|
com.google.api.gax.httpjson.longrunning.OperationsClient |
getHttpJsonOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation
returned by another API method call.
|
com.google.longrunning.OperationsClient |
getOperationsClient()
Returns the OperationsClient that can be used to query the status of a long-running operation
returned by another API method call.
|
PredictionServiceSettings |
getSettings() |
PredictionServiceStub |
getStub() |
boolean |
isShutdown() |
boolean |
isTerminated() |
PredictResponse |
predict(ModelName name,
ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
PredictResponse |
predict(PredictRequest request)
Perform an online prediction.
|
PredictResponse |
predict(String name,
ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> |
predictCallable()
Perform an online prediction.
|
void |
shutdown() |
void |
shutdownNow() |
protected PredictionServiceClient(PredictionServiceSettings settings) throws IOException
IOExceptionprotected PredictionServiceClient(PredictionServiceStub stub)
public static final PredictionServiceClient create() throws IOException
IOExceptionpublic static final PredictionServiceClient create(PredictionServiceSettings settings) throws IOException
IOExceptionpublic static final PredictionServiceClient create(PredictionServiceStub stub)
public final PredictionServiceSettings getSettings()
public PredictionServiceStub getStub()
public final com.google.longrunning.OperationsClient getOperationsClient()
@BetaApi public final com.google.api.gax.httpjson.longrunning.OperationsClient getHttpJsonOperationsClient()
public final PredictResponse predict(ModelName name, ExamplePayload payload, Map<String,String> params)
[prediction_type][google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type].
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name, payload, params);
}
name - Required. Name of the model requested to serve the prediction.payload - Required. Payload to perform a prediction on. The payload must match the problem
type that the model was trained to solve.params - Additional domain-specific parameters, any string must be up to 25000 characters
long.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for an image, it will only produce results that have at least this confidence score. The default is 0.5.
* For Image Object Detection: `score_threshold` - (float) When Model detects objects on the image, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be returned in the response. Default is 100, the requested value may be limited by server.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictResponse predict(String name, ExamplePayload payload, Map<String,String> params)
[prediction_type][google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type].
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
String name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString();
ExamplePayload payload = ExamplePayload.newBuilder().build();
Map<String, String> params = new HashMap<>();
PredictResponse response = predictionServiceClient.predict(name, payload, params);
}
name - Required. Name of the model requested to serve the prediction.payload - Required. Payload to perform a prediction on. The payload must match the problem
type that the model was trained to solve.params - Additional domain-specific parameters, any string must be up to 25000 characters
long.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for an image, it will only produce results that have at least this confidence score. The default is 0.5.
* For Image Object Detection: `score_threshold` - (float) When Model detects objects on the image, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be returned in the response. Default is 100, the requested value may be limited by server.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final PredictResponse predict(PredictRequest request)
[prediction_type][google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type].
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PredictRequest request =
PredictRequest.newBuilder()
.setName(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString())
.setPayload(ExamplePayload.newBuilder().build())
.putAllParams(new HashMap<String, String>())
.build();
PredictResponse response = predictionServiceClient.predict(request);
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.UnaryCallable<PredictRequest,PredictResponse> predictCallable()
[prediction_type][google.cloud.automl.v1beta1.TablesModelMetadata.prediction_type].
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
PredictRequest request =
PredictRequest.newBuilder()
.setName(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString())
.setPayload(ExamplePayload.newBuilder().build())
.putAllParams(new HashMap<String, String>())
.build();
ApiFuture<PredictResponse> future =
predictionServiceClient.predictCallable().futureCall(request);
// Do something.
PredictResponse response = future.get();
}
public final com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> batchPredictAsync(ModelName name, BatchPredictInputConfig inputConfig, BatchPredictOutputConfig outputConfig, Map<String,String> params)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
Map<String, String> params = new HashMap<>();
BatchPredictResult response =
predictionServiceClient.batchPredictAsync(name, inputConfig, outputConfig, params).get();
}
name - Required. Name of the model requested to serve the batch prediction.inputConfig - Required. The input configuration for batch prediction.outputConfig - Required. The Configuration specifying where output predictions should be
written.params - Required. Additional domain-specific parameters for the predictions, any string
must be up to 25000 characters long.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for a text snippet, it will only produce results that have at least this confidence score. The default is 0.5.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for an image, it will only produce results that have at least this confidence score. The default is 0.5.
`score_threshold` - (float) When Model detects objects on the image, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be produced per image. Default is 100, the requested value may be limited by server.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for a video, it will only produce results that have at least this confidence score. The default is 0.5. `segment_classification` - (boolean) Set to true to request segment-level classification. AutoML Video Intelligence returns labels and their confidence scores for the entire segment of the video that user specified in the request configuration. The default is "true". `shot_classification` - (boolean) Set to true to request shot-level classification. AutoML Video Intelligence determines the boundaries for each camera shot in the entire segment of the video that user specified in the request configuration. AutoML Video Intelligence then returns labels and their confidence scores for each detected shot, along with the start and end time of the shot. WARNING: Model evaluation is not done for this classification type, the quality of it depends on training data, but there are no metrics provided to describe that quality. The default is "false". `1s_interval_classification` - (boolean) Set to true to request classification for a video at one-second intervals. AutoML Video Intelligence returns labels and their confidence scores for each second of the entire segment of the video that user specified in the request configuration. WARNING: Model evaluation is not done for this classification type, the quality of it depends on training data, but there are no metrics provided to describe that quality. The default is "false".
feature_imp<span>ortan</span>ce - (boolean) Whether feature importance should be populated in the returned TablesAnnotations. The default is false.
`score_threshold` - (float) When Model detects objects on video frames, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be returned per frame. Default is 100, the requested value may be limited by server. `min_bounding_box_size` - (float) Only bounding boxes with shortest edge at least that long as a relative value of video frame size will be returned. Value in 0 to 1 range. Default is 0.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> batchPredictAsync(String name, BatchPredictInputConfig inputConfig, BatchPredictOutputConfig outputConfig, Map<String,String> params)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
String name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString();
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
Map<String, String> params = new HashMap<>();
BatchPredictResult response =
predictionServiceClient.batchPredictAsync(name, inputConfig, outputConfig, params).get();
}
name - Required. Name of the model requested to serve the batch prediction.inputConfig - Required. The input configuration for batch prediction.outputConfig - Required. The Configuration specifying where output predictions should be
written.params - Required. Additional domain-specific parameters for the predictions, any string
must be up to 25000 characters long.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for a text snippet, it will only produce results that have at least this confidence score. The default is 0.5.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for an image, it will only produce results that have at least this confidence score. The default is 0.5.
`score_threshold` - (float) When Model detects objects on the image, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be produced per image. Default is 100, the requested value may be limited by server.
`score_threshold` - (float) A value from 0.0 to 1.0. When the model makes predictions for a video, it will only produce results that have at least this confidence score. The default is 0.5. `segment_classification` - (boolean) Set to true to request segment-level classification. AutoML Video Intelligence returns labels and their confidence scores for the entire segment of the video that user specified in the request configuration. The default is "true". `shot_classification` - (boolean) Set to true to request shot-level classification. AutoML Video Intelligence determines the boundaries for each camera shot in the entire segment of the video that user specified in the request configuration. AutoML Video Intelligence then returns labels and their confidence scores for each detected shot, along with the start and end time of the shot. WARNING: Model evaluation is not done for this classification type, the quality of it depends on training data, but there are no metrics provided to describe that quality. The default is "false". `1s_interval_classification` - (boolean) Set to true to request classification for a video at one-second intervals. AutoML Video Intelligence returns labels and their confidence scores for each second of the entire segment of the video that user specified in the request configuration. WARNING: Model evaluation is not done for this classification type, the quality of it depends on training data, but there are no metrics provided to describe that quality. The default is "false".
feature_imp<span>ortan</span>ce - (boolean) Whether feature importance should be populated in the returned TablesAnnotations. The default is false.
`score_threshold` - (float) When Model detects objects on video frames, it will only produce bounding boxes which have at least this confidence score. Value in 0 to 1 range, default is 0.5. `max_bounding_box_count` - (int64) No more than this number of bounding boxes will be returned per frame. Default is 100, the requested value may be limited by server. `min_bounding_box_size` - (float) Only bounding boxes with shortest edge at least that long as a relative value of video frame size will be returned. Value in 0 to 1 range. Default is 0.
com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.longrunning.OperationFuture<BatchPredictResult,OperationMetadata> batchPredictAsync(BatchPredictRequest request)
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
BatchPredictRequest request =
BatchPredictRequest.newBuilder()
.setName(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString())
.setInputConfig(BatchPredictInputConfig.newBuilder().build())
.setOutputConfig(BatchPredictOutputConfig.newBuilder().build())
.putAllParams(new HashMap<String, String>())
.build();
BatchPredictResult response = predictionServiceClient.batchPredictAsync(request).get();
}
request - The request object containing all of the parameters for the API call.com.google.api.gax.rpc.ApiException - if the remote call failspublic final com.google.api.gax.rpc.OperationCallable<BatchPredictRequest,BatchPredictResult,OperationMetadata> batchPredictOperationCallable()
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
BatchPredictRequest request =
BatchPredictRequest.newBuilder()
.setName(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString())
.setInputConfig(BatchPredictInputConfig.newBuilder().build())
.setOutputConfig(BatchPredictOutputConfig.newBuilder().build())
.putAllParams(new HashMap<String, String>())
.build();
OperationFuture<BatchPredictResult, OperationMetadata> future =
predictionServiceClient.batchPredictOperationCallable().futureCall(request);
// Do something.
BatchPredictResult response = future.get();
}
public final com.google.api.gax.rpc.UnaryCallable<BatchPredictRequest,com.google.longrunning.Operation> batchPredictCallable()
Sample code:
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in
// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
BatchPredictRequest request =
BatchPredictRequest.newBuilder()
.setName(ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]").toString())
.setInputConfig(BatchPredictInputConfig.newBuilder().build())
.setOutputConfig(BatchPredictOutputConfig.newBuilder().build())
.putAllParams(new HashMap<String, String>())
.build();
ApiFuture<Operation> future =
predictionServiceClient.batchPredictCallable().futureCall(request);
// Do something.
Operation response = future.get();
}
public final void close()
close in interface AutoCloseablepublic void shutdown()
shutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isShutdown()
isShutdown in interface com.google.api.gax.core.BackgroundResourcepublic boolean isTerminated()
isTerminated in interface com.google.api.gax.core.BackgroundResourcepublic void shutdownNow()
shutdownNow in interface com.google.api.gax.core.BackgroundResourcepublic boolean awaitTermination(long duration,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface com.google.api.gax.core.BackgroundResourceInterruptedExceptionCopyright © 2022 Google LLC. All rights reserved.