


import importlib.metadata
import subprocess
import sys

def       _a	(					SCREAMING_SNAKE_CASE						:					bool = False  ):







 """simple docstring"""



 UpperCamelCase__       :     int 							= False
 if "IPython" in sys.modules:
  UpperCamelCase__       :     Optional[int] 							= '''google.colab''' in str(sys.modules['''IPython'''].get_ipython()  )

 if in_colab:
  if upgrade:
   UpperCamelCase__       :     Dict 							= ['''pip''', '''install''', '''-U''', '''torch''']
   subprocess.run(SCREAMING_SNAKE_CASE     ,			check=SCREAMING_SNAKE_CASE  )
  # get the current version of torch
  UpperCamelCase__       :     Dict 							= importlib.metadata.version('''torch'''  )
  UpperCamelCase__       :     List[Any] 							= torch_version[: torch_version.rindex('''.'''  )]
  UpperCamelCase__       :     Optional[int] 							= F"https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"
  UpperCamelCase__       :     int 							= ['''pip''', '''install''', xla_wheel]
  subprocess.run(SCREAMING_SNAKE_CASE     ,			check=SCREAMING_SNAKE_CASE  )
 else:
  raise RuntimeError('''`install_xla` utility works only on google colab.'''  )






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							:							Any    							=					logging.get_logger(__name__)  # pylint: disable=invalid-name

class    __magic_name__						(  __lowerCAmelCase):
     A:		Dict								=   ["safety_checker", "feature_extractor"]


     def __init__(   self				:  List[str]				,						lowerCamelCase__				:  AutoencoderKL				,						lowerCamelCase__				:  CLIPTextModel				,						lowerCamelCase__				:  CLIPTokenizer				,						lowerCamelCase__				:  UNetaDConditionModel				,						lowerCamelCase__				:  Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]				,						lowerCamelCase__				:  StableDiffusionSafetyChecker				,						lowerCamelCase__				:  CLIPImageProcessor				,						lowerCamelCase__				:  bool = True				,						)	->   Optional[Any]:





      '''simple docstring'''






      super().__init__()
      if hasattr(scheduler.config				,						'''steps_offset'''      ) and scheduler.config.steps_offset != 1:
       UpperCamelCase__       :     int 							= (
           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'''				,						lowerCamelCase__				,						standard_warn=lowerCamelCase__      )
       UpperCamelCase__       :     int 							= dict(scheduler.config      )
       UpperCamelCase__       :     List[str] 							= 1
       UpperCamelCase__       :     List[str] 							= FrozenDict(lowerCamelCase__      )

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


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





      '''simple docstring'''






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


     def   UpperCAmelCase__						(   self				:  str				,						lowerCamelCase__				:  Optional[Union[str, int]] = "auto"      )	->   str:





      '''simple docstring'''






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


     def   UpperCAmelCase__						(   self				:  int      )	->   Tuple:





      '''simple docstring'''






      self.enable_attention_slicing(lowerCamelCase__      )


     @torch.no_grad()
     def   UpperCAmelCase__						(   self				:  Optional[Any]				,						lowerCamelCase__				:  Union[str, List[str]]				,						lowerCamelCase__				:  Union[torch.FloatTensor, PIL.Image.Image]				,						lowerCamelCase__				:  Union[torch.FloatTensor, PIL.Image.Image]				,						lowerCamelCase__				:  float = 0.8				,						lowerCamelCase__				:  Optional[int] = 50				,						lowerCamelCase__				:  Optional[float] = 7.5				,						lowerCamelCase__				:  Optional[Union[str, List[str]]] = None				,						lowerCamelCase__				:  Optional[int] = 1				,						lowerCamelCase__				:  Optional[float] = 0.0				,						lowerCamelCase__				:  Optional[torch.Generator] = None				,						lowerCamelCase__				:  Optional[str] = "pil"				,						lowerCamelCase__				:  bool = True				,						lowerCamelCase__				:  Optional[Callable[[int, int, torch.FloatTensor], None]] = None				,						lowerCamelCase__				:  int = 1				,						)	->   Optional[Any]:





      '''simple docstring'''






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


     @torch.no_grad()
     def   UpperCAmelCase__						(   self				:  Any				,						lowerCamelCase__				:  Union[str, List[str]]				,						lowerCamelCase__				:  Union[torch.FloatTensor, PIL.Image.Image]				,						lowerCamelCase__				:  float = 0.8				,						lowerCamelCase__				:  Optional[int] = 50				,						lowerCamelCase__				:  Optional[float] = 7.5				,						lowerCamelCase__				:  Optional[Union[str, List[str]]] = None				,						lowerCamelCase__				:  Optional[int] = 1				,						lowerCamelCase__				:  Optional[float] = 0.0				,						lowerCamelCase__				:  Optional[torch.Generator] = None				,						lowerCamelCase__				:  Optional[str] = "pil"				,						lowerCamelCase__				:  bool = True				,						lowerCamelCase__				:  Optional[Callable[[int, int, torch.FloatTensor], None]] = None				,						lowerCamelCase__				:  int = 1				,						**lowerCamelCase__				:  str				,						)	->   str:





      '''simple docstring'''






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


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





      '''simple docstring'''






      return StableDiffusionPipeline(**self.components      )(
          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__				,						)


