
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											=     get_tests_dir('fixtures')
A											=     get_tests_dir('fixtures/dummy_feature_extractor_config.json')
A											=     get_tests_dir('fixtures/dummy-config.json')


class SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= 0


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= AutoFeatureExtractor.from_pretrained(A__							)
				self.assertIsInstance(A__ ,				A__							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				with tempfile.TemporaryDirectory() as tmpdirname:
					snake_case_      		= WavaVecaConfig()

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

					config_dict.pop('feature_extractor_type'							)
					snake_case_      		= WavaVecaFeatureExtractor(**A__							)

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

					snake_case_      		= AutoFeatureExtractor.from_pretrained(A__							)

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

				self.assertIsInstance(A__ ,				A__							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= 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'							):
					snake_case_      		= 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\)'							):
					snake_case_      		= 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.' ,				):
					snake_case_      		= AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


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

				snake_case_      		= 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__							)
					snake_case_      		= 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
					snake_case_      		= CustomFeatureExtractor.from_pretrained(A__							)
					with tempfile.TemporaryDirectory() as tmp_dir:
						feature_extractor.save_pretrained(A__							)
						snake_case_      		= 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 SCREAMING_SNAKE_CASE  (		__snake_case							):






							"""simple docstring"""




							__A						  =  True

				try:
					AutoConfig.register('custom' ,				A__							)
					AutoFeatureExtractor.register(A__ ,				A__							)
					# If remote code is not set, the default is to use local
					snake_case_      		= 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.
					snake_case_      		= 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
					snake_case_      		= 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]



from ..utils import DummyObject, requires_backends


class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







class SCREAMING_SNAKE_CASE  (		metaclass=__snake_case							):






			"""simple docstring"""




			__A						  =  ["""torch""", """transformers""", """onnx"""]


			def __init__(    self ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(self ,				['torch', 'transformers', 'onnx']							)


			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)







			@classmethod
			def 			__lowerCAmelCase (    cls ,				*__UpperCamelCase ,				**__UpperCamelCase							):

				"""simple docstring"""


				requires_backends(cls ,				['torch', 'transformers', 'onnx']							)
