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


lowerCamelCase__           =	get_tests_dir("""fixtures""")
lowerCamelCase__           =	get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
lowerCamelCase__           =	get_tests_dir("""fixtures/dummy-config.json""")




class 				A__ (			unittest.TestCase):

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Union[str, Any]		    =   0

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Tuple		    =   AutoFeatureExtractor.from_pretrained('facebook/wav2vec2-base-960h'				)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		List[Any]		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self				):
			with tempfile.TemporaryDirectory() as tmpdirname:
				__lowerCAmelCase    :		str		    =   WavaVecaConfig()

				# remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
				__lowerCAmelCase    :		Any		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				).to_dict()

				config_dict.pop('feature_extractor_type'				)
				__lowerCAmelCase    :		Any		    =   WavaVecaFeatureExtractor(**_SCREAMING_SNAKE_CASE				)

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

				__lowerCAmelCase    :		int		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				)

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

			self.assertIsInstance(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self				):
			__lowerCAmelCase    :		Optional[int]		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				)
			self.assertIsInstance(_SCREAMING_SNAKE_CASE ,	_SCREAMING_SNAKE_CASE				)

		def 							__lowerCamelCase							(   self				):
			with self.assertRaisesRegex(
			    _SCREAMING_SNAKE_CASE ,	'bert-base is not a local folder and is not a valid model identifier'				):
				__lowerCAmelCase    :		int		    =   AutoFeatureExtractor.from_pretrained('bert-base'				)

		def 							__lowerCamelCase							(   self				):
			with self.assertRaisesRegex(
			    _SCREAMING_SNAKE_CASE ,	R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'				):
				__lowerCAmelCase    :		int		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE ,	revision='aaaaaa'				)

		def 							__lowerCamelCase							(   self				):
			with self.assertRaisesRegex(
			    _SCREAMING_SNAKE_CASE ,	'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.' ,	):
				__lowerCAmelCase    :		List[Any]		    =   AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'				)

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

			__lowerCAmelCase    :		str		    =   AutoFeatureExtractor.from_pretrained(
			    'hf-internal-testing/test_dynamic_feature_extractor' ,	trust_remote_code=_SCREAMING_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(_SCREAMING_SNAKE_CASE				)
				__lowerCAmelCase    :		List[Any]		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE ,	trust_remote_code=_SCREAMING_SNAKE_CASE				)
			self.assertEqual(reloaded_feature_extractor.__class__.__name__ ,	'NewFeatureExtractor'				)

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

				# Now that the config is registered, it can be used as any other config with the auto-API
				__lowerCAmelCase    :		Union[str, Any]		    =   CustomFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				)
				with tempfile.TemporaryDirectory() as tmp_dir:
					feature_extractor.save_pretrained(_SCREAMING_SNAKE_CASE				)
					__lowerCAmelCase    :		Any		    =   AutoFeatureExtractor.from_pretrained(_SCREAMING_SNAKE_CASE				)
					self.assertIsInstance(_SCREAMING_SNAKE_CASE ,	_SCREAMING_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 							__lowerCamelCase							(   self				):




			class 				A__ (			_lowerCamelCase):
					A_		: Any            =			True

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

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

"""simple docstring"""







from .testing import (
    are_the_same_tensors,
    execute_subprocess_async,
    require_bnb,
    require_cpu,
    require_cuda,
    require_huggingface_suite,
    require_mps,
    require_multi_gpu,
    require_multi_xpu,
    require_safetensors,
    require_single_gpu,
    require_single_xpu,
    require_torch_min_version,
    require_tpu,
    require_xpu,
    skip,
    slow,
)
from .training import RegressionDataset, RegressionModel, RegressionModelaXPU


from .scripts import test_script, test_sync, test_ops  # isort: skip