CoolFace
Modelpublic

diffusers/tools

sourceHugging Facecreativeml-openrail-mupdated 3y agoView on Hugging Face
11likes27downloads
run_local_xl.py77 linesDownload Raw Back to root
1#!/usr/bin/env python32from diffusers import DiffusionPipeline, EulerDiscreteScheduler, StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler3from diffusers import StableDiffusionXLPipeline, StableDiffusionXLImg2ImgPipeline, AutoencoderKL4import time5from pytorch_lightning import seed_everything6import os7from huggingface_hub import HfApi8# from compel import Compel9import torch10import sys11from pathlib import Path12import requests13from PIL import Image14from io import BytesIO15import xformers16 17api = HfApi()18start_time = time.time()19 20# use_refiner = bool(int(sys.argv[1]))21use_refiner = False22use_diffusers = True23path = "stabilityai/stable-diffusion-xl-base-1.0"24refiner_path = "stabilityai/stable-diffusion-xl-refiner-1.0"25vae_path = "madebyollin/sdxl-vae-fp16-fix"26 27vae = AutoencoderKL.from_pretrained(vae_path, torch_dtype=torch.float16)28if use_diffusers:29    # pipe = StableDiffusionXLPipeline.from_pretrained(path, vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True, local_files_only=True)30    pipe = StableDiffusionXLPipeline.from_pretrained(path, torch_dtype=torch.float16, vae=vae, variant="fp16", use_safetensors=True, local_files_only=True, add_watermarker=False)31    import ipdb; ipdb.set_trace()32    # pipe.enable_xformers_memory_efficient_attention()33    print(time.time() - start_time)34    pipe.to("cuda")35 36    if use_refiner:37        start_time = time.time()38        refiner = StableDiffusionXLImg2ImgPipeline.from_pretrained(refiner_path, vae=vae, torch_dtype=torch.float16, use_safetensors=True, variant="fp16")39        print(time.time() - start_time)40        refiner.to("cuda")41        # refiner.enable_sequential_cpu_offload()42else:43    start_time = time.time()44    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)45    print(time.time() - start_time)46    pipe.to("cuda")47 48    if use_refiner:49        start_time = time.time()50        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)51        print(time.time() - start_time)52        refiner.to("cuda")53 54 55prompt = "An astronaut riding a green horse on Mars"56steps = 2057seed = 058seed_everything(seed)59start_time = time.time()60image = pipe(prompt=prompt, num_inference_steps=steps, output_type="latent" if use_refiner else "pil").images[0]61print(time.time() - start_time)62 63if use_refiner:64    image = refiner(prompt=prompt, num_inference_steps=steps - 10, image=image).images[0]65 66file_name = f"aaa"67path = os.path.join(Path.home(), "images", "ediffi_sdxl", f"{file_name}.png")68image.save(path)69 70api.upload_file(71    path_or_fileobj=path,72    path_in_repo=path.split("/")[-1],73    repo_id="patrickvonplaten/images",74    repo_type="dataset",75)76print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")77