Spaces:
Running
on
Zero
Running
on
Zero
zhiweili
commited on
Commit
•
fec2939
1
Parent(s):
cd21710
add multi contronet
Browse files- app_haircolor_inpaint_15.py +30 -10
app_haircolor_inpaint_15.py
CHANGED
@@ -15,6 +15,10 @@ from diffusers import (
|
|
15 |
DDIMScheduler,
|
16 |
)
|
17 |
|
|
|
|
|
|
|
|
|
18 |
BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
|
19 |
|
20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -24,10 +28,18 @@ DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res,
|
|
24 |
|
25 |
DEFAULT_CATEGORY = "hair"
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
basepipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
33 |
BASE_MODEL,
|
@@ -50,11 +62,15 @@ def image_to_image(
|
|
50 |
num_steps: int,
|
51 |
guidance_scale: float,
|
52 |
generate_size: int,
|
|
|
|
|
53 |
):
|
54 |
run_task_time = 0
|
55 |
time_cost_str = ''
|
56 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
57 |
-
|
|
|
|
|
58 |
|
59 |
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
60 |
generated_image = basepipeline(
|
@@ -66,6 +82,7 @@ def image_to_image(
|
|
66 |
control_image=control_image,
|
67 |
guidance_scale=guidance_scale,
|
68 |
num_inference_steps=num_steps,
|
|
|
69 |
).images[0]
|
70 |
|
71 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
@@ -100,14 +117,17 @@ def create_demo() -> gr.Blocks:
|
|
100 |
with gr.Column():
|
101 |
edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
|
102 |
generate_size = gr.Number(label="Generate Size", value=512)
|
103 |
-
seed = gr.Number(label="Seed", value=8)
|
104 |
-
category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
|
105 |
with gr.Column():
|
106 |
num_steps = gr.Slider(minimum=1, maximum=100, value=15, step=1, label="Num Steps")
|
107 |
guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
|
108 |
-
mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
|
109 |
with gr.Column():
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
g_btn = gr.Button("Edit Image")
|
112 |
|
113 |
with gr.Row():
|
@@ -127,7 +147,7 @@ def create_demo() -> gr.Blocks:
|
|
127 |
outputs=[origin_area_image, mask_image, croper],
|
128 |
).success(
|
129 |
fn=image_to_image,
|
130 |
-
inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size],
|
131 |
outputs=[generated_image, generated_cost],
|
132 |
).success(
|
133 |
fn=restore_result,
|
|
|
15 |
DDIMScheduler,
|
16 |
)
|
17 |
|
18 |
+
from controlnet_aux import (
|
19 |
+
CannyDetector,
|
20 |
+
)
|
21 |
+
|
22 |
BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
|
23 |
|
24 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
28 |
|
29 |
DEFAULT_CATEGORY = "hair"
|
30 |
|
31 |
+
canny_detector = CannyDetector()
|
32 |
+
|
33 |
+
controlnet = [
|
34 |
+
ControlNetModel.from_pretrained(
|
35 |
+
"lllyasviel/control_v11p_sd15_inpaint",
|
36 |
+
torch_dtype=torch.float16,
|
37 |
+
),
|
38 |
+
ControlNetModel.from_pretrained(
|
39 |
+
"diffusers/controlnet-canny-sdxl-1.0",
|
40 |
+
torch_dtype=torch.float16,
|
41 |
+
),
|
42 |
+
]
|
43 |
|
44 |
basepipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
45 |
BASE_MODEL,
|
|
|
62 |
num_steps: int,
|
63 |
guidance_scale: float,
|
64 |
generate_size: int,
|
65 |
+
inpaint_scale: float = 1.0,
|
66 |
+
canny_scale: float = 0.5,
|
67 |
):
|
68 |
run_task_time = 0
|
69 |
time_cost_str = ''
|
70 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
71 |
+
canny_image = canny_detector(input_image, int(generate_size*0.375), generate_size)
|
72 |
+
inpaint_image = make_inpaint_condition(input_image, mask_image)
|
73 |
+
control_image = [inpaint_image, canny_image]
|
74 |
|
75 |
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
76 |
generated_image = basepipeline(
|
|
|
82 |
control_image=control_image,
|
83 |
guidance_scale=guidance_scale,
|
84 |
num_inference_steps=num_steps,
|
85 |
+
controlnet_conditioning_scale=[inpaint_scale, canny_scale]
|
86 |
).images[0]
|
87 |
|
88 |
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
|
|
117 |
with gr.Column():
|
118 |
edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
|
119 |
generate_size = gr.Number(label="Generate Size", value=512)
|
|
|
|
|
120 |
with gr.Column():
|
121 |
num_steps = gr.Slider(minimum=1, maximum=100, value=15, step=1, label="Num Steps")
|
122 |
guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
|
|
|
123 |
with gr.Column():
|
124 |
+
with gr.Accordion("Advanced Options", open=False):
|
125 |
+
inpaint_scale = gr.Slider(minimum=0, maximum=3, value=1, step=0.1, label="Inpaint Scale")
|
126 |
+
canny_scale = gr.Slider(minimum=0, maximum=3, value=0.8, step=0.1, label="Canny Scale")
|
127 |
+
mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
|
128 |
+
mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
|
129 |
+
seed = gr.Number(label="Seed", value=8)
|
130 |
+
category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
|
131 |
g_btn = gr.Button("Edit Image")
|
132 |
|
133 |
with gr.Row():
|
|
|
147 |
outputs=[origin_area_image, mask_image, croper],
|
148 |
).success(
|
149 |
fn=image_to_image,
|
150 |
+
inputs=[origin_area_image, mask_image, edit_prompt,seed, num_steps, guidance_scale, generate_size, inpaint_scale, canny_scale],
|
151 |
outputs=[generated_image, generated_cost],
|
152 |
).success(
|
153 |
fn=restore_result,
|