





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


_SCREAMING_SNAKE_CASE					=						get_tests_dir("""fixtures""")
_SCREAMING_SNAKE_CASE					=						get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
_SCREAMING_SNAKE_CASE					=						get_tests_dir("""fixtures/dummy-config.json""")
class SCREAMING_SNAKE_CASE_  (   unittest.TestCase   ):






						def UpperCAmelCase_			( self : Tuple		)							->      Dict:







							"""simple docstring"""


							snake_case_						:     List[Any]     =		0






						def UpperCAmelCase_			( self : Tuple		)							->      Optional[Any]:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : Any		)							->      List[Any]:







							"""simple docstring"""


							snake_case_						:     List[str]     =		AutoFeatureExtractor.from_pretrained(_A		)
							self.assertIsInstance(_A						,				_A		)






						def UpperCAmelCase_			( self : Any		)							->      Union[str, Any]:







							"""simple docstring"""


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

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

								config_dict.pop('feature_extractor_type'		)
								snake_case_						:     List[str]     =		WavaVecaFeatureExtractor(**_A		)

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

								snake_case_						:     Union[str, Any]     =		AutoFeatureExtractor.from_pretrained(_A		)

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

							self.assertIsInstance(_A						,				_A		)






						def UpperCAmelCase_			( self : Tuple		)							->      str:







							"""simple docstring"""


							snake_case_						:     Any     =		AutoFeatureExtractor.from_pretrained(_A		)
							self.assertIsInstance(_A						,				_A		)






						def UpperCAmelCase_			( self : Optional[int]		)							->      List[Any]:







							"""simple docstring"""


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






						def UpperCAmelCase_			( self : Dict		)							->      Optional[int]:







							"""simple docstring"""


							with self.assertRaisesRegex(
							    _A						,				R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'		):
								snake_case_						:     Optional[int]     =		AutoFeatureExtractor.from_pretrained(_A						,				revision='aaaaaa'		)






						def UpperCAmelCase_			( self : List[str]		)							->      Union[str, Any]:







							"""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_						:     int     =		AutoFeatureExtractor.from_pretrained('hf-internal-testing/config-no-model'		)






						def UpperCAmelCase_			( self : List[str]		)							->      Optional[Any]:







							"""simple docstring"""


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

							snake_case_						:     str     =		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_						:     List[str]     =		AutoFeatureExtractor.from_pretrained(_A						,				trust_remote_code=_A		)
							self.assertEqual(reloaded_feature_extractor.__class__.__name__						,				'NewFeatureExtractor'		)






						def UpperCAmelCase_			( self : Any		)							->      List[str]:







							"""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_						:     Tuple     =		CustomFeatureExtractor.from_pretrained(_A		)
								with tempfile.TemporaryDirectory() as tmp_dir:
									feature_extractor.save_pretrained(_A		)
									snake_case_						:     Tuple     =		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 UpperCAmelCase_			( self : Dict		)							->      List[Any]:







							"""simple docstring"""


							class SCREAMING_SNAKE_CASE_  (   snake_case_   ):
													__magic_name__:     Dict	 =    True

							try:
								AutoConfig.register('custom'						,				_A		)
								AutoFeatureExtractor.register(_A						,				_A		)
								# If remote code is not set, the default is to use local
								snake_case_						:     Any     =		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_						:     Any     =		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_						:     Tuple     =		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 typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


_SCREAMING_SNAKE_CASE 		=						{
    """configuration_xlm""": ["""XLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """XLMConfig""", """XLMOnnxConfig"""],
    """tokenization_xlm""": ["""XLMTokenizer"""],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_SCREAMING_SNAKE_CASE 		=						[
						    """XLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """XLMForMultipleChoice""",
						    """XLMForQuestionAnswering""",
						    """XLMForQuestionAnsweringSimple""",
						    """XLMForSequenceClassification""",
						    """XLMForTokenClassification""",
						    """XLMModel""",
						    """XLMPreTrainedModel""",
						    """XLMWithLMHeadModel""",
						]

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_SCREAMING_SNAKE_CASE 		=						[
						    """TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
						    """TFXLMForMultipleChoice""",
						    """TFXLMForQuestionAnsweringSimple""",
						    """TFXLMForSequenceClassification""",
						    """TFXLMForTokenClassification""",
						    """TFXLMMainLayer""",
						    """TFXLMModel""",
						    """TFXLMPreTrainedModel""",
						    """TFXLMWithLMHeadModel""",
						]


if TYPE_CHECKING:
						from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
						from .tokenization_xlm import XLMTokenizer

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_xlm import (
												    XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    XLMForMultipleChoice,
												    XLMForQuestionAnswering,
												    XLMForQuestionAnsweringSimple,
												    XLMForSequenceClassification,
												    XLMForTokenClassification,
												    XLMModel,
												    XLMPreTrainedModel,
												    XLMWithLMHeadModel,
												)

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_xlm import (
												    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFXLMForMultipleChoice,
												    TFXLMForQuestionAnsweringSimple,
												    TFXLMForSequenceClassification,
												    TFXLMForTokenClassification,
												    TFXLMMainLayer,
												    TFXLMModel,
												    TFXLMPreTrainedModel,
												    TFXLMWithLMHeadModel,
												)

else:
						import sys

						_SCREAMING_SNAKE_CASE 		=						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

