


'''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


__snake_case											=					get_tests_dir('''fixtures''')
__snake_case											=					get_tests_dir('''fixtures/dummy_feature_extractor_config.json''')
__snake_case											=					get_tests_dir('''fixtures/dummy-config.json''')
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[int]							 =			0




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			AutoFeatureExtractor.from_pretrained('''facebook/wav2vec2-base-960h'''							)
							self.assertIsInstance(UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			AutoFeatureExtractor.from_pretrained(UpperCamelCase_							)
							self.assertIsInstance(UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							with tempfile.TemporaryDirectory() as tmpdirname:
												UpperCamelCase__       :Union[str, Any]							 =			WavaVecaConfig()

												# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
												UpperCamelCase__       :Optional[int]							 =			AutoFeatureExtractor.from_pretrained(UpperCamelCase_							).to_dict()

												config_dict.pop('''feature_extractor_type'''							)
												UpperCamelCase__       :Tuple							 =			WavaVecaFeatureExtractor(**UpperCamelCase_							)

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

												UpperCamelCase__       :int							 =			AutoFeatureExtractor.from_pretrained(UpperCamelCase_							)

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

							self.assertIsInstance(UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			AutoFeatureExtractor.from_pretrained(UpperCamelCase_							)
							self.assertIsInstance(UpperCamelCase_   ,       UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							with self.assertRaisesRegex(
							    UpperCamelCase_   ,       '''bert-base is not a local folder and is not a valid model identifier'''							):
												UpperCamelCase__       :Optional[Any]							 =			AutoFeatureExtractor.from_pretrained('''bert-base'''							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							with self.assertRaisesRegex(
							    UpperCamelCase_   ,       r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''							):
												UpperCamelCase__       :Any							 =			AutoFeatureExtractor.from_pretrained(UpperCamelCase_   ,       revision='''aaaaaa'''							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							with self.assertRaisesRegex(
							    UpperCamelCase_   ,       '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''   ,       ):
												UpperCamelCase__       :str							 =			AutoFeatureExtractor.from_pretrained('''hf-internal-testing/config-no-model'''							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							with self.assertRaises(UpperCamelCase_							):
												UpperCamelCase__       :List[str]							 =			AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor'''							)
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(UpperCamelCase_							):
												UpperCamelCase__       :Any							 =			AutoFeatureExtractor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=UpperCamelCase_							)

							UpperCamelCase__       :str							 =			AutoFeatureExtractor.from_pretrained(
							    '''hf-internal-testing/test_dynamic_feature_extractor'''   ,       trust_remote_code=UpperCamelCase_							)
							self.assertEqual(feature_extractor.__class__.__name__   ,       '''NewFeatureExtractor'''							)

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




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


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

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

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

							'''simple docstring'''


							class        lowercase     (							A__ ):

									"""simple docstring"""

									_a   			=    True

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

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

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__snake_case											=					{'''configuration_vit_msn''': ['''VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''ViTMSNConfig''']}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case											=					[
		    '''VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ViTMSNModel''',
		    '''ViTMSNForImageClassification''',
		    '''ViTMSNPreTrainedModel''',
		]

if TYPE_CHECKING:
		from .configuration_vit_msn import VIT_MSN_PRETRAINED_CONFIG_ARCHIVE_MAP, ViTMSNConfig

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_vit_msn import (
				    VIT_MSN_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ViTMSNForImageClassification,
				    ViTMSNModel,
				    ViTMSNPreTrainedModel,
				)

else:
		import sys

		__snake_case											=					_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)