#!/usr/bin/env python3 from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, AutoencoderKL import time from pytorch_lightning import seed_everything import os from huggingface_hub import HfApi # from compel import Compel import torch import sys from pathlib import Path import requests from PIL import Image from io import BytesIO api = HfApi() start_time = time.time() # use_refiner = bool(int(sys.argv[1])) use_refiner = True use_diffusers = True path = "/home/patrick/sai/stable-diffusion-xl-base-1.0" refiner_path = "/home/patrick/sai/stable-diffusion-xl-refiner-1.0" vae_path = "/home/patrick/sai/stable-diffusion-xl-base-1.0/vae/" vae_path = "/home/patrick/sai/sdxl-vae" vae = AutoencoderKL.from_pretrained(vae_path, torch_dtype=torch.float16, force_upcast=True) if use_diffusers: # pipe = StableDiffusionXLPipeline.from_pretrained(path, vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True) pipe = StableDiffusionXLPipeline.from_pretrained(path, torch_dtype=torch.float16, vae=vae, variant="fp16", use_safetensors=True, local_files_only=True) print(time.time() - start_time) pipe.to("cuda") if use_refiner: start_time = time.time() refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(refiner_path, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16") print(time.time() - start_time) refiner.to("cuda") # refiner.enable_sequential_cpu_offload() else: start_time = time.time() pipe = StableDiffusionXLPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-base-0.9/blob/main/sd_xl_base_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True) print(time.time() - start_time) pipe.to("cuda") if use_refiner: start_time = time.time() refiner = StableDiffusionXLImg2ImgPipeline.from_single_file("https://huggingface.co/stabilityai/stable-diffusion-xl-refiner-0.9/blob/main/sd_xl_refiner_0.9.safetensors", torch_dtype=torch.float16, use_safetensors=True) print(time.time() - start_time) refiner.to("cuda") prompt = "An astronaut riding a green horse on Mars" steps = 20 seed = 0 seed_everything(seed) start_time = time.time() image = pipe(prompt=prompt, num_inference_steps=steps, output_type="latent" if use_refiner else "pil").images[0] print(time.time() - start_time) if use_refiner: image = refiner(prompt=prompt, num_inference_steps=steps - 10, image=image).images[0] file_name = f"aaa" path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png") image.save(path) api.upload_file( path_or_fileobj=path, path_in_repo=path.split("/")[-1], repo_id="patrickvonplaten/images", repo_type="dataset", ) print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")