Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import time
|
| 4 |
+
from os import path
|
| 5 |
+
|
| 6 |
+
cache_path = path.join(path.dirname(path.abspath(__file__)), "models")
|
| 7 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_path
|
| 8 |
+
os.environ["HF_HUB_CACHE"] = cache_path
|
| 9 |
+
os.environ["HF_HOME"] = cache_path
|
| 10 |
+
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import torch
|
| 13 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 14 |
+
|
| 15 |
+
from scheduling_tcd import TCDScheduler
|
| 16 |
+
|
| 17 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 18 |
+
|
| 19 |
+
class timer:
|
| 20 |
+
def __init__(self, method_name="timed process"):
|
| 21 |
+
self.method = method_name
|
| 22 |
+
|
| 23 |
+
def __enter__(self):
|
| 24 |
+
self.start = time.time()
|
| 25 |
+
print(f"{self.method} starts")
|
| 26 |
+
|
| 27 |
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
| 28 |
+
end = time.time()
|
| 29 |
+
print(f"{self.method} took {str(round(end - self.start, 2))}s")
|
| 30 |
+
|
| 31 |
+
if not path.exists(cache_path):
|
| 32 |
+
os.makedirs(cache_path, exist_ok=True)
|
| 33 |
+
|
| 34 |
+
controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-scribble", torch_dtype=torch.float16, use_safetensors=True)
|
| 35 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16, safety_checker=None)
|
| 36 |
+
pipe.to(device="cuda", dtype=torch.float16)
|
| 37 |
+
pipe.load_lora_weights("ByteDance/Hyper-SD", weight_name="Hyper-SD15-1step-lora.safetensors", adapter_name="default")
|
| 38 |
+
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config, timestep_spacing ="trailing")
|
| 39 |
+
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
with gr.Column():
|
| 42 |
+
with gr.Row():
|
| 43 |
+
with gr.Column():
|
| 44 |
+
num_images = gr.Slider(label="Number of Images", minimum=1, maximum=8, step=1, value=4, interactive=True)
|
| 45 |
+
steps = gr.Slider(label="Inference Steps", minimum=1, maximum=8, step=1, value=1, interactive=True)
|
| 46 |
+
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)
|
| 47 |
+
controlnet_scale = gr.Number(label="ControlNet Conditioning Scale", value=1.0, interactive=True)
|
| 48 |
+
prompt = gr.Text(label="Prompt", value="a photo of a cat", interactive=True)
|
| 49 |
+
seed = gr.Number(label="Seed", value=3413, interactive=True)
|
| 50 |
+
scribble = gr.Image(source="canvas", tool="color-sketch", shape=(512, 512), height=768, width=768, type="pil")
|
| 51 |
+
btn = gr.Button(value="run")
|
| 52 |
+
with gr.Column():
|
| 53 |
+
output = gr.Gallery(height=768)
|
| 54 |
+
|
| 55 |
+
def process_image(steps, prompt, controlnet_scale, eta, seed, scribble, num_images):
|
| 56 |
+
global pipe
|
| 57 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.float16), timer("inference"):
|
| 58 |
+
return pipe(
|
| 59 |
+
prompt=[prompt]*num_images,
|
| 60 |
+
image=[scribble]*num_images,
|
| 61 |
+
generator=torch.Generator().manual_seed(int(seed)),
|
| 62 |
+
num_inference_steps=steps,
|
| 63 |
+
guidance_scale=0.,
|
| 64 |
+
eta=eta,
|
| 65 |
+
controlnet_conditioning_scale=controlnet_scale
|
| 66 |
+
).images
|
| 67 |
+
|
| 68 |
+
reactive_controls = [steps, prompt, controlnet_scale, eta, seed, scribble, num_images]
|
| 69 |
+
|
| 70 |
+
for control in reactive_controls:
|
| 71 |
+
control.change(fn=process_image, inputs=reactive_controls, outputs=[output])
|
| 72 |
+
|
| 73 |
+
btn.click(process_image, inputs=reactive_controls, outputs=[output])
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
# parser = argparse.ArgumentParser()
|
| 77 |
+
# parser.add_argument("--port", default=7891, type=int)
|
| 78 |
+
# args = parser.parse_args()
|
| 79 |
+
# demo.launch(server_name="0.0.0.0", server_port=args.port)
|
| 80 |
+
demo.launch()
|