'''simple docstring'''


import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
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


__lowerCAmelCase	:       Dict						       =	get_tests_dir("fixtures/dummy-config.json")






class 						A   (   unittest.TestCase   ):

							'''simple docstring'''





							def       snake_case__						(      self				:							Optional[Any]	)							->	Tuple:
								__UpperCAmelCase							=			0





							def       snake_case__						(      self				:							int	)							->	Any:
								self.assertIsNotNone(transformers.models.auto.__spec__	)
								self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''	)	)





							def       snake_case__						(      self				:							List[str]	)							->	Dict:
								__UpperCAmelCase							=			AutoConfig.from_pretrained('''bert-base-uncased'''	)
								self.assertIsInstance(__lowerCamelCase     ,		__lowerCamelCase	)





							def       snake_case__						(      self				:							Dict	)							->	Dict:
								__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase	)
								self.assertIsInstance(__lowerCamelCase     ,		__lowerCamelCase	)





							def       snake_case__						(      self				:							Any	)							->	Optional[int]:
								__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase	)
								self.assertIsInstance(__lowerCamelCase     ,		__lowerCamelCase	)





							def       snake_case__						(      self				:							Optional[int]	)							->	str:
								__UpperCAmelCase							=			AutoConfig.for_model('''roberta'''	)
								self.assertIsInstance(__lowerCamelCase     ,		__lowerCamelCase	)





							def       snake_case__						(      self				:							Optional[Any]	)							->	Dict:
								with tempfile.TemporaryDirectory() as tmp_dir:
									# This model name contains bert and roberta, but roberta ends up being picked.
									__UpperCAmelCase							=			os.path.join(__lowerCamelCase     ,		'''fake-roberta'''	)
									os.makedirs(__lowerCamelCase     ,		exist_ok=__lowerCamelCase	)
									with open(os.path.join(__lowerCamelCase     ,		'''config.json'''	)     ,		'''w'''	) as f:
										f.write(json.dumps({}	)	)
									__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase	)
									self.assertEqual(type(__lowerCamelCase	)     ,		__lowerCamelCase	)





							def       snake_case__						(      self				:							Optional[Any]	)							->	str:
								try:
									AutoConfig.register('''custom'''     ,		__lowerCamelCase	)
									# Wrong model type will raise an error
									with self.assertRaises(__lowerCamelCase	):
										AutoConfig.register('''model'''     ,		__lowerCamelCase	)
									# Trying to register something existing in the Transformers library will raise an error
									with self.assertRaises(__lowerCamelCase	):
										AutoConfig.register('''bert'''     ,		__lowerCamelCase	)

									# Now that the config is registered, it can be used as any other config with the auto-API
									__UpperCAmelCase							=			CustomConfig()
									with tempfile.TemporaryDirectory() as tmp_dir:
										config.save_pretrained(__lowerCamelCase	)
										__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase	)
										self.assertIsInstance(__lowerCamelCase     ,		__lowerCamelCase	)

								finally:
									if "custom" in CONFIG_MAPPING._extra_content:
										del CONFIG_MAPPING._extra_content["custom"]





							def       snake_case__						(      self				:							Optional[int]	)							->	Optional[int]:
								with self.assertRaisesRegex(
								    __lowerCamelCase     ,		'''bert-base is not a local folder and is not a valid model identifier'''	):
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''bert-base'''	)





							def       snake_case__						(      self				:							Dict	)							->	Any:
								with self.assertRaisesRegex(
								    __lowerCamelCase     ,		r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''	):
									__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase     ,		revision='''aaaaaa'''	)





							def       snake_case__						(      self				:							Optional[int]	)							->	str:
								with self.assertRaisesRegex(
								    __lowerCamelCase     ,		'''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''     ,		):
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo'''	)





							def       snake_case__						(      self				:							Dict	)							->	List[str]:
								# If remote code is not set, we will time out when asking whether to load the model.
								with self.assertRaises(__lowerCamelCase	):
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''	)
								# If remote code is disabled, we can't load this config.
								with self.assertRaises(__lowerCamelCase	):
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''     ,		trust_remote_code=__lowerCamelCase	)

								__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''     ,		trust_remote_code=__lowerCamelCase	)
								self.assertEqual(config.__class__.__name__     ,		'''NewModelConfig'''	)

								# Test config can be reloaded.
								with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(__lowerCamelCase	)
									__UpperCAmelCase							=			AutoConfig.from_pretrained(__lowerCamelCase     ,		trust_remote_code=__lowerCamelCase	)
								self.assertEqual(reloaded_config.__class__.__name__     ,		'''NewModelConfig'''	)






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






								class 						A   (   _A   ):

															'''simple docstring'''





															a_        =      '''new-model'''

								try:
									AutoConfig.register('''new-model'''     ,		__lowerCamelCase	)
									# If remote code is not set, the default is to use local
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''	)
									self.assertEqual(config.__class__.__name__     ,		'''NewModelConfigLocal'''	)

									# If remote code is disabled, we load the local one.
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''     ,		trust_remote_code=__lowerCamelCase	)
									self.assertEqual(config.__class__.__name__     ,		'''NewModelConfigLocal'''	)

									# If remote is enabled, we load from the Hub
									__UpperCAmelCase							=			AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''     ,		trust_remote_code=__lowerCamelCase	)
									self.assertEqual(config.__class__.__name__     ,		'''NewModelConfig'''	)

								finally:
									if "new-model" in CONFIG_MAPPING._extra_content:
										del CONFIG_MAPPING._extra_content["new-model"]

'''simple docstring'''


from typing import TYPE_CHECKING

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


__lowerCAmelCase	:       List[Any]						       =	{
    "configuration_audio_spectrogram_transformer": [
        "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "ASTConfig",
    ]
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowerCAmelCase	:       str						       =	[
					    "AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "ASTForAudioClassification",
					    "ASTModel",
					    "ASTPreTrainedModel",
					]

try:
					if not is_speech_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowerCAmelCase	:       Union[str, Any]						       =	["ASTFeatureExtractor"]

if TYPE_CHECKING:
					from .configuration_audio_spectrogram_transformer import (
					    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
					    ASTConfig,
					)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_audio_spectrogram_transformer import (
										    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    ASTForAudioClassification,
										    ASTModel,
										    ASTPreTrainedModel,
										)

					try:
										if not is_speech_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
					import sys

					__lowerCAmelCase	:       Optional[Any]						       =	_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
