




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

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


__A        =    "CompVis/stable-diffusion-v1-1"
__A        =    "CompVis/stable-diffusion-v1-2"
__A        =    "CompVis/stable-diffusion-v1-3"
__A        =    "CompVis/stable-diffusion-v1-4"







class     __lowerCAmelCase (       __magic_name__ ):

			"""simple docstring"""







			def __init__(      self			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__			,						lowerCamelCase__ = True			,						)    ->      Any:



							'''simple docstring'''
							super()._init_()

							__lowerCamelCase		       =    StableDiffusionPipeline.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    StableDiffusionPipeline.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    StableDiffusionPipeline.from_pretrained(lowerCamelCase__							)
							__lowerCamelCase		       =    StableDiffusionPipeline(
							    vae=lowerCamelCase__			,						text_encoder=lowerCamelCase__			,						tokenizer=lowerCamelCase__			,						unet=lowerCamelCase__			,						scheduler=lowerCamelCase__			,						safety_checker=lowerCamelCase__			,						feature_extractor=lowerCamelCase__			,						requires_safety_checker=lowerCamelCase__			,						)

							self.register_modules(pipelinea=self.pipea			,						pipelinea=self.pipea			,						pipelinea=self.pipea			,						pipelinea=self.pipea							)


			@property
			def       lowercase_    (      self							)    ->      Dict[str, Any]:



							'''simple docstring'''
							return {k: getattr(self			,						lowerCamelCase__							) for k in self.config.keys() if not k.startswith('_'							)}


			def       lowercase_    (      self			,						lowerCamelCase__ = "auto"							)    ->      Any:



							'''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(lowerCamelCase__							)


			def       lowercase_    (      self							)    ->      Optional[int]:



							'''simple docstring'''
							self.enable_attention_slicing(lowerCamelCase__							)


			@torch.no_grad()
			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 50			,						lowerCamelCase__ = 7.5			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						lowerCamelCase__ = 0.0			,						lowerCamelCase__ = None			,						lowerCamelCase__ = None			,						lowerCamelCase__ = "pil"			,						lowerCamelCase__ = True			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						**lowerCamelCase__			,						)    ->      List[str]:



							'''simple docstring'''
							return self.pipea(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)


			@torch.no_grad()
			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 50			,						lowerCamelCase__ = 7.5			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						lowerCamelCase__ = 0.0			,						lowerCamelCase__ = None			,						lowerCamelCase__ = None			,						lowerCamelCase__ = "pil"			,						lowerCamelCase__ = True			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						**lowerCamelCase__			,						)    ->      Dict:



							'''simple docstring'''
							return self.pipea(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)


			@torch.no_grad()
			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 50			,						lowerCamelCase__ = 7.5			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						lowerCamelCase__ = 0.0			,						lowerCamelCase__ = None			,						lowerCamelCase__ = None			,						lowerCamelCase__ = "pil"			,						lowerCamelCase__ = True			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						**lowerCamelCase__			,						)    ->      str:



							'''simple docstring'''
							return self.pipea(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)


			@torch.no_grad()
			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 50			,						lowerCamelCase__ = 7.5			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						lowerCamelCase__ = 0.0			,						lowerCamelCase__ = None			,						lowerCamelCase__ = None			,						lowerCamelCase__ = "pil"			,						lowerCamelCase__ = True			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						**lowerCamelCase__			,						)    ->      Any:



							'''simple docstring'''
							return self.pipea(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)






			@torch.no_grad()
			def       lowercase_    (      self			,						lowerCamelCase__			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 512			,						lowerCamelCase__ = 50			,						lowerCamelCase__ = 7.5			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						lowerCamelCase__ = 0.0			,						lowerCamelCase__ = None			,						lowerCamelCase__ = None			,						lowerCamelCase__ = "pil"			,						lowerCamelCase__ = True			,						lowerCamelCase__ = None			,						lowerCamelCase__ = 1			,						**lowerCamelCase__			,						)    ->      List[str]:



							'''simple docstring'''
							__lowerCamelCase		       =    'cuda' if torch.cuda.is_available() else 'cpu'
							self.to(lowerCamelCase__							)

							# Checks if the height and width are divisible by 8 or not
							if height % 8 != 0 or width % 8 != 0:
											raise ValueError(f"""`height` and `width` must be divisible by 8 but are {height} and {width}."""							)

							# Get first result from Stable Diffusion Checkpoint v1.1
							__lowerCamelCase		       =    self.textaimg_sda_a(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)

							# Get first result from Stable Diffusion Checkpoint v1.2
							__lowerCamelCase		       =    self.textaimg_sda_a(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)

							# Get first result from Stable Diffusion Checkpoint v1.3
							__lowerCamelCase		       =    self.textaimg_sda_a(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)

							# Get first result from Stable Diffusion Checkpoint v1.4
							__lowerCamelCase		       =    self.textaimg_sda_a(
							    prompt=lowerCamelCase__			,						height=lowerCamelCase__			,						width=lowerCamelCase__			,						num_inference_steps=lowerCamelCase__			,						guidance_scale=lowerCamelCase__			,						negative_prompt=lowerCamelCase__			,						num_images_per_prompt=lowerCamelCase__			,						eta=lowerCamelCase__			,						generator=lowerCamelCase__			,						latents=lowerCamelCase__			,						output_type=lowerCamelCase__			,						return_dict=lowerCamelCase__			,						callback=lowerCamelCase__			,						callback_steps=lowerCamelCase__			,						**lowerCamelCase__			,						)

							# Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
							return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]							)








class     __lowerCAmelCase :

			"""simple docstring"""







			def __init__(      self			,						lowerCamelCase__							)    ->      Any:



							'''simple docstring'''
							__lowerCamelCase		       =    n
							__lowerCamelCase		       =    [None] * self.n
							__lowerCamelCase		       =    0  # index of the first element
							__lowerCamelCase		       =    0
							__lowerCamelCase		       =    0


			def __len__(      self							)    ->      int:



							'''simple docstring'''
							return self.size


			def       lowercase_    (      self							)    ->      bool:



							'''simple docstring'''
							return self.size == 0


			def       lowercase_    (      self							)    ->      str:



							'''simple docstring'''
							return False if self.is_empty() else self.array[self.front]


			def       lowercase_    (      self			,						lowerCamelCase__							)    ->      Optional[Any]:



							'''simple docstring'''
							if self.size >= self.n:
											raise Exception('QUEUE IS FULL'							)

							__lowerCamelCase		       =    data
							__lowerCamelCase		       =    (self.rear + 1) % self.n
							self.size += 1
							return self






			def       lowercase_    (      self							)    ->      Tuple:



							'''simple docstring'''
							if self.size == 0:
											raise Exception('UNDERFLOW'							)

							__lowerCamelCase		       =    self.array[self.front]
							__lowerCamelCase		       =    None
							__lowerCamelCase		       =    (self.front + 1) % self.n
							self.size -= 1
							return temp


