


"""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   UpperCAmelCase_      ( self			:				Any   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				0




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	Optional[int]:
						__SCREAMING_SNAKE_CASE						   =				AutoImageProcessor.from_pretrained("openai/clip-vit-base-patch32"   )
						self.assertIsInstance(UpperCAmelCase__			,			UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	Union[str, Any]:
						with tempfile.TemporaryDirectory() as tmpdirname:
											__SCREAMING_SNAKE_CASE						   =				Path(UpperCAmelCase__   ) / "preprocessor_config.json"
											__SCREAMING_SNAKE_CASE						   =				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						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__   )
											self.assertIsInstance(UpperCAmelCase__			,			UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	Any:
						# Ensure we can load the image processor from the feature extractor config
						with tempfile.TemporaryDirectory() as tmpdirname:
											__SCREAMING_SNAKE_CASE						   =				Path(UpperCAmelCase__   ) / "preprocessor_config.json"
											__SCREAMING_SNAKE_CASE						   =				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						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__   )
											self.assertIsInstance(UpperCAmelCase__			,			UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	Tuple:
						with tempfile.TemporaryDirectory() as tmpdirname:
											__SCREAMING_SNAKE_CASE						   =				CLIPConfig()

											# Create a dummy config file with image_proceesor_type
											__SCREAMING_SNAKE_CASE						   =				Path(UpperCAmelCase__   ) / "preprocessor_config.json"
											__SCREAMING_SNAKE_CASE						   =				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						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__   ).to_dict()

											config_dict.pop("image_processor_type"   )
											__SCREAMING_SNAKE_CASE						   =				CLIPImageProcessor(**UpperCAmelCase__   )

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

											__SCREAMING_SNAKE_CASE						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__   )

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




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	Any:
						with tempfile.TemporaryDirectory() as tmpdirname:
											__SCREAMING_SNAKE_CASE						   =				Path(UpperCAmelCase__   ) / "preprocessor_config.json"
											json.dump(
											    {"image_processor_type": "CLIPImageProcessor", "processor_class": "CLIPProcessor"}			,			open(UpperCAmelCase__			,			"w"   )			,			)

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




	def   UpperCAmelCase_      ( self			:				str   )       ->	str:
						with self.assertRaisesRegex(
						    UpperCAmelCase__			,			"clip-base is not a local folder and is not a valid model identifier"   ):
											__SCREAMING_SNAKE_CASE						   =				AutoImageProcessor.from_pretrained("clip-base"   )




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	List[str]:
						with self.assertRaisesRegex(
						    UpperCAmelCase__			,			R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"   ):
											__SCREAMING_SNAKE_CASE						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__			,			revision="aaaaaa"   )




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

						__SCREAMING_SNAKE_CASE						   =				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						   =				AutoImageProcessor.from_pretrained(UpperCAmelCase__			,			trust_remote_code=UpperCAmelCase__   )
						self.assertEqual(reloaded_image_processor.__class__.__name__			,			"NewImageProcessor"   )




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	List[Any]:
						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						   =				Path(UpperCAmelCase__   ) / "preprocessor_config.json"
																__SCREAMING_SNAKE_CASE						   =				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						   =				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						   =				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   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	List[Any]:




						class  UpperCamelCase_	( UpperCamelCase):

							"""simple docstring"""





							snake_case__				:  Any    					=			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						   =				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=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						   =				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"""







def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					if upper_limit < 0:
										raise ValueError("Limit for the Catalan sequence must be ≥ 0"   )

					__SCREAMING_SNAKE_CASE						   =				[0] * (upper_limit + 1)

					# Base case: C(0) = C(1) = 1
					__SCREAMING_SNAKE_CASE						   =				1
					if upper_limit > 0:
										__SCREAMING_SNAKE_CASE						   =				1

					# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
					for i in range(2  ,						upper_limit + 1   ):
										for j in range(lowerCAmelCase_   ):
															catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

					return catalan_list


if __name__ == "__main__":
		print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''')
		print('''\n*** Enter -1 at any time to quit ***''')
		print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''')
		try:
				while True:
						a__				:							List[str]						=     int(input().strip())
						if N < 0:
								print('''\n********* Goodbye!! ************''')
								break
						else:
								print(F"The Catalan numbers from 0 through {N} are:")
								print(catalan_numbers(N))
								print('''Try another upper limit for the sequence: ''', end='''''')
		except (NameError, ValueError):
				print('''\n********* Invalid input, goodbye! ************\n''')

		import doctest

		doctest.testmod()
