|
| 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 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +from unittest import TestCase |
| 16 | +from urllib.error import HTTPError |
| 17 | +from unittest.mock import Mock, patch |
| 18 | +from sagemaker.huggingface.llm_utils import get_huggingface_model_metadata |
| 19 | + |
| 20 | +MOCK_HF_ID = "mock_hf_id" |
| 21 | +MOCK_HF_HUB_TOKEN = "mock_hf_hub_token" |
| 22 | +MOCK_HF_MODEL_METADATA_JSON = {"mock_key": "mock_value"} |
| 23 | + |
| 24 | + |
| 25 | +class LlmUtilsTests(TestCase): |
| 26 | + @patch("sagemaker.huggingface.llm_utils.urllib") |
| 27 | + @patch("sagemaker.huggingface.llm_utils.json") |
| 28 | + def test_huggingface_model_metadata_success(self, mock_json, mock_urllib): |
| 29 | + mock_json.load.return_value = MOCK_HF_MODEL_METADATA_JSON |
| 30 | + ret_json = get_huggingface_model_metadata(MOCK_HF_ID) |
| 31 | + |
| 32 | + mock_urllib.request.urlopen.assert_called_once_with( |
| 33 | + f"https://huggingface.co/api/models/{MOCK_HF_ID}" |
| 34 | + ) |
| 35 | + self.assertEqual(ret_json["mock_key"], "mock_value") |
| 36 | + |
| 37 | + @patch("sagemaker.huggingface.llm_utils.urllib") |
| 38 | + @patch("sagemaker.huggingface.llm_utils.json") |
| 39 | + def test_huggingface_model_metadata_gated_success(self, mock_json, mock_urllib): |
| 40 | + mock_json.load.return_value = MOCK_HF_MODEL_METADATA_JSON |
| 41 | + mock_hf_model_metadata_url = Mock() |
| 42 | + mock_urllib.request.Request.side_effect = mock_hf_model_metadata_url |
| 43 | + |
| 44 | + ret_json = get_huggingface_model_metadata(MOCK_HF_ID, MOCK_HF_HUB_TOKEN) |
| 45 | + |
| 46 | + mock_urllib.request.Request.assert_called_once_with( |
| 47 | + f"https://huggingface.co/api/models/{MOCK_HF_ID}", |
| 48 | + None, |
| 49 | + {"Authorization": "Bearer " + MOCK_HF_HUB_TOKEN}, |
| 50 | + ) |
| 51 | + self.assertEqual(ret_json["mock_key"], "mock_value") |
| 52 | + |
| 53 | + @patch("sagemaker.huggingface.llm_utils.urllib") |
| 54 | + def test_huggingface_model_metadata_unauthorized_exception(self, mock_urllib): |
| 55 | + mock_urllib.request.urlopen.side_effect = HTTPError( |
| 56 | + code=401, msg="Unauthorized", url=None, hdrs=None, fp=None |
| 57 | + ) |
| 58 | + with self.assertRaises(ValueError) as context: |
| 59 | + get_huggingface_model_metadata(MOCK_HF_ID) |
| 60 | + |
| 61 | + expected_error_msg = ( |
| 62 | + "Trying to access a gated/private HuggingFace model without valid credentials. " |
| 63 | + "Please provide a HUGGING_FACE_HUB_TOKEN in env_vars" |
| 64 | + ) |
| 65 | + self.assertEquals(expected_error_msg, str(context.exception)) |
| 66 | + |
| 67 | + @patch("sagemaker.huggingface.llm_utils.urllib") |
| 68 | + def test_huggingface_model_metadata_general_exception(self, mock_urllib): |
| 69 | + mock_urllib.request.urlopen.side_effect = TimeoutError("timed out") |
| 70 | + with self.assertRaises(ValueError) as context: |
| 71 | + get_huggingface_model_metadata(MOCK_HF_ID) |
| 72 | + |
| 73 | + expected_error_msg = ( |
| 74 | + f"Did not find model metadata for the following HuggingFace Model ID {MOCK_HF_ID}" |
| 75 | + ) |
| 76 | + self.assertEquals(expected_error_msg, str(context.exception)) |
0 commit comments