Spaces:
Runtime error
Runtime error
Delete diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_app.py
Browse files
diffusion_webui/diffusion_models/controlnet/controlnet_inpaint/controlnet_inpaint_app.py
DELETED
|
@@ -1,206 +0,0 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
-
import torch
|
| 5 |
-
from diffusers import (
|
| 6 |
-
ControlNetModel,
|
| 7 |
-
StableDiffusionControlNetPipeline,
|
| 8 |
-
)
|
| 9 |
-
from PIL import Image
|
| 10 |
-
|
| 11 |
-
from diffusion_webui.utils.model_list import (
|
| 12 |
-
controlnet_canny_model_list,
|
| 13 |
-
stable_model_list,
|
| 14 |
-
)
|
| 15 |
-
from diffusion_webui.utils.scheduler_list import (
|
| 16 |
-
SCHEDULER_LIST,
|
| 17 |
-
get_scheduler_list,
|
| 18 |
-
)
|
| 19 |
-
|
| 20 |
-
# https://github.com/mikonvergence/ControlNetInpaint
|
| 21 |
-
|
| 22 |
-
class StableDiffusionControlInpaintNetCannyGenerator:
|
| 23 |
-
def __init__(self):
|
| 24 |
-
self.pipe = None
|
| 25 |
-
|
| 26 |
-
def controlnet_canny_inpaint(
|
| 27 |
-
self,
|
| 28 |
-
image_path: str,
|
| 29 |
-
):
|
| 30 |
-
image = image_path["image"].convert("RGB").resize((512, 512))
|
| 31 |
-
image = np.array(image)
|
| 32 |
-
|
| 33 |
-
image = cv2.Canny(image, 100, 200)
|
| 34 |
-
image = image[:, :, None]
|
| 35 |
-
image = np.concatenate([image, image, image], axis=2)
|
| 36 |
-
image = Image.fromarray(image)
|
| 37 |
-
|
| 38 |
-
return image
|
| 39 |
-
|
| 40 |
-
def load_model(self, stable_model_path, controlnet_model_path, scheduler):
|
| 41 |
-
if self.pipe is None:
|
| 42 |
-
controlnet = ControlNetModel.from_pretrained(
|
| 43 |
-
controlnet_model_path, torch_dtype=torch.float16
|
| 44 |
-
)
|
| 45 |
-
self.pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 46 |
-
pretrained_model_name_or_path=stable_model_path,
|
| 47 |
-
controlnet=controlnet,
|
| 48 |
-
safety_checker=None,
|
| 49 |
-
torch_dtype=torch.float16,
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
self.pipe = get_scheduler_list(pipe=self.pipe, scheduler=scheduler)
|
| 53 |
-
self.pipe.to("cuda")
|
| 54 |
-
self.pipe.enable_xformers_memory_efficient_attention()
|
| 55 |
-
|
| 56 |
-
return self.pipe
|
| 57 |
-
|
| 58 |
-
def generate_image(
|
| 59 |
-
self,
|
| 60 |
-
image_path: str,
|
| 61 |
-
stable_model_path: str,
|
| 62 |
-
controlnet_model_path: str,
|
| 63 |
-
prompt: str,
|
| 64 |
-
negative_prompt: str,
|
| 65 |
-
num_images_per_prompt: int,
|
| 66 |
-
guidance_scale: int,
|
| 67 |
-
num_inference_step: int,
|
| 68 |
-
scheduler: str,
|
| 69 |
-
seed_generator: int,
|
| 70 |
-
):
|
| 71 |
-
|
| 72 |
-
image = self.controlnet_canny_inpaint(image_path=image_path)
|
| 73 |
-
|
| 74 |
-
pipe = self.load_model(
|
| 75 |
-
stable_model_path=stable_model_path,
|
| 76 |
-
controlnet_model_path=controlnet_model_path,
|
| 77 |
-
scheduler=scheduler,
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
if seed_generator == 0:
|
| 81 |
-
random_seed = torch.randint(0, 1000000, (1,))
|
| 82 |
-
generator = torch.manual_seed(random_seed)
|
| 83 |
-
else:
|
| 84 |
-
generator = torch.manual_seed(seed_generator)
|
| 85 |
-
|
| 86 |
-
output = pipe(
|
| 87 |
-
prompt=prompt,
|
| 88 |
-
image=image,
|
| 89 |
-
negative_prompt=negative_prompt,
|
| 90 |
-
num_images_per_prompt=num_images_per_prompt,
|
| 91 |
-
num_inference_steps=num_inference_step,
|
| 92 |
-
guidance_scale=guidance_scale,
|
| 93 |
-
generator=generator,
|
| 94 |
-
).images
|
| 95 |
-
|
| 96 |
-
return output
|
| 97 |
-
|
| 98 |
-
def app():
|
| 99 |
-
with gr.Blocks():
|
| 100 |
-
with gr.Row():
|
| 101 |
-
with gr.Column():
|
| 102 |
-
controlnet_canny_inpaint_image_file = gr.Image(
|
| 103 |
-
source="upload",
|
| 104 |
-
tool="sketch",
|
| 105 |
-
elem_id="image_upload",
|
| 106 |
-
type="pil",
|
| 107 |
-
label="Upload",
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
controlnet_canny_inpaint_prompt = gr.Textbox(
|
| 111 |
-
lines=1, placeholder="Prompt", show_label=False
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
controlnet_canny_inpaint_negative_prompt = gr.Textbox(
|
| 115 |
-
lines=1,
|
| 116 |
-
show_label=False,
|
| 117 |
-
placeholder="Negative Prompt",
|
| 118 |
-
)
|
| 119 |
-
with gr.Row():
|
| 120 |
-
with gr.Column():
|
| 121 |
-
controlnet_canny_inpaint_stable_model_id = (
|
| 122 |
-
gr.Dropdown(
|
| 123 |
-
choices=stable_model_list,
|
| 124 |
-
value=stable_model_list[0],
|
| 125 |
-
label="Stable Model Id",
|
| 126 |
-
)
|
| 127 |
-
)
|
| 128 |
-
|
| 129 |
-
controlnet_canny_inpaint_guidance_scale = gr.Slider(
|
| 130 |
-
minimum=0.1,
|
| 131 |
-
maximum=15,
|
| 132 |
-
step=0.1,
|
| 133 |
-
value=7.5,
|
| 134 |
-
label="Guidance Scale",
|
| 135 |
-
)
|
| 136 |
-
|
| 137 |
-
controlnet_canny_inpaint_num_inference_step = (
|
| 138 |
-
gr.Slider(
|
| 139 |
-
minimum=1,
|
| 140 |
-
maximum=100,
|
| 141 |
-
step=1,
|
| 142 |
-
value=50,
|
| 143 |
-
label="Num Inference Step",
|
| 144 |
-
)
|
| 145 |
-
)
|
| 146 |
-
controlnet_canny_inpaint_num_images_per_prompt = (
|
| 147 |
-
gr.Slider(
|
| 148 |
-
minimum=1,
|
| 149 |
-
maximum=10,
|
| 150 |
-
step=1,
|
| 151 |
-
value=1,
|
| 152 |
-
label="Number Of Images",
|
| 153 |
-
)
|
| 154 |
-
)
|
| 155 |
-
with gr.Row():
|
| 156 |
-
with gr.Column():
|
| 157 |
-
controlnet_canny_inpaint_model_id = gr.Dropdown(
|
| 158 |
-
choices=controlnet_canny_model_list,
|
| 159 |
-
value=controlnet_canny_model_list[0],
|
| 160 |
-
label="Controlnet Model Id",
|
| 161 |
-
)
|
| 162 |
-
controlnet_canny_inpaint_scheduler = (
|
| 163 |
-
gr.Dropdown(
|
| 164 |
-
choices=SCHEDULER_LIST,
|
| 165 |
-
value=SCHEDULER_LIST[0],
|
| 166 |
-
label="Scheduler",
|
| 167 |
-
)
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
controlnet_canny_inpaint_seed_generator = (
|
| 171 |
-
gr.Slider(
|
| 172 |
-
minimum=0,
|
| 173 |
-
maximum=1000000,
|
| 174 |
-
step=1,
|
| 175 |
-
value=0,
|
| 176 |
-
label="Seed Generator",
|
| 177 |
-
)
|
| 178 |
-
)
|
| 179 |
-
|
| 180 |
-
controlnet_canny_inpaint_predict = gr.Button(
|
| 181 |
-
value="Generator"
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
with gr.Column():
|
| 185 |
-
output_image = gr.Gallery(
|
| 186 |
-
label="Generated images",
|
| 187 |
-
show_label=False,
|
| 188 |
-
elem_id="gallery",
|
| 189 |
-
).style(grid=(1, 2))
|
| 190 |
-
|
| 191 |
-
controlnet_canny_inpaint_predict.click(
|
| 192 |
-
fn=StableDiffusionControlInpaintNetCannyGenerator().generate_image,
|
| 193 |
-
inputs=[
|
| 194 |
-
controlnet_canny_inpaint_image_file,
|
| 195 |
-
controlnet_canny_inpaint_stable_model_id,
|
| 196 |
-
controlnet_canny_inpaint_model_id,
|
| 197 |
-
controlnet_canny_inpaint_prompt,
|
| 198 |
-
controlnet_canny_inpaint_negative_prompt,
|
| 199 |
-
controlnet_canny_inpaint_num_images_per_prompt,
|
| 200 |
-
controlnet_canny_inpaint_guidance_scale,
|
| 201 |
-
controlnet_canny_inpaint_num_inference_step,
|
| 202 |
-
controlnet_canny_inpaint_scheduler,
|
| 203 |
-
controlnet_canny_inpaint_seed_generator,
|
| 204 |
-
],
|
| 205 |
-
outputs=[output_image],
|
| 206 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|