|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""A class for AsyncInferenceResponse""" |
| 14 | + |
| 15 | +from __future__ import print_function, absolute_import |
| 16 | + |
| 17 | +from botocore.exceptions import ClientError |
| 18 | +from sagemaker.s3 import parse_s3_url |
| 19 | +from sagemaker.async_inference import WaiterConfig |
| 20 | +from sagemaker.exceptions import ObjectNotExistedError, UnexpectedClientError |
| 21 | + |
| 22 | + |
| 23 | +class AsyncInferenceResponse(object): |
| 24 | + """Response from Async Inference endpoint |
| 25 | +
|
| 26 | + This response object provides a method to check the async Amazon S3 |
| 27 | + output path. If result object exists in that path, decode and return |
| 28 | + the result |
| 29 | + """ |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + predictor_async, |
| 34 | + output_path, |
| 35 | + ): |
| 36 | + """Initialize an AsyncInferenceResponse object. |
| 37 | +
|
| 38 | + AsyncInferenceResponse can help users to get async inference result |
| 39 | + from the Amazon S3 output path |
| 40 | +
|
| 41 | + Args: |
| 42 | + predictor_async (sagemaker.predictor.AsyncPredictor): The ``AsyncPredictor`` |
| 43 | + that return this response. |
| 44 | + output_path (str): The Amazon S3 location that endpoints upload inference responses |
| 45 | + to. |
| 46 | + """ |
| 47 | + self.predictor_async = predictor_async |
| 48 | + self.output_path = output_path |
| 49 | + self._result = None |
| 50 | + |
| 51 | + def get_result( |
| 52 | + self, |
| 53 | + waiter_config=None, |
| 54 | + ): |
| 55 | + """Get result from the async Amazon S3 output path |
| 56 | +
|
| 57 | + Args: |
| 58 | + waiter_config (sagemaker.async_inference.waiter_config.WaiterConfig): Configuration |
| 59 | + for the waiter. The pre-defined value for the delay between poll is 15 seconds |
| 60 | + and the default max attempts is 60 |
| 61 | + Raises: |
| 62 | + ValueError: If a wrong type of object is provided as ``waiter_config`` |
| 63 | + Returns: |
| 64 | + object: Inference result in the given Amazon S3 output path. If a deserializer was |
| 65 | + specified when creating the AsyncPredictor, the result of the deserializer is |
| 66 | + returned. Otherwise the response returns the sequence of bytes |
| 67 | + as is. |
| 68 | + """ |
| 69 | + if waiter_config is not None and not isinstance(waiter_config, WaiterConfig): |
| 70 | + raise ValueError("waiter_config should be a WaiterConfig object") |
| 71 | + |
| 72 | + if self._result is None: |
| 73 | + if waiter_config is None: |
| 74 | + self._result = self._get_result_from_s3(self.output_path) |
| 75 | + else: |
| 76 | + self._result = self.predictor_async._wait_for_output( |
| 77 | + self.output_path, waiter_config |
| 78 | + ) |
| 79 | + return self._result |
| 80 | + |
| 81 | + def _get_result_from_s3( |
| 82 | + self, |
| 83 | + output_path, |
| 84 | + ): |
| 85 | + """Get inference result from the output Amazon S3 path""" |
| 86 | + bucket, key = parse_s3_url(output_path) |
| 87 | + try: |
| 88 | + response = self.predictor_async.s3_client.get_object(Bucket=bucket, Key=key) |
| 89 | + return self.predictor_async.predictor._handle_response(response) |
| 90 | + except ClientError as ex: |
| 91 | + if ex.response["Error"]["Code"] == "NoSuchKey": |
| 92 | + raise ObjectNotExistedError( |
| 93 | + message="Inference could still be running", |
| 94 | + output_path=output_path, |
| 95 | + ) |
| 96 | + raise UnexpectedClientError( |
| 97 | + message=ex.response["Error"]["Message"], |
| 98 | + ) |
0 commit comments