Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import os
|
3 |
+
import time
|
4 |
+
from os import path
|
5 |
+
from safetensors.torch import load_file
|
6 |
+
|
7 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
8 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
9 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
10 |
+
os.environ["HF_HOME"] = cache_path
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
import torch
|
14 |
+
from diffusers import StableDiffusionXLPipeline, LCMScheduler
|
15 |
+
|
16 |
+
# from scheduling_tcd import TCDScheduler
|
17 |
+
|
18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
+
|
20 |
+
class timer:
|
21 |
+
def __init__(self, method_name="timed process"):
|
22 |
+
self.method = method_name
|
23 |
+
|
24 |
+
def __enter__(self):
|
25 |
+
self.start = time.time()
|
26 |
+
print(f"{self.method} starts")
|
27 |
+
|
28 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
29 |
+
end = time.time()
|
30 |
+
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
31 |
+
|
32 |
+
if not path.exists(cache_path):
|
33 |
+
os.makedirs(cache_path, exist_ok=True)
|
34 |
+
|
35 |
+
pipe = StableDiffusionXLPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.bfloat16, safety_checker=None)
|
36 |
+
pipe.to(device="cuda", dtype=torch.bfloat16)
|
37 |
+
unet_state = load_file(hf_hub_download("ByteDance/Hyper-SD", "Hyper-SDXL-1step-Unet.safetensors"), device="cuda")
|
38 |
+
pipe.unet.load_state_dict(unet_state)
|
39 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config, timestep_spacing ="trailing")
|
40 |
+
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
with gr.Column():
|
43 |
+
with gr.Row():
|
44 |
+
with gr.Column():
|
45 |
+
num_images = gr.Slider(label="Number of Images", minimum=1, maximum=8, step=1, value=4, interactive=True)
|
46 |
+
height = gr.Number(label="Image Height", value=1024, interactive=True)
|
47 |
+
width = gr.Number(label="Image Width", value=1024, interactive=True)
|
48 |
+
# steps = gr.Slider(label="Inference Steps", minimum=1, maximum=8, step=1, value=1, interactive=True)
|
49 |
+
# eta = gr.Number(label="Eta (Corresponds to parameter eta (η) in the DDIM paper, i.e. 0.0 eqauls DDIM, 1.0 equals LCM)", value=1., interactive=True)
|
50 |
+
prompt = gr.Text(label="Prompt", value="a photo of a cat", interactive=True)
|
51 |
+
seed = gr.Number(label="Seed", value=3413, interactive=True)
|
52 |
+
btn = gr.Button(value="run")
|
53 |
+
with gr.Column():
|
54 |
+
output = gr.Gallery(width=1024, height=768)
|
55 |
+
|
56 |
+
def process_image(num_images, height, width, prompt, seed):
|
57 |
+
global pipe
|
58 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
59 |
+
return pipe(
|
60 |
+
prompt=[prompt]*num_images,
|
61 |
+
generator=torch.Generator().manual_seed(int(seed)),
|
62 |
+
num_inference_steps=1,
|
63 |
+
guidance_scale=0.,
|
64 |
+
height=int(height),
|
65 |
+
width=int(width),
|
66 |
+
timesteps=[800]
|
67 |
+
).images
|
68 |
+
|
69 |
+
reactive_controls = [num_images, height, width, prompt, seed]
|
70 |
+
|
71 |
+
# for control in reactive_controls:
|
72 |
+
# control.change(fn=process_image, inputs=reactive_controls, outputs=[output])
|
73 |
+
|
74 |
+
btn.click(process_image, inputs=reactive_controls, outputs=[output])
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|