RamAnanth1 commited on
Commit
7a5c6a0
1 Parent(s): 0c0c966

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import einops
3
+ import gradio as gr
4
+ import numpy as np
5
+ import torch
6
+
7
+
8
+ from pytorch_lightning import seed_everything
9
+ from util import resize_image, HWC3, apply_canny
10
+ from ldm.models.diffusion.ddim import DDIMSampler
11
+
12
+ from cldm.model import create_model, load_state_dict
13
+
14
+ from huggingface_hub import hf_hub_url, cached_download
15
+ REPO_ID = "lllyasviel/ControlNet"
16
+ FILENAME = "models/control_sd15_canny.pth"
17
+
18
+ model = create_model('./models/cldm_v15.yaml')
19
+ model.load_state_dict(load_state_dict(cached_download(
20
+ hf_hub_url(REPO_ID, FILENAME)
21
+ ), location='cpu'))
22
+ ddim_sampler = DDIMSampler(model)
23
+
24
+ def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, scale, seed, eta, low_threshold, high_threshold):
25
+ with torch.no_grad():
26
+ img = resize_image(HWC3(input_image), image_resolution)
27
+ H, W, C = img.shape
28
+
29
+ detected_map = apply_canny(img, low_threshold, high_threshold)
30
+ detected_map = HWC3(detected_map)
31
+
32
+ control = torch.from_numpy(detected_map.copy()).float() / 255.0
33
+ control = torch.stack([control for _ in range(num_samples)], dim=0)
34
+ control = einops.rearrange(control, 'b h w c -> b c h w').clone()
35
+
36
+ seed_everything(seed)
37
+
38
+ cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
39
+ un_cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
40
+ shape = (4, H // 8, W // 8)
41
+
42
+ samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
43
+ shape, cond, verbose=False, eta=eta,
44
+ unconditional_guidance_scale=scale,
45
+ unconditional_conditioning=un_cond)
46
+ x_samples = model.decode_first_stage(samples)
47
+ 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)
48
+
49
+ results = [x_samples[i] for i in range(num_samples)]
50
+ return [255 - detected_map] + results
51
+
52
+
53
+ block = gr.Blocks().queue()
54
+ with block:
55
+ with gr.Row():
56
+ gr.Markdown("## Control Stable Diffusion with Canny Edge Maps")
57
+ with gr.Row():
58
+ with gr.Column():
59
+ input_image = gr.Image(source='upload', type="numpy")
60
+ prompt = gr.Textbox(label="Prompt")
61
+ run_button = gr.Button(label="Run")
62
+ with gr.Accordion("Advanced options", open=False):
63
+ num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
64
+ image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=256)
65
+ low_threshold = gr.Slider(label="Canny low threshold", minimum=1, maximum=255, value=100, step=1)
66
+ high_threshold = gr.Slider(label="Canny high threshold", minimum=1, maximum=255, value=200, step=1)
67
+ ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
68
+ scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
69
+ seed = gr.Slider(label="Seed", minimum=0, maximum=2147483647, step=1, randomize=True)
70
+ eta = gr.Number(label="eta (DDIM)", value=0.0)
71
+ a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
72
+ n_prompt = gr.Textbox(label="Negative Prompt",
73
+ value='longbody, lowres, bad anatomy, bad hands, missing fingers, pubic hair,extra digit, fewer digits, cropped, worst quality, low quality')
74
+ with gr.Column():
75
+ result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
76
+ ips = [input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, ddim_steps, scale, seed, eta, low_threshold, high_threshold]
77
+ run_button.click(fn=process, inputs=ips, outputs=[result_gallery])
78
+
79
+
80
+ block.launch(debug = True)