





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        __SCREAMING_SNAKE_CASE					(     __a		):






							def __init__(						self  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase  , __lowerCAmelCase = True  , ):
														super()._init_()

														UpperCamelCase__				     =			StableDiffusionPipeline.from_pretrained(a_  )
														UpperCamelCase__				     =			StableDiffusionPipeline.from_pretrained(a_  )
														UpperCamelCase__				     =			StableDiffusionPipeline.from_pretrained(a_  )
														UpperCamelCase__				     =			StableDiffusionPipeline(
														    vae=a_  , text_encoder=a_  , tokenizer=a_  , unet=a_  , scheduler=a_  , safety_checker=a_  , feature_extractor=a_  , requires_safety_checker=a_  , )

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






							@property
							def 				_lowerCamelCase		(						self  ):
														return {k: getattr(self  , a_  ) for k in self.config.keys() if not k.startswith("""_"""  )}






							def 				_lowerCamelCase		(						self  , __lowerCAmelCase = "auto"  ):



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






							def 				_lowerCamelCase		(						self  ):
														self.enable_attention_slicing(a_  )






							@torch.no_grad()
							def 				_lowerCamelCase		(						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  , ):
														return self.pipea(
														    prompt=a_  , height=a_  , width=a_  , num_inference_steps=a_  , guidance_scale=a_  , negative_prompt=a_  , num_images_per_prompt=a_  , eta=a_  , generator=a_  , latents=a_  , output_type=a_  , return_dict=a_  , callback=a_  , callback_steps=a_  , **a_  , )






							@torch.no_grad()
							def 				_lowerCamelCase		(						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  , ):
														return self.pipea(
														    prompt=a_  , height=a_  , width=a_  , num_inference_steps=a_  , guidance_scale=a_  , negative_prompt=a_  , num_images_per_prompt=a_  , eta=a_  , generator=a_  , latents=a_  , output_type=a_  , return_dict=a_  , callback=a_  , callback_steps=a_  , **a_  , )






							@torch.no_grad()
							def 				_lowerCamelCase		(						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  , ):
														return self.pipea(
														    prompt=a_  , height=a_  , width=a_  , num_inference_steps=a_  , guidance_scale=a_  , negative_prompt=a_  , num_images_per_prompt=a_  , eta=a_  , generator=a_  , latents=a_  , output_type=a_  , return_dict=a_  , callback=a_  , callback_steps=a_  , **a_  , )






							@torch.no_grad()
							def 				_lowerCamelCase		(						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  , ):
														return self.pipea(
														    prompt=a_  , height=a_  , width=a_  , num_inference_steps=a_  , guidance_scale=a_  , negative_prompt=a_  , num_images_per_prompt=a_  , eta=a_  , generator=a_  , latents=a_  , output_type=a_  , return_dict=a_  , callback=a_  , callback_steps=a_  , **a_  , )







							@torch.no_grad()
							def 				_lowerCamelCase		(						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  , ):
														UpperCamelCase__				     =			"""cuda""" if torch.cuda.is_available() else """cpu"""
														self.to(a_  )

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

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

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

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

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











import argparse

from transformers import CLIPImageProcessor, CLIPVisionModelWithProjection

from diffusers import UnCLIPImageVariationPipeline, UnCLIPPipeline


if __name__ == "__main__":
					UpperCamelCase__											= argparse.ArgumentParser()

					parser.add_argument("--dump_path", default=None, type=str, required=True, help="Path to the output model.")

					parser.add_argument(
					    "--txt2img_unclip",
					    default="kakaobrain/karlo-v1-alpha",
					    type=str,
					    required=False,
					    help="The pretrained txt2img unclip.",
					)

					UpperCamelCase__											= parser.parse_args()

					UpperCamelCase__											= UnCLIPPipeline.from_pretrained(args.txtaimg_unclip)

					UpperCamelCase__											= CLIPImageProcessor()
					UpperCamelCase__											= CLIPVisionModelWithProjection.from_pretrained("openai/clip-vit-large-patch14")

					UpperCamelCase__											= UnCLIPImageVariationPipeline(
					    decoder=txtaimg.decoder,
					    text_encoder=txtaimg.text_encoder,
					    tokenizer=txtaimg.tokenizer,
					    text_proj=txtaimg.text_proj,
					    feature_extractor=feature_extractor,
					    image_encoder=image_encoder,
					    super_res_first=txtaimg.super_res_first,
					    super_res_last=txtaimg.super_res_last,
					    decoder_scheduler=txtaimg.decoder_scheduler,
					    super_res_scheduler=txtaimg.super_res_scheduler,
					)

					imgaimg.save_pretrained(args.dump_path)




