Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from share import *
|
| 2 |
+
import config
|
| 3 |
+
|
| 4 |
+
import cv2
|
| 5 |
+
import einops
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import torch
|
| 9 |
+
import random
|
| 10 |
+
|
| 11 |
+
from pytorch_lightning import seed_everything
|
| 12 |
+
from annotator.util import resize_image, HWC3
|
| 13 |
+
from cldm.model import create_model, load_state_dict
|
| 14 |
+
from cldm.ddim_hacked import DDIMSampler
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
model = create_model('./models/cldm_v15.yaml').cpu()
|
| 18 |
+
model.load_state_dict(load_state_dict('./models/control_sd15_scribble.pth', location='cuda'))
|
| 19 |
+
model = model.cuda()
|
| 20 |
+
ddim_sampler = DDIMSampler(model)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta):
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
img = resize_image(HWC3(input_image['mask'][:, :, 0]), image_resolution)
|
| 26 |
+
H, W, C = img.shape
|
| 27 |
+
|
| 28 |
+
detected_map = np.zeros_like(img, dtype=np.uint8)
|
| 29 |
+
detected_map[np.min(img, axis=2) > 127] = 255
|
| 30 |
+
|
| 31 |
+
control = torch.from_numpy(detected_map.copy()).float().cuda() / 255.0
|
| 32 |
+
control = torch.stack([control for _ in range(num_samples)], dim=0)
|
| 33 |
+
control = einops.rearrange(control, 'b h w c -> b c h w').clone()
|
| 34 |
+
|
| 35 |
+
if seed == -1:
|
| 36 |
+
seed = random.randint(0, 65535)
|
| 37 |
+
seed_everything(seed)
|
| 38 |
+
|
| 39 |
+
if config.save_memory:
|
| 40 |
+
model.low_vram_shift(is_diffusing=False)
|
| 41 |
+
|
| 42 |
+
cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
|
| 43 |
+
un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
|
| 44 |
+
shape = (4, H // 8, W // 8)
|
| 45 |
+
|
| 46 |
+
if config.save_memory:
|
| 47 |
+
model.low_vram_shift(is_diffusing=True)
|
| 48 |
+
|
| 49 |
+
model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13) # Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01
|
| 50 |
+
samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
|
| 51 |
+
shape, cond, verbose=False, eta=eta,
|
| 52 |
+
unconditional_guidance_scale=scale,
|
| 53 |
+
unconditional_conditioning=un_cond)
|
| 54 |
+
|
| 55 |
+
if config.save_memory:
|
| 56 |
+
model.low_vram_shift(is_diffusing=False)
|
| 57 |
+
|
| 58 |
+
x_samples = model.decode_first_stage(samples)
|
| 59 |
+
x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)
|
| 60 |
+
|
| 61 |
+
results = [x_samples[i] for i in range(num_samples)]
|
| 62 |
+
return [255 - detected_map] + results
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def create_canvas(w, h):
|
| 66 |
+
return np.zeros(shape=(h, w, 3), dtype=np.uint8) + 255
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
block = gr.Blocks().queue()
|
| 70 |
+
with block:
|
| 71 |
+
with gr.Row():
|
| 72 |
+
gr.Markdown("## Control Stable Diffusion with Interactive Scribbles")
|
| 73 |
+
with gr.Row():
|
| 74 |
+
with gr.Column():
|
| 75 |
+
canvas_width = gr.Slider(label="Canvas Width", minimum=256, maximum=1024, value=512, step=1)
|
| 76 |
+
canvas_height = gr.Slider(label="Canvas Height", minimum=256, maximum=1024, value=512, step=1)
|
| 77 |
+
create_button = gr.Button(label="Start", value='Open drawing canvas!')
|
| 78 |
+
input_image = gr.Image(source='upload', type='numpy', tool='sketch')
|
| 79 |
+
gr.Markdown(value='Do not forget to change your brush width to make it thinner. (Gradio do not allow developers to set brush width so you need to do it manually.) '
|
| 80 |
+
'Just click on the small pencil icon in the upper right corner of the above block.')
|
| 81 |
+
create_button.click(fn=create_canvas, inputs=[canvas_width, canvas_height], outputs=[input_image])
|
| 82 |
+
prompt = gr.Textbox(label="Prompt")
|
| 83 |
+
run_button = gr.Button(label="Run")
|
| 84 |
+
with gr.Accordion("Advanced options", open=False):
|
| 85 |
+
num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
|
| 86 |
+
image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
|
| 87 |
+
strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
|
| 88 |
+
guess_mode = gr.Checkbox(label='Guess Mode', value=False)
|
| 89 |
+
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
|
| 90 |
+
scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
|
| 91 |
+
seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
|
| 92 |
+
eta = gr.Number(label="eta (DDIM)", value=0.0)
|
| 93 |
+
a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
|
| 94 |
+
n_prompt = gr.Textbox(label="Negative Prompt",
|
| 95 |
+
value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
|
| 96 |
+
with gr.Column():
|
| 97 |
+
result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
|
| 98 |
+
ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, guess_mode, strength, scale, seed, eta]
|
| 99 |
+
run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
block.launch()
|