


'''simple docstring'''






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_   :      Any				    =      logging.get_logger(__name__)


if is_vision_available():
 import PIL




class 	a       (       _SCREAMING_SNAKE_CASE    ):
 _lowerCAmelCase	   =		["""pixel_values"""]
 def __init__(	self ,			__magic_name__ = True ,			__magic_name__ = None ,			__magic_name__ = PILImageResampling.BICUBIC ,			__magic_name__ = True ,			__magic_name__ = None ,			__magic_name__ = True ,			__magic_name__ = 1 / 2_55 ,			__magic_name__ = True ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = True ,			**__magic_name__ ,			)   ->							None:
      super().__init__(**__magic_name__  )
      _a      =							size if size is not None else {'shortest_edge': 2_24}
      _a      =							get_size_dict(__magic_name__ ,			default_to_square=__magic_name__  )
      _a      =							crop_size if crop_size is not None else {'height': 2_24, 'width': 2_24}
      _a      =							get_size_dict(__magic_name__ ,			default_to_square=__magic_name__ ,			param_name='crop_size'  )

      _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 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ = PILImageResampling.BICUBIC ,			__magic_name__ = None ,			**__magic_name__ ,			)   ->							np.ndarray:
      _a      =							get_size_dict(__magic_name__ ,			default_to_square=__magic_name__  )
      if "shortest_edge" not in size:
           raise ValueError(f'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'  )
      _a      =							get_resize_output_image_size(__magic_name__ ,			size=size['shortest_edge'] ,			default_to_square=__magic_name__  )
      return resize(__magic_name__ ,			size=__magic_name__ ,			resample=__magic_name__ ,			data_format=__magic_name__ ,			**__magic_name__  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ = None ,			**__magic_name__ ,			)   ->							np.ndarray:
      _a      =							get_size_dict(__magic_name__  )
      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(__magic_name__ ,			size=(size['height'], size['width']) ,			data_format=__magic_name__ ,			**__magic_name__  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ = None ,			**__magic_name__ ,			)   ->							List[Any]:
      return rescale(__magic_name__ ,			scale=__magic_name__ ,			data_format=__magic_name__ ,			**__magic_name__  )
 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ ,			__magic_name__ = None ,			**__magic_name__ ,			)   ->							np.ndarray:
      return normalize(__magic_name__ ,			mean=__magic_name__ ,			std=__magic_name__ ,			data_format=__magic_name__ ,			**__magic_name__  )




 def 							__UpperCAmelCase						(	self ,			__magic_name__ ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = None ,			__magic_name__ = ChannelDimension.FIRST ,			**__magic_name__ ,			)   ->							PIL.Image.Image:
      _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(__magic_name__ ,			param_name='size' ,			default_to_square=__magic_name__  )
      _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(__magic_name__ ,			param_name='crop_size' ,			default_to_square=__magic_name__  )
      _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(__magic_name__  )

      if not valid_images(__magic_name__  ):
           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(__magic_name__  ) for image in images]

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

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

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

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

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

      _a      =							[to_channel_dimension_format(__magic_name__ ,			__magic_name__  ) for image in images]

      _a      =							{'pixel_values': images}
      return BatchFeature(data=__magic_name__ ,			tensor_type=__magic_name__  )





'''simple docstring'''






import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible




class 	a       (       unittest.TestCase    ):
 def 							__UpperCAmelCase						(	self  )   ->							int:
      _a      =							[
          'safety_checker/pytorch_model.bin',
          'safety_checker/model.safetensors',
          'vae/diffusion_pytorch_model.bin',
          'vae/diffusion_pytorch_model.safetensors',
          'text_encoder/pytorch_model.bin',
          'text_encoder/model.safetensors',
          'unet/diffusion_pytorch_model.bin',
          'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertTrue(is_safetensors_compatible(__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Dict:
      _a      =							[
          'unet/diffusion_pytorch_model.bin',
          'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertTrue(is_safetensors_compatible(__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Optional[int]:
      _a      =							[
          'safety_checker/pytorch_model.bin',
          'safety_checker/model.safetensors',
          'vae/diffusion_pytorch_model.bin',
          'vae/diffusion_pytorch_model.safetensors',
          'text_encoder/pytorch_model.bin',
          'text_encoder/model.safetensors',
          'unet/diffusion_pytorch_model.bin',
          # Removed: 'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertFalse(is_safetensors_compatible(__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Optional[Any]:
      _a      =							[
          'text_encoder/pytorch_model.bin',
          'text_encoder/model.safetensors',
      ]
      self.assertTrue(is_safetensors_compatible(__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							str:
      _a      =							[
          'safety_checker/pytorch_model.bin',
          'safety_checker/model.safetensors',
          'vae/diffusion_pytorch_model.bin',
          'vae/diffusion_pytorch_model.safetensors',
          'text_encoder/pytorch_model.bin',
          # Removed: 'text_encoder/model.safetensors',
          'unet/diffusion_pytorch_model.bin',
          'unet/diffusion_pytorch_model.safetensors',
      ]
      self.assertFalse(is_safetensors_compatible(__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							List[Any]:
      _a      =							[
          'safety_checker/pytorch_model.fp16.bin',
          'safety_checker/model.fp16.safetensors',
          'vae/diffusion_pytorch_model.fp16.bin',
          'vae/diffusion_pytorch_model.fp16.safetensors',
          'text_encoder/pytorch_model.fp16.bin',
          'text_encoder/model.fp16.safetensors',
          'unet/diffusion_pytorch_model.fp16.bin',
          'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _a      =							'fp16'
      self.assertTrue(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Any:
      _a      =							[
          'unet/diffusion_pytorch_model.fp16.bin',
          'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _a      =							'fp16'
      self.assertTrue(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							int:
      # pass variant but use the non-variant filenames
      _a      =							[
          'unet/diffusion_pytorch_model.bin',
          'unet/diffusion_pytorch_model.safetensors',
      ]
      _a      =							'fp16'
      self.assertTrue(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							List[str]:
      _a      =							[
          'safety_checker/pytorch_model.fp16.bin',
          'safety_checker/model.fp16.safetensors',
          'vae/diffusion_pytorch_model.fp16.bin',
          'vae/diffusion_pytorch_model.fp16.safetensors',
          'text_encoder/pytorch_model.fp16.bin',
          'text_encoder/model.fp16.safetensors',
          'unet/diffusion_pytorch_model.fp16.bin',
          # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _a      =							'fp16'
      self.assertFalse(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							Union[str, Any]:
      _a      =							[
          'text_encoder/pytorch_model.fp16.bin',
          'text_encoder/model.fp16.safetensors',
      ]
      _a      =							'fp16'
      self.assertTrue(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )
 def 							__UpperCAmelCase						(	self  )   ->							List[Any]:
      # pass variant but use the non-variant filenames
      _a      =							[
          'text_encoder/pytorch_model.bin',
          'text_encoder/model.safetensors',
      ]
      _a      =							'fp16'
      self.assertTrue(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )




 def 							__UpperCAmelCase						(	self  )   ->							List[Any]:
      _a      =							[
          'safety_checker/pytorch_model.fp16.bin',
          'safety_checker/model.fp16.safetensors',
          'vae/diffusion_pytorch_model.fp16.bin',
          'vae/diffusion_pytorch_model.fp16.safetensors',
          'text_encoder/pytorch_model.fp16.bin',
          # 'text_encoder/model.fp16.safetensors',
          'unet/diffusion_pytorch_model.fp16.bin',
          'unet/diffusion_pytorch_model.fp16.safetensors',
      ]
      _a      =							'fp16'
      self.assertFalse(is_safetensors_compatible(__magic_name__ ,			variant=__magic_name__  )  )

