




"""simple docstring"""





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      a__				(	__magic_name__   ):


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    AutoencoderKL      ,  UpperCamelCase_      :    CLIPTextModel      ,  UpperCamelCase_      :    CLIPTokenizer      ,  UpperCamelCase_      :    UNetaDConditionModel      ,  UpperCamelCase_      :    Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]      ,  UpperCamelCase_      :    StableDiffusionSafetyChecker      ,  UpperCamelCase_      :    CLIPImageProcessor      ,  UpperCamelCase_      :    bool = True      ,  ):


						"""simple docstring"""




						super()._init_()

						__UpperCAmelCase	: Dict		=					StableDiffusionPipeline.from_pretrained(UpperCamelCase_)
						__UpperCAmelCase	: str		=					StableDiffusionPipeline.from_pretrained(UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					StableDiffusionPipeline.from_pretrained(UpperCamelCase_)
						__UpperCAmelCase	: Optional[Any]		=					StableDiffusionPipeline(
						    vae=UpperCamelCase_      ,  text_encoder=UpperCamelCase_      ,  tokenizer=UpperCamelCase_      ,  unet=UpperCamelCase_      ,  scheduler=UpperCamelCase_      ,  safety_checker=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_      ,  requires_safety_checker=UpperCamelCase_      ,  )

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


			@property
			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




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


			def       a_     (      self      :    str      ,  UpperCamelCase_      :    Optional[Union[str, int]] = "auto"):


						"""simple docstring"""




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


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						self.enable_attention_slicing(UpperCamelCase_)


			@torch.no_grad()
			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    Union[str, List[str]]      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 50      ,  UpperCamelCase_      :    float = 7.5      ,  UpperCamelCase_      :    Optional[Union[str, List[str]]] = None      ,  UpperCamelCase_      :    Optional[int] = 1      ,  UpperCamelCase_      :    float = 0.0      ,  UpperCamelCase_      :    Optional[torch.Generator] = None      ,  UpperCamelCase_      :    Optional[torch.FloatTensor] = None      ,  UpperCamelCase_      :    Optional[str] = "pil"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,  UpperCamelCase_      :    int = 1      ,  **UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




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


			@torch.no_grad()
			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    Union[str, List[str]]      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 50      ,  UpperCamelCase_      :    float = 7.5      ,  UpperCamelCase_      :    Optional[Union[str, List[str]]] = None      ,  UpperCamelCase_      :    Optional[int] = 1      ,  UpperCamelCase_      :    float = 0.0      ,  UpperCamelCase_      :    Optional[torch.Generator] = None      ,  UpperCamelCase_      :    Optional[torch.FloatTensor] = None      ,  UpperCamelCase_      :    Optional[str] = "pil"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,  UpperCamelCase_      :    int = 1      ,  **UpperCamelCase_      :    int      ,  ):


						"""simple docstring"""




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


			@torch.no_grad()
			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    Union[str, List[str]]      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 50      ,  UpperCamelCase_      :    float = 7.5      ,  UpperCamelCase_      :    Optional[Union[str, List[str]]] = None      ,  UpperCamelCase_      :    Optional[int] = 1      ,  UpperCamelCase_      :    float = 0.0      ,  UpperCamelCase_      :    Optional[torch.Generator] = None      ,  UpperCamelCase_      :    Optional[torch.FloatTensor] = None      ,  UpperCamelCase_      :    Optional[str] = "pil"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,  UpperCamelCase_      :    int = 1      ,  **UpperCamelCase_      :    Union[str, Any]      ,  ):


						"""simple docstring"""




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


			@torch.no_grad()
			def       a_     (      self      :    Optional[Any]      ,  UpperCamelCase_      :    Union[str, List[str]]      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 50      ,  UpperCamelCase_      :    float = 7.5      ,  UpperCamelCase_      :    Optional[Union[str, List[str]]] = None      ,  UpperCamelCase_      :    Optional[int] = 1      ,  UpperCamelCase_      :    float = 0.0      ,  UpperCamelCase_      :    Optional[torch.Generator] = None      ,  UpperCamelCase_      :    Optional[torch.FloatTensor] = None      ,  UpperCamelCase_      :    Optional[str] = "pil"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,  UpperCamelCase_      :    int = 1      ,  **UpperCamelCase_      :    str      ,  ):


						"""simple docstring"""




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


			@torch.no_grad()
			def       a_     (      self      :    Dict      ,  UpperCamelCase_      :    Union[str, List[str]]      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 512      ,  UpperCamelCase_      :    int = 50      ,  UpperCamelCase_      :    float = 7.5      ,  UpperCamelCase_      :    Optional[Union[str, List[str]]] = None      ,  UpperCamelCase_      :    Optional[int] = 1      ,  UpperCamelCase_      :    float = 0.0      ,  UpperCamelCase_      :    Optional[torch.Generator] = None      ,  UpperCamelCase_      :    Optional[torch.FloatTensor] = None      ,  UpperCamelCase_      :    Optional[str] = "pil"      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,  UpperCamelCase_      :    int = 1      ,  **UpperCamelCase_      :    Any      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					"cuda" if torch.cuda.is_available() else "cpu"
						self.to(UpperCamelCase_)

						# 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
						__UpperCAmelCase	: Optional[Any]		=					self.textaimg_sda_a(
						    prompt=UpperCamelCase_      ,  height=UpperCamelCase_      ,  width=UpperCamelCase_      ,  num_inference_steps=UpperCamelCase_      ,  guidance_scale=UpperCamelCase_      ,  negative_prompt=UpperCamelCase_      ,  num_images_per_prompt=UpperCamelCase_      ,  eta=UpperCamelCase_      ,  generator=UpperCamelCase_      ,  latents=UpperCamelCase_      ,  output_type=UpperCamelCase_      ,  return_dict=UpperCamelCase_      ,  callback=UpperCamelCase_      ,  callback_steps=UpperCamelCase_      ,  **UpperCamelCase_      ,  )

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

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

						# Get first result from Stable Diffusion Checkpoint v1.4
						__UpperCAmelCase	: Optional[Any]		=					self.textaimg_sda_a(
						    prompt=UpperCamelCase_      ,  height=UpperCamelCase_      ,  width=UpperCamelCase_      ,  num_inference_steps=UpperCamelCase_      ,  guidance_scale=UpperCamelCase_      ,  negative_prompt=UpperCamelCase_      ,  num_images_per_prompt=UpperCamelCase_      ,  eta=UpperCamelCase_      ,  generator=UpperCamelCase_      ,  latents=UpperCamelCase_      ,  output_type=UpperCamelCase_      ,  return_dict=UpperCamelCase_      ,  callback=UpperCamelCase_      ,  callback_steps=UpperCamelCase_      ,  **UpperCamelCase_      ,  )

						# 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]])








"""simple docstring"""





import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format



class      a__				:
			lowercase_    =	None


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.feature_extraction_class(**self.feat_extract_dict)
						__UpperCAmelCase	: Any		=					json.loads(feat_extract.to_json_string())
						for key, value in self.feat_extract_dict.items():
									self.assertEqual(obj[key]      ,  UpperCamelCase_)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.feature_extraction_class(**self.feat_extract_dict)

						with tempfile.TemporaryDirectory() as tmpdirname:
									__UpperCAmelCase	: Dict		=					os.path.join(UpperCamelCase_      ,  "feat_extract.json")
									feat_extract_first.to_json_file(UpperCamelCase_)
									__UpperCAmelCase	: Optional[Any]		=					self.feature_extraction_class.from_json_file(UpperCamelCase_)

						self.assertEqual(feat_extract_second.to_dict()      ,  feat_extract_first.to_dict())


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.feature_extraction_class(**self.feat_extract_dict)

						with tempfile.TemporaryDirectory() as tmpdirname:
									__UpperCAmelCase	: Tuple		=					feat_extract_first.save_pretrained(UpperCamelCase_)[0]
									check_json_file_has_correct_format(UpperCamelCase_)
									__UpperCAmelCase	: List[Any]		=					self.feature_extraction_class.from_pretrained(UpperCamelCase_)

						self.assertEqual(feat_extract_second.to_dict()      ,  feat_extract_first.to_dict())


			def       a_     (      self      :    Union[str, Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					self.feature_extraction_class()
						self.assertIsNotNone(UpperCamelCase_)


