|
| 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 | +"""This module contains functions to obtain JumpStart model payloads.""" |
| 14 | +from __future__ import absolute_import |
| 15 | +from copy import deepcopy |
| 16 | +from typing import Dict, Optional |
| 17 | +from sagemaker.jumpstart.constants import ( |
| 18 | + DEFAULT_JUMPSTART_SAGEMAKER_SESSION, |
| 19 | + JUMPSTART_DEFAULT_REGION_NAME, |
| 20 | +) |
| 21 | +from sagemaker.jumpstart.enums import ( |
| 22 | + JumpStartScriptScope, |
| 23 | +) |
| 24 | +from sagemaker.jumpstart.types import JumpStartSerializablePayload |
| 25 | +from sagemaker.jumpstart.utils import ( |
| 26 | + verify_model_region_and_return_specs, |
| 27 | +) |
| 28 | +from sagemaker.session import Session |
| 29 | + |
| 30 | + |
| 31 | +def _retrieve_example_payloads( |
| 32 | + model_id: str, |
| 33 | + model_version: str, |
| 34 | + region: Optional[str], |
| 35 | + tolerate_vulnerable_model: bool = False, |
| 36 | + tolerate_deprecated_model: bool = False, |
| 37 | + sagemaker_session: Session = DEFAULT_JUMPSTART_SAGEMAKER_SESSION, |
| 38 | +) -> Optional[Dict[str, JumpStartSerializablePayload]]: |
| 39 | + """Returns example payloads. |
| 40 | +
|
| 41 | + Args: |
| 42 | + model_id (str): JumpStart model ID of the JumpStart model for which to |
| 43 | + get example payloads. |
| 44 | + model_version (str): Version of the JumpStart model for which to retrieve the |
| 45 | + example payloads. |
| 46 | + region (Optional[str]): Region for which to retrieve the |
| 47 | + example payloads. |
| 48 | + tolerate_vulnerable_model (bool): True if vulnerable versions of model |
| 49 | + specifications should be tolerated (exception not raised). If False, raises an |
| 50 | + exception if the script used by this version of the model has dependencies with known |
| 51 | + security vulnerabilities. (Default: False). |
| 52 | + tolerate_deprecated_model (bool): True if deprecated versions of model |
| 53 | + specifications should be tolerated (exception not raised). If False, raises |
| 54 | + an exception if the version of the model is deprecated. (Default: False). |
| 55 | + sagemaker_session (sagemaker.session.Session): A SageMaker Session |
| 56 | + object, used for SageMaker interactions. If not |
| 57 | + specified, one is created using the default AWS configuration |
| 58 | + chain. (Default: sagemaker.jumpstart.constants.DEFAULT_JUMPSTART_SAGEMAKER_SESSION). |
| 59 | + Returns: |
| 60 | + Optional[Dict[str, JumpStartSerializablePayload]]: dictionary mapping payload aliases |
| 61 | + to the serializable payload object. |
| 62 | + """ |
| 63 | + |
| 64 | + if region is None: |
| 65 | + region = JUMPSTART_DEFAULT_REGION_NAME |
| 66 | + |
| 67 | + model_specs = verify_model_region_and_return_specs( |
| 68 | + model_id=model_id, |
| 69 | + version=model_version, |
| 70 | + scope=JumpStartScriptScope.INFERENCE, |
| 71 | + region=region, |
| 72 | + tolerate_vulnerable_model=tolerate_vulnerable_model, |
| 73 | + tolerate_deprecated_model=tolerate_deprecated_model, |
| 74 | + sagemaker_session=sagemaker_session, |
| 75 | + ) |
| 76 | + |
| 77 | + default_payloads = model_specs.default_payloads |
| 78 | + |
| 79 | + if default_payloads: |
| 80 | + for payload in default_payloads.values(): |
| 81 | + payload.accept = getattr( |
| 82 | + payload, "accept", model_specs.predictor_specs.default_accept_type |
| 83 | + ) |
| 84 | + |
| 85 | + return deepcopy(default_payloads) if default_payloads else None |
0 commit comments