Spaces:
Runtime error
Runtime error
import torch | |
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler | |
from huggingface_hub import hf_hub_download | |
from safetensors.torch import load_file | |
import gradio as gr | |
import spaces | |
base = "stabilityai/stable-diffusion-xl-base-1.0" | |
repo = "ByteDance/SDXL-Lightning" | |
ckpt = "sdxl_lightning_4step_unet.safetensors" # Use the correct ckpt for your step setting! | |
# Load model. | |
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16) | |
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda")) | |
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda") | |
# Ensure sampler uses "trailing" timesteps and "sample" prediction type. | |
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing") | |
# Load model. | |
def generate(prompt, steps): | |
image = pipe(prompt, num_inference_steps=steps, guidance_scale=0).images[0] | |
return image | |
output_image = gr.Image(type="pil") | |
inputs=[ | |
gr.inputs.Textbox(label="Prompt (What you want in the image)", default="Cinematic portrait of a handsome cat with a suit and sunglasses"), | |
gr.inputs.Slider(minimum=1, maximum=10, step=0, default=1, label="Number of Images(coming soon <3)") | |
] | |
demo = gr.Interface(fn=generate, inputs=inputs, outputs=output_image) | |
demo.launch() |