


'''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       lowercase_				(							self	)				->     Dict:
         __lowerCamelCase   :	Dict								=		0







    def       lowercase_				(							self	)				->     Optional[Any]:
         __lowerCamelCase   :	Any								=		AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'	)
         self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Any:
         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase   :	str								=		Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
              __lowerCamelCase   :	Optional[Any]								=		Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
              json.dump(
                  {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)			,     )
              json.dump({'model_type': 'clip'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)	)

              __lowerCamelCase   :	int								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
              self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     List[str]:
         # Ensure we can load the image processor from the feature extractor config
         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase   :	Optional[int]								=		Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
              __lowerCamelCase   :	str								=		Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
              json.dump(
                  {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)			,     )
              json.dump({'model_type': 'clip'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)	)

              __lowerCamelCase   :	List[Any]								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
              self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     List[Any]:
         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase   :	List[Any]								=		CLIPConfig()

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

              # remove image_processor_type to make sure config.json alone is enough to load image processor locally
              __lowerCamelCase   :	Dict								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	).to_dict()

              config_dict.pop('image_processor_type'	)
              __lowerCamelCase   :	Optional[int]								=		CLIPImageProcessor(**SCREAMING_SNAKE_CASE_	)

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

              __lowerCamelCase   :	Dict								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)

              # make sure private variable is not incorrectly saved
              __lowerCamelCase   :	List[Any]								=		json.loads(config.to_json_string()	)
              self.assertTrue('_processor_class' not in dict_as_saved	)

         self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     List[str]:
         with tempfile.TemporaryDirectory() as tmpdirname:
              __lowerCamelCase   :	List[Any]								=		Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
              json.dump(
                  {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)			,     )

              __lowerCamelCase   :	str								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
              self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)







    def       lowercase_				(							self	)				->     Tuple:
         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE_			,     'clip-base is not a local folder and is not a valid model identifier'	):
              __lowerCamelCase   :	Any								=		AutoImageProcessor.from_pretrained('clip-base'	)







    def       lowercase_				(							self	)				->     Optional[Any]:
         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE_			,     r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'	):
              __lowerCamelCase   :	Tuple								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_			,     revision='aaaaaa'	)







    def       lowercase_				(							self	)				->     Union[str, Any]:
         with self.assertRaisesRegex(
             SCREAMING_SNAKE_CASE_			,     'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.'			,     ):
              __lowerCamelCase   :	str								=		AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model'	)







    def       lowercase_				(							self	)				->     str:
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(SCREAMING_SNAKE_CASE_	):
              __lowerCamelCase   :	Union[str, Any]								=		AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'	)
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(SCREAMING_SNAKE_CASE_	):
              __lowerCamelCase   :	str								=		AutoImageProcessor.from_pretrained(
                  'hf-internal-testing/test_dynamic_image_processor'			,     trust_remote_code=SCREAMING_SNAKE_CASE_	)

         __lowerCamelCase   :	Tuple								=		AutoImageProcessor.from_pretrained(
             'hf-internal-testing/test_dynamic_image_processor'			,     trust_remote_code=SCREAMING_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(SCREAMING_SNAKE_CASE_	)
              __lowerCamelCase   :	int								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_			,     trust_remote_code=SCREAMING_SNAKE_CASE_	)
         self.assertEqual(reloaded_image_processor.__class__.__name__			,     'NewImageProcessor'	)







    def       lowercase_				(							self	)				->     Dict:
         try:
              AutoConfig.register('custom'			,     SCREAMING_SNAKE_CASE_	)
              AutoImageProcessor.register(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)
              # Trying to register something existing in the Transformers library will raise an error
              with self.assertRaises(SCREAMING_SNAKE_CASE_	):
                   AutoImageProcessor.register(SCREAMING_SNAKE_CASE_			,     SCREAMING_SNAKE_CASE_	)

              with tempfile.TemporaryDirectory() as tmpdirname:
                   __lowerCamelCase   :	int								=		Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
                   __lowerCamelCase   :	List[str]								=		Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
                   json.dump(
                       {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)			,     )
                   json.dump({'model_type': 'clip'}			,     open(SCREAMING_SNAKE_CASE_			,     'w'	)	)

                   __lowerCamelCase   :	List[str]								=		CustomImageProcessor.from_pretrained(SCREAMING_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(SCREAMING_SNAKE_CASE_	)
                   __lowerCamelCase   :	Union[str, Any]								=		AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
                   self.assertIsInstance(SCREAMING_SNAKE_CASE_			,     SCREAMING_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       lowercase_				(							self	)				->     Any:





         class    UpperCAmelCase_  (_UpperCAmelCase  ):





             """simple docstring"""






             lowerCamelCase  :					Optional[Any]							     =			True

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

              # If remote is enabled, we load from the Hub
              __lowerCamelCase   :	List[str]								=		AutoImageProcessor.from_pretrained(
                  'hf-internal-testing/test_dynamic_image_processor'			,     trust_remote_code=SCREAMING_SNAKE_CASE_	)
              self.assertEqual(image_processor.__class__.__name__			,     'NewImageProcessor'	)
              self.assertTrue(not hasattr(SCREAMING_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'''



A__		:					Optional[int]												=  """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""



def 			UpperCAmelCase__					(	UpperCAmelCase_    :				bytes							)     ->		bytes:
     # Make sure the supplied data is a bytes-like object
     if not isinstance(UpperCAmelCase_      ,    UpperCAmelCase_							):
          __lowerCamelCase   :	Dict								=		F'a bytes-like object is required, not \'{data.__class__.__name__}\''
          raise TypeError(UpperCAmelCase_							)

     __lowerCamelCase   :	Optional[int]								=		''.join(bin(UpperCAmelCase_							)[2:].zfill(8							) for byte in data							)

     __lowerCamelCase   :	Any								=		len(UpperCAmelCase_							) % 6 != 0

     if padding_needed:
          # The padding that will be added later
          __lowerCamelCase   :	int								=		B'=' * ((6 - len(UpperCAmelCase_							) % 6) // 2)

          # Append binary_stream with arbitrary binary digits (0's by default) to make its
          # length a multiple of 6.
          binary_stream += "0" * (6 - len(UpperCAmelCase_							) % 6)
     else:
          __lowerCamelCase   :	str								=		B''

     # Encode every 6 binary digits to their corresponding Base64 character
     return (
         "".join(
             B64_CHARSET[int(binary_stream[index : index + 6]      ,    2							)]
             for index in range(0      ,    len(UpperCAmelCase_							)      ,    6							)							).encode()
         + padding
     )



def 			UpperCAmelCase__					(	UpperCAmelCase_    :				str							)     ->		bytes:
     # Make sure encoded_data is either a string or a bytes-like object
     if not isinstance(UpperCAmelCase_      ,    UpperCAmelCase_							) and not isinstance(UpperCAmelCase_      ,    UpperCAmelCase_							):
          __lowerCamelCase   :	Dict								=		(
              'argument should be a bytes-like object or ASCII string, '
              F'not \'{encoded_data.__class__.__name__}\''
          )
          raise TypeError(UpperCAmelCase_							)

     # In case encoded_data is a bytes-like object, make sure it contains only
     # ASCII characters so we convert it to a string object
     if isinstance(UpperCAmelCase_      ,    UpperCAmelCase_							):
          try:
               __lowerCamelCase   :	int								=		encoded_data.decode('utf-8'							)
          except UnicodeDecodeError:
               raise ValueError('base64 encoded data should only contain ASCII characters'							)

     __lowerCamelCase   :	Union[str, Any]								=		encoded_data.count('='							)

     # Check if the encoded string contains non base64 characters
     if padding:
          assert all(
              char in B64_CHARSET for char in encoded_data[:-padding]							), "Invalid base64 character(s) found."
     else:
          assert all(
              char in B64_CHARSET for char in encoded_data							), "Invalid base64 character(s) found."

     # Check the padding
     assert len(UpperCAmelCase_							) % 4 == 0 and padding < 3, "Incorrect padding"

     if padding:
          # Remove padding if there is one
          __lowerCamelCase   :	Any								=		encoded_data[:-padding]

          __lowerCamelCase   :	Optional[Any]								=		''.join(
              bin(B64_CHARSET.index(UpperCAmelCase_							)							)[2:].zfill(6							) for char in encoded_data							)[: -padding * 2]
     else:
          __lowerCamelCase   :	Any								=		''.join(
              bin(B64_CHARSET.index(UpperCAmelCase_							)							)[2:].zfill(6							) for char in encoded_data							)

     __lowerCamelCase   :	str								=		[
         int(binary_stream[index : index + 8]      ,    2							)
         for index in range(0      ,    len(UpperCAmelCase_							)      ,    8							)
     ]

     return bytes(UpperCAmelCase_							)


if __name__ == "__main__":
      import doctest

      doctest.testmod()






