






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        _SCREAMING_SNAKE_CASE						(  unittest.TestCase							):





							"""simple docstring"""





							def  lowercase		(   self:     List[Any]    ):

												'''simple docstring'''

												a__          =  0




							def  lowercase		(   self:     List[str]    ):

												'''simple docstring'''

												a__          =  AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''    )
												self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )




							def  lowercase		(   self:     Dict    ):

												'''simple docstring'''

												with tempfile.TemporaryDirectory() as tmpdirname:
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''preprocessor_config.json'''
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''config.json'''
																	json.dump(
																	    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )				,		)
																	json.dump({'''model_type''': '''clip'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )    )

																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )




							def  lowercase		(   self:     Optional[Any]    ):

												'''simple docstring'''

												with tempfile.TemporaryDirectory() as tmpdirname:
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''preprocessor_config.json'''
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''config.json'''
																	json.dump(
																	    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )				,		)
																	json.dump({'''model_type''': '''clip'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )    )

																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )




							def  lowercase		(   self:     List[Any]    ):

												'''simple docstring'''

												with tempfile.TemporaryDirectory() as tmpdirname:
																	a__          =  CLIPConfig()

																	# Create a dummy config file with image_proceesor_type
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''preprocessor_config.json'''
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''config.json'''
																	json.dump(
																	    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )				,		)
																	json.dump({'''model_type''': '''clip'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )    )

																	# remove image_processor_type to make sure config.json alone is enough to load image processor locally
																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    ).to_dict()

																	config_dict.pop('''image_processor_type'''    )
																	a__          =  CLIPImageProcessor(**__SCREAMING_SNAKE_CASE    )

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

																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )

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

												self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )




							def  lowercase		(   self:     Optional[Any]    ):

												'''simple docstring'''

												with tempfile.TemporaryDirectory() as tmpdirname:
																	a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''preprocessor_config.json'''
																	json.dump(
																	    {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )				,		)

																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )
																	self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )




							def  lowercase		(   self:     Any    ):

												'''simple docstring'''

												with self.assertRaisesRegex(
												    __SCREAMING_SNAKE_CASE				,		'''clip-base is not a local folder and is not a valid model identifier'''    ):
																	a__          =  AutoImageProcessor.from_pretrained('''clip-base'''    )




							def  lowercase		(   self:     int    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     int    ):

												'''simple docstring'''

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




							def  lowercase		(   self:     Tuple    ):

												'''simple docstring'''

												with self.assertRaises(__SCREAMING_SNAKE_CASE    ):
																	a__          =  AutoImageProcessor.from_pretrained('''hf-internal-testing/test_dynamic_image_processor'''    )
												# If remote code is disabled, we can't load this config.
												with self.assertRaises(__SCREAMING_SNAKE_CASE    ):
																	a__          =  AutoImageProcessor.from_pretrained(
																	    '''hf-internal-testing/test_dynamic_image_processor'''				,		trust_remote_code=__SCREAMING_SNAKE_CASE    )

												a__          =  AutoImageProcessor.from_pretrained(
												    '''hf-internal-testing/test_dynamic_image_processor'''				,		trust_remote_code=__SCREAMING_SNAKE_CASE    )
												self.assertEqual(image_processor.__class__.__name__				,		'''NewImageProcessor'''    )

												# Test image processor can be reloaded.
												with tempfile.TemporaryDirectory() as tmp_dir:
																	image_processor.save_pretrained(__SCREAMING_SNAKE_CASE    )
																	a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE				,		trust_remote_code=__SCREAMING_SNAKE_CASE    )
												self.assertEqual(reloaded_image_processor.__class__.__name__				,		'''NewImageProcessor'''    )




							def  lowercase		(   self:     Any    ):

												'''simple docstring'''

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

																	with tempfile.TemporaryDirectory() as tmpdirname:
																						a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''preprocessor_config.json'''
																						a__          =  Path(__SCREAMING_SNAKE_CASE    ) / '''config.json'''
																						json.dump(
																						    {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )				,		)
																						json.dump({'''model_type''': '''clip'''}				,		open(__SCREAMING_SNAKE_CASE				,		'''w'''    )    )

																						a__          =  CustomImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )

																	# 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(__SCREAMING_SNAKE_CASE    )
																						a__          =  AutoImageProcessor.from_pretrained(__SCREAMING_SNAKE_CASE    )
																						self.assertIsInstance(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )

												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  lowercase		(   self:     Optional[int]    ):

												'''simple docstring'''

												class        _SCREAMING_SNAKE_CASE						(  __UpperCAmelCase							):





																			"""simple docstring"""





																			_SCREAMING_SNAKE_CASE    =True

												try:
																	AutoConfig.register('''custom'''				,		__SCREAMING_SNAKE_CASE    )
																	AutoImageProcessor.register(__SCREAMING_SNAKE_CASE				,		__SCREAMING_SNAKE_CASE    )
																	# If remote code is not set, the default is to use local
																	a__          =  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.
																	a__          =  AutoImageProcessor.from_pretrained(
																	    '''hf-internal-testing/test_dynamic_image_processor'''				,		trust_remote_code=__SCREAMING_SNAKE_CASE    )
																	self.assertEqual(image_processor.__class__.__name__				,		'''NewImageProcessor'''    )
																	self.assertTrue(image_processor.is_local    )

																	# If remote is enabled, we load from the Hub
																	a__          =  AutoImageProcessor.from_pretrained(
																	    '''hf-internal-testing/test_dynamic_image_processor'''				,		trust_remote_code=__SCREAMING_SNAKE_CASE    )
																	self.assertEqual(image_processor.__class__.__name__				,		'''NewImageProcessor'''    )
																	self.assertTrue(not hasattr(__SCREAMING_SNAKE_CASE				,		'''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"""







def        SCREAMING_SNAKE_CASE       (      lowerCamelCase_):
					assert column_title.isupper()
					a__          =  0
					a__          =  len(lowerCamelCase_) - 1
					a__          =  0

					while index >= 0:
										a__          =  (ord(column_title[index]) - 64) * pow(26 ,     lowerCamelCase_)
										answer += value
										power += 1
										index -= 1

					return answer


if __name__ == "__main__":
	from doctest import testmod

	testmod()


