
"""simple docstring"""






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

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / "utils"))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402




class 				lowercase__(				unittest.TestCase   ):
	'''simple docstring'''

	def   __lowerCAmelCase (						self    :str					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Tuple				= 0





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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[str]				= AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''					)
			self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)





	def   __lowerCAmelCase (						self    :Dict					)							->			List[str]:







			'''simple docstring'''




			with tempfile.TemporaryDirectory() as tmpdirname:
					SCREAMING_SNAKE_CASE       :     Optional[Any]				= Path(__UpperCamelCase					) / '''preprocessor_config.json'''
					SCREAMING_SNAKE_CASE       :     List[str]				= Path(__UpperCamelCase					) / '''config.json'''
					json.dump(
					    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(__UpperCamelCase		, '''w'''					)		, )
					json.dump({'''model_type''': '''clip'''}		, open(__UpperCamelCase		, '''w'''					)					)

					SCREAMING_SNAKE_CASE       :     str				= AutoImageProcessor.from_pretrained(__UpperCamelCase					)
					self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)





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







			'''simple docstring'''




			with tempfile.TemporaryDirectory() as tmpdirname:
					SCREAMING_SNAKE_CASE       :     Tuple				= Path(__UpperCamelCase					) / '''preprocessor_config.json'''
					SCREAMING_SNAKE_CASE       :     Optional[int]				= Path(__UpperCamelCase					) / '''config.json'''
					json.dump(
					    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		, open(__UpperCamelCase		, '''w'''					)		, )
					json.dump({'''model_type''': '''clip'''}		, open(__UpperCamelCase		, '''w'''					)					)

					SCREAMING_SNAKE_CASE       :     Any				= AutoImageProcessor.from_pretrained(__UpperCamelCase					)
					self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)





	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			Dict:







			'''simple docstring'''




			with tempfile.TemporaryDirectory() as tmpdirname:
					SCREAMING_SNAKE_CASE       :     Optional[int]				= CLIPConfig()

					# Create a dummy config file with image_proceesor_type
					SCREAMING_SNAKE_CASE       :     List[Any]				= Path(__UpperCamelCase					) / '''preprocessor_config.json'''
					SCREAMING_SNAKE_CASE       :     Tuple				= Path(__UpperCamelCase					) / '''config.json'''
					json.dump(
					    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(__UpperCamelCase		, '''w'''					)		, )
					json.dump({'''model_type''': '''clip'''}		, open(__UpperCamelCase		, '''w'''					)					)

					# remove image_processor_type to make sure config.json alone is enough to load image processor locally
					SCREAMING_SNAKE_CASE       :     str				= AutoImageProcessor.from_pretrained(__UpperCamelCase					).to_dict()

					config_dict.pop('''image_processor_type'''					)
					SCREAMING_SNAKE_CASE       :     int				= CLIPImageProcessor(**__UpperCamelCase					)

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

					SCREAMING_SNAKE_CASE       :     Tuple				= AutoImageProcessor.from_pretrained(__UpperCamelCase					)

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

			self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)





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







			'''simple docstring'''




			with tempfile.TemporaryDirectory() as tmpdirname:
					SCREAMING_SNAKE_CASE       :     List[Any]				= Path(__UpperCamelCase					) / '''preprocessor_config.json'''
					json.dump(
					    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}		, open(__UpperCamelCase		, '''w'''					)		, )

					SCREAMING_SNAKE_CASE       :     Tuple				= AutoImageProcessor.from_pretrained(__UpperCamelCase					)
					self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)





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







			'''simple docstring'''




			with self.assertRaisesRegex(
			    __UpperCamelCase		, '''clip-base is not a local folder and is not a valid model identifier'''					):
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= AutoImageProcessor.from_pretrained('''clip-base'''					)





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







			'''simple docstring'''




			with self.assertRaisesRegex(
			    __UpperCamelCase		, R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
					SCREAMING_SNAKE_CASE       :     Dict				= AutoImageProcessor.from_pretrained(__UpperCamelCase		, revision='''aaaaaa'''					)





	def   __lowerCAmelCase (						self    :str					)							->			str:







			'''simple docstring'''




			with self.assertRaisesRegex(
			    __UpperCamelCase		, '''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''		, ):
					SCREAMING_SNAKE_CASE       :     str				= AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''					)





	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Optional[int]:







			'''simple docstring'''




			with self.assertRaises(__UpperCamelCase					):
					SCREAMING_SNAKE_CASE       :     List[str]				= AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''					)
			# If remote code is disabled, we can't load this config.
			with self.assertRaises(__UpperCamelCase					):
					SCREAMING_SNAKE_CASE       :     Dict				= AutoImageProcessor.from_pretrained(
					    '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=__UpperCamelCase					)

			SCREAMING_SNAKE_CASE       :     Dict				= AutoImageProcessor.from_pretrained(
			    '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=__UpperCamelCase					)
			self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''					)

			# Test image processor can be reloaded.
			with tempfile.TemporaryDirectory() as tmp_dir:
					image_processor.save_pretrained(__UpperCamelCase					)
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= AutoImageProcessor.from_pretrained(__UpperCamelCase		, trust_remote_code=__UpperCamelCase					)
			self.assertEqual(reloaded_image_processor.__class__.__name__		, '''NewImageProcessor'''					)





	def   __lowerCAmelCase (						self    :str					)							->			List[str]:







			'''simple docstring'''




			try:
					AutoConfig.register('''custom'''		, __UpperCamelCase					)
					AutoImageProcessor.register(__UpperCamelCase		, __UpperCamelCase					)
					# Trying to register something existing in the Transformers library will raise an error
					with self.assertRaises(__UpperCamelCase					):
							AutoImageProcessor.register(__UpperCamelCase		, __UpperCamelCase					)

					with tempfile.TemporaryDirectory() as tmpdirname:
							SCREAMING_SNAKE_CASE       :     List[Any]				= Path(__UpperCamelCase					) / '''preprocessor_config.json'''
							SCREAMING_SNAKE_CASE       :     Optional[Any]				= Path(__UpperCamelCase					) / '''config.json'''
							json.dump(
							    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}		, open(__UpperCamelCase		, '''w'''					)		, )
							json.dump({'''model_type''': '''clip'''}		, open(__UpperCamelCase		, '''w'''					)					)

							SCREAMING_SNAKE_CASE       :     Optional[Any]				= CustomImageProcessor.from_pretrained(__UpperCamelCase					)

					# Now that the config is registered, it can be used as any other config with the auto-API
					with tempfile.TemporaryDirectory() as tmp_dir:
							image_processor.save_pretrained(__UpperCamelCase					)
							SCREAMING_SNAKE_CASE       :     Optional[Any]				= AutoImageProcessor.from_pretrained(__UpperCamelCase					)
							self.assertIsInstance(__UpperCamelCase		, __UpperCamelCase					)

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







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







			'''simple docstring'''




			class 				lowercase__(				_UpperCAmelCase   ):
				'''simple docstring'''

				UpperCamelCase				=  True

			try:
					AutoConfig.register('''custom'''		, __UpperCamelCase					)
					AutoImageProcessor.register(__UpperCamelCase		, __UpperCamelCase					)
					# If remote code is not set, the default is to use local
					SCREAMING_SNAKE_CASE       :     str				= AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''					)
					self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''					)
					self.assertTrue(image_processor.is_local					)

					# If remote code is disabled, we load the local one.
					SCREAMING_SNAKE_CASE       :     Optional[Any]				= AutoImageProcessor.from_pretrained(
					    '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=__UpperCamelCase					)
					self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''					)
					self.assertTrue(image_processor.is_local					)

					# If remote is enabled, we load from the Hub
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= AutoImageProcessor.from_pretrained(
					    '''hf-internal-testing/test_dynamic_image_processor'''		, trust_remote_code=__UpperCamelCase					)
					self.assertEqual(image_processor.__class__.__name__		, '''NewImageProcessor'''					)
					self.assertTrue(not hasattr(__UpperCamelCase		, '''is_local'''					)					)

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







"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCamelCase__ :       Optional[Any]						     =			{
    "configuration_funnel": ["FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP", "FunnelConfig"],
    "convert_funnel_original_tf_checkpoint_to_pytorch": [],
    "tokenization_funnel": ["FunnelTokenizer"],
}

try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase__ :       Optional[Any]						     =			["FunnelTokenizerFast"]

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase__ :       Dict						     =			[
					    "FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "FunnelBaseModel",
					    "FunnelForMaskedLM",
					    "FunnelForMultipleChoice",
					    "FunnelForPreTraining",
					    "FunnelForQuestionAnswering",
					    "FunnelForSequenceClassification",
					    "FunnelForTokenClassification",
					    "FunnelModel",
					    "FunnelPreTrainedModel",
					    "load_tf_weights_in_funnel",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowerCamelCase__ :       Tuple						     =			[
					    "TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TFFunnelBaseModel",
					    "TFFunnelForMaskedLM",
					    "TFFunnelForMultipleChoice",
					    "TFFunnelForPreTraining",
					    "TFFunnelForQuestionAnswering",
					    "TFFunnelForSequenceClassification",
					    "TFFunnelForTokenClassification",
					    "TFFunnelModel",
					    "TFFunnelPreTrainedModel",
					]


if TYPE_CHECKING:
					from .configuration_funnel import FUNNEL_PRETRAINED_CONFIG_ARCHIVE_MAP, FunnelConfig
					from .tokenization_funnel import FunnelTokenizer

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_funnel_fast import FunnelTokenizerFast

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_funnel import (
										    FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
										    FunnelBaseModel,
										    FunnelForMaskedLM,
										    FunnelForMultipleChoice,
										    FunnelForPreTraining,
										    FunnelForQuestionAnswering,
										    FunnelForSequenceClassification,
										    FunnelForTokenClassification,
										    FunnelModel,
										    FunnelPreTrainedModel,
										    load_tf_weights_in_funnel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_funnel import (
										    TF_FUNNEL_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFFunnelBaseModel,
										    TFFunnelForMaskedLM,
										    TFFunnelForMultipleChoice,
										    TFFunnelForPreTraining,
										    TFFunnelForQuestionAnswering,
										    TFFunnelForSequenceClassification,
										    TFFunnelForTokenClassification,
										    TFFunnelModel,
										    TFFunnelPreTrainedModel,
										)

else:
					import sys

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





