





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


_a     :   Optional[int]						=   get_tests_dir("fixtures")
_a     :   Optional[Any]						=   get_tests_dir("fixtures/dummy_feature_extractor_config.json")
_a     :   Union[str, Any]						=   get_tests_dir("fixtures/dummy-config.json")






class       __A					(					unittest.TestCase				):

			'''simple docstring'''







			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	0





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained(_A							)
				self.assertIsInstance(_A ,						_A							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				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(_A							).to_dict()

					config_dict.pop('''feature_extractor_type'''							)
					lowerCamelCase__              =	WavaVecaFeatureExtractor(**_A							)

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

					lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained(_A							)

					# 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(_A ,						_A							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained(_A							)
				self.assertIsInstance(_A ,						_A							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				with self.assertRaisesRegex(
				    _A ,						'''bert-base is not a local folder and is not a valid model identifier'''							):
					lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained('''bert-base'''							)





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







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





			def 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				with self.assertRaisesRegex(
				    _A ,						'''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 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				with self.assertRaises(_A							):
					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(_A							):
					lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained(
					    '''hf-internal-testing/test_dynamic_feature_extractor''' ,						trust_remote_code=_A							)

				lowerCamelCase__              =	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							)
					lowerCamelCase__              =	AutoFeatureExtractor.from_pretrained(_A ,						trust_remote_code=_A							)
				self.assertEqual(reloaded_feature_extractor.__class__.__name__ ,						'''NewFeatureExtractor'''							)





			def 	__lowerCamelCase       (					self							):



				'''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
					lowerCamelCase__              =	CustomFeatureExtractor.from_pretrained(_A							)
					with tempfile.TemporaryDirectory() as tmp_dir:
						feature_extractor.save_pretrained(_A							)
						lowerCamelCase__              =	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 	__lowerCamelCase       (					self							):



				'''simple docstring'''







				class       __A					(					a__				):

							'''simple docstring'''







							lowerCAmelCase_							  =		True

				try:
					AutoConfig.register('''custom''' ,						_A							)
					AutoFeatureExtractor.register(_A ,						_A							)
					# 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=_A							)
					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=_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]










_a						=   "\n# Transformers 설치 방법\n! pip install transformers datasets\n# 마지막 릴리스 대신 소스에서 설치하려면, 위 명령을 주석으로 바꾸고 아래 명령을 해제하세요.\n# ! pip install git+https://github.com/huggingface/transformers.git\n"

_a						=   [{"type": "code", "content": INSTALL_CONTENT}]
_a						=   {
    "{processor_class}": "FakeProcessorClass",
    "{model_class}": "FakeModelClass",
    "{object_class}": "FakeObjectClass",
}



