


"""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   _UpperCamelCase  (   unittest.TestCase		):
							'''simple docstring'''







							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			0

							def 			snake_case				(     self	):
										__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"	)
										self.assertIsInstance(__a    ,   __a	)

							def 			snake_case				(     self	):
										with tempfile.TemporaryDirectory() as tmpdirname:
													__lowerCAmelCase				    =			Path(__a	) / "preprocessor_config.json"
													__lowerCAmelCase				    =			Path(__a	) / "config.json"
													json.dump(
													    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}    ,   open(__a    ,   "w"	)    ,   )
													json.dump({"model_type": "clip"}    ,   open(__a    ,   "w"	)	)

													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(__a	)
													self.assertIsInstance(__a    ,   __a	)

							def 			snake_case				(     self	):
										# Ensure we can load the image processor from the feature extractor config
										with tempfile.TemporaryDirectory() as tmpdirname:
													__lowerCAmelCase				    =			Path(__a	) / "preprocessor_config.json"
													__lowerCAmelCase				    =			Path(__a	) / "config.json"
													json.dump(
													    {"feature_extractor_type": "CLIPFeatureExtractor", "processor_class": "CLIPProcessor"}    ,   open(__a    ,   "w"	)    ,   )
													json.dump({"model_type": "clip"}    ,   open(__a    ,   "w"	)	)

													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(__a	)
													self.assertIsInstance(__a    ,   __a	)

							def 			snake_case				(     self	):
										with tempfile.TemporaryDirectory() as tmpdirname:
													__lowerCAmelCase				    =			CLIPConfig()

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

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

													config_dict.pop("image_processor_type"	)
													__lowerCAmelCase				    =			CLIPImageProcessor(**__a	)

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

													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(__a	)

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

										self.assertIsInstance(__a    ,   __a	)

							def 			snake_case				(     self	):
										with tempfile.TemporaryDirectory() as tmpdirname:
													__lowerCAmelCase				    =			Path(__a	) / "preprocessor_config.json"
													json.dump(
													    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}    ,   open(__a    ,   "w"	)    ,   )

													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(__a	)
													self.assertIsInstance(__a    ,   __a	)

							def 			snake_case				(     self	):
										with self.assertRaisesRegex(
										    __a    ,   "clip-base is not a local folder and is not a valid model identifier"	):
													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained("clip-base"	)

							def 			snake_case				(     self	):
										with self.assertRaisesRegex(
										    __a    ,   R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"	):
													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(__a    ,   revision="aaaaaa"	)

							def 			snake_case				(     self	):
										with self.assertRaisesRegex(
										    __a    ,   "hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."    ,   ):
													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained("hf-internal-testing/config-no-model"	)

							def 			snake_case				(     self	):
										# If remote code is not set, we will time out when asking whether to load the model.
										with self.assertRaises(__a	):
													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained("hf-internal-testing/test_dynamic_image_processor"	)
										# If remote code is disabled, we can't load this config.
										with self.assertRaises(__a	):
													__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(
													    "hf-internal-testing/test_dynamic_image_processor"    ,   trust_remote_code=__a	)

										__lowerCAmelCase				    =			AutoImageProcessor.from_pretrained(
										    "hf-internal-testing/test_dynamic_image_processor"    ,   trust_remote_code=__a	)
										self.assertEqual(image_processor.__class__.__name__    ,   "NewImageProcessor"	)

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

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

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

																__lowerCAmelCase				    =			CustomImageProcessor.from_pretrained(__a	)

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

										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 			snake_case				(     self	):
										class   _UpperCamelCase  (   lowerCAmelCase__		):
																	'''simple docstring'''







																	__UpperCAmelCase						:    Dict				      =True

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

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


A     :   Optional[int]			    =			{
    "configuration_altclip": [
        "ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "AltCLIPConfig",
        "AltCLIPTextConfig",
        "AltCLIPVisionConfig",
    ],
    "processing_altclip": ["AltCLIPProcessor"],
}

try:
	if not is_torch_available():
		raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
	pass
else:
	A     :   Tuple			    =			[
	    "ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST",
	    "AltCLIPPreTrainedModel",
	    "AltCLIPModel",
	    "AltCLIPTextModel",
	    "AltCLIPVisionModel",
	]


if TYPE_CHECKING:
	from .configuration_altclip import (
	    ALTCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
	    AltCLIPConfig,
	    AltCLIPTextConfig,
	    AltCLIPVisionConfig,
	)
	from .processing_altclip import AltCLIPProcessor

	try:
		if not is_torch_available():
			raise OptionalDependencyNotAvailable()
	except OptionalDependencyNotAvailable:
		pass
	else:
		from .modeling_altclip import (
		    ALTCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
		    AltCLIPModel,
		    AltCLIPPreTrainedModel,
		    AltCLIPTextModel,
		    AltCLIPVisionModel,
		)


else:
	import sys

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




