

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


__A     :					Dict     = logging.get_logger(__name__)


if is_vision_available():
	import PIL


class        _SCREAMING_SNAKE_CASE	(   lowercase__):
						_UpperCamelCase:Union[str, Any]     =						['''pixel_values''']







						def __init__(       self			,		_SCREAMING_SNAKE_CASE = True			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC			,		_SCREAMING_SNAKE_CASE = True			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = True			,		_SCREAMING_SNAKE_CASE = 1 / 255			,		_SCREAMING_SNAKE_CASE = True			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = True			,		**_SCREAMING_SNAKE_CASE			,		)->				None:
													super().__init__(**SCREAMING_SNAKE_CASE__				)
													lowerCamelCase_				     =size if size is not None else {'shortest_edge': 224}
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__			,		default_to_square=SCREAMING_SNAKE_CASE__				)
													lowerCamelCase_				     =crop_size if crop_size is not None else {'height': 224, 'width': 224}
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__			,		default_to_square=SCREAMING_SNAKE_CASE__			,		param_name="""crop_size"""				)

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







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC			,		_SCREAMING_SNAKE_CASE = None			,		**_SCREAMING_SNAKE_CASE			,		)->				np.ndarray:
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__			,		default_to_square=SCREAMING_SNAKE_CASE__				)
													if "shortest_edge" not in size:
																				raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'				)
													lowerCamelCase_				     =get_resize_output_image_size(SCREAMING_SNAKE_CASE__			,		size=size["""shortest_edge"""]			,		default_to_square=SCREAMING_SNAKE_CASE__				)
													return resize(SCREAMING_SNAKE_CASE__			,		size=SCREAMING_SNAKE_CASE__			,		resample=SCREAMING_SNAKE_CASE__			,		data_format=SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE = None			,		**_SCREAMING_SNAKE_CASE			,		)->				np.ndarray:
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__				)
													if "height" not in size or "width" not in size:
																				raise ValueError(f'The `size` parameter must contain the keys (height, width). Got {size.keys()}'				)
													return center_crop(SCREAMING_SNAKE_CASE__			,		size=(size["""height"""], size["""width"""])			,		data_format=SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE = None			,		**_SCREAMING_SNAKE_CASE			,		)->				Optional[int]:
													return rescale(SCREAMING_SNAKE_CASE__			,		scale=SCREAMING_SNAKE_CASE__			,		data_format=SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE = None			,		**_SCREAMING_SNAKE_CASE			,		)->				np.ndarray:
													return normalize(SCREAMING_SNAKE_CASE__			,		mean=SCREAMING_SNAKE_CASE__			,		std=SCREAMING_SNAKE_CASE__			,		data_format=SCREAMING_SNAKE_CASE__			,		**SCREAMING_SNAKE_CASE__				)







						def     _snake_case							(       self			,		_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = None			,		_SCREAMING_SNAKE_CASE = ChannelDimension.FIRST			,		**_SCREAMING_SNAKE_CASE			,		)->				PIL.Image.Image:
													lowerCamelCase_				     =do_resize if do_resize is not None else self.do_resize
													lowerCamelCase_				     =size if size is not None else self.size
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__			,		param_name="""size"""			,		default_to_square=SCREAMING_SNAKE_CASE__				)
													lowerCamelCase_				     =resample if resample is not None else self.resample
													lowerCamelCase_				     =do_center_crop if do_center_crop is not None else self.do_center_crop
													lowerCamelCase_				     =crop_size if crop_size is not None else self.crop_size
													lowerCamelCase_				     =get_size_dict(SCREAMING_SNAKE_CASE__			,		param_name="""crop_size"""			,		default_to_square=SCREAMING_SNAKE_CASE__				)
													lowerCamelCase_				     =do_rescale if do_rescale is not None else self.do_rescale
													lowerCamelCase_				     =rescale_factor if rescale_factor is not None else self.rescale_factor
													lowerCamelCase_				     =do_normalize if do_normalize is not None else self.do_normalize
													lowerCamelCase_				     =image_mean if image_mean is not None else self.image_mean
													lowerCamelCase_				     =image_std if image_std is not None else self.image_std
													lowerCamelCase_				     =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

													lowerCamelCase_				     =make_list_of_images(SCREAMING_SNAKE_CASE__				)

													if not valid_images(SCREAMING_SNAKE_CASE__				):
																				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:
																				lowerCamelCase_				     =[convert_to_rgb(SCREAMING_SNAKE_CASE__				) for image in images]

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

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

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

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

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

													lowerCamelCase_				     =[to_channel_dimension_format(SCREAMING_SNAKE_CASE__			,		SCREAMING_SNAKE_CASE__				) for image in images]

													lowerCamelCase_				     ={'pixel_values': images}
													return BatchFeature(data=SCREAMING_SNAKE_CASE__			,		tensor_type=SCREAMING_SNAKE_CASE__				)




import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class  SCREAMING_SNAKE_CASE__  (				lowercase__			):
      snake_case__				:     Tuple        =						['''image_processor''', '''tokenizer''']
      snake_case__				:     Union[str, Any]        =						'''CLIPImageProcessor'''
      snake_case__				:     Dict        =						('''CLIPTokenizer''', '''CLIPTokenizerFast''')
      def __init__(       self				:       Optional[int]     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							SCREAMING_SNAKE_CASE__				:       List[str]=None     ,							**SCREAMING_SNAKE_CASE__				:       int     )		->			Any:
             a_					:		List[Any]							=  None
             if "feature_extractor" in kwargs:
                    warnings.warn(
                        'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                        ' instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
                    a_					:		Tuple							=  kwargs.pop('feature_extractor'     )

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

             super().__init__(SCREAMING_SNAKE_CASE__     ,							SCREAMING_SNAKE_CASE__     )
      def __call__(       self				:       List[Any]     ,							SCREAMING_SNAKE_CASE__				:       Optional[Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							SCREAMING_SNAKE_CASE__				:       Union[str, Any]=None     ,							**SCREAMING_SNAKE_CASE__				:       str     )		->			Optional[Any]:

             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_					:		List[str]							=  self.tokenizer(SCREAMING_SNAKE_CASE__     ,							return_tensors=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

             if images is not None:
                    a_					:		Dict							=  self.image_processor(SCREAMING_SNAKE_CASE__     ,							return_tensors=SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )

             if text is not None and images is not None:
                    a_					:		Dict							=  image_features.pixel_values
                    return encoding
             elif text is not None:
                    return encoding
             else:
                    return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE__     )     ,							tensor_type=SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Any     ,							*SCREAMING_SNAKE_CASE__				:       List[Any]     ,							**SCREAMING_SNAKE_CASE__				:       Optional[int]     )		->			List[Any]:
             return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )
      def        SCREAMING_SNAKE_CASE							(       self				:       Optional[Any]     ,							*SCREAMING_SNAKE_CASE__				:       List[Any]     ,							**SCREAMING_SNAKE_CASE__				:       Dict     )		->			Optional[Any]:
             return self.tokenizer.decode(*SCREAMING_SNAKE_CASE__     ,							**SCREAMING_SNAKE_CASE__     )
      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       List[Any]     )		->			Any:
             a_					:		str							=  self.tokenizer.model_input_names
             a_					:		Tuple							=  self.image_processor.model_input_names
             return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names     )     )
      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			str:
             warnings.warn(
                 '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
             return self.image_processor_class

      @property
      def        SCREAMING_SNAKE_CASE							(       self				:       str     )		->			Tuple:
             warnings.warn(
                 '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'     ,							SCREAMING_SNAKE_CASE__     ,							)
             return self.image_processor


