




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


if is_vision_available():
					import PIL


UpperCamelCase__            =     logging.get_logger(__name__)
class      a__       (   snake_case__ ):
						_a				:       int   =       ["""pixel_values"""]






						def __init__(  self						,     _A = True						,     _A = None						,     _A = True						,     _A = None						,     _A = PILImageResampling.BICUBIC						,     _A = True						,     _A = 1 / 2_5_5						,     _A = True						,     _A = None						,     _A = None						,     **_A						,     ):




											"""simple docstring"""



											super().__init__(**_A     )
											__lowerCAmelCase							  =			crop_size if crop_size is not None else {"height": 2_5_6, "width": 2_5_6}
											__lowerCAmelCase							  =			get_size_dict(_A						,     param_name="crop_size"     )
											__lowerCAmelCase							  =			size if size is not None else {"height": 2_2_4, "width": 2_2_4}
											__lowerCAmelCase							  =			get_size_dict(_A     )

											__lowerCAmelCase							  =			do_center_crop
											__lowerCAmelCase							  =			crop_size
											__lowerCAmelCase							  =			do_resize
											__lowerCAmelCase							  =			size
											__lowerCAmelCase							  =			resample
											__lowerCAmelCase							  =			do_rescale
											__lowerCAmelCase							  =			rescale_factor
											__lowerCAmelCase							  =			do_normalize
											__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 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A = None						,     _A = None						,     **_A						,     ):




											"""simple docstring"""



											__lowerCAmelCase							  =			self.size if size is None else size
											__lowerCAmelCase							  =			get_size_dict(_A     )
											__lowerCAmelCase							  =			get_size_dict(_A						,     param_name="crop_size"     )

											__lowerCAmelCase  ,				__lowerCAmelCase							  =			get_image_size(_A     )
											__lowerCAmelCase							  =			min(_A						,     _A     )
											__lowerCAmelCase							  =			(size["height"] / crop_size["height"]) * min_dim
											__lowerCAmelCase							  =			(size["width"] / crop_size["width"]) * min_dim
											return center_crop(_A						,     size=(cropped_height, cropped_width)						,     data_format=_A						,     **_A     )






						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A = PIL.Image.BICUBIC						,     _A = None						,     **_A						,     ):




											"""simple docstring"""



											__lowerCAmelCase							  =			get_size_dict(_A     )
											if "height" not in size or "width" not in size:
																raise ValueError(f"""The size dictionary must contain the keys 'height' and 'width'. Got {size.keys()}"""     )
											return resize(
											    _A						,     size=(size["height"], size["width"])						,     resample=_A						,     data_format=_A						,     **_A     )






						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A = None						,     **_A						,     ):




											"""simple docstring"""



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






						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A						,     _A = None						,     **_A						,     ):




											"""simple docstring"""



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






						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = None						,     _A = ChannelDimension.FIRST						,     **_A						,     ):




											"""simple docstring"""



											__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(_A						,     param_name="crop_size"     )
											__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(_A     )
											__lowerCAmelCase							  =			resample if resample is not None else self.resample
											__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							  =			make_list_of_images(_A     )

											if not valid_images(_A     ):
																raise ValueError(
																    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
																    "torch.Tensor, tf.Tensor or jax.ndarray."     )

											if do_center_crop and crop_size is None:
																raise ValueError("If `do_center_crop` is set to `True`, `crop_size` must be provided."     )

											if do_resize and size is None:
																raise ValueError("Size must be specified if do_resize 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 image standard deviation must be specified if do_normalize is True."     )

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

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

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

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

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

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

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










from typing import Any, Callable, Dict, List, Optional, Union

import PIL.Image
import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionImgaImgPipeline,
    StableDiffusionInpaintPipelineLegacy,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.configuration_utils import FrozenDict
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import deprecate, logging


UpperCamelCase__            =     logging.get_logger(__name__)  # pylint: disable=invalid-name
class      a__       (   snake_case__ ):
						_a				:       Tuple   =       ["""safety_checker""", """feature_extractor"""]






						def __init__(  self						,     _A						,     _A						,     _A						,     _A						,     _A						,     _A						,     _A						,     _A = True						,     ):




											"""simple docstring"""



											super().__init__()
											if hasattr(scheduler.config						,     "steps_offset"     ) and scheduler.config.steps_offset != 1:
																__lowerCAmelCase							  =			(
																    f"""The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`"""
																    f""" should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure """
																    "to update the config accordingly as leaving `steps_offset` might led to incorrect results"
																    " in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,"
																    " it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`"
																    " file"
																)
																deprecate("steps_offset!=1"						,     "1.0.0"						,     _A						,     standard_warn=_A     )
																__lowerCAmelCase							  =			dict(scheduler.config     )
																__lowerCAmelCase							  =			1
																__lowerCAmelCase							  =			FrozenDict(_A     )

											self.register_modules(
											    vae=_A						,     text_encoder=_A						,     tokenizer=_A						,     unet=_A						,     scheduler=_A						,     safety_checker=_A						,     feature_extractor=_A						,     )
											self.register_to_config(requires_safety_checker=_A     )






						@property
						def 							__SCREAMING_SNAKE_CASE(  self     ):




											"""simple docstring"""



											return {k: getattr(self						,     _A     ) for k in self.config.keys() if not k.startswith("_"     )}






						def 							__SCREAMING_SNAKE_CASE(  self						,     _A = "auto"     ):




											"""simple docstring"""



											if slice_size == "auto":
																# half the attention head size is usually a good trade-off between
																# speed and memory
																__lowerCAmelCase							  =			self.unet.config.attention_head_dim // 2
											self.unet.set_attention_slice(_A     )






						def 							__SCREAMING_SNAKE_CASE(  self     ):




											"""simple docstring"""



											self.enable_attention_slicing(_A     )






						@torch.no_grad()
						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A						,     _A = 0.8						,     _A = 5_0						,     _A = 7.5						,     _A = None						,     _A = 1						,     _A = 0.0						,     _A = None						,     _A = "pil"						,     _A = True						,     _A = None						,     _A = 1						,     ):




											"""simple docstring"""



											return StableDiffusionInpaintPipelineLegacy(**self.components     )(
											    prompt=_A						,     image=_A						,     mask_image=_A						,     strength=_A						,     num_inference_steps=_A						,     guidance_scale=_A						,     negative_prompt=_A						,     num_images_per_prompt=_A						,     eta=_A						,     generator=_A						,     output_type=_A						,     return_dict=_A						,     callback=_A						,     )






						@torch.no_grad()
						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A						,     _A = 0.8						,     _A = 5_0						,     _A = 7.5						,     _A = None						,     _A = 1						,     _A = 0.0						,     _A = None						,     _A = "pil"						,     _A = True						,     _A = None						,     _A = 1						,     **_A						,     ):




											"""simple docstring"""



											return StableDiffusionImgaImgPipeline(**self.components     )(
											    prompt=_A						,     image=_A						,     strength=_A						,     num_inference_steps=_A						,     guidance_scale=_A						,     negative_prompt=_A						,     num_images_per_prompt=_A						,     eta=_A						,     generator=_A						,     output_type=_A						,     return_dict=_A						,     callback=_A						,     callback_steps=_A						,     )






						@torch.no_grad()
						def 							__SCREAMING_SNAKE_CASE(  self						,     _A						,     _A = 5_1_2						,     _A = 5_1_2						,     _A = 5_0						,     _A = 7.5						,     _A = None						,     _A = 1						,     _A = 0.0						,     _A = None						,     _A = None						,     _A = "pil"						,     _A = True						,     _A = None						,     _A = 1						,     ):




											"""simple docstring"""



											return StableDiffusionPipeline(**self.components     )(
											    prompt=_A						,     height=_A						,     width=_A						,     num_inference_steps=_A						,     guidance_scale=_A						,     negative_prompt=_A						,     num_images_per_prompt=_A						,     eta=_A						,     generator=_A						,     latents=_A						,     output_type=_A						,     return_dict=_A						,     callback=_A						,     callback_steps=_A						,     )




