



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


__UpperCAmelCase	       =	'''CompVis/stable-diffusion-v1-1'''
__UpperCAmelCase	       =	'''CompVis/stable-diffusion-v1-2'''
__UpperCAmelCase	       =	'''CompVis/stable-diffusion-v1-3'''
__UpperCAmelCase	       =	'''CompVis/stable-diffusion-v1-4'''



class     lowerCAmelCase_		(  a__							):
       def __init__(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = True,      )					->		Dict:
          super()._init_()

          UpperCamelCase   :  List[Any]						     =     StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  List[str]						     =     StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  str						     =     StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  Tuple						     =     StableDiffusionPipeline(
              vae=SCREAMING_SNAKE_CASE_,      text_encoder=SCREAMING_SNAKE_CASE_,      tokenizer=SCREAMING_SNAKE_CASE_,      unet=SCREAMING_SNAKE_CASE_,      scheduler=SCREAMING_SNAKE_CASE_,      safety_checker=SCREAMING_SNAKE_CASE_,      feature_extractor=SCREAMING_SNAKE_CASE_,      requires_safety_checker=SCREAMING_SNAKE_CASE_,      )

          self.register_modules(pipelinea=self.pipea,      pipelinea=self.pipea,      pipelinea=self.pipea,      pipelinea=self.pipea	)
       @property
       def  snake_case_	(      self	)					->		Dict[str, Any]:
          return {k: getattr(self,      SCREAMING_SNAKE_CASE_	) for k in self.config.keys() if not k.startswith('_'	)}
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_ = "auto"	)					->		Optional[int]:
          if slice_size == "auto":
             # half the attention head size is usually a good trade-off between
             # speed and memory
             UpperCamelCase   :  List[str]						     =     self.unet.config.attention_head_dim // 2
          self.unet.set_attention_slice(SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		Tuple:
          self.enable_attention_slicing(SCREAMING_SNAKE_CASE_	)
       @torch.no_grad()
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 50,      SCREAMING_SNAKE_CASE_ = 7.5,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 0.0,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      **SCREAMING_SNAKE_CASE_,      )					->		Optional[Any]:
          return self.pipea(
              prompt=SCREAMING_SNAKE_CASE_,      height=SCREAMING_SNAKE_CASE_,      width=SCREAMING_SNAKE_CASE_,      num_inference_steps=SCREAMING_SNAKE_CASE_,      guidance_scale=SCREAMING_SNAKE_CASE_,      negative_prompt=SCREAMING_SNAKE_CASE_,      num_images_per_prompt=SCREAMING_SNAKE_CASE_,      eta=SCREAMING_SNAKE_CASE_,      generator=SCREAMING_SNAKE_CASE_,      latents=SCREAMING_SNAKE_CASE_,      output_type=SCREAMING_SNAKE_CASE_,      return_dict=SCREAMING_SNAKE_CASE_,      callback=SCREAMING_SNAKE_CASE_,      callback_steps=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )
       @torch.no_grad()
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 50,      SCREAMING_SNAKE_CASE_ = 7.5,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 0.0,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      **SCREAMING_SNAKE_CASE_,      )					->		Optional[int]:
          return self.pipea(
              prompt=SCREAMING_SNAKE_CASE_,      height=SCREAMING_SNAKE_CASE_,      width=SCREAMING_SNAKE_CASE_,      num_inference_steps=SCREAMING_SNAKE_CASE_,      guidance_scale=SCREAMING_SNAKE_CASE_,      negative_prompt=SCREAMING_SNAKE_CASE_,      num_images_per_prompt=SCREAMING_SNAKE_CASE_,      eta=SCREAMING_SNAKE_CASE_,      generator=SCREAMING_SNAKE_CASE_,      latents=SCREAMING_SNAKE_CASE_,      output_type=SCREAMING_SNAKE_CASE_,      return_dict=SCREAMING_SNAKE_CASE_,      callback=SCREAMING_SNAKE_CASE_,      callback_steps=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )
       @torch.no_grad()
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 50,      SCREAMING_SNAKE_CASE_ = 7.5,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 0.0,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      **SCREAMING_SNAKE_CASE_,      )					->		Optional[Any]:
          return self.pipea(
              prompt=SCREAMING_SNAKE_CASE_,      height=SCREAMING_SNAKE_CASE_,      width=SCREAMING_SNAKE_CASE_,      num_inference_steps=SCREAMING_SNAKE_CASE_,      guidance_scale=SCREAMING_SNAKE_CASE_,      negative_prompt=SCREAMING_SNAKE_CASE_,      num_images_per_prompt=SCREAMING_SNAKE_CASE_,      eta=SCREAMING_SNAKE_CASE_,      generator=SCREAMING_SNAKE_CASE_,      latents=SCREAMING_SNAKE_CASE_,      output_type=SCREAMING_SNAKE_CASE_,      return_dict=SCREAMING_SNAKE_CASE_,      callback=SCREAMING_SNAKE_CASE_,      callback_steps=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )
       @torch.no_grad()
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 50,      SCREAMING_SNAKE_CASE_ = 7.5,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 0.0,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      **SCREAMING_SNAKE_CASE_,      )					->		Optional[int]:
          return self.pipea(
              prompt=SCREAMING_SNAKE_CASE_,      height=SCREAMING_SNAKE_CASE_,      width=SCREAMING_SNAKE_CASE_,      num_inference_steps=SCREAMING_SNAKE_CASE_,      guidance_scale=SCREAMING_SNAKE_CASE_,      negative_prompt=SCREAMING_SNAKE_CASE_,      num_images_per_prompt=SCREAMING_SNAKE_CASE_,      eta=SCREAMING_SNAKE_CASE_,      generator=SCREAMING_SNAKE_CASE_,      latents=SCREAMING_SNAKE_CASE_,      output_type=SCREAMING_SNAKE_CASE_,      return_dict=SCREAMING_SNAKE_CASE_,      callback=SCREAMING_SNAKE_CASE_,      callback_steps=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )






       @torch.no_grad()
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 512,      SCREAMING_SNAKE_CASE_ = 50,      SCREAMING_SNAKE_CASE_ = 7.5,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 0.0,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 1,      **SCREAMING_SNAKE_CASE_,      )					->		int:
          UpperCamelCase   :  Optional[Any]						     =     'cuda' if torch.cuda.is_available() else 'cpu'
          self.to(SCREAMING_SNAKE_CASE_	)

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

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

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

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

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








from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader



class     lowerCAmelCase_		(  a__							):
       def __init__(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = False,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = True,      SCREAMING_SNAKE_CASE_ = "arrow",      **SCREAMING_SNAKE_CASE_,      )					->		int:
          super().__init__(
              split=SCREAMING_SNAKE_CASE_,      features=SCREAMING_SNAKE_CASE_,      cache_dir=SCREAMING_SNAKE_CASE_,      keep_in_memory=SCREAMING_SNAKE_CASE_,      streaming=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )
          UpperCamelCase   :  Any						     =     load_from_cache_file
          UpperCamelCase   :  Any						     =     file_format
          UpperCamelCase   :  str						     =     Spark(
              df=SCREAMING_SNAKE_CASE_,      features=SCREAMING_SNAKE_CASE_,      cache_dir=SCREAMING_SNAKE_CASE_,      working_dir=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )






       def  snake_case_	(      self	)					->		Tuple:
          if self.streaming:
             return self.builder.as_streaming_dataset(split=self.split	)
          UpperCamelCase   :  Dict						     =     None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
          self.builder.download_and_prepare(
              download_mode=SCREAMING_SNAKE_CASE_,      file_format=self._file_format,      )
          return self.builder.as_dataset(split=self.split	)



