
'''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__   :		Any   ='CompVis/stable-diffusion-v1-1'
A__   :		Tuple   ='CompVis/stable-diffusion-v1-2'
A__   :		Any   ='CompVis/stable-diffusion-v1-3'
A__   :		str   ='CompVis/stable-diffusion-v1-4'







class  __A				( lowerCAmelCase__			):
 def __init__(   self			:    List[str]				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    str				,   lowerCamelCase			:    List[str]				,   lowerCamelCase			:    List[Any] = True				,   ):



   """simple docstring"""





   super()._init_()

   __A		:     Any												=		StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE       )
   __A		:     List[str]												=		StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE       )
   __A		:     str												=		StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE       )
   __A		:     Optional[Any]												=		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 		lowercase_(   self			:    Optional[Any]       ):



   """simple docstring"""





   return {k: getattr(self				,   _SCREAMING_SNAKE_CASE       ) for k in self.config.keys() if not k.startswith("""_"""       )}
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Optional[int] = "auto"       ):



   """simple docstring"""





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



   """simple docstring"""





   self.enable_attention_slicing(_SCREAMING_SNAKE_CASE       )
 @torch.no_grad()
 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    List[str] = 5_12				,   lowerCamelCase			:    Union[str, Any] = 5_12				,   lowerCamelCase			:    Tuple = 50				,   lowerCamelCase			:    List[Any] = 7.5				,   lowerCamelCase			:    List[Any] = None				,   lowerCamelCase			:    Dict = 1				,   lowerCamelCase			:    Optional[int] = 0.0				,   lowerCamelCase			:    Tuple = None				,   lowerCamelCase			:    Optional[int] = None				,   lowerCamelCase			:    int = "pil"				,   lowerCamelCase			:    Tuple = True				,   lowerCamelCase			:    str = None				,   lowerCamelCase			:    int = 1				,   **lowerCamelCase			:    str				,   ):



   """simple docstring"""





   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 		lowercase_(   self			:    Optional[int]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Union[str, Any] = 5_12				,   lowerCamelCase			:    List[Any] = 5_12				,   lowerCamelCase			:    str = 50				,   lowerCamelCase			:    List[str] = 7.5				,   lowerCamelCase			:    Any = None				,   lowerCamelCase			:    Tuple = 1				,   lowerCamelCase			:    Dict = 0.0				,   lowerCamelCase			:    str = None				,   lowerCamelCase			:    Union[str, Any] = None				,   lowerCamelCase			:    int = "pil"				,   lowerCamelCase			:    Tuple = True				,   lowerCamelCase			:    int = None				,   lowerCamelCase			:    Optional[Any] = 1				,   **lowerCamelCase			:    Any				,   ):



   """simple docstring"""





   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 		lowercase_(   self			:    List[Any]				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Tuple = 5_12				,   lowerCamelCase			:    Tuple = 5_12				,   lowerCamelCase			:    Union[str, Any] = 50				,   lowerCamelCase			:    str = 7.5				,   lowerCamelCase			:    Dict = None				,   lowerCamelCase			:    Union[str, Any] = 1				,   lowerCamelCase			:    List[str] = 0.0				,   lowerCamelCase			:    Optional[Any] = None				,   lowerCamelCase			:    Tuple = None				,   lowerCamelCase			:    List[str] = "pil"				,   lowerCamelCase			:    List[str] = True				,   lowerCamelCase			:    str = None				,   lowerCamelCase			:    Union[str, Any] = 1				,   **lowerCamelCase			:    List[Any]				,   ):



   """simple docstring"""





   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 		lowercase_(   self			:    List[Any]				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    List[str] = 5_12				,   lowerCamelCase			:    Union[str, Any] = 50				,   lowerCamelCase			:    Dict = 7.5				,   lowerCamelCase			:    Union[str, Any] = None				,   lowerCamelCase			:    Optional[int] = 1				,   lowerCamelCase			:    int = 0.0				,   lowerCamelCase			:    Any = None				,   lowerCamelCase			:    List[str] = None				,   lowerCamelCase			:    List[Any] = "pil"				,   lowerCamelCase			:    Dict = True				,   lowerCamelCase			:    List[str] = None				,   lowerCamelCase			:    Optional[int] = 1				,   **lowerCamelCase			:    Any				,   ):



   """simple docstring"""





   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 		lowercase_(   self			:    str				,   lowerCamelCase			:    Union[str, Any]				,   lowerCamelCase			:    Optional[int] = 5_12				,   lowerCamelCase			:    List[Any] = 5_12				,   lowerCamelCase			:    List[Any] = 50				,   lowerCamelCase			:    Optional[int] = 7.5				,   lowerCamelCase			:    List[Any] = None				,   lowerCamelCase			:    Dict = 1				,   lowerCamelCase			:    int = 0.0				,   lowerCamelCase			:    List[str] = None				,   lowerCamelCase			:    str = None				,   lowerCamelCase			:    Tuple = "pil"				,   lowerCamelCase			:    str = True				,   lowerCamelCase			:    Dict = None				,   lowerCamelCase			:    str = 1				,   **lowerCamelCase			:    Any				,   ):



   """simple docstring"""





   __A		:     Dict												=		"""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
   __A		:     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 first result from Stable Diffusion Checkpoint v1.2
   __A		:     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 first result from Stable Diffusion Checkpoint v1.3
   __A		:     List[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
   __A		:     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]]       )





'''simple docstring'''
from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


A__   :		Dict 		=logging.get_logger(__name__)







class  __A				( _SCREAMING_SNAKE_CASE			):
 lowerCamelCase				       =['''input_values''', '''padding_mask''']
 def __init__(   self			:    Optional[int]				,   lowerCamelCase			:    int = 1				,   lowerCamelCase			:    int = 2_40_00				,   lowerCamelCase			:    float = 0.0				,   lowerCamelCase			:    float = None				,   lowerCamelCase			:    float = None				,   **lowerCamelCase			:    Dict				,   ):



   """simple docstring"""





   super().__init__(feature_size=lowerCamelCase				,   sampling_rate=lowerCamelCase				,   padding_value=lowerCamelCase				,   **lowerCamelCase       )
   __A		:     str												=		chunk_length_s
   __A		:     str												=		overlap
 @property
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   if self.chunk_length_s is None:
     return None
   else:
     return int(self.chunk_length_s * self.sampling_rate       )
 @property
 def 		lowercase_(   self			:    List[str]       ):



   """simple docstring"""





   if self.chunk_length_s is None or self.overlap is None:
     return None
   else:
     return max(1				,   int((1.0 - self.overlap) * self.chunk_length       )       )






 def __call__(   self			:    List[str]				,   lowerCamelCase			:    Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]				,   lowerCamelCase			:    Optional[Union[bool, str, PaddingStrategy]] = None				,   lowerCamelCase			:    Optional[bool] = False				,   lowerCamelCase			:    Optional[int] = None				,   lowerCamelCase			:    Optional[Union[str, TensorType]] = None				,   lowerCamelCase			:    Optional[int] = None				,   ):



   """simple docstring"""





   if sampling_rate is not None:
     if sampling_rate != self.sampling_rate:
       raise ValueError(
           f"The model corresponding to this feature extractor: {self} was trained using a sampling rate of"
           f" {self.sampling_rate}. Please make sure that the provided audio input was sampled with"
           f" {self.sampling_rate} and not {sampling_rate}."       )
   else:
     logger.warning(
         """It is strongly recommended to pass the `sampling_rate` argument to this function. """
         """Failing to do so can result in silent errors that might be hard to debug."""       )

   if padding and truncation:
     raise ValueError("""Both padding and truncation were set. Make sure you only set one."""       )
   elif padding is None:
     # by default let's pad the inputs
     __A		:     List[Any]												=		True

   __A		:     List[str]												=		bool(
       isinstance(lowerCamelCase				,   (list, tuple)       ) and (isinstance(raw_audio[0]				,   (np.ndarray, tuple, list)       ))       )

   if is_batched:
     __A		:     Optional[Any]												=		[np.asarray(lowerCamelCase				,   dtype=np.floataa       ).T for audio in raw_audio]
   elif not is_batched and not isinstance(lowerCamelCase				,   np.ndarray       ):
     __A		:     Union[str, Any]												=		np.asarray(lowerCamelCase				,   dtype=np.floataa       )
   elif isinstance(lowerCamelCase				,   np.ndarray       ) and raw_audio.dtype is np.dtype(np.floataa       ):
     __A		:     Optional[Any]												=		raw_audio.astype(np.floataa       )

   # always return batch
   if not is_batched:
     __A		:     Tuple												=		[np.asarray(lowerCamelCase       ).T]

   # verify inputs are valid
   for idx, example in enumerate(lowerCamelCase       ):
     if example.ndim > 2:
       raise ValueError(f"Expected input shape (channels, length) but got shape {example.shape}"       )
     if self.feature_size == 1 and example.ndim != 1:
       raise ValueError(f"Expected mono audio but example has {example.shape[-1]} channels"       )
     if self.feature_size == 2 and example.shape[-1] != 2:
       raise ValueError(f"Expected stereo audio but example has {example.shape[-1]} channels"       )

   __A		:     Optional[Any]												=		None
   __A		:     Dict												=		BatchFeature({"""input_values""": raw_audio}       )
   if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
     if truncation:
       __A		:     Optional[Any]												=		min(array.shape[0] for array in raw_audio       )
       __A		:     Dict												=		int(np.floor(max_length / self.chunk_stride       )       )
       __A		:     Union[str, Any]												=		(nb_step - 1) * self.chunk_stride + self.chunk_length
     elif padding:
       __A		:     int												=		max(array.shape[0] for array in raw_audio       )
       __A		:     Optional[Any]												=		int(np.ceil(max_length / self.chunk_stride       )       )
       __A		:     Any												=		(nb_step - 1) * self.chunk_stride + self.chunk_length
       __A		:     Optional[int]												=		"""max_length"""
     else:
       __A		:     Optional[Any]												=		input_values

        # normal padding on batch
   if padded_inputs is None:
     __A		:     Tuple												=		self.pad(
         lowerCamelCase				,   max_length=lowerCamelCase				,   truncation=lowerCamelCase				,   padding=lowerCamelCase				,   return_attention_mask=lowerCamelCase				,   )
     if padding:
       __A		:     List[Any]												=		padded_inputs.pop("""attention_mask"""       )

   __A		:     List[Any]												=		[]
   for example in padded_inputs.pop("""input_values"""       ):
     if self.feature_size == 1:
       __A		:     Optional[Any]												=		example[..., None]
     input_values.append(example.T       )

   __A		:     str												=		input_values
   if return_tensors is not None:
     __A		:     Any												=		padded_inputs.convert_to_tensors(lowerCamelCase       )

   return padded_inputs



