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





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         A       :   Any					=						0





     def 				__lowerCAmelCase							(							self	) ->				int:






         """simple docstring"""






         A       :   Tuple					=						AutoImageProcessor.from_pretrained('''openai/clip-vit-base-patch32'''	)
         self.assertIsInstance(snake_case_						,			snake_case_	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Dict					=						Path(snake_case_	) / 'preprocessor_config.json'
             A       :   List[Any]					=						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'''	)	)

             A       :   List[Any]					=						AutoImageProcessor.from_pretrained(snake_case_	)
             self.assertIsInstance(snake_case_						,			snake_case_	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   Optional[int]					=						Path(snake_case_	) / 'preprocessor_config.json'
             A       :   str					=						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'''	)	)

             A       :   Optional[int]					=						AutoImageProcessor.from_pretrained(snake_case_	)
             self.assertIsInstance(snake_case_						,			snake_case_	)





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






         """simple docstring"""






         with tempfile.TemporaryDirectory() as tmpdirname:
             A       :   str					=						CLIPConfig()

             # Create a dummy config file with image_proceesor_type
             A       :   Optional[Any]					=						Path(snake_case_	) / 'preprocessor_config.json'
             A       :   List[str]					=						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
             A       :   Optional[Any]					=						AutoImageProcessor.from_pretrained(snake_case_	).to_dict()

             config_dict.pop('''image_processor_type'''	)
             A       :   Optional[Any]					=						CLIPImageProcessor(**snake_case_	)

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

             A       :   Optional[Any]					=						AutoImageProcessor.from_pretrained(snake_case_	)

             # make sure private variable is not incorrectly saved
             A       :   List[Any]					=						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]:






         """simple docstring"""






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

             A       :   Any					=						AutoImageProcessor.from_pretrained(snake_case_	)
             self.assertIsInstance(snake_case_						,			snake_case_	)





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






         """simple docstring"""






         with self.assertRaisesRegex(
             snake_case_						,			'''clip-base is not a local folder and is not a valid model identifier'''	):
             A       :   Union[str, Any]					=						AutoImageProcessor.from_pretrained('''clip-base'''	)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with self.assertRaisesRegex(
             snake_case_						,			R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''	):
             A       :   Optional[int]					=						AutoImageProcessor.from_pretrained(snake_case_						,			revision='''aaaaaa'''	)





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






         """simple docstring"""






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





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         with self.assertRaises(snake_case_	):
             A       :   List[str]					=						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_	):
             A       :   List[str]					=						AutoImageProcessor.from_pretrained(
                 '''hf-internal-testing/test_dynamic_image_processor'''						,			trust_remote_code=snake_case_	)

         A       :   List[str]					=						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_	)
             A       :   Any					=						AutoImageProcessor.from_pretrained(snake_case_						,			trust_remote_code=snake_case_	)
         self.assertEqual(reloaded_image_processor.__class__.__name__						,			'''NewImageProcessor'''	)





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






         """simple docstring"""






         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:
                 A       :   Optional[Any]					=						Path(snake_case_	) / 'preprocessor_config.json'
                 A       :   Tuple					=						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'''	)	)

                 A       :   List[str]					=						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_	)
                 A       :   Any					=						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[int]:






         """simple docstring"""






         class  A				(							UpperCAmelCase__						):
              __magic_name__						=		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
             A       :   List[Any]					=						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.
             A       :   Optional[Any]					=						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
             A       :   Optional[Any]					=						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"""




def 						UpperCAmelCase__		(     _UpperCAmelCase					):






  """simple docstring"""





  A_  :       List[str]										=  hex_num.strip()
  if not hex_num:
    raise ValueError('No value was passed to the function'					)

  A_  :       Any										=  hex_num[0] == '-'
  if is_negative:
    A_  :       Optional[Any]										=  hex_num[1:]

  try:
    A_  :       Tuple										=  int(_UpperCAmelCase ,					16					)
  except ValueError:
    raise ValueError('Invalid value was passed to the function'					)

  A_  :       Union[str, Any]										=  ''
  while int_num > 0:
    A_  :       Optional[Any]										=  str(int_num % 2					) + bin_str
    int_num >>= 1

  return int(('-' + bin_str) if is_negative else bin_str					)


if __name__ == "__main__":
   import doctest

   doctest.testmod()