async def test_batch_predict_field_headers_async(): client = PredictionServiceAsyncClient( credentials=credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. request = prediction_service.BatchPredictRequest() request.name = "name/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client._client._transport.batch_predict), "__call__" ) as call: call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( operations_pb2.Operation(name="operations/op") ) await client.batch_predict(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the field header was sent. _, _, kw = call.mock_calls[0] assert ("x-goog-request-params", "name=name/value",) in kw["metadata"]
async def test_batch_predict_async(transport: str = "grpc_asyncio"): client = PredictionServiceAsyncClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = prediction_service.BatchPredictRequest() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object( type(client._client._transport.batch_predict), "__call__" ) as call: # Designate an appropriate return value for the call. call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( operations_pb2.Operation(name="operations/spam") ) response = await client.batch_predict(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the response is the type that we expect. assert isinstance(response, future.Future)
def test_batch_predict_flattened_error(): client = PredictionServiceClient(credentials=credentials.AnonymousCredentials(),) # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): client.batch_predict( prediction_service.BatchPredictRequest(), name="name_value", input_config=io.BatchPredictInputConfig( gcs_source=io.GcsSource(input_uris=["input_uris_value"]) ), output_config=io.BatchPredictOutputConfig( gcs_destination=io.GcsDestination( output_uri_prefix="output_uri_prefix_value" ) ), params={"key_value": "value_value"}, )
def batch_predict( self, request: prediction_service.BatchPredictRequest = None, *, name: str = None, input_config: io.BatchPredictInputConfig = None, output_config: io.BatchPredictOutputConfig = None, params: Sequence[ prediction_service.BatchPredictRequest.ParamsEntry] = None, retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), ) -> operation.Operation: r"""Perform a batch prediction. Unlike the online [Predict][google.cloud.automl.v1beta1.PredictionService.Predict], batch prediction result won't be immediately available in the response. Instead, a long running operation object is returned. User can poll the operation result via [GetOperation][google.longrunning.Operations.GetOperation] method. Once the operation is done, [BatchPredictResult][google.cloud.automl.v1beta1.BatchPredictResult] is returned in the [response][google.longrunning.Operation.response] field. Available for following ML problems: - Image Classification - Image Object Detection - Video Classification - Video Object Tracking \* Text Extraction - Tables Args: request (google.cloud.automl_v1beta1.types.BatchPredictRequest): The request object. Request message for [PredictionService.BatchPredict][google.cloud.automl.v1beta1.PredictionService.BatchPredict]. name (str): Required. Name of the model requested to serve the batch prediction. This corresponds to the ``name`` field on the ``request`` instance; if ``request`` is provided, this should not be set. input_config (google.cloud.automl_v1beta1.types.BatchPredictInputConfig): Required. The input configuration for batch prediction. This corresponds to the ``input_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. output_config (google.cloud.automl_v1beta1.types.BatchPredictOutputConfig): Required. The Configuration specifying where output predictions should be written. This corresponds to the ``output_config`` field on the ``request`` instance; if ``request`` is provided, this should not be set. params (Sequence[google.cloud.automl_v1beta1.types.BatchPredictRequest.ParamsEntry]): Required. Additional domain-specific parameters for the predictions, any string must be up to 25000 characters long. - For Text Classification: ``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. - 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 produced per image. Default is 100, the requested value may be limited by server. - 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". - For Tables: feature_importance - (boolean) Whether feature importance should be populated in the returned TablesAnnotations. The default is false. - For Video Object Tracking: ``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. This corresponds to the ``params`` field on the ``request`` instance; if ``request`` is provided, this should not be set. retry (google.api_core.retry.Retry): Designation of what errors, if any, should be retried. timeout (float): The timeout for this request. metadata (Sequence[Tuple[str, str]]): Strings which should be sent along with the request as metadata. Returns: google.api_core.operation.Operation: An object representing a long-running operation. The result type for the operation will be :class:`google.cloud.automl_v1beta1.types.BatchPredictResult` Result of the Batch Predict. This message is returned in [response][google.longrunning.Operation.response] of the operation returned by the [PredictionService.BatchPredict][google.cloud.automl.v1beta1.PredictionService.BatchPredict]. """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have # gotten any keyword arguments that map to the request. has_flattened_params = any([name, input_config, output_config, params]) if request is not None and has_flattened_params: raise ValueError('If the `request` argument is set, then none of ' 'the individual field arguments should be set.') # Minor optimization to avoid making a copy if the user passes # in a prediction_service.BatchPredictRequest. # There's no risk of modifying the input as we've already verified # there are no flattened fields. if not isinstance(request, prediction_service.BatchPredictRequest): request = prediction_service.BatchPredictRequest(request) # If we have keyword arguments corresponding to fields on the # request, apply these. if name is not None: request.name = name if input_config is not None: request.input_config = input_config if output_config is not None: request.output_config = output_config if params is not None: request.params = params # Wrap the RPC method; this adds retry and timeout information, # and friendly error handling. rpc = self._transport._wrapped_methods[self._transport.batch_predict] # Certain fields should be provided within the metadata header; # add these here. metadata = tuple(metadata) + (gapic_v1.routing_header.to_grpc_metadata( (("name", request.name), )), ) # Send the request. response = rpc( request, retry=retry, timeout=timeout, metadata=metadata, ) # Wrap the response in an operation future. response = operation.from_gapic( response, self._transport.operations_client, prediction_service.BatchPredictResult, metadata_type=operations.OperationMetadata, ) # Done; return the response. return response