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


_lowercase		:				List[Any]	       =		get_tests_dir("fixtures/dummy-config.json")





class 					__magic_name__		(					unittest.TestCase):







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										lowercase_      :    Optional[int]       		= 0







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										self.assertIsNotNone(transformers.models.auto.__spec__					)
										self.assertIsNotNone(importlib.util.find_spec("""transformers.models.auto"""					)					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										lowercase_      :    Any       		= AutoConfig.from_pretrained("""bert-base-uncased"""					)
										self.assertIsInstance(lowercase_       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										lowercase_      :    List[Any]       		= AutoConfig.from_pretrained(lowercase_					)
										self.assertIsInstance(lowercase_       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						str					):
										lowercase_      :    List[Any]       		= AutoConfig.from_pretrained(lowercase_					)
										self.assertIsInstance(lowercase_       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										lowercase_      :    str       		= AutoConfig.for_model("""roberta"""					)
										self.assertIsInstance(lowercase_       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						List[str]					):
										with tempfile.TemporaryDirectory() as tmp_dir:
																	# This model name contains bert and roberta, but roberta ends up being picked.
																	lowercase_      :    Union[str, Any]       		= os.path.join(lowercase_       ,    """fake-roberta"""					)
																	os.makedirs(lowercase_       ,    exist_ok=lowercase_					)
																	with open(os.path.join(lowercase_       ,    """config.json"""					)       ,    """w"""					) as f:
																								f.write(json.dumps({}					)					)
																	lowercase_      :    Any       		= AutoConfig.from_pretrained(lowercase_					)
																	self.assertEqual(type(lowercase_					)       ,    lowercase_					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):
										try:
																	AutoConfig.register("""custom"""       ,    lowercase_					)
																	# Wrong model type will raise an error
																	with self.assertRaises(lowercase_					):
																								AutoConfig.register("""model"""       ,    lowercase_					)
																	# Trying to register something existing in the Transformers library will raise an error
																	with self.assertRaises(lowercase_					):
																								AutoConfig.register("""bert"""       ,    lowercase_					)

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

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Any					):
										with self.assertRaisesRegex(
										    lowercase_       ,    """bert-base is not a local folder and is not a valid model identifier"""					):
																	lowercase_      :    Optional[int]       		= AutoConfig.from_pretrained("""bert-base"""					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										with self.assertRaisesRegex(
										    lowercase_       ,    r"""aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"""					):
																	lowercase_      :    Optional[Any]       		= AutoConfig.from_pretrained(lowercase_       ,    revision="""aaaaaa"""					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[int]					):
										with self.assertRaisesRegex(
										    lowercase_       ,    """hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."""       ,    ):
																	lowercase_      :    Tuple       		= AutoConfig.from_pretrained("""hf-internal-testing/no-config-test-repo"""					)







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Optional[Any]					):
										# If remote code is not set, we will time out when asking whether to load the model.
										with self.assertRaises(lowercase_					):
																	lowercase_      :    str       		= AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""					)
										# If remote code is disabled, we can't load this config.
										with self.assertRaises(lowercase_					):
																	lowercase_      :    Union[str, Any]       		= AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""       ,    trust_remote_code=lowercase_					)

										lowercase_      :    Dict       		= AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""       ,    trust_remote_code=lowercase_					)
										self.assertEqual(config.__class__.__name__       ,    """NewModelConfig"""					)

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







			def 			SCREAMING_SNAKE_CASE_  (			self       :						Union[str, Any]					):





										class 					__magic_name__		(					_UpperCAmelCase):
													UpperCamelCase__       = '''new-model'''

										try:
																	AutoConfig.register("""new-model"""       ,    lowercase_					)
																	# If remote code is not set, the default is to use local
																	lowercase_      :    Dict       		= 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.
																	lowercase_      :    Dict       		= AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""       ,    trust_remote_code=lowercase_					)
																	self.assertEqual(config.__class__.__name__       ,    """NewModelConfigLocal"""					)

																	# If remote is enabled, we load from the Hub
																	lowercase_      :    Optional[Any]       		= AutoConfig.from_pretrained("""hf-internal-testing/test_dynamic_model"""       ,    trust_remote_code=lowercase_					)
																	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

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_lowercase		:				List[Any]	       =		{"configuration_focalnet": ["FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "FocalNetConfig"]}


try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowercase		:				int	       =		[
						    "FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST",
						    "FocalNetForImageClassification",
						    "FocalNetForMaskedImageModeling",
						    "FocalNetBackbone",
						    "FocalNetModel",
						    "FocalNetPreTrainedModel",
						]

if TYPE_CHECKING:
						from .configuration_focalnet import FOCALNET_PRETRAINED_CONFIG_ARCHIVE_MAP, FocalNetConfig

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_focalnet import (
												    FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST,
												    FocalNetBackbone,
												    FocalNetForImageClassification,
												    FocalNetForMaskedImageModeling,
												    FocalNetModel,
												    FocalNetPreTrainedModel,
												)

else:
						import sys

						_lowercase		:				Union[str, Any]	       =		_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





