Spaces:
Sleeping
Sleeping
| import torch | |
| from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler | |
| from huggingface_hub import hf_hub_download | |
| from safetensors.torch import load_file | |
| 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. | |
| pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing") | |
| def generateTransparentImage(text): | |
| # Ensure using the same inference steps as the loaded model and CFG set to 0. | |
| image = pipe(text+', full body, transparent background', num_inference_steps=4, guidance_scale=0).images[0] | |
| return image | |
| if __name__ == "__main__": | |
| text = "a cat" | |
| img = generateTransparentImage(text) | |
| img.save("output.png") |