

from typing import Dict, List, Optional, Union

import numpy as np

from transformers.utils import is_vision_available
from transformers.utils.generic import TensorType

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import logging


if is_vision_available():
						import PIL

lowerCAmelCase							:  List[Any]   	=						logging.get_logger(__name__)







def 			A_      (    _UpperCAmelCase    ):
						if isinstance(_UpperCAmelCase				,	(list, tuple)    ) and isinstance(videos[0]				,	(list, tuple)    ) and is_valid_image(videos[0][0]    ):
												return videos

						elif isinstance(_UpperCAmelCase				,	(list, tuple)    ) and is_valid_image(videos[0]    ):
												return [videos]

						elif is_valid_image(_UpperCAmelCase    ):
												return [[videos]]

						raise ValueError(f"Could not make batched video from {videos}"    )



class   __lowercase				( UpperCAmelCase_	):




			"""simple docstring"""







			_UpperCAmelCase   :							List[str]										=     ['''pixel_values''']



			def __init__( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     PILImageResampling = PILImageResampling.BILINEAR   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     Union[int, float] = 1 / 255   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						**lowerCAmelCase__  :     Optional[Any]   ,						):
									super().__init__(**lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       size if size is not None else {"shortest_edge": 256}
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       get_size_dict(lowerCAmelCase__   ,						default_to_square=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: int  		=       crop_size if crop_size is not None else {"height": 224, "width": 224}
									SCREAMING_SNAKE_CASE_: List[Any]  		=       get_size_dict(lowerCAmelCase__   ,						param_name="crop_size")

									SCREAMING_SNAKE_CASE_: Any  		=       do_resize
									SCREAMING_SNAKE_CASE_: str  		=       size
									SCREAMING_SNAKE_CASE_: Tuple  		=       do_center_crop
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       crop_size
									SCREAMING_SNAKE_CASE_: Any  		=       resample
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       do_rescale
									SCREAMING_SNAKE_CASE_: str  		=       rescale_factor
									SCREAMING_SNAKE_CASE_: Dict  		=       offset
									SCREAMING_SNAKE_CASE_: Any  		=       do_normalize
									SCREAMING_SNAKE_CASE_: List[str]  		=       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
									SCREAMING_SNAKE_CASE_: List[str]  		=       image_std if image_std is not None else IMAGENET_STANDARD_STD



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     Dict[str, int]   ,						lowerCAmelCase__  :     PILImageResampling = PILImageResampling.BILINEAR   ,						lowerCAmelCase__  :     Optional[Union[str, ChannelDimension]] = None   ,						**lowerCAmelCase__  :     List[str]   ,						):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       get_size_dict(lowerCAmelCase__   ,						default_to_square=lowerCAmelCase__)
									if "shortest_edge" in size:
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       get_resize_output_image_size(lowerCAmelCase__   ,						size["shortest_edge"]   ,						default_to_square=lowerCAmelCase__)
									elif "height" in size and "width" in size:
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       (size["height"], size["width"])
									else:
															raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
									return resize(lowerCAmelCase__   ,						size=lowerCAmelCase__   ,						resample=lowerCAmelCase__   ,						data_format=lowerCAmelCase__   ,						**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     Dict[str, int]   ,						lowerCAmelCase__  :     Optional[Union[str, ChannelDimension]] = None   ,						**lowerCAmelCase__  :     int   ,						):
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       get_size_dict(lowerCAmelCase__)
									if "height" not in size or "width" not in size:
															raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}")
									return center_crop(lowerCAmelCase__   ,						size=(size["height"], size["width"])   ,						data_format=lowerCAmelCase__   ,						**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     Union[int, float]   ,						lowerCAmelCase__  :     bool = True   ,						lowerCAmelCase__  :     Optional[Union[str, ChannelDimension]] = None   ,						**lowerCAmelCase__  :     Tuple   ,						):
									SCREAMING_SNAKE_CASE_: List[str]  		=       image.astype(np.floataa)
									if offset:
															SCREAMING_SNAKE_CASE_: str  		=       image - (scale / 2)
									return rescale(lowerCAmelCase__   ,						scale=lowerCAmelCase__   ,						data_format=lowerCAmelCase__   ,						**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     Union[float, List[float]]   ,						lowerCAmelCase__  :     Union[float, List[float]]   ,						lowerCAmelCase__  :     Optional[Union[str, ChannelDimension]] = None   ,						**lowerCAmelCase__  :     int   ,						):
									return normalize(lowerCAmelCase__   ,						mean=lowerCAmelCase__   ,						std=lowerCAmelCase__   ,						data_format=lowerCAmelCase__   ,						**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     ImageInput   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     PILImageResampling = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     float = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						lowerCAmelCase__  :     Optional[ChannelDimension] = ChannelDimension.FIRST   ,						):
									if do_resize and size is None or resample is None:
															raise ValueError("Size and resample 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.")

									if offset and not do_rescale:
															raise ValueError("For offset, do_rescale must also be set to True.")

									# All transformations expect numpy arrays.
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       to_numpy_array(lowerCAmelCase__)

									if do_resize:
															SCREAMING_SNAKE_CASE_: List[Any]  		=       self.resize(image=lowerCAmelCase__   ,						size=lowerCAmelCase__   ,						resample=lowerCAmelCase__)

									if do_center_crop:
															SCREAMING_SNAKE_CASE_: Any  		=       self.center_crop(lowerCAmelCase__   ,						size=lowerCAmelCase__)

									if do_rescale:
															SCREAMING_SNAKE_CASE_: List[Any]  		=       self.rescale(image=lowerCAmelCase__   ,						scale=lowerCAmelCase__   ,						offset=lowerCAmelCase__)

									if do_normalize:
															SCREAMING_SNAKE_CASE_: Tuple  		=       self.normalize(image=lowerCAmelCase__   ,						mean=lowerCAmelCase__   ,						std=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Any  		=       to_channel_dimension_format(lowerCAmelCase__   ,						lowerCAmelCase__)
									return image




			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     ImageInput   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     PILImageResampling = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Dict[str, int] = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     float = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     bool = None   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						lowerCAmelCase__  :     Optional[Union[float, List[float]]] = None   ,						lowerCAmelCase__  :     Optional[Union[str, TensorType]] = None   ,						lowerCAmelCase__  :     ChannelDimension = ChannelDimension.FIRST   ,						**lowerCAmelCase__  :     Optional[int]   ,						):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       do_resize if do_resize is not None else self.do_resize
									SCREAMING_SNAKE_CASE_: int  		=       resample if resample is not None else self.resample
									SCREAMING_SNAKE_CASE_: Any  		=       do_center_crop if do_center_crop is not None else self.do_center_crop
									SCREAMING_SNAKE_CASE_: str  		=       do_rescale if do_rescale is not None else self.do_rescale
									SCREAMING_SNAKE_CASE_: int  		=       rescale_factor if rescale_factor is not None else self.rescale_factor
									SCREAMING_SNAKE_CASE_: Dict  		=       offset if offset is not None else self.offset
									SCREAMING_SNAKE_CASE_: str  		=       do_normalize if do_normalize is not None else self.do_normalize
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       image_mean if image_mean is not None else self.image_mean
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       image_std if image_std is not None else self.image_std

									SCREAMING_SNAKE_CASE_: Tuple  		=       size if size is not None else self.size
									SCREAMING_SNAKE_CASE_: str  		=       get_size_dict(lowerCAmelCase__   ,						default_to_square=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: int  		=       crop_size if crop_size is not None else self.crop_size
									SCREAMING_SNAKE_CASE_: List[Any]  		=       get_size_dict(lowerCAmelCase__   ,						param_name="crop_size")

									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.")

									SCREAMING_SNAKE_CASE_: List[Any]  		=       make_batched(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       [
									    [
									        self._preprocess_image(
									            image=lowerCAmelCase__   ,						do_resize=lowerCAmelCase__   ,						size=lowerCAmelCase__   ,						resample=lowerCAmelCase__   ,						do_center_crop=lowerCAmelCase__   ,						crop_size=lowerCAmelCase__   ,						do_rescale=lowerCAmelCase__   ,						rescale_factor=lowerCAmelCase__   ,						offset=lowerCAmelCase__   ,						do_normalize=lowerCAmelCase__   ,						image_mean=lowerCAmelCase__   ,						image_std=lowerCAmelCase__   ,						data_format=lowerCAmelCase__   ,						)
									        for img in video
									    ]
									    for video in videos
									]

									SCREAMING_SNAKE_CASE_: Any  		=       {"pixel_values": videos}
									return BatchFeature(data=lowerCAmelCase__   ,						tensor_type=lowerCAmelCase__)









from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
						if not (is_transformers_available() and is_torch_available() and is_transformers_version(""">=""", """4.25.0""")):
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						from ...utils.dummy_torch_and_transformers_objects import UnCLIPImageVariationPipeline, UnCLIPPipeline
else:
						from .pipeline_unclip import UnCLIPPipeline
						from .pipeline_unclip_image_variation import UnCLIPImageVariationPipeline
						from .text_proj import UnCLIPTextProjModel






