File size: 1,352 Bytes
0024d7b
 
 
 
 
 
ab79cec
 
 
 
0024d7b
ab79cec
 
 
 
0024d7b
ab79cec
 
 
 
0024d7b
 
 
ab79cec
0024d7b
 
 
 
 
 
ab79cec
 
 
0024d7b
 
 
 
 
ab79cec
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import gradio as gr
import torch
from diffusers import StableDiffusionXLPipeline, EulerDiscreteScheduler
from huggingface_hub import hf_hub_download
import spaces

# Constants
base = "stabilityai/stable-diffusion-xl-base-1.0"
repo = "ByteDance/SDXL-Lightning"
ckpt = "sdxl_lightning_4step_unet.pth"

# Model Initialization
pipe = StableDiffusionXLPipeline.from_pretrained(base, torch_dtype=torch.float16, variant="fp16").to("cuda")
pipe.unet.load_state_dict(torch.load(hf_hub_download(repo, ckpt), map_location="cuda"))
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing")

# Function 
@spaces.GPU
def generate_image(prompt):
    # Generate image using the preloaded model
    image = pipe(prompt, num_inference_steps=4, guidance_scale=0).images[0]
    return image

# Gradio Interface
description = """
This demo utilizes the SDXL-Lightning model by ByteDance, which is a fast text-to-image generative model capable of producing high-quality images in 4 steps.
As a community effort, this demo was put together by AngryPenguin. Link to model: https://huggingface.co/ByteDance/SDXL-Lightning
"""

demo = gr.Interface(
    fn=generate_image,
    inputs="text",
    outputs="image",
    title="Text-to-Image with SDXL Lightning ⚡",
    description=description
)

demo.queue(max_size=20)
demo.launch()