

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


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







class 						__magic_name__			(							unittest.TestCase							):

   '''simple docstring'''




   def  _UpperCAmelCase (				self       )							->    Any:






     """simple docstring"""






     a__  =0



   def  _UpperCAmelCase (				self       )							->    int:






     """simple docstring"""






     a__  =AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''       )
     self.assertIsInstance(__a,    __a       )



   def  _UpperCAmelCase (				self       )							->    str:






     """simple docstring"""






     a__  =AutoFeatureExtractor.from_pretrained(__a       )
     self.assertIsInstance(__a,    __a       )



   def  _UpperCAmelCase (				self       )							->    Optional[int]:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    List[str]:






     """simple docstring"""






     a__  =AutoFeatureExtractor.from_pretrained(__a       )
     self.assertIsInstance(__a,    __a       )



   def  _UpperCAmelCase (				self       )							->    List[Any]:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    Dict:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    Tuple:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    Union[str, Any]:






     """simple docstring"""






     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  _UpperCAmelCase (				self       )							->    Dict:






     """simple docstring"""






     class 						__magic_name__			(							__a							):

        '''simple docstring'''




        lowerCamelCase__ :	Tuple    =   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]








lowerCamelCase       :       Optional[Any]        =				'''ABCDEFGHIJKLMNOPQRSTUVWXYZ'''
def 					snake_case_						(   ):
							__lowercase					:							List[str]			=   input("""Enter message: """					)
							__lowercase					:							int			=   input("""Enter key [alphanumeric]: """					)
							__lowercase					:							Optional[Any]			=   input("""Encrypt/Decrypt [e/d]: """					)

							if mode.lower().startswith("""e"""					):
														__lowercase					:							Optional[int]			=   """encrypt"""
														__lowercase					:							Dict			=   encrypt_message(lowerCAmelCase_				,				lowerCAmelCase_					)
							elif mode.lower().startswith("""d"""					):
														__lowercase					:							Union[str, Any]			=   """decrypt"""
														__lowercase					:							Optional[int]			=   decrypt_message(lowerCAmelCase_				,				lowerCAmelCase_					)

							print(F"\n{mode.title()}ed message:"					)
							print(lowerCAmelCase_					)
def 					snake_case_						(   lowerCAmelCase_				:  str				,				lowerCAmelCase_				:  str					):
							return translate_message(lowerCAmelCase_				,				lowerCAmelCase_				,				"""encrypt"""					)
def 					snake_case_						(   lowerCAmelCase_				:  str				,				lowerCAmelCase_				:  str					):
							return translate_message(lowerCAmelCase_				,				lowerCAmelCase_				,				"""decrypt"""					)
def 					snake_case_						(   lowerCAmelCase_				:  str				,				lowerCAmelCase_				:  str				,				lowerCAmelCase_				:  str					):
							__lowercase					:							Union[str, Any]			=   []
							__lowercase					:							Tuple			=   0
							__lowercase					:							Dict			=   key.upper()

							for symbol in message:
														__lowercase					:							Optional[Any]			=   LETTERS.find(symbol.upper()					)
														if num != -1:
																					if mode == "encrypt":
																												num += LETTERS.find(key[key_index]					)
																					elif mode == "decrypt":
																												num -= LETTERS.find(key[key_index]					)

																					num %= len(lowerCAmelCase_					)

																					if symbol.isupper():
																												translated.append(LETTERS[num]					)
																					elif symbol.islower():
																												translated.append(LETTERS[num].lower()					)

																					key_index += 1
																					if key_index == len(lowerCAmelCase_					):
																												__lowercase					:							str			=   0
														else:
																					translated.append(lowerCAmelCase_					)
							return "".join(lowerCAmelCase_					)


if __name__ == "__main__":
	main()