


import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test



class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					@parameterized.expand([(None,), ("""foo.json""",)]						)
					def     lowerCamelCase__	(self       :		Optional[Any] ,   _UpperCAmelCase       :		Optional[Any]						)		->  Optional[Any]:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   bad_words_ids=[[1, 2, 3], [4, 5]] ,   )
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(_UpperCAmelCase ,   config_name=_UpperCAmelCase						)
									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase ,   config_name=_UpperCAmelCase						)

							# Checks parameters that were specified
							self.assertEqual(loaded_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(loaded_config.temperature ,   0.7						)
							self.assertEqual(loaded_config.length_penalty ,   1.0						)
							self.assertEqual(loaded_config.bad_words_ids ,   [[1, 2, 3], [4, 5]]						)

							# Checks parameters that were not specified (defaults)
							self.assertEqual(loaded_config.top_k ,   50						)
							self.assertEqual(loaded_config.max_length ,   20						)
							self.assertEqual(loaded_config.max_time ,   _UpperCAmelCase						)

					def     lowerCamelCase__	(self       :		Any						)		->  List[Any]:




							"""simple docstring"""


							lowercase__	     =						AutoConfig.from_pretrained("""gpt2"""						)
							lowercase__	     =						GenerationConfig.from_model_config(_UpperCAmelCase						)
							lowercase__	     =						GenerationConfig()

							# The generation config has loaded a few non-default parameters from the model config
							self.assertNotEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

							# One of those parameters is eos_token_id -- check if it matches
							self.assertNotEqual(generation_config_from_model.eos_token_id ,   default_generation_config.eos_token_id						)
							self.assertEqual(generation_config_from_model.eos_token_id ,   model_config.eos_token_id						)

					def     lowerCamelCase__	(self       :		List[Any]						)		->  str:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							lowercase__	     =						{
							    """max_new_tokens""": 1024,
							    """foo""": """bar""",
							}
							lowercase__	     =						copy.deepcopy(_UpperCAmelCase						)
							lowercase__	     =						generation_config.update(**_UpperCAmelCase						)

							# update_kwargs was not modified (no side effects)
							self.assertEqual(_UpperCAmelCase ,   _UpperCAmelCase						)

							# update_kwargs was used to update the config on valid attributes
							self.assertEqual(generation_config.max_new_tokens ,   1024						)

							# `.update()` returns a dictionary of unused kwargs
							self.assertEqual(_UpperCAmelCase ,   {"""foo""": """bar"""}						)

					def     lowerCamelCase__	(self       :		str						)		->  Optional[int]:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							lowercase__	     =						"""bar"""

							with tempfile.TemporaryDirectory("""test-generation-config"""						) as tmp_dir:
									generation_config.save_pretrained(_UpperCAmelCase						)

									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase						)
							# update_kwargs was used to update the config on valid attributes
							self.assertEqual(new_config.foo ,   """bar"""						)

							lowercase__	     =						GenerationConfig.from_model_config(_UpperCAmelCase						)
							assert not hasattr(_UpperCAmelCase ,   """foo"""						)  # no new kwargs should be initialized if from config


					def     lowerCamelCase__	(self       :		Tuple						)		->  Union[str, Any]:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig()
							self.assertEqual(default_config.temperature ,   1.0						)
							self.assertEqual(default_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(default_config.num_beams ,   1						)

							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   bad_words_ids=[[1, 2, 3], [4, 5]] ,   )
							self.assertEqual(config.temperature ,   0.7						)
							self.assertEqual(config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(config.num_beams ,   1						)

							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(_UpperCAmelCase						)
									lowercase__	     =						GenerationConfig.from_pretrained(_UpperCAmelCase ,   temperature=1.0						)

							self.assertEqual(loaded_config.temperature ,   1.0						)
							self.assertEqual(loaded_config.do_sample ,   _UpperCAmelCase						)
							self.assertEqual(loaded_config.num_beams ,   1						)  # default value






@is_staging_test
class 				A       (			unittest.TestCase				):
					'''simple docstring'''






					@classmethod
					def     lowerCamelCase__	(cls       :		int						)		->  Optional[Any]:




							"""simple docstring"""


							lowercase__	     =						TOKEN
							HfFolder.save_token(_UpperCAmelCase						)

					@classmethod
					def     lowerCamelCase__	(cls       :		Tuple						)		->  List[Any]:




							"""simple docstring"""


							try:
									delete_repo(token=cls._token ,   repo_id="""test-generation-config"""						)
							except HTTPError:
									pass

							try:
									delete_repo(token=cls._token ,   repo_id="""valid_org/test-generation-config-org"""						)
							except HTTPError:
									pass

					def     lowerCamelCase__	(self       :		Dict						)		->  str:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   )
							config.push_to_hub("""test-generation-config""" ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained(f'''{USER}/test-generation-config'''						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)

        # Reset repo
							delete_repo(token=self._token ,   repo_id="""test-generation-config"""						)

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(
									    _UpperCAmelCase ,   repo_id="""test-generation-config""" ,   push_to_hub=_UpperCAmelCase ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained(f'''{USER}/test-generation-config'''						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)


					def     lowerCamelCase__	(self       :		int						)		->  Any:




							"""simple docstring"""


							lowercase__	     =						GenerationConfig(
							    do_sample=_UpperCAmelCase ,   temperature=0.7 ,   length_penalty=1.0 ,   )
							config.push_to_hub("""valid_org/test-generation-config-org""" ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained("""valid_org/test-generation-config-org"""						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)

        # Reset repo
							delete_repo(token=self._token ,   repo_id="""valid_org/test-generation-config-org"""						)

							# Push to hub via save_pretrained
							with tempfile.TemporaryDirectory() as tmp_dir:
									config.save_pretrained(
									    _UpperCAmelCase ,   repo_id="""valid_org/test-generation-config-org""" ,   push_to_hub=_UpperCAmelCase ,   use_auth_token=self._token						)

							lowercase__	     =						GenerationConfig.from_pretrained("""valid_org/test-generation-config-org"""						)
							for k, v in config.to_dict().items():
									if k != "transformers_version":
											self.assertEqual(_UpperCAmelCase ,   getattr(_UpperCAmelCase ,   _UpperCAmelCase						)						)





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





     '''simple docstring'''



     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       snake_case__						= 0

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       snake_case__						= AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''   )
       self.assertIsInstance(_a     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:str   ):
       with tempfile.TemporaryDirectory() as tmpdirname:
         snake_case__						= Path(_a   ) / '''preprocessor_config.json'''
         snake_case__						= Path(_a   ) / '''config.json'''
         json.dump(
             {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}     ,		open(_a     ,		'''w'''   )     ,		)
         json.dump({'''model_type''': '''clip'''}     ,		open(_a     ,		'''w'''   )   )

         snake_case__						= AutoImageProcessor.from_pretrained(_a   )
         self.assertIsInstance(_a     ,		_a   )

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

         snake_case__						= AutoImageProcessor.from_pretrained(_a   )
         self.assertIsInstance(_a     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       with tempfile.TemporaryDirectory() as tmpdirname:
         snake_case__						= CLIPConfig()

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

         config_dict.pop('''image_processor_type'''   )
         snake_case__						= CLIPImageProcessor(**_a   )

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

         snake_case__						= AutoImageProcessor.from_pretrained(_a   )

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

       self.assertIsInstance(_a     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]   ):
       with tempfile.TemporaryDirectory() as tmpdirname:
         snake_case__						= Path(_a   ) / '''preprocessor_config.json'''
         json.dump(
             {'''image_processor_type''': '''CLIPImageProcessor''', '''processor_class''': '''CLIPProcessor'''}     ,		open(_a     ,		'''w'''   )     ,		)

         snake_case__						= AutoImageProcessor.from_pretrained(_a   )
         self.assertIsInstance(_a     ,		_a   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       with self.assertRaisesRegex(
           _a     ,		'''clip-base is not a local folder and is not a valid model identifier'''   ):
         snake_case__						= AutoImageProcessor.from_pretrained('''clip-base'''   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Union[str, Any]   ):
       with self.assertRaisesRegex(
           _a     ,		r'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''   ):
         snake_case__						= AutoImageProcessor.from_pretrained(_a     ,		revision='''aaaaaa'''   )

     def 	SCREAMING_SNAKE_CASE__						(					self:List[Any]   ):
       with self.assertRaisesRegex(
           _a     ,		'''hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'''     ,		):
         snake_case__						= AutoImageProcessor.from_pretrained('''hf-internal-testing/config-no-model'''   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       # If remote code is not set, we will time out when asking whether to load the model.
       with self.assertRaises(_a   ):
         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(_a   ):
         snake_case__						= AutoImageProcessor.from_pretrained(
             '''hf-internal-testing/test_dynamic_image_processor'''     ,		trust_remote_code=_a   )

       snake_case__						= 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   )
         snake_case__						= AutoImageProcessor.from_pretrained(_a     ,		trust_remote_code=_a   )
       self.assertEqual(reloaded_image_processor.__class__.__name__     ,		'''NewImageProcessor'''   )

     def 	SCREAMING_SNAKE_CASE__						(					self:Dict   ):
       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:
           snake_case__						= Path(_a   ) / '''preprocessor_config.json'''
           snake_case__						= Path(_a   ) / '''config.json'''
           json.dump(
               {'''feature_extractor_type''': '''CLIPFeatureExtractor''', '''processor_class''': '''CLIPProcessor'''}     ,		open(_a     ,		'''w'''   )     ,		)
           json.dump({'''model_type''': '''clip'''}     ,		open(_a     ,		'''w'''   )   )

           snake_case__						= 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   )
           snake_case__						= 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 	SCREAMING_SNAKE_CASE__						(					self:Optional[int]   ):
       class 		__magic_name__						(snake_case_				):





            '''simple docstring'''



            __lowercase				:							List[str]	   =       True

       try:
         AutoConfig.register('''custom'''     ,		_a   )
         AutoImageProcessor.register(_a     ,		_a   )
         # If remote code is not set, the default is to use local
         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.
         snake_case__						= 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
         snake_case__						= 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]

