




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__ :   Tuple						=							get_tests_dir('''fixtures''')
UpperCamelCase__ :   Any						=							get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
UpperCamelCase__ :   Any						=							get_tests_dir('''fixtures/dummy-config.json''')







class 				lowerCAmelCase_       (					unittest.TestCase							):






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      0






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Tuple					  =      AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h' )
							self.assertIsInstance(snake_case__      ,snake_case__ )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      AutoFeatureExtractor.from_pretrained(snake_case__ )
							self.assertIsInstance(snake_case__      ,snake_case__ )






				def 		snake_case	(			self ):
							with tempfile.TemporaryDirectory() as tmpdirname:
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      WavaVecaConfig()

										# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      AutoFeatureExtractor.from_pretrained(snake_case__ ).to_dict()

										config_dict.pop('feature_extractor_type' )
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      WavaVecaFeatureExtractor(**snake_case__ )

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

										SCREAMING_SNAKE_CASE_   : List[str]					  =      AutoFeatureExtractor.from_pretrained(snake_case__ )

										# make sure private variable is not incorrectly saved
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      json.loads(config.to_json_string() )
										self.assertTrue('_processor_class' not in dict_as_saved )

							self.assertIsInstance(snake_case__      ,snake_case__ )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : List[str]					  =      AutoFeatureExtractor.from_pretrained(snake_case__ )
							self.assertIsInstance(snake_case__      ,snake_case__ )






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






				def 		snake_case	(			self ):
							with self.assertRaisesRegex(
							    snake_case__      ,R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)' ):
										SCREAMING_SNAKE_CASE_   : Any					  =      AutoFeatureExtractor.from_pretrained(snake_case__      ,revision='aaaaaa' )






				def 		snake_case	(			self ):
							with self.assertRaisesRegex(
							    snake_case__      ,'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'      ,):
										SCREAMING_SNAKE_CASE_   : Any					  =      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(snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      AutoFeatureExtractor.from_pretrained(
										    'hf-internal-testing/test_dynamic_feature_extractor' )
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(snake_case__ ):
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      AutoFeatureExtractor.from_pretrained(
										    'hf-internal-testing/test_dynamic_feature_extractor'      ,trust_remote_code=snake_case__ )

							SCREAMING_SNAKE_CASE_   : Tuple					  =      AutoFeatureExtractor.from_pretrained(
							    'hf-internal-testing/test_dynamic_feature_extractor'      ,trust_remote_code=snake_case__ )
							self.assertEqual(feature_extractor.__class__.__name__      ,'NewFeatureExtractor' )

							# Test feature extractor can be reloaded.
							with tempfile.TemporaryDirectory() as tmp_dir:
										feature_extractor.save_pretrained(snake_case__ )
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      AutoFeatureExtractor.from_pretrained(snake_case__      ,trust_remote_code=snake_case__ )
							self.assertEqual(reloaded_feature_extractor.__class__.__name__      ,'NewFeatureExtractor' )






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

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

							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 				lowerCAmelCase_       (					lowerCamelCase_							):
											__a     :       Any						=     True

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

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









def        __UpperCAmelCase						(							lowerCamelCase_ :     str  )       ->  bool:







			"""simple docstring"""





			SCREAMING_SNAKE_CASE_   : Any					  =      0
			for ch in input_str:
						SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      ord(lowerCamelCase_  )
						SCREAMING_SNAKE_CASE_   : Tuple					  =      pow(2  ,							lowerCamelCase_  )

						# If we already turned on bit for current character's unicode
						if bitmap >> ch_unicode & 1 == 1:
									return False
						bitmap |= ch_bit_index_on
			return True


if __name__ == "__main__":
						import doctest

						doctest.testmod()



