



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

						'''simple docstring'''




						def 			_A      (       self						):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE										=   0






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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

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

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

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

																				__SCREAMING_SNAKE_CASE										=   AutoImageProcessor.from_pretrained(lowerCAmelCase_						)

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

													self.assertIsInstance(lowerCAmelCase_ ,				lowerCAmelCase_						)






						def 			_A      (       self						):







													'''simple docstring'''

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

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






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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

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






						def 			_A      (       self						):







													'''simple docstring'''

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

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

																											__SCREAMING_SNAKE_CASE										=   CustomImageProcessor.from_pretrained(lowerCAmelCase_						)

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

													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 			_A      (       self						):







													'''simple docstring'''

													class   UpperCAmelCase_    (  __A			):

																			'''simple docstring'''




																			UpperCamelCase__     :					Any							    =    True

													try:
																				AutoConfig.register('custom' ,				lowerCAmelCase_						)
																				AutoImageProcessor.register(lowerCAmelCase_ ,				lowerCAmelCase_						)
																				# If remote code is not set, the default is to use local
																				__SCREAMING_SNAKE_CASE										=   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										=   AutoImageProcessor.from_pretrained(
																				    'hf-internal-testing/test_dynamic_image_processor' ,				trust_remote_code=lowerCAmelCase_						)
																				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										=   AutoImageProcessor.from_pretrained(
																				    'hf-internal-testing/test_dynamic_image_processor' ,				trust_remote_code=lowerCAmelCase_						)
																				self.assertEqual(image_processor.__class__.__name__ ,				'NewImageProcessor'						)
																				self.assertTrue(not hasattr(lowerCAmelCase_ ,				'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 .integrations import (
    is_optuna_available,
    is_ray_available,
    is_sigopt_available,
    is_wandb_available,
    run_hp_search_optuna,
    run_hp_search_ray,
    run_hp_search_sigopt,
    run_hp_search_wandb,
)
from .trainer_utils import (
    HPSearchBackend,
    default_hp_space_optuna,
    default_hp_space_ray,
    default_hp_space_sigopt,
    default_hp_space_wandb,
)
from .utils import logging


lowerCamelCase_							      = logging.get_logger(__name__)







class      UpperCamelCase_			:
	__magic_name__       =							42
	__magic_name__       =							None





	@staticmethod
	def 	_SCREAMING_SNAKE_CASE ( )		->     Optional[int]:
				raise NotImplementedError





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      **lowerCAmelCase_   :					Union[str, Any]		)		->     Optional[Any]:
				raise NotImplementedError





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]      ,      lowerCAmelCase_   :					Union[str, Any]		)		->     str:
				raise NotImplementedError





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     Dict:
				if not self.is_available():
							raise RuntimeError(
							    f"""You picked the {self.name} backend, but it is not installed. Run {self.pip_install()}."""		)





	@classmethod
	def 	_SCREAMING_SNAKE_CASE ( cls   :					Tuple		)		->     List[str]:
				return f"""`pip install {cls.pip_package or cls.name}`"""







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''optuna'''





	@staticmethod
	def 	_SCREAMING_SNAKE_CASE ( )		->     List[str]:
				return is_optuna_available()





	def 	_SCREAMING_SNAKE_CASE ( self   :					int      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      **lowerCAmelCase_   :					Tuple		)		->     int:
				return run_hp_search_optuna(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_      ,      **lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[str]      ,      lowerCAmelCase_   :					Dict		)		->     Optional[Any]:
				return default_hp_space_optuna(lowerCAmelCase_		)







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''ray'''
	__magic_name__       =							'''\'ray[tune]\''''





	@staticmethod
	def 	_SCREAMING_SNAKE_CASE ( )		->     Optional[int]:
				return is_ray_available()





	def 	_SCREAMING_SNAKE_CASE ( self   :					List[str]      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      **lowerCAmelCase_   :					Tuple		)		->     List[str]:
				return run_hp_search_ray(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_      ,      **lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					Tuple		)		->     Optional[int]:
				return default_hp_space_ray(lowerCAmelCase_		)







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''sigopt'''





	@staticmethod
	def 	_SCREAMING_SNAKE_CASE ( )		->     List[Any]:
				return is_sigopt_available()





	def 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					str      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      **lowerCAmelCase_   :					str		)		->     str:
				return run_hp_search_sigopt(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_      ,      **lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple      ,      lowerCAmelCase_   :					Any		)		->     Any:
				return default_hp_space_sigopt(lowerCAmelCase_		)







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''wandb'''





	@staticmethod
	def 	_SCREAMING_SNAKE_CASE ( )		->     int:
				return is_wandb_available()





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict      ,      lowerCAmelCase_   :					Tuple      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					str      ,      **lowerCAmelCase_   :					Optional[Any]		)		->     Optional[Any]:
				return run_hp_search_wandb(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lowerCAmelCase_      ,      **lowerCAmelCase_		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]      ,      lowerCAmelCase_   :					List[str]		)		->     Any:
				return default_hp_space_wandb(lowerCAmelCase_		)





lowerCamelCase_							      = {
    HPSearchBackend(backend.name): backend for backend in [OptunaBackend, RayTuneBackend, SigOptBackend, WandbBackend]
}

def 							snake_case (  ):
			UpperCAmelCase_	:					List[str]     	=   [backend for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values() if backend.is_available()]
			if len(A__		) > 0:
						UpperCAmelCase_	:					Optional[Any]     	=   available_backends[0].name
						if len(A__		) > 1:
									logger.info(
									    F"""{len(A__		)} hyperparameter search backends available. Using {name} as the default."""		)
						return name
			raise RuntimeError(
			    "No hyperparameter search backend available.\n"
			    + "\n".join(
			        F""" - To install {backend.name} run {backend.pip_install()}"""
			        for backend in ALL_HYPERPARAMETER_SEARCH_BACKENDS.values()		)		)



