code: 
  
  
  
  
  
  import os
  import warnings
  from pathlib import Path
  from typing import Optional, Union
  
  from .dynamic_module_utils import custom_object_save
  from .tokenization_utils_base import PreTrainedTokenizerBase
  from .utils import PushToHubMixin, copy_func, direct_transformers_import, logging
  
  
  __lowerCAmelCase     =			logging.get_logger(__name__)
  
  # Dynamically import the Transformers module to grab the attribute classes of the processor form their names.
  __lowerCAmelCase     =			direct_transformers_import(Path(__file__).parent)
  
  
  __lowerCAmelCase     =			{
      "AutoTokenizer": "PreTrainedTokenizerBase",
      "AutoFeatureExtractor": "FeatureExtractionMixin",
      "AutoImageProcessor": "ImageProcessingMixin",
  }
  
  class   _UpperCAmelCase	(   a_					):
  					__A          =   ['feature_extractor', 'tokenizer']
  					# Names need to be attr_class for attr in attributes
  					__A          =   None
  					__A          =   None
  					__A          =   None
  
  
  
  
  					def __init__(						self    ,      *__lowerCAmelCase    ,      **__lowerCAmelCase       )						->  List[Any]:
  							# Sanitize args and kwargs
  							for key in kwargs:
  									if key not in self.attributes:
  											raise TypeError(F"""Unexpected keyword argument {key}."""       )
  							for arg, attribute_name in zip(__lowerCAmelCase    ,      self.attributes       ):
  									if attribute_name in kwargs:
  											raise TypeError(F"""Got multiple values for argument {attribute_name}."""       )
  									else:
  											UpperCamelCase__         =	arg
  
  							if len(__lowerCAmelCase       ) != len(self.attributes       ):
  									raise ValueError(
  									    F"""This processor requires {len(self.attributes       )} arguments: {', '.join(self.attributes       )}. Got """
  									    F"""{len(__lowerCAmelCase       )} arguments instead."""       )
  
  							# Check each arg is of the proper class (this will also catch a user initializing in the wrong order)
  							for attribute_name, arg in kwargs.items():
  									UpperCamelCase__         =	getattr(self    ,      F"""{attribute_name}_class"""       )
  									# Nothing is ever going to be an instance of "AutoXxx", in that case we check the base class.
  									UpperCamelCase__         =	AUTO_TO_BASE_CLASS_MAPPING.get(__lowerCAmelCase    ,      __lowerCAmelCase       )
  									if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  											UpperCamelCase__         =	tuple(getattr(__lowerCAmelCase    ,      __lowerCAmelCase       ) for n in class_name if n is not None       )
  									else:
  											UpperCamelCase__         =	getattr(__lowerCAmelCase    ,      __lowerCAmelCase       )
  
  									if not isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  											raise ValueError(
  											    F"""Received a {type(__lowerCAmelCase       ).__name__} for argument {attribute_name}, but a {class_name} was expected."""       )
  
  									setattr(self    ,      __lowerCAmelCase    ,      __lowerCAmelCase       )
  
  
  
  
  					def __repr__(						self       )						->  str:
  							UpperCamelCase__         =	[F"""- {name}: {repr(getattr(self    ,      __lowerCAmelCase       )       )}""" for name in self.attributes]
  							UpperCamelCase__         =	'\n'.join(__lowerCAmelCase       )
  							return F"""{self.__class__.__name__}:\n{attributes_repr}"""
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase = False    ,      **__lowerCAmelCase       )						->  Union[str, Any]:
  							os.makedirs(__lowerCAmelCase    ,      exist_ok=__lowerCAmelCase       )
  
  							if push_to_hub:
  									UpperCamelCase__         =	kwargs.pop("""commit_message"""    ,      __lowerCAmelCase       )
  									UpperCamelCase__         =	kwargs.pop("""repo_id"""    ,      save_directory.split(os.path.sep       )[-1]       )
  									UpperCamelCase__         =	self._create_repo(__lowerCAmelCase    ,      **__lowerCAmelCase       )
  									UpperCamelCase__         =	self._get_files_timestamps(__lowerCAmelCase       )
  							# If we have a custom config, we copy the file defining it in the folder and set the attributes so it can be
  							# loaded from the Hub.
  							if self._auto_class is not None:
  									UpperCamelCase__         =	[getattr(self    ,      __lowerCAmelCase       ) for attribute_name in self.attributes]
  									UpperCamelCase__         =	[(a.init_kwargs if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ) else a) for a in attrs]
  									custom_object_save(self    ,      __lowerCAmelCase    ,      config=__lowerCAmelCase       )
  
  							for attribute_name in self.attributes:
  									UpperCamelCase__         =	getattr(self    ,      __lowerCAmelCase       )
  									# Include the processor class in the attribute config so this processor can then be reloaded with the
  									# `AutoProcessor` API.
  									if hasattr(__lowerCAmelCase    ,      """_set_processor_class"""       ):
  											attribute._set_processor_class(self.__class__.__name__       )
  									attribute.save_pretrained(__lowerCAmelCase       )
  
  							if self._auto_class is not None:
  									# We added an attribute to the init_kwargs of the tokenizers, which needs to be cleaned up.
  									for attribute_name in self.attributes:
  											UpperCamelCase__         =	getattr(self    ,      __lowerCAmelCase       )
  											if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  													del attribute.init_kwargs["auto_map"]
  
  							if push_to_hub:
  									self._upload_modified_files(
  									    __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      commit_message=__lowerCAmelCase    ,      token=kwargs.get("""use_auth_token"""       )    ,      )
  
  
  
  
  					@classmethod
  					def 	snake_case_		(						cls    ,      __lowerCAmelCase    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = False    ,      __lowerCAmelCase = False    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = "main"    ,      **__lowerCAmelCase    ,      )						->  Any:
  							UpperCamelCase__         =	cache_dir
  							UpperCamelCase__         =	force_download
  							UpperCamelCase__         =	local_files_only
  							UpperCamelCase__         =	revision
  
  							UpperCamelCase__         =	kwargs.pop("""use_auth_token"""    ,      __lowerCAmelCase       )
  							if use_auth_token is not None:
  									warnings.warn(
  									    """The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers."""    ,      __lowerCAmelCase       )
  									if token is not None:
  											raise ValueError(
  											    """`token` and `use_auth_token` are both specified. Please set only the argument `token`."""       )
  									UpperCamelCase__         =	use_auth_token
  
  							if token is not None:
  									# change to `token` in a follow-up PR
  									UpperCamelCase__         =	token
  
  							UpperCamelCase__         =	cls._get_arguments_from_pretrained(__lowerCAmelCase    ,      **__lowerCAmelCase       )
  							return cls(*__lowerCAmelCase       )
  
  
  
  
  					@classmethod
  					def 	snake_case_		(						cls    ,      __lowerCAmelCase="AutoProcessor"       )						->  List[str]:
  							if not isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  									UpperCamelCase__         =	auto_class.__name__
  
  							import transformers.models.auto as auto_module
  
  							if not hasattr(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  									raise ValueError(F"""{auto_class} is not a valid auto class."""       )
  
  							UpperCamelCase__         =	auto_class
  
  
  
  
  					@classmethod
  					def 	snake_case_		(						cls    ,      __lowerCAmelCase    ,      **__lowerCAmelCase       )						->  Dict:
  							UpperCamelCase__         =	[]
  							for attribute_name in cls.attributes:
  									UpperCamelCase__         =	getattr(cls    ,      F"""{attribute_name}_class"""       )
  									if isinstance(__lowerCAmelCase    ,      __lowerCAmelCase       ):
  											UpperCamelCase__         =	tuple(getattr(__lowerCAmelCase    ,      __lowerCAmelCase       ) if n is not None else None for n in class_name       )
  											UpperCamelCase__         =	kwargs.get("""use_fast"""    ,      __lowerCAmelCase       )
  											if use_fast and classes[1] is not None:
  													UpperCamelCase__         =	classes[1]
  											else:
  													UpperCamelCase__         =	classes[0]
  									else:
  											UpperCamelCase__         =	getattr(__lowerCAmelCase    ,      __lowerCAmelCase       )
  
  									args.append(attribute_class.from_pretrained(__lowerCAmelCase    ,      **__lowerCAmelCase       )       )
  							return args
  
  
  
  
  					@property
  					def 	snake_case_		(						self       )						->  int:
  							UpperCamelCase__         =	getattr(self    ,      self.attributes[0]       )
  							return getattr(__lowerCAmelCase    ,      """model_input_names"""    ,      __lowerCAmelCase       )
  
  
  
  __lowerCAmelCase     =			copy_func(ProcessorMixin.push_to_hub)
  if ProcessorMixin.push_to_hub.__doc__ is not None:
  				__lowerCAmelCase     =			ProcessorMixin.push_to_hub.__doc__.format(
  				    object="processor", object_class="AutoProcessor", object_files="processor files"
  				)
  
  
  
  
  
  
style_context: 
  
  
  
  
  
  from typing import Dict, List, Optional, Union
  
  import numpy as np
  
  from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
  from ...image_transforms import (
      get_resize_output_image_size,
      normalize,
      pad,
      rescale,
      resize,
      to_channel_dimension_format,
  )
  from ...image_utils import (
      IMAGENET_STANDARD_MEAN,
      IMAGENET_STANDARD_STD,
      ChannelDimension,
      ImageInput,
      PILImageResampling,
      get_image_size,
      make_list_of_images,
      to_numpy_array,
      valid_images,
  )
  from ...utils import TensorType, logging
  from ...utils.import_utils import is_vision_available
  
  
  __lowerCAmelCase     =			logging.get_logger(__name__)
  
  
  if is_vision_available():
  				import PIL
  
  
  
  
  class   _UpperCAmelCase	(   _lowercase					):
  					__A          =   ["pixel_values"]
  
  
  
  
  					def __init__(						self    ,      __lowerCAmelCase = True    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = PILImageResampling.BILINEAR    ,      __lowerCAmelCase = True    ,      __lowerCAmelCase = False    ,      __lowerCAmelCase = True    ,      __lowerCAmelCase = True    ,      __lowerCAmelCase = 1 / 255    ,      __lowerCAmelCase = True    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      **__lowerCAmelCase    ,      )						->  None:
  							super().__init__(**__lowerCAmelCase       )
  
  							UpperCamelCase__         =	size if size is not None else {"""height""": 2_560, """width""": 1_920}
  							if isinstance(__lowerCAmelCase    ,      (tuple, list)       ):
  									# The previous feature extractor size parameter was in (width, height) format
  									UpperCamelCase__         =	size[::-1]
  							UpperCamelCase__         =	get_size_dict(__lowerCAmelCase       )
  
  							UpperCamelCase__         =	do_resize
  							UpperCamelCase__         =	size
  							UpperCamelCase__         =	resample
  							UpperCamelCase__         =	do_thumbnail
  							UpperCamelCase__         =	do_align_long_axis
  							UpperCamelCase__         =	do_pad
  							UpperCamelCase__         =	do_rescale
  							UpperCamelCase__         =	rescale_factor
  							UpperCamelCase__         =	do_normalize
  							UpperCamelCase__         =	image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
  							UpperCamelCase__         =	image_std if image_std is not None else IMAGENET_STANDARD_STD
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = None       )						->  np.ndarray:
  							UpperCamelCase__  ,			UpperCamelCase__         =	get_image_size(__lowerCAmelCase       )
  							UpperCamelCase__  ,			UpperCamelCase__         =	size["""height"""], size["""width"""]
  
  							if (output_width < output_height and input_width > input_height) or (
  							    output_width > output_height and input_width < input_height
  							):
  									UpperCamelCase__         =	np.rotaa(__lowerCAmelCase    ,      3       )
  
  							if data_format is not None:
  									UpperCamelCase__         =	to_channel_dimension_format(__lowerCAmelCase    ,      __lowerCAmelCase       )
  
  							return image
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = False    ,      __lowerCAmelCase = None    ,      )						->  np.ndarray:
  							UpperCamelCase__  ,			UpperCamelCase__         =	size["""height"""], size["""width"""]
  							UpperCamelCase__  ,			UpperCamelCase__         =	get_image_size(__lowerCAmelCase       )
  
  							UpperCamelCase__         =	output_width - input_width
  							UpperCamelCase__         =	output_height - input_height
  
  							if random_padding:
  									UpperCamelCase__         =	np.random.randint(low=0    ,      high=delta_height + 1       )
  									UpperCamelCase__         =	np.random.randint(low=0    ,      high=delta_width + 1       )
  							else:
  									UpperCamelCase__         =	delta_height // 2
  									UpperCamelCase__         =	delta_width // 2
  
  							UpperCamelCase__         =	delta_height - pad_top
  							UpperCamelCase__         =	delta_width - pad_left
  
  							UpperCamelCase__         =	((pad_top, pad_bottom), (pad_left, pad_right))
  							return pad(__lowerCAmelCase    ,      __lowerCAmelCase    ,      data_format=__lowerCAmelCase       )
  
  
  
  
  					def 	snake_case_		(						self    ,      *__lowerCAmelCase    ,      **__lowerCAmelCase       )						->  List[Any]:
  							logger.info("""pad is deprecated and will be removed in version 4.27. Please use pad_image instead."""       )
  							return self.pad_image(*__lowerCAmelCase    ,      **__lowerCAmelCase       )
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = PILImageResampling.BICUBIC    ,      __lowerCAmelCase = None    ,      **__lowerCAmelCase    ,      )						->  np.ndarray:
  							UpperCamelCase__  ,			UpperCamelCase__         =	get_image_size(__lowerCAmelCase       )
  							UpperCamelCase__  ,			UpperCamelCase__         =	size["""height"""], size["""width"""]
  
  							# We always resize to the smallest of either the input or output size.
  							UpperCamelCase__         =	min(__lowerCAmelCase    ,      __lowerCAmelCase       )
  							UpperCamelCase__         =	min(__lowerCAmelCase    ,      __lowerCAmelCase       )
  
  							if height == input_height and width == input_width:
  									return image
  
  							if input_height > input_width:
  									UpperCamelCase__         =	int(input_width * height / input_height       )
  							elif input_width > input_height:
  									UpperCamelCase__         =	int(input_height * width / input_width       )
  
  							return resize(
  							    __lowerCAmelCase    ,      size=(height, width)    ,      resample=__lowerCAmelCase    ,      reducing_gap=2.0    ,      data_format=__lowerCAmelCase    ,      **__lowerCAmelCase       )
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = PILImageResampling.BICUBIC    ,      __lowerCAmelCase = None    ,      **__lowerCAmelCase    ,      )						->  np.ndarray:
  							UpperCamelCase__         =	get_size_dict(__lowerCAmelCase       )
  							UpperCamelCase__         =	min(size["""height"""]    ,      size["""width"""]       )
  							UpperCamelCase__         =	get_resize_output_image_size(__lowerCAmelCase    ,      size=__lowerCAmelCase    ,      default_to_square=__lowerCAmelCase       )
  							UpperCamelCase__         =	resize(__lowerCAmelCase    ,      size=__lowerCAmelCase    ,      resample=__lowerCAmelCase    ,      data_format=__lowerCAmelCase    ,      **__lowerCAmelCase       )
  							return resized_image
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = None    ,      **__lowerCAmelCase    ,      )						->  List[str]:
  							return rescale(__lowerCAmelCase    ,      scale=__lowerCAmelCase    ,      data_format=__lowerCAmelCase    ,      **__lowerCAmelCase       )
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase    ,      __lowerCAmelCase = None    ,      **__lowerCAmelCase    ,      )						->  np.ndarray:
  							return normalize(__lowerCAmelCase    ,      mean=__lowerCAmelCase    ,      std=__lowerCAmelCase    ,      data_format=__lowerCAmelCase    ,      **__lowerCAmelCase       )
  
  
  
  
  					def 	snake_case_		(						self    ,      __lowerCAmelCase    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = False    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = None    ,      __lowerCAmelCase = ChannelDimension.FIRST    ,      **__lowerCAmelCase    ,      )						->  PIL.Image.Image:
  							UpperCamelCase__         =	do_resize if do_resize is not None else self.do_resize
  							UpperCamelCase__         =	size if size is not None else self.size
  							if isinstance(__lowerCAmelCase    ,      (tuple, list)       ):
  									# Previous feature extractor had size in (width, height) format
  									UpperCamelCase__         =	size[::-1]
  							UpperCamelCase__         =	get_size_dict(__lowerCAmelCase       )
  							UpperCamelCase__         =	resample if resample is not None else self.resample
  							UpperCamelCase__         =	do_thumbnail if do_thumbnail is not None else self.do_thumbnail
  							UpperCamelCase__         =	do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
  							UpperCamelCase__         =	do_pad if do_pad is not None else self.do_pad
  							UpperCamelCase__         =	do_rescale if do_rescale is not None else self.do_rescale
  							UpperCamelCase__         =	rescale_factor if rescale_factor is not None else self.rescale_factor
  							UpperCamelCase__         =	do_normalize if do_normalize is not None else self.do_normalize
  							UpperCamelCase__         =	image_mean if image_mean is not None else self.image_mean
  							UpperCamelCase__         =	image_std if image_std is not None else self.image_std
  
  							UpperCamelCase__         =	make_list_of_images(__lowerCAmelCase       )
  
  							if not valid_images(__lowerCAmelCase       ):
  									raise ValueError(
  									    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
  									    """torch.Tensor, tf.Tensor or jax.ndarray."""       )
  
  							if do_resize and size is None:
  									raise ValueError("""Size must be specified if do_resize is True."""       )
  
  							if do_rescale and rescale_factor is None:
  									raise ValueError("""Rescale factor must be specified if do_rescale is True."""       )
  
  							if do_pad and size is None:
  									raise ValueError("""Size must be specified if do_pad is True."""       )
  
  							if do_normalize and (image_mean is None or image_std is None):
  									raise ValueError("""Image mean and std must be specified if do_normalize is True."""       )
  
  							# All transformations expect numpy arrays.
  							UpperCamelCase__         =	[to_numpy_array(__lowerCAmelCase       ) for image in images]
  
  							if do_align_long_axis:
  									UpperCamelCase__         =	[self.align_long_axis(__lowerCAmelCase    ,      size=__lowerCAmelCase       ) for image in images]
  
  							if do_resize:
  									UpperCamelCase__         =	[self.resize(image=__lowerCAmelCase    ,      size=__lowerCAmelCase    ,      resample=__lowerCAmelCase       ) for image in images]
  
  							if do_thumbnail:
  									UpperCamelCase__         =	[self.thumbnail(image=__lowerCAmelCase    ,      size=__lowerCAmelCase       ) for image in images]
  
  							if do_pad:
  									UpperCamelCase__         =	[self.pad_image(image=__lowerCAmelCase    ,      size=__lowerCAmelCase    ,      random_padding=__lowerCAmelCase       ) for image in images]
  
  							if do_rescale:
  									UpperCamelCase__         =	[self.rescale(image=__lowerCAmelCase    ,      scale=__lowerCAmelCase       ) for image in images]
  
  							if do_normalize:
  									UpperCamelCase__         =	[self.normalize(image=__lowerCAmelCase    ,      mean=__lowerCAmelCase    ,      std=__lowerCAmelCase       ) for image in images]
  
  							UpperCamelCase__         =	[to_channel_dimension_format(__lowerCAmelCase    ,      __lowerCAmelCase       ) for image in images]
  
  							UpperCamelCase__         =	{"""pixel_values""": images}
  							return BatchFeature(data=__lowerCAmelCase    ,      tensor_type=__lowerCAmelCase       )
  
  
  
  
  
  