Spaces:
Running
on
Zero
Running
on
Zero
zhiweili
commited on
Commit
•
80b4f1b
1
Parent(s):
8cd0a6f
change to inpaint_15
Browse files- app.py +1 -1
- app_haircolor_inpaint_15.py +138 -0
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
from
|
4 |
|
5 |
with gr.Blocks(css="style.css") as demo:
|
6 |
with gr.Tabs():
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
from app_haircolor_inpaint_15 import create_demo as create_demo_haircolor
|
4 |
|
5 |
with gr.Blocks(css="style.css") as demo:
|
6 |
with gr.Tabs():
|
app_haircolor_inpaint_15.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import spaces
|
2 |
+
import gradio as gr
|
3 |
+
import time
|
4 |
+
import torch
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
from PIL import Image
|
8 |
+
from segment_utils import(
|
9 |
+
segment_image_withmask,
|
10 |
+
restore_result,
|
11 |
+
)
|
12 |
+
from diffusers import (
|
13 |
+
StableDiffusionControlNetInpaintPipeline,
|
14 |
+
ControlNetModel,
|
15 |
+
DDIMScheduler,
|
16 |
+
)
|
17 |
+
|
18 |
+
BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-inpainting"
|
19 |
+
|
20 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
21 |
+
|
22 |
+
DEFAULT_EDIT_PROMPT = "a woman, blue hair, high detailed"
|
23 |
+
DEFAULT_NEGATIVE_PROMPT = "worst quality, normal quality, low quality, low res, blurry, text, watermark, logo, banner, extra digits, cropped, jpeg artifacts, signature, username, error, sketch ,duplicate, ugly, monochrome, horror, geometry, mutation, disgusting, poorly drawn face, bad face, fused face, ugly face, worst face, asymmetrical, unrealistic skin texture, bad proportions, out of frame, poorly drawn hands, cloned face, double face"
|
24 |
+
|
25 |
+
DEFAULT_CATEGORY = "hair"
|
26 |
+
|
27 |
+
controlnet = ControlNetModel.from_pretrained(
|
28 |
+
"lllyasviel/control_v11p_sd15_inpaint",
|
29 |
+
torch_dtype=torch.float16,
|
30 |
+
)
|
31 |
+
|
32 |
+
basepipeline = StableDiffusionControlNetInpaintPipeline.from_pretrained(
|
33 |
+
BASE_MODEL,
|
34 |
+
torch_dtype=torch.float16,
|
35 |
+
use_safetensors=True,
|
36 |
+
controlnet=controlnet,
|
37 |
+
)
|
38 |
+
basepipeline.scheduler = DDIMScheduler.from_config(basepipeline.scheduler.config)
|
39 |
+
|
40 |
+
basepipeline = basepipeline.to(DEVICE)
|
41 |
+
|
42 |
+
basepipeline.enable_model_cpu_offload()
|
43 |
+
|
44 |
+
@spaces.GPU(duration=30)
|
45 |
+
def image_to_image(
|
46 |
+
input_image: Image,
|
47 |
+
mask_image: Image,
|
48 |
+
edit_prompt: str,
|
49 |
+
seed: int,
|
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 |
+
control_image = make_inpaint_condition(input_image, mask_image)
|
58 |
+
|
59 |
+
generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
60 |
+
generated_image = basepipeline(
|
61 |
+
generator=generator,
|
62 |
+
prompt=edit_prompt,
|
63 |
+
negative_prompt=DEFAULT_NEGATIVE_PROMPT,
|
64 |
+
image=input_image,
|
65 |
+
mask_image=mask_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)
|
72 |
+
|
73 |
+
return generated_image, time_cost_str
|
74 |
+
|
75 |
+
def make_inpaint_condition(image, image_mask):
|
76 |
+
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
|
77 |
+
image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
|
78 |
+
|
79 |
+
assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
|
80 |
+
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
81 |
+
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
82 |
+
image = torch.from_numpy(image)
|
83 |
+
return image
|
84 |
+
|
85 |
+
def get_time_cost(run_task_time, time_cost_str):
|
86 |
+
now_time = int(time.time()*1000)
|
87 |
+
if run_task_time == 0:
|
88 |
+
time_cost_str = 'start'
|
89 |
+
else:
|
90 |
+
if time_cost_str != '':
|
91 |
+
time_cost_str += f'-->'
|
92 |
+
time_cost_str += f'{now_time - run_task_time}'
|
93 |
+
run_task_time = now_time
|
94 |
+
return run_task_time, time_cost_str
|
95 |
+
|
96 |
+
def create_demo() -> gr.Blocks:
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
croper = gr.State()
|
99 |
+
with gr.Row():
|
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 |
+
mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
|
111 |
+
g_btn = gr.Button("Edit Image")
|
112 |
+
|
113 |
+
with gr.Row():
|
114 |
+
with gr.Column():
|
115 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
116 |
+
with gr.Column():
|
117 |
+
restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
|
118 |
+
with gr.Column():
|
119 |
+
origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
|
120 |
+
generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
|
121 |
+
generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
122 |
+
mask_image = gr.Image(label="Mask Image", type="pil", interactive=False)
|
123 |
+
|
124 |
+
g_btn.click(
|
125 |
+
fn=segment_image_withmask,
|
126 |
+
inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
|
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,
|
134 |
+
inputs=[croper, category, generated_image],
|
135 |
+
outputs=[restored_image],
|
136 |
+
)
|
137 |
+
|
138 |
+
return demo
|