Aore commited on
Commit
b5516a1
·
1 Parent(s): 0606a5b

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -120
app.py DELETED
@@ -1,120 +0,0 @@
1
- from share import *
2
- import config
3
- import os
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 annotator.uniformer import UniformerDetector
14
- from cldm.model import create_model, load_state_dict
15
- from cldm.ddim_hacked import DDIMSampler
16
-
17
- from PIL import Image, ImageChops
18
- from IPython.display import display
19
-
20
- # os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
21
-
22
- apply_uniformer = UniformerDetector()
23
-
24
- model = create_model('./models/cldm_v15.yaml').cpu()
25
-
26
- model.load_state_dict(load_state_dict('./models/control_sks_crack_ppl.ckpt', location='cuda'))
27
- model.load_state_dict(load_state_dict('./models/sks_crack500_epoch_19.ckpt', location='cuda'))
28
-
29
- model = model.cuda()
30
- ddim_sampler = DDIMSampler(model)
31
-
32
- def process(input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution,
33
- ddim_steps, guess_mode, strength, scale, seed, eta):
34
- with torch.no_grad():
35
- input_image = np.array(input_image)
36
- input_image = input_image.astype('uint8')
37
-
38
- input_image = HWC3(input_image)
39
- detected_map = apply_uniformer(resize_image(input_image, detect_resolution))
40
- img = resize_image(input_image, image_resolution)
41
- H, W, C = img.shape
42
-
43
- detected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_NEAREST)
44
-
45
- control = torch.from_numpy(detected_map.copy()).float().cuda() / 255.0
46
- control = torch.stack([control for _ in range(num_samples)], dim=0)
47
- control = einops.rearrange(control, 'b h w c -> b c h w').clone()
48
-
49
- if seed == -1:
50
- seed = random.randint(0, 65535)
51
- seed_everything(seed)
52
-
53
- if config.save_memory:
54
- model.low_vram_shift(is_diffusing=False)
55
-
56
- cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}
57
- un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}
58
- shape = (4, H // 8, W // 8)
59
-
60
- if config.save_memory:
61
- model.low_vram_shift(is_diffusing=True)
62
-
63
- 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
64
- samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,
65
- shape, cond, verbose=False, eta=eta,
66
- unconditional_guidance_scale=scale,
67
- unconditional_conditioning=un_cond)
68
-
69
- if config.save_memory:
70
- model.low_vram_shift(is_diffusing=False)
71
-
72
- x_samples = model.decode_first_stage(samples)
73
- 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)
74
-
75
- results = [x_samples[i] for i in range(num_samples)]
76
- return [detected_map] + results
77
-
78
- block = gr.Blocks().queue()
79
- with block:
80
- with gr.Row():
81
- gr.Markdown("## Control Stable Diffusion with Segmentation Maps")
82
- with gr.Row():
83
- with gr.Column():
84
- with gr.Tabs(elem_id="mode_img2img") as tab_list:
85
- with gr.TabItem('img2img', id='img2img', elem_id="img2img_img2img_tab") as tab_img2img:
86
- with gr.Row().style(height=500):
87
- with gr.Column():
88
- init_img = gr.Image(label="Image for img2img", elem_id="img2img_image", show_label=False, source="upload", interactive=True, type="pil", tool="editor", image_mode="RGBA").style(height=480)
89
- init_run_button = gr.Button(label="Run")
90
-
91
- with gr.TabItem('Sketch', id='img2img_sketch', elem_id="img2img_img2img_sketch_tab") as tab_sketch:
92
- with gr.Row().style(height=500):
93
- with gr.Column():
94
- sketch = gr.Image(label="Image for inpainting with mask", show_label=False, elem_id="img2maskimg", source="canvas", interactive=True, type="pil", tool="color-sketch", image_mode="RGBA").style(height=480)
95
- sketch_run_button = gr.Button(label="Run")
96
- with gr.Row():
97
- prompt = gr.Textbox(label="Prompt")
98
-
99
- with gr.Row():
100
- with gr.Accordion("Advanced options", open=False):
101
- num_samples = gr.Slider(label="Images", minimum=1, maximum=12, value=1, step=1)
102
- image_resolution = gr.Slider(label="Image Resolution", minimum=256, maximum=768, value=512, step=64)
103
- strength = gr.Slider(label="Control Strength", minimum=0.0, maximum=2.0, value=1.0, step=0.01)
104
- guess_mode = gr.Checkbox(label='Guess Mode', value=False)
105
- detect_resolution = gr.Slider(label="Segmentation Resolution", minimum=128, maximum=1024, value=512, step=1)
106
- ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=100, value=20, step=1)
107
- scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
108
- seed = gr.Slider(label="Seed", minimum=-1, maximum=2147483647, step=1, randomize=True)
109
- eta = gr.Number(label="eta (DDIM)", value=0.0)
110
- a_prompt = gr.Textbox(label="Added Prompt", value='best quality, extremely detailed')
111
- n_prompt = gr.Textbox(label="Negative Prompt",
112
- value='longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality')
113
- with gr.Column():
114
- result_gallery = gr.Gallery(label='Output', show_label=False, elem_id="gallery").style(grid=2, height='auto')
115
- init_ips = [init_img, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, guess_mode, strength, scale, seed, eta]
116
- sketch_ips = [sketch, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, guess_mode, strength, scale, seed, eta]
117
- init_run_button.click(fn=process, inputs=init_ips, outputs=[result_gallery])
118
- sketch_run_button.click(fn=process, inputs=sketch_ips, outputs=[result_gallery])
119
-
120
- block.launch(server_name='127.0.0.1', share=True)