@Generated(value="by gapic-generator") @BetaApi public class PredictionServiceClient extends Object implements 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:
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:
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
.build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
To customize the endpoint:
PredictionServiceSettings predictionServiceSettings =
PredictionServiceSettings.newBuilder().setEndpoint(myEndpoint).build();
PredictionServiceClient predictionServiceClient =
PredictionServiceClient.create(predictionServiceSettings);
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) |
OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> |
batchPredictAsync(com.google.cloud.automl.v1beta1.BatchPredictRequest request)
Perform a batch prediction.
|
OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> |
batchPredictAsync(com.google.cloud.automl.v1beta1.ModelName name,
com.google.cloud.automl.v1beta1.BatchPredictInputConfig inputConfig,
com.google.cloud.automl.v1beta1.BatchPredictOutputConfig outputConfig,
Map<String,String> params)
Perform a batch prediction.
|
OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> |
batchPredictAsync(String name,
com.google.cloud.automl.v1beta1.BatchPredictInputConfig inputConfig,
com.google.cloud.automl.v1beta1.BatchPredictOutputConfig outputConfig,
Map<String,String> params)
Perform a batch prediction.
|
UnaryCallable<com.google.cloud.automl.v1beta1.BatchPredictRequest,Operation> |
batchPredictCallable()
Perform a batch prediction.
|
OperationCallable<com.google.cloud.automl.v1beta1.BatchPredictRequest,com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.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.
|
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() |
com.google.cloud.automl.v1beta1.PredictResponse |
predict(com.google.cloud.automl.v1beta1.ModelName name,
com.google.cloud.automl.v1beta1.ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
com.google.cloud.automl.v1beta1.PredictResponse |
predict(com.google.cloud.automl.v1beta1.PredictRequest request)
Perform an online prediction.
|
com.google.cloud.automl.v1beta1.PredictResponse |
predict(String name,
com.google.cloud.automl.v1beta1.ExamplePayload payload,
Map<String,String> params)
Perform an online prediction.
|
UnaryCallable<com.google.cloud.automl.v1beta1.PredictRequest,com.google.cloud.automl.v1beta1.PredictResponse> |
predictCallable()
Perform an online prediction.
|
void |
shutdown() |
void |
shutdownNow() |
protected PredictionServiceClient(PredictionServiceSettings settings) throws IOException
IOException
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") protected PredictionServiceClient(PredictionServiceStub stub)
public static final PredictionServiceClient create() throws IOException
IOException
public static final PredictionServiceClient create(PredictionServiceSettings settings) throws IOException
IOException
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public static final PredictionServiceClient create(PredictionServiceStub stub)
public final PredictionServiceSettings getSettings()
@BetaApi(value="A restructuring of stub classes is planned, so this may break in the future") public PredictionServiceStub getStub()
@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final OperationsClient getOperationsClient()
public final com.google.cloud.automl.v1beta1.PredictResponse predict(com.google.cloud.automl.v1beta1.ModelName name, com.google.cloud.automl.v1beta1.ExamplePayload payload, Map<String,String> params)
Sample code:
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
- 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.
* For Image Classification:
`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.
ApiException
- if the remote call failspublic final com.google.cloud.automl.v1beta1.PredictResponse predict(String name, com.google.cloud.automl.v1beta1.ExamplePayload payload, Map<String,String> params)
Sample code:
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.toString(), payload, params);
}
name
- 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.
* For Image Classification:
`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.
ApiException
- if the remote call failspublic final com.google.cloud.automl.v1beta1.PredictResponse predict(com.google.cloud.automl.v1beta1.PredictRequest request)
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
PredictRequest request = PredictRequest.newBuilder()
.setName(name.toString())
.setPayload(payload)
.build();
PredictResponse response = predictionServiceClient.predict(request);
}
request
- The request object containing all of the parameters for the API call.ApiException
- if the remote call failspublic final UnaryCallable<com.google.cloud.automl.v1beta1.PredictRequest,com.google.cloud.automl.v1beta1.PredictResponse> predictCallable()
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
ExamplePayload payload = ExamplePayload.newBuilder().build();
PredictRequest request = PredictRequest.newBuilder()
.setName(name.toString())
.setPayload(payload)
.build();
ApiFuture<PredictResponse> future = predictionServiceClient.predictCallable().futureCall(request);
// Do something
PredictResponse response = future.get();
}
@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> batchPredictAsync(com.google.cloud.automl.v1beta1.ModelName name, com.google.cloud.automl.v1beta1.BatchPredictInputConfig inputConfig, com.google.cloud.automl.v1beta1.BatchPredictOutputConfig outputConfig, Map<String,String> params)
Sample code:
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
- 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
- Additional domain-specific parameters for the predictions, any string must be up
to 25000 characters long.
* For Video Classification : `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".
ApiException
- if the remote call fails@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> batchPredictAsync(String name, com.google.cloud.automl.v1beta1.BatchPredictInputConfig inputConfig, com.google.cloud.automl.v1beta1.BatchPredictOutputConfig outputConfig, Map<String,String> params)
Sample code:
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.toString(), inputConfig, outputConfig, params).get();
}
name
- 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
- Additional domain-specific parameters for the predictions, any string must be up
to 25000 characters long.
* For Video Classification : `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".
ApiException
- if the remote call fails@BetaApi(value="The surface for long-running operations is not stable yet and may change in the future.") public final OperationFuture<com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> batchPredictAsync(com.google.cloud.automl.v1beta1.BatchPredictRequest request)
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
BatchPredictRequest request = BatchPredictRequest.newBuilder()
.setName(name.toString())
.setInputConfig(inputConfig)
.setOutputConfig(outputConfig)
.build();
BatchPredictResult response = predictionServiceClient.batchPredictAsync(request).get();
}
request
- The request object containing all of the parameters for the API call.ApiException
- if the remote call fails@BetaApi(value="The surface for use by generated code is not stable yet and may change in the future.") public final OperationCallable<com.google.cloud.automl.v1beta1.BatchPredictRequest,com.google.cloud.automl.v1beta1.BatchPredictResult,com.google.cloud.automl.v1beta1.OperationMetadata> batchPredictOperationCallable()
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
BatchPredictRequest request = BatchPredictRequest.newBuilder()
.setName(name.toString())
.setInputConfig(inputConfig)
.setOutputConfig(outputConfig)
.build();
OperationFuture<BatchPredictResult, OperationMetadata> future = predictionServiceClient.batchPredictOperationCallable().futureCall(request);
// Do something
BatchPredictResult response = future.get();
}
public final UnaryCallable<com.google.cloud.automl.v1beta1.BatchPredictRequest,Operation> batchPredictCallable()
Sample code:
try (PredictionServiceClient predictionServiceClient = PredictionServiceClient.create()) {
ModelName name = ModelName.of("[PROJECT]", "[LOCATION]", "[MODEL]");
BatchPredictInputConfig inputConfig = BatchPredictInputConfig.newBuilder().build();
BatchPredictOutputConfig outputConfig = BatchPredictOutputConfig.newBuilder().build();
BatchPredictRequest request = BatchPredictRequest.newBuilder()
.setName(name.toString())
.setInputConfig(inputConfig)
.setOutputConfig(outputConfig)
.build();
ApiFuture<Operation> future = predictionServiceClient.batchPredictCallable().futureCall(request);
// Do something
Operation response = future.get();
}
public final void close()
close
in interface AutoCloseable
public void shutdown()
shutdown
in interface BackgroundResource
public boolean isShutdown()
isShutdown
in interface BackgroundResource
public boolean isTerminated()
isTerminated
in interface BackgroundResource
public void shutdownNow()
shutdownNow
in interface BackgroundResource
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException
awaitTermination
in interface BackgroundResource
InterruptedException
Copyright © 2019 Google LLC. All rights reserved.