Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from diffusers import StableDiffusionXLPipeline, UNet2DConditionModel, EulerDiscreteScheduler
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
from safetensors.torch import load_file
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
base = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 8 |
+
repo = "ByteDance/SDXL-Lightning"
|
| 9 |
+
ckpt = "sdxl_lightning_1step_unet_x0.safetensors" # Use the correct ckpt for your step setting!
|
| 10 |
+
|
| 11 |
+
# Load model.
|
| 12 |
+
|
| 13 |
+
def generate():
|
| 14 |
+
unet = UNet2DConditionModel.from_config(base, subfolder="unet").to("cuda", torch.float16)
|
| 15 |
+
unet.load_state_dict(load_file(hf_hub_download(repo, ckpt), device="cuda"))
|
| 16 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(base, unet=unet, torch_dtype=torch.float16, variant="fp16").to("cuda")
|
| 17 |
+
# Ensure sampler uses "trailing" timesteps and "sample" prediction type.
|
| 18 |
+
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing", prediction_type="sample")
|
| 19 |
+
|
| 20 |
+
# Ensure using the same inference steps as the loaded model and CFG set to 0.
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def greet(prompt):
|
| 24 |
+
image = pipe(prompt, num_inference_steps=1, guidance_scale=0).images[0].save("output.png")
|
| 25 |
+
return image
|
| 26 |
+
|
| 27 |
+
demo = gr.Interface(fn=greet, inputs="text", outputs="image")
|
| 28 |
+
demo.launch()
|