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






					'''simple docstring'''


					def 			lowerCAmelCase_					(						self			:				Dict		)							->       List[str]:
									SCREAMING_SNAKE_CASE__		  =				0



					def 			lowerCAmelCase_					(						self			:				str		)							->       Tuple:
									SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"		)
									self.assertIsInstance(_snake_case   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				int		)							->       Dict:
									with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__		  =				Path(_snake_case		) / "preprocessor_config.json"
													SCREAMING_SNAKE_CASE__		  =				Path(_snake_case		) / "config.json"
													json.dump(
													    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}   , open(_snake_case   , "w"		)   , )
													json.dump({"model_type": "clip"}   , open(_snake_case   , "w"		)		)

													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case		)
													self.assertIsInstance(_snake_case   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Optional[Any]		)							->       Tuple:
									# Ensure we can load the image processor from the feature extractor config
									with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__		  =				Path(_snake_case		) / "preprocessor_config.json"
													SCREAMING_SNAKE_CASE__		  =				Path(_snake_case		) / "config.json"
													json.dump(
													    {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}   , open(_snake_case   , "w"		)   , )
													json.dump({"model_type": "clip"}   , open(_snake_case   , "w"		)		)

													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case		)
													self.assertIsInstance(_snake_case   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       Tuple:
									with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__		  =				CLIPConfig()

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

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

													config_dict.pop("image_processor_type"		)
													SCREAMING_SNAKE_CASE__		  =				CLIPImageProcessor(**_snake_case		)

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

													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case		)

													# 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(_snake_case   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				Optional[int]		)							->       List[str]:
									with tempfile.TemporaryDirectory() as tmpdirname:
													SCREAMING_SNAKE_CASE__		  =				Path(_snake_case		) / "preprocessor_config.json"
													json.dump(
													    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}   , open(_snake_case   , "w"		)   , )

													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case		)
													self.assertIsInstance(_snake_case   , _snake_case		)



					def 			lowerCAmelCase_					(						self			:				List[Any]		)							->       int:
									with self.assertRaisesRegex(
									    _snake_case   , "clip-base is not a local folder and is not a valid model identifier"		):
													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained("clip-base"		)



					def 			lowerCAmelCase_					(						self			:				Dict		)							->       str:
									with self.assertRaisesRegex(
									    _snake_case   , r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"		):
													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case   , revision="aaaaaa"		)



					def 			lowerCAmelCase_					(						self			:				Union[str, Any]		)							->       List[Any]:
									with self.assertRaisesRegex(
									    _snake_case   , "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 			lowerCAmelCase_					(						self			:				int		)							->       List[Any]:
									# If remote code is not set, we will time out when asking whether to load the model.
									with self.assertRaises(_snake_case		):
													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(_snake_case		):
													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(
													    "hf-internal-testing/test_dynamic_image_processor"   , trust_remote_code=_snake_case		)

									SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(
									    "hf-internal-testing/test_dynamic_image_processor"   , trust_remote_code=_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(_snake_case		)
													SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case   , trust_remote_code=_snake_case		)
									self.assertEqual(reloaded_image_processor.__class__.__name__   , "NewImageProcessor"		)



					def 			lowerCAmelCase_					(						self			:				List[Any]		)							->       Optional[Any]:
									try:
													AutoConfig.register("custom"   , _snake_case		)
													AutoImageProcessor.register(_snake_case   , _snake_case		)
													# Trying to register something existing in the Transformers library will raise an error
													with self.assertRaises(_snake_case		):
																	AutoImageProcessor.register(_snake_case   , _snake_case		)

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

																	SCREAMING_SNAKE_CASE__		  =				CustomImageProcessor.from_pretrained(_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(_snake_case		)
																	SCREAMING_SNAKE_CASE__		  =				AutoImageProcessor.from_pretrained(_snake_case		)
																	self.assertIsInstance(_snake_case   , _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 			lowerCAmelCase_					(						self			:				Optional[Any]		)							->       int:

									class lowerCamelCase		(_SCREAMING_SNAKE_CASE      ):






														'''simple docstring'''


														a   	=		True

									try:
													AutoConfig.register("custom"   , _snake_case		)
													AutoImageProcessor.register(_snake_case   , _snake_case		)
													# 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=_snake_case		)
													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=_snake_case		)
													self.assertEqual(image_processor.__class__.__name__   , "NewImageProcessor"		)
													self.assertTrue(not hasattr(_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"""






import os

from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE, hf_cache_home


_A   					=    HUGGINGFACE_HUB_CACHE


_A   					=    'config.json'
_A   					=    'diffusion_pytorch_model.bin'
_A   					=    'diffusion_flax_model.msgpack'
_A   					=    'model.onnx'
_A   					=    'diffusion_pytorch_model.safetensors'
_A   					=    'weights.pb'
_A   					=    'https://huggingface.co'
_A   					=    default_cache_path
_A   					=    'diffusers_modules'
_A   					=    os.getenv('HF_MODULES_CACHE', os.path.join(hf_cache_home, 'modules'))
_A   					=    ['fp16', 'non-ema']
_A   					=    '.self_attn'




