



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


snake_case__       :  Tuple     		= get_tests_dir("""fixtures/dummy-config.json""")

class 	_A (			unittest.TestCase						):


						'''simple docstring'''





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






							'''simple docstring'''



							__lowercase									=      0



						def      _snake_case						(						self						:				Tuple						):






							'''simple docstring'''



							self.assertIsNotNone(transformers.models.auto.__spec__						)
							self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"						)						)



						def      _snake_case						(						self						:				List[Any]						):






							'''simple docstring'''



							__lowercase									=      AutoConfig.from_pretrained("bert-base-uncased"						)
							self.assertIsInstance(UpperCamelCase_				,   UpperCamelCase_						)



						def      _snake_case						(						self						:				Optional[Any]						):






							'''simple docstring'''



							__lowercase									=      AutoConfig.from_pretrained(UpperCamelCase_						)
							self.assertIsInstance(UpperCamelCase_				,   UpperCamelCase_						)



						def      _snake_case						(						self						:				List[str]						):






							'''simple docstring'''



							__lowercase									=      AutoConfig.from_pretrained(UpperCamelCase_						)
							self.assertIsInstance(UpperCamelCase_				,   UpperCamelCase_						)



						def      _snake_case						(						self						:				List[str]						):






							'''simple docstring'''



							__lowercase									=      AutoConfig.for_model("roberta"						)
							self.assertIsInstance(UpperCamelCase_				,   UpperCamelCase_						)



						def      _snake_case						(						self						:				Optional[Any]						):






							'''simple docstring'''



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



						def      _snake_case						(						self						:				Any						):






							'''simple docstring'''



							try:
								AutoConfig.register("custom"				,   UpperCamelCase_						)
								# Wrong model type will raise an error
								with self.assertRaises(UpperCamelCase_						):
									AutoConfig.register("model"				,   UpperCamelCase_						)
								# Trying to register something existing in the Transformers library will raise an error
								with self.assertRaises(UpperCamelCase_						):
									AutoConfig.register("bert"				,   UpperCamelCase_						)

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

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



						def      _snake_case						(						self						:				Optional[Any]						):






							'''simple docstring'''



							with self.assertRaisesRegex(
							    UpperCamelCase_				,   "bert-base is not a local folder and is not a valid model identifier"						):
								__lowercase									=      AutoConfig.from_pretrained("bert-base"						)



						def      _snake_case						(						self						:				Tuple						):






							'''simple docstring'''



							with self.assertRaisesRegex(
							    UpperCamelCase_				,   R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"						):
								__lowercase									=      AutoConfig.from_pretrained(UpperCamelCase_				,   revision="aaaaaa"						)



						def      _snake_case						(						self						:				Optional[int]						):






							'''simple docstring'''



							with self.assertRaisesRegex(
							    UpperCamelCase_				,   "hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."				,   ):
								__lowercase									=      AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo"						)



						def      _snake_case						(						self						:				Dict						):






							'''simple docstring'''



							with self.assertRaises(UpperCamelCase_						):
								__lowercase									=      AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"						)
							# If remote code is disabled, we can't load this config.
							with self.assertRaises(UpperCamelCase_						):
								__lowercase									=      AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"				,   trust_remote_code=UpperCamelCase_						)

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

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



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






							'''simple docstring'''



							class 	_A (			A__						):


													'''simple docstring'''





													_snake_case      :  List[str]   				=   """new-model"""

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

								# If remote is enabled, we load from the Hub
								__lowercase									=      AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"				,   trust_remote_code=UpperCamelCase_						)
								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_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__snake_case				 =	{
    '''configuration_clip''': [
        '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPConfig''',
        '''CLIPOnnxConfig''',
        '''CLIPTextConfig''',
        '''CLIPVisionConfig''',
    ],
    '''processing_clip''': ['''CLIPProcessor'''],
    '''tokenization_clip''': ['''CLIPTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	['''CLIPTokenizerFast''']

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	['''CLIPFeatureExtractor''']
		__snake_case				 =	['''CLIPImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''CLIPModel''',
		    '''CLIPPreTrainedModel''',
		    '''CLIPTextModel''',
		    '''CLIPTextModelWithProjection''',
		    '''CLIPVisionModel''',
		    '''CLIPVisionModelWithProjection''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFCLIPModel''',
		    '''TFCLIPPreTrainedModel''',
		    '''TFCLIPTextModel''',
		    '''TFCLIPVisionModel''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''FlaxCLIPModel''',
		    '''FlaxCLIPPreTrainedModel''',
		    '''FlaxCLIPTextModel''',
		    '''FlaxCLIPTextPreTrainedModel''',
		    '''FlaxCLIPVisionModel''',
		    '''FlaxCLIPVisionPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_clip import (
		    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    CLIPConfig,
		    CLIPOnnxConfig,
		    CLIPTextConfig,
		    CLIPVisionConfig,
		)
		from .processing_clip import CLIPProcessor
		from .tokenization_clip import CLIPTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_clip_fast import CLIPTokenizerFast

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_clip import CLIPFeatureExtractor
				from .image_processing_clip import CLIPImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_clip import (
				    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    CLIPModel,
				    CLIPPreTrainedModel,
				    CLIPTextModel,
				    CLIPTextModelWithProjection,
				    CLIPVisionModel,
				    CLIPVisionModelWithProjection,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_clip import (
				    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFCLIPModel,
				    TFCLIPPreTrainedModel,
				    TFCLIPTextModel,
				    TFCLIPVisionModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_clip import (
				    FlaxCLIPModel,
				    FlaxCLIPPreTrainedModel,
				    FlaxCLIPTextModel,
				    FlaxCLIPTextPreTrainedModel,
				    FlaxCLIPVisionModel,
				    FlaxCLIPVisionPreTrainedModel,
				)


else:
		import sys

		__snake_case				 =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)