CoolFace
Apppublic

chendl/compositional_test

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
test_feature_extraction_common.py181 linesDownload Raw Back to tests
1# coding=utf-82# Copyright 2021 HuggingFace Inc.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8#     http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15 16 17import json18import os19import sys20import tempfile21import unittest22import unittest.mock as mock23from pathlib import Path24 25from huggingface_hub import HfFolder, delete_repo26from requests.exceptions import HTTPError27 28from transformers import AutoFeatureExtractor, Wav2Vec2FeatureExtractor29from transformers.testing_utils import TOKEN, USER, check_json_file_has_correct_format, get_tests_dir, is_staging_test30 31 32sys.path.append(str(Path(__file__).parent.parent / "utils"))33 34from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E40235 36 37SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR = get_tests_dir("fixtures")38 39 40class FeatureExtractionSavingTestMixin:41    test_cast_dtype = None42 43    def test_feat_extract_to_json_string(self):44        feat_extract = self.feature_extraction_class(**self.feat_extract_dict)45        obj = json.loads(feat_extract.to_json_string())46        for key, value in self.feat_extract_dict.items():47            self.assertEqual(obj[key], value)48 49    def test_feat_extract_to_json_file(self):50        feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)51 52        with tempfile.TemporaryDirectory() as tmpdirname:53            json_file_path = os.path.join(tmpdirname, "feat_extract.json")54            feat_extract_first.to_json_file(json_file_path)55            feat_extract_second = self.feature_extraction_class.from_json_file(json_file_path)56 57        self.assertEqual(feat_extract_second.to_dict(), feat_extract_first.to_dict())58 59    def test_feat_extract_from_and_save_pretrained(self):60        feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)61 62        with tempfile.TemporaryDirectory() as tmpdirname:63            saved_file = feat_extract_first.save_pretrained(tmpdirname)[0]64            check_json_file_has_correct_format(saved_file)65            feat_extract_second = self.feature_extraction_class.from_pretrained(tmpdirname)66 67        self.assertEqual(feat_extract_second.to_dict(), feat_extract_first.to_dict())68 69    def test_init_without_params(self):70        feat_extract = self.feature_extraction_class()71        self.assertIsNotNone(feat_extract)72 73 74class FeatureExtractorUtilTester(unittest.TestCase):75    def test_cached_files_are_used_when_internet_is_down(self):76        # A mock response for an HTTP head request to emulate server down77        response_mock = mock.Mock()78        response_mock.status_code = 50079        response_mock.headers = {}80        response_mock.raise_for_status.side_effect = HTTPError81        response_mock.json.return_value = {}82 83        # Download this model to make sure it's in the cache.84        _ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2")85        # Under the mock environment we get a 500 error when trying to reach the model.86        with mock.patch("requests.request", return_value=response_mock) as mock_head:87            _ = Wav2Vec2FeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2")88            # This check we did call the fake head request89            mock_head.assert_called()90 91    def test_legacy_load_from_url(self):92        # This test is for deprecated behavior and can be removed in v593        _ = Wav2Vec2FeatureExtractor.from_pretrained(94            "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"95        )96 97 98@is_staging_test99class FeatureExtractorPushToHubTester(unittest.TestCase):100    @classmethod101    def setUpClass(cls):102        cls._token = TOKEN103        HfFolder.save_token(TOKEN)104 105    @classmethod106    def tearDownClass(cls):107        try:108            delete_repo(token=cls._token, repo_id="test-feature-extractor")109        except HTTPError:110            pass111 112        try:113            delete_repo(token=cls._token, repo_id="valid_org/test-feature-extractor-org")114        except HTTPError:115            pass116 117        try:118            delete_repo(token=cls._token, repo_id="test-dynamic-feature-extractor")119        except HTTPError:120            pass121 122    def test_push_to_hub(self):123        feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)124        feature_extractor.push_to_hub("test-feature-extractor", use_auth_token=self._token)125 126        new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor")127        for k, v in feature_extractor.__dict__.items():128            self.assertEqual(v, getattr(new_feature_extractor, k))129 130        # Reset repo131        delete_repo(token=self._token, repo_id="test-feature-extractor")132 133        # Push to hub via save_pretrained134        with tempfile.TemporaryDirectory() as tmp_dir:135            feature_extractor.save_pretrained(136                tmp_dir, repo_id="test-feature-extractor", push_to_hub=True, use_auth_token=self._token137            )138 139        new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(f"{USER}/test-feature-extractor")140        for k, v in feature_extractor.__dict__.items():141            self.assertEqual(v, getattr(new_feature_extractor, k))142 143    def test_push_to_hub_in_organization(self):144        feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)145        feature_extractor.push_to_hub("valid_org/test-feature-extractor", use_auth_token=self._token)146 147        new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("valid_org/test-feature-extractor")148        for k, v in feature_extractor.__dict__.items():149            self.assertEqual(v, getattr(new_feature_extractor, k))150 151        # Reset repo152        delete_repo(token=self._token, repo_id="valid_org/test-feature-extractor")153 154        # Push to hub via save_pretrained155        with tempfile.TemporaryDirectory() as tmp_dir:156            feature_extractor.save_pretrained(157                tmp_dir, repo_id="valid_org/test-feature-extractor-org", push_to_hub=True, use_auth_token=self._token158            )159 160        new_feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org")161        for k, v in feature_extractor.__dict__.items():162            self.assertEqual(v, getattr(new_feature_extractor, k))163 164    def test_push_to_hub_dynamic_feature_extractor(self):165        CustomFeatureExtractor.register_for_auto_class()166        feature_extractor = CustomFeatureExtractor.from_pretrained(SAMPLE_FEATURE_EXTRACTION_CONFIG_DIR)167 168        feature_extractor.push_to_hub("test-dynamic-feature-extractor", use_auth_token=self._token)169 170        # This has added the proper auto_map field to the config171        self.assertDictEqual(172            feature_extractor.auto_map,173            {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"},174        )175 176        new_feature_extractor = AutoFeatureExtractor.from_pretrained(177            f"{USER}/test-dynamic-feature-extractor", trust_remote_code=True178        )179        # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module180        self.assertEqual(new_feature_extractor.__class__.__name__, "CustomFeatureExtractor")181