


'''simple docstring'''






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


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




class 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      0







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''  )
														self.assertIsInstance(lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_  )
														self.assertIsInstance(lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				str:
														with tempfile.TemporaryDirectory() as tmpdirname:
																					lowerCAmelCase__			  =      WavaVecaConfig()

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

																					config_dict.pop('''feature_extractor_type'''  )
																					lowerCAmelCase__			  =      WavaVecaFeatureExtractor(**lowerCamelCase_  )

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

																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_  )

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

														self.assertIsInstance(lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_  )
														self.assertIsInstance(lowerCamelCase_   ,       lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														with self.assertRaisesRegex(
														    lowerCamelCase_   ,       '''bert-base is not a local folder and is not a valid model identifier'''  ):
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained('''bert-base'''  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[str]:
														with self.assertRaisesRegex(
														    lowerCamelCase_   ,       r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''  ):
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(lowerCamelCase_   ,       revision='''aaaaaa'''  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														with self.assertRaisesRegex(
														    lowerCamelCase_   ,       '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''   ,       ):
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														# If remote code is not set, we will time out when asking whether to load the model.
														with self.assertRaises(lowerCamelCase_  ):
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''  )
														# If remote code is disabled, we can't load this config.
														with self.assertRaises(lowerCamelCase_  ):
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=lowerCamelCase_  )

														lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(
														    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=lowerCamelCase_  )
														self.assertEqual(feature_extractor.__class__.__name__   ,       '''NewFeatureExtractor'''  )

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







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														try:
																					AutoConfig.register('''custom'''   ,       lowerCamelCase_  )
																					AutoFeatureExtractor.register(lowerCamelCase_   ,       lowerCamelCase_  )
																					# Trying to register something existing in the Transformers library will raise an error
																					with self.assertRaises(lowerCamelCase_  ):
																												AutoFeatureExtractor.register(lowerCamelCase_   ,       lowerCamelCase_  )

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

														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     __SCREAMING_SNAKE_CASE				(						self  )			->				str:




														class 						a__							(				a__    ):





																					'''simple docstring'''






																					lowercase__     :  str      		= True

														try:
																					AutoConfig.register('''custom'''   ,       lowerCamelCase_  )
																					AutoFeatureExtractor.register(lowerCamelCase_   ,       lowerCamelCase_  )
																					# If remote code is not set, the default is to use local
																					lowerCAmelCase__			  =      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.
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=lowerCamelCase_  )
																					self.assertEqual(feature_extractor.__class__.__name__   ,       '''NewFeatureExtractor'''  )
																					self.assertTrue(feature_extractor.is_local  )

																					# If remote is enabled, we load from the Hub
																					lowerCAmelCase__			  =      AutoFeatureExtractor.from_pretrained(
																					    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=lowerCamelCase_  )
																					self.assertEqual(feature_extractor.__class__.__name__   ,       '''NewFeatureExtractor'''  )
																					self.assertTrue(not hasattr(lowerCamelCase_   ,       '''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]


import string


def 							__lowerCAmelCase     (							_UpperCamelCase		:	str						)		->				str:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      ''
 for i in sequence:
  SCREAMING_SNAKE_CASE		    =      ord(_UpperCamelCase						)
  if 65 <= extract <= 90:
   output += chr(1_55 - extract						)
  elif 97 <= extract <= 1_22:
   output += chr(2_19 - extract						)
  else:
   output += i
 return output


def 							__lowerCAmelCase     (							_UpperCamelCase		:	str						)		->				str:
 '''simple docstring'''


 SCREAMING_SNAKE_CASE		    =      string.ascii_letters
 SCREAMING_SNAKE_CASE		    =      string.ascii_lowercase[::-1] + string.ascii_uppercase[::-1]
 return "".join(
     letters_reversed[letters.index(_UpperCamelCase						)] if c in letters else c for c in sequence						)


def 							__lowerCAmelCase     (							)		->				None:
 '''simple docstring'''


 from timeit import timeit

 print('Running performance benchmarks...'						)
 SCREAMING_SNAKE_CASE		    =      'from string import printable ; from __main__ import atbash, atbash_slow'
 print(f"""> atbash_slow(): {timeit('atbash_slow(printable)'		,				setup=_UpperCamelCase						)} seconds"""						)
 print(f""">      atbash(): {timeit('atbash(printable)'		,				setup=_UpperCamelCase						)} seconds"""						)


if __name__ == "__main__":
      for example in ("ABCDEFGH", "123GGjj", "testStringtest", "with space"):
            print(F"""{example} encrypted in atbash: {atbash(example)}""")
      benchmark()

