


import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


SCREAMING_SNAKE_CASE							    =							get_tests_dir('fixtures')
SCREAMING_SNAKE_CASE							    =							get_tests_dir('fixtures/dummy_feature_extractor_config.json')
SCREAMING_SNAKE_CASE							    =							get_tests_dir('fixtures/dummy-config.json')


class     __UpperCAmelCase							(       unittest.TestCase							):


     """simple docstring"""


     def 	snake_case_  (	self    ):
        __a					=  0


     def 	snake_case_  (	self    ):
        __a					=  AutoFeatureExtractor.from_pretrained("""facebook/wav2vec2-base-960h"""    )
        self.assertIsInstance(__A  ,       __A    )


     def 	snake_case_  (	self    ):
        __a					=  AutoFeatureExtractor.from_pretrained(__A    )
        self.assertIsInstance(__A  ,       __A    )


     def 	snake_case_  (	self    ):
        with tempfile.TemporaryDirectory() as tmpdirname:
           __a					=  WavaVecaConfig()

           # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
           __a					=  AutoFeatureExtractor.from_pretrained(__A    ).to_dict()

           config_dict.pop("""feature_extractor_type"""    )
           __a					=  WavaVecaFeatureExtractor(**__A    )

           # save in new folder
           model_config.save_pretrained(__A    )
           config.save_pretrained(__A    )

           __a					=  AutoFeatureExtractor.from_pretrained(__A    )

           # make sure private variable is not incorrectly saved
           __a					=  json.loads(config.to_json_string()    )
           self.assertTrue("""_processor_class""" not in dict_as_saved    )

        self.assertIsInstance(__A  ,       __A    )


     def 	snake_case_  (	self    ):
        __a					=  AutoFeatureExtractor.from_pretrained(__A    )
        self.assertIsInstance(__A  ,       __A    )


     def 	snake_case_  (	self    ):
        with self.assertRaisesRegex(
            __A  ,       """bert-base is not a local folder and is not a valid model identifier"""    ):
           __a					=  AutoFeatureExtractor.from_pretrained("""bert-base"""    )


     def 	snake_case_  (	self    ):
        with self.assertRaisesRegex(
            __A  ,       R"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""    ):
           __a					=  AutoFeatureExtractor.from_pretrained(__A  ,       revision="""aaaaaa"""    )


     def 	snake_case_  (	self    ):
        with self.assertRaisesRegex(
            __A  ,       """hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."""  ,       ):
           __a					=  AutoFeatureExtractor.from_pretrained("""hf-internal-testing/config-no-model"""    )


     def 	snake_case_  (	self    ):
        # If remote code is not set, we will time out when asking whether to load the model.
        with self.assertRaises(__A    ):
           __a					=  AutoFeatureExtractor.from_pretrained(
               """hf-internal-testing/test_dynamic_feature_extractor"""    )
        # If remote code is disabled, we can't load this config.
        with self.assertRaises(__A    ):
           __a					=  AutoFeatureExtractor.from_pretrained(
               """hf-internal-testing/test_dynamic_feature_extractor"""  ,       trust_remote_code=__A    )

        __a					=  AutoFeatureExtractor.from_pretrained(
            """hf-internal-testing/test_dynamic_feature_extractor"""  ,       trust_remote_code=__A    )
        self.assertEqual(feature_extractor.__class__.__name__  ,       """NewFeatureExtractor"""    )

        # Test feature extractor can be reloaded.
        with tempfile.TemporaryDirectory() as tmp_dir:
           feature_extractor.save_pretrained(__A    )
           __a					=  AutoFeatureExtractor.from_pretrained(__A  ,       trust_remote_code=__A    )
        self.assertEqual(reloaded_feature_extractor.__class__.__name__  ,       """NewFeatureExtractor"""    )


     def 	snake_case_  (	self    ):
        try:
           AutoConfig.register("""custom"""  ,       __A    )
           AutoFeatureExtractor.register(__A  ,       __A    )
           # Trying to register something existing in the Transformers library will raise an error
           with self.assertRaises(__A    ):
              AutoFeatureExtractor.register(__A  ,       __A    )

           # Now that the config is registered, it can be used as any other config with the auto-API
           __a					=  CustomFeatureExtractor.from_pretrained(__A    )
           with tempfile.TemporaryDirectory() as tmp_dir:
              feature_extractor.save_pretrained(__A    )
              __a					=  AutoFeatureExtractor.from_pretrained(__A    )
              self.assertIsInstance(__A  ,       __A    )

        finally:
           if "custom" in CONFIG_MAPPING._extra_content:
              del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
              del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]


     def 	snake_case_  (	self    ):


        class     __UpperCAmelCase							(       __A							):


             """simple docstring"""
             _lowerCamelCase   =      True

        try:
           AutoConfig.register("""custom"""  ,       __A    )
           AutoFeatureExtractor.register(__A  ,       __A    )
           # If remote code is not set, the default is to use local
           __a					=  AutoFeatureExtractor.from_pretrained(
               """hf-internal-testing/test_dynamic_feature_extractor"""    )
           self.assertEqual(feature_extractor.__class__.__name__  ,       """NewFeatureExtractor"""    )
           self.assertTrue(feature_extractor.is_local    )

           # If remote code is disabled, we load the local one.
           __a					=  AutoFeatureExtractor.from_pretrained(
               """hf-internal-testing/test_dynamic_feature_extractor"""  ,       trust_remote_code=__A    )
           self.assertEqual(feature_extractor.__class__.__name__  ,       """NewFeatureExtractor"""    )
           self.assertTrue(feature_extractor.is_local    )

           # If remote is enabled, we load from the Hub
           __a					=  AutoFeatureExtractor.from_pretrained(
               """hf-internal-testing/test_dynamic_feature_extractor"""  ,       trust_remote_code=__A    )
           self.assertEqual(feature_extractor.__class__.__name__  ,       """NewFeatureExtractor"""    )
           self.assertTrue(not hasattr(__A  ,       """is_local"""    )    )

        finally:
           if "custom" in CONFIG_MAPPING._extra_content:
              del CONFIG_MAPPING._extra_content["custom"]
           if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
              del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]







'''simple docstring'''






def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   _UpperCAmelCase  :  Optional[Any]				       =set()

   # Replace all the whitespace in our sentence
   _UpperCAmelCase  :  Dict				       =input_str.replace(' '			,       ''				)
   for alpha in input_str:
      if "a" <= alpha.lower() <= "z":
         frequency.add(alpha.lower()				)
   return len(__lowerCamelCase				) == 2_6





def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   _UpperCAmelCase  :  Tuple				       =[False] * 2_6
   for char in input_str:
      if char.islower():
         _UpperCAmelCase  :  Dict				       =True
      elif char.isupper():
         _UpperCAmelCase  :  List[str]				       =True
   return all(__lowerCamelCase				)





def lowerCamelCase__     (		__lowerCamelCase			:				str = "The quick brown fox jumps over the lazy dog"			,       ):







   '''simple docstring'''




   return len({char for char in input_str.lower() if char.isalpha()}				) == 2_6





def lowerCamelCase__     (		):







   '''simple docstring'''




   from timeit import timeit

   _UpperCAmelCase  :  List[Any]				       ='from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest'
   print(timeit('is_pangram()'			,       setup=__lowerCamelCase				)				)
   print(timeit('is_pangram_faster()'			,       setup=__lowerCamelCase				)				)
   print(timeit('is_pangram_fastest()'			,       setup=__lowerCamelCase				)				)
   # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
   # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 benchmark()

