






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 (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


UpperCAmelCase__            =	logging.get_logger(__name__)


if is_vision_available():
  import PIL






class 	lowercase_      (			lowercase     ):






      '''simple docstring'''




      __snake_case  						=						['''pixel_values''']







      def __init__(   self   :		Dict				,      __UpperCAmelCase   :		bool = True				,      __UpperCAmelCase   :		Dict[str, int] = None				,      __UpperCAmelCase   :		PILImageResampling = PILImageResampling.BICUBIC				,      __UpperCAmelCase   :		bool = True				,      __UpperCAmelCase   :		Dict[str, int] = None				,      __UpperCAmelCase   :		bool = True				,      __UpperCAmelCase   :		Union[int, float] = 1 / 255				,      __UpperCAmelCase   :		bool = True				,      __UpperCAmelCase   :		Optional[Union[float, List[float]]] = None				,      __UpperCAmelCase   :		Optional[Union[float, List[float]]] = None				,      __UpperCAmelCase   :		bool = True				,      **__UpperCAmelCase   :		List[str]				,      )					->None:




       """simple docstring"""


       super().__init__(**__UpperCAmelCase						)
       a							=   size if size is not None else {'''shortest_edge''': 224}
       a							=   get_size_dict(__UpperCAmelCase				,      default_to_square=__UpperCAmelCase						)
       a							=   crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
       a							=   get_size_dict(__UpperCAmelCase						)

       a							=   do_resize
       a							=   size
       a							=   resample
       a							=   do_center_crop
       a							=   crop_size
       a							=   do_rescale
       a							=   rescale_factor
       a							=   do_normalize
       a							=   image_mean if image_mean is not None else OPENAI_CLIP_MEAN
       a							=   image_std if image_std is not None else OPENAI_CLIP_STD
       a							=   do_convert_rgb







      def    __lowerCAmelCase      (   self   :		Tuple				,      __UpperCAmelCase   :		np.ndarray				,      __UpperCAmelCase   :		Dict[str, int]				,      __UpperCAmelCase   :		PILImageResampling = PILImageResampling.BICUBIC				,      __UpperCAmelCase   :		Optional[Union[str, ChannelDimension]] = None				,      **__UpperCAmelCase   :		Optional[int]				,      )					->np.ndarray:




       """simple docstring"""


       a							=   get_size_dict(__UpperCAmelCase				,      default_to_square=__UpperCAmelCase						)
       a							=   get_resize_output_image_size(
           __UpperCAmelCase				,      size=(size['''height'''], size['''width'''])				,      default_to_square=__UpperCAmelCase						)
       return resize(__UpperCAmelCase				,      size=__UpperCAmelCase				,      resample=__UpperCAmelCase				,      data_format=__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		Optional[Any]				,      __UpperCAmelCase   :		np.ndarray				,      __UpperCAmelCase   :		Dict[str, int]				,      __UpperCAmelCase   :		Optional[Union[str, ChannelDimension]] = None				,      **__UpperCAmelCase   :		List[str]				,      )					->np.ndarray:




       """simple docstring"""


       a							=   get_size_dict(__UpperCAmelCase						)
       return center_crop(__UpperCAmelCase				,      size=(size['''height'''], size['''width'''])				,      data_format=__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		Optional[int]				,      __UpperCAmelCase   :		np.ndarray				,      __UpperCAmelCase   :		Union[int, float]				,      __UpperCAmelCase   :		Optional[Union[str, ChannelDimension]] = None				,      **__UpperCAmelCase   :		Union[str, Any]				,      )					->Optional[Any]:




       """simple docstring"""


       return rescale(__UpperCAmelCase				,      scale=__UpperCAmelCase				,      data_format=__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		np.ndarray				,      __UpperCAmelCase   :		Union[float, List[float]]				,      __UpperCAmelCase   :		Union[float, List[float]]				,      __UpperCAmelCase   :		Optional[Union[str, ChannelDimension]] = None				,      **__UpperCAmelCase   :		Union[str, Any]				,      )					->np.ndarray:




       """simple docstring"""


       return normalize(__UpperCAmelCase				,      mean=__UpperCAmelCase				,      std=__UpperCAmelCase				,      data_format=__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		Any				,      __UpperCAmelCase   :		ImageInput				,      __UpperCAmelCase   :		bool = None				,      __UpperCAmelCase   :		Dict[str, int] = None				,      __UpperCAmelCase   :		PILImageResampling = None				,      __UpperCAmelCase   :		bool = None				,      __UpperCAmelCase   :		int = None				,      __UpperCAmelCase   :		bool = None				,      __UpperCAmelCase   :		float = None				,      __UpperCAmelCase   :		bool = None				,      __UpperCAmelCase   :		Optional[Union[float, List[float]]] = None				,      __UpperCAmelCase   :		Optional[Union[float, List[float]]] = None				,      __UpperCAmelCase   :		bool = None				,      __UpperCAmelCase   :		Optional[Union[str, TensorType]] = None				,      __UpperCAmelCase   :		Optional[ChannelDimension] = ChannelDimension.FIRST				,      **__UpperCAmelCase   :		Union[str, Any]				,      )					->PIL.Image.Image:




       """simple docstring"""


       a							=   do_resize if do_resize is not None else self.do_resize
       a							=   size if size is not None else self.size
       a							=   get_size_dict(__UpperCAmelCase				,      default_to_square=__UpperCAmelCase						)
       a							=   resample if resample is not None else self.resample
       a							=   do_center_crop if do_center_crop is not None else self.do_center_crop
       a							=   crop_size if crop_size is not None else self.crop_size
       a							=   get_size_dict(__UpperCAmelCase						)
       a							=   do_rescale if do_rescale is not None else self.do_rescale
       a							=   rescale_factor if rescale_factor is not None else self.rescale_factor
       a							=   do_normalize if do_normalize is not None else self.do_normalize
       a							=   image_mean if image_mean is not None else self.image_mean
       a							=   image_std if image_std is not None else self.image_std
       a							=   do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

       a							=   make_list_of_images(__UpperCAmelCase						)

       if not valid_images(__UpperCAmelCase						):
        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_center_crop and crop_size is None:
        raise ValueError('''Crop size must be specified if do_center_crop is True.'''						)

       if do_rescale and rescale_factor is None:
        raise ValueError('''Rescale factor must be specified if do_rescale 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.'''						)

       # PIL RGBA images are converted to RGB
       if do_convert_rgb:
        a							=   [convert_to_rgb(__UpperCAmelCase						) for image in images]

       # All transformations expect numpy arrays.
       a							=   [to_numpy_array(__UpperCAmelCase						) for image in images]

       if do_resize:
        a							=   [self.resize(image=__UpperCAmelCase				,      size=__UpperCAmelCase				,      resample=__UpperCAmelCase						) for image in images]

       if do_center_crop:
        a							=   [self.center_crop(image=__UpperCAmelCase				,      size=__UpperCAmelCase						) for image in images]

       if do_rescale:
        a							=   [self.rescale(image=__UpperCAmelCase				,      scale=__UpperCAmelCase						) for image in images]

       if do_normalize:
        a							=   [self.normalize(image=__UpperCAmelCase				,      mean=__UpperCAmelCase				,      std=__UpperCAmelCase						) for image in images]

       a							=   [to_channel_dimension_format(__UpperCAmelCase				,      __UpperCAmelCase						) for image in images]

       a							=   {'''pixel_values''': images}
       return BatchFeature(data=__UpperCAmelCase				,      tensor_type=__UpperCAmelCase						)










import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class 	lowercase_      (			lowercase     ):






      '''simple docstring'''




      __snake_case  						=						['''image_processor''', '''tokenizer''']
      __snake_case  						=						'''AutoImageProcessor'''
      __snake_case  						=						'''AutoTokenizer'''







      def __init__(   self   :		Any				,      __UpperCAmelCase   :		Union[str, Any]=None				,      __UpperCAmelCase   :		int=None				,      **__UpperCAmelCase   :		Union[str, Any]						)					->Dict:




       """simple docstring"""


       a							=   None
       if "feature_extractor" in kwargs:
        warnings.warn(
            '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
            ''' instead.'''				,      __UpperCAmelCase				,      )
        a							=   kwargs.pop('''feature_extractor'''						)

       a							=   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
        raise ValueError('''You have to specify an image_processor.'''						)
       if tokenizer is None:
        raise ValueError('''You have to specify a tokenizer.'''						)

       super().__init__(__UpperCAmelCase				,      __UpperCAmelCase						)
       a							=   self.image_processor







      def __call__(   self   :		List[Any]				,      __UpperCAmelCase   :		Optional[Any]=None				,      __UpperCAmelCase   :		Optional[int]=None				,      __UpperCAmelCase   :		str=None				,      **__UpperCAmelCase   :		List[Any]						)					->Dict:




       """simple docstring"""


       if text is None and images is None:
        raise ValueError('''You have to specify either text or images. Both cannot be none.'''						)

       if text is not None:
        a							=   self.tokenizer(__UpperCAmelCase				,      return_tensors=__UpperCAmelCase				,      **__UpperCAmelCase						)

       if images is not None:
        a							=   self.image_processor(__UpperCAmelCase				,      return_tensors=__UpperCAmelCase				,      **__UpperCAmelCase						)

       if text is not None and images is not None:
        a							=   image_features.pixel_values
        return encoding
       elif text is not None:
        return encoding
       else:
        return BatchEncoding(data=dict(**__UpperCAmelCase						)				,      tensor_type=__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		List[Any]				,      *__UpperCAmelCase   :		Optional[Any]				,      **__UpperCAmelCase   :		List[str]						)					->Optional[Any]:




       """simple docstring"""


       return self.tokenizer.batch_decode(*__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		str				,      *__UpperCAmelCase   :		List[Any]				,      **__UpperCAmelCase   :		Optional[Any]						)					->Union[str, Any]:




       """simple docstring"""


       return self.tokenizer.decode(*__UpperCAmelCase				,      **__UpperCAmelCase						)







      @property
      def    __lowerCAmelCase      (   self   :		str						)					->str:




       """simple docstring"""


       a							=   self.tokenizer.model_input_names
       a							=   self.image_processor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names						)						)







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




       """simple docstring"""


       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''				,      __UpperCAmelCase				,      )
       return self.image_processor_class







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




       """simple docstring"""


       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''				,      __UpperCAmelCase				,      )
       return self.image_processor


