






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,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


lowerCamelCase			:					Optional[Any]         =	logging.get_logger(__name__)



class 							_UpperCamelCase			(a_			):
      snake_case_  =							["""pixel_values"""]


      def __init__(	self					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = PILImageResampling.BICUBIC					,						__UpperCamelCase = True					,						__UpperCamelCase = True					,						__UpperCamelCase = 1 / 2_5_5					,						__UpperCamelCase = None					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	None:
           super().__init__(**__UpperCamelCase	)
           __lowerCAmelCase								=					size if size is not None else {"height": 2_2_4, "width": 2_2_4}
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           __lowerCAmelCase								=					crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase					,						default_to_square=__UpperCamelCase					,						param_name="crop_size"	)

           __lowerCAmelCase								=					do_resize
           __lowerCAmelCase								=					do_rescale
           __lowerCAmelCase								=					do_normalize
           __lowerCAmelCase								=					do_center_crop
           __lowerCAmelCase								=					crop_size
           __lowerCAmelCase								=					size
           __lowerCAmelCase								=					resample
           __lowerCAmelCase								=					rescale_factor
           __lowerCAmelCase								=					image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
           __lowerCAmelCase								=					image_std if image_std is not None else IMAGENET_DEFAULT_STD


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = PILImageResampling.BILINEAR					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)

           if "shortest_edge" in size:
                __lowerCAmelCase								=					get_resize_output_image_size(__UpperCamelCase					,						size=size["shortest_edge"]					,						default_to_square=__UpperCamelCase	)
                # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
           elif "height" in size and "width" in size:
                __lowerCAmelCase								=					(size["height"], size["width"])
           else:
                raise ValueError(F"""Size must contain 'height' and 'width' keys or 'shortest_edge' key. Got {size.keys()}"""	)
           return resize(__UpperCamelCase					,						size=__UpperCamelCase					,						resample=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           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(__UpperCamelCase					,						size=(size["height"], size["width"])					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase	)->	np.ndarray:
           return rescale(__UpperCamelCase					,						scale=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           return normalize(__UpperCamelCase					,						mean=__UpperCamelCase					,						std=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = ChannelDimension.FIRST					,						**__UpperCamelCase					,						)->	BatchFeature:
           __lowerCAmelCase								=					do_resize if do_resize is not None else self.do_resize
           __lowerCAmelCase								=					do_rescale if do_rescale is not None else self.do_rescale
           __lowerCAmelCase								=					do_normalize if do_normalize is not None else self.do_normalize
           __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(__UpperCamelCase					,						param_name="crop_size"					,						default_to_square=__UpperCamelCase	)
           __lowerCAmelCase								=					resample if resample is not None else self.resample
           __lowerCAmelCase								=					rescale_factor if rescale_factor is not None else self.rescale_factor
           __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								=					size if size is not None else self.size
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)

           if not is_batched(__UpperCamelCase	):
                __lowerCAmelCase								=					[images]

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

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

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

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

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

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

           __lowerCAmelCase								=					[to_channel_dimension_format(__UpperCamelCase					,						__UpperCamelCase	) for image in images]

           __lowerCAmelCase								=					{"pixel_values": images}
           return BatchFeature(data=__UpperCamelCase					,						tensor_type=__UpperCamelCase	)














import os
import sys
import unittest


lowerCamelCase			:					int         =	os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, '''utils'''))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
lowerCamelCase			:					Any         =	os.path.join(git_repo_path, '''src''', '''diffusers''')



class 							_UpperCamelCase			(unittest.TestCase			):


      def  __UpperCAmelCase		(	self	)->	Optional[int]:
           __lowerCAmelCase								=					find_backend("    if not is_torch_available():"	)
           self.assertEqual(__UpperCamelCase					,						"torch"	)

           # backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
           # self.assertEqual(backend_with_underscore, "tensorflow_text")

           __lowerCAmelCase								=					find_backend("    if not (is_torch_available() and is_transformers_available()):"	)
           self.assertEqual(__UpperCamelCase					,						"torch_and_transformers"	)

           # double_backend_with_underscore = find_backend(
           #    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
           # )
           # self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

           __lowerCAmelCase								=					find_backend(
               "    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):"	)
           self.assertEqual(__UpperCamelCase					,						"torch_and_transformers_and_onnx"	)


      def  __UpperCAmelCase		(	self	)->	Dict:
           __lowerCAmelCase								=					read_init()
           # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
           self.assertIn("torch"					,						__UpperCamelCase	)
           self.assertIn("torch_and_transformers"					,						__UpperCamelCase	)
           self.assertIn("flax_and_transformers"					,						__UpperCamelCase	)
           self.assertIn("torch_and_transformers_and_onnx"					,						__UpperCamelCase	)

           # Likewise, we can't assert on the exact content of a key
           self.assertIn("UNet2DModel"					,						objects["torch"]	)
           self.assertIn("FlaxUNet2DConditionModel"					,						objects["flax"]	)
           self.assertIn("StableDiffusionPipeline"					,						objects["torch_and_transformers"]	)
           self.assertIn("FlaxStableDiffusionPipeline"					,						objects["flax_and_transformers"]	)
           self.assertIn("LMSDiscreteScheduler"					,						objects["torch_and_scipy"]	)
           self.assertIn("OnnxStableDiffusionPipeline"					,						objects["torch_and_transformers_and_onnx"]	)


      def  __UpperCAmelCase		(	self	)->	Any:
           __lowerCAmelCase								=					create_dummy_object("CONSTANT"					,						"'torch'"	)
           self.assertEqual(__UpperCamelCase					,						"\nCONSTANT = None\n"	)

           __lowerCAmelCase								=					create_dummy_object("function"					,						"'torch'"	)
           self.assertEqual(
               __UpperCamelCase					,						"\ndef function(*args, **kwargs):\n    requires_backends(function, 'torch')\n"	)

           __lowerCAmelCase								=					"\nclass FakeClass(metaclass=DummyObject):\n    _backends = 'torch'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, 'torch')\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, 'torch')\n"
           __lowerCAmelCase								=					create_dummy_object("FakeClass"					,						"'torch'"	)
           self.assertEqual(__UpperCamelCase					,						__UpperCamelCase	)


      def  __UpperCAmelCase		(	self	)->	Tuple:
           __lowerCAmelCase								=					"# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, [\"torch\"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = [\"torch\"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, [\"torch\"])\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, [\"torch\"])\n"
           __lowerCAmelCase								=					create_dummy_files({"torch": ["CONSTANT", "function", "FakeClass"]}	)
           self.assertEqual(dummy_files["torch"]					,						__UpperCamelCase	)






