|
import spaces |
|
import gradio as gr |
|
import torch |
|
from diffusers import UNet2DConditionModel, StableDiffusionXLPipeline, EulerDiscreteScheduler |
|
from huggingface_hub import hf_hub_download |
|
from safetensors.torch import load_file |
|
import rembg |
|
from io import BytesIO |
|
import PIL.Image as Image |
|
import cv2 |
|
import numpy |
|
|
|
base = "stabilityai/stable-diffusion-xl-base-1.0" |
|
repo = "ByteDance/SDXL-Lightning" |
|
ckpt = "sdxl_lightning_4step_unet.safetensors" |
|
|
|
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") |
|
|
|
pipe.scheduler = EulerDiscreteScheduler.from_config(pipe.scheduler.config, timestep_spacing="trailing") |
|
|
|
|
|
@spaces.GPU |
|
def generate_image(prompt): |
|
prompt += "no background, side view, minimalist shot" |
|
|
|
image = pipe(prompt, num_inference_steps=4, guidance_scale=0).images[0] |
|
|
|
return image |
|
|
|
_TITLE = "Shoe Generator" |
|
with gr.Blocks(_TITLE) as ShoeGen: |
|
with gr.Row(): |
|
with gr.Column(): |
|
prompt = gr.Textbox(label="Enter a discription of a shoe") |
|
|
|
button_gen = gr.Button("Generate Image") |
|
with gr.Column(): |
|
image = gr.Image(label="Generated Image", show_download_button=True) |
|
|
|
button_gen.click(generate_image, inputs=[prompt], outputs=[image]) |
|
|
|
ShoeGen.launch() |
|
|