Spaces:
Running
on
Zero
Running
on
Zero
zhiweili
commited on
Commit
•
ca18077
1
Parent(s):
049f1dd
test p2p
Browse files- app.py +1 -1
- app_haircolor_img2img.py +160 -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_img2img import create_demo as create_demo_haircolor
|
4 |
|
5 |
with gr.Blocks(css="style.css") as demo:
|
6 |
with gr.Tabs():
|
app_haircolor_img2img.py
ADDED
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
10 |
+
restore_result,
|
11 |
+
)
|
12 |
+
from diffusers import (
|
13 |
+
StableDiffusionControlNetImg2ImgPipeline,
|
14 |
+
ControlNetModel,
|
15 |
+
DDIMScheduler,
|
16 |
+
DPMSolverMultistepScheduler,
|
17 |
+
EulerAncestralDiscreteScheduler,
|
18 |
+
UniPCMultistepScheduler,
|
19 |
+
)
|
20 |
+
|
21 |
+
from controlnet_aux import (
|
22 |
+
CannyDetector,
|
23 |
+
LineartDetector,
|
24 |
+
PidiNetDetector,
|
25 |
+
HEDdetector,
|
26 |
+
)
|
27 |
+
|
28 |
+
BASE_MODEL = "stable-diffusion-v1-5/stable-diffusion-v1-5"
|
29 |
+
|
30 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
31 |
+
|
32 |
+
DEFAULT_EDIT_PROMPT = "change to blue hair"
|
33 |
+
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"
|
34 |
+
|
35 |
+
DEFAULT_CATEGORY = "hair"
|
36 |
+
|
37 |
+
canny_detector = CannyDetector()
|
38 |
+
lineart_detector = LineartDetector.from_pretrained("lllyasviel/Annotators")
|
39 |
+
lineart_detector = lineart_detector.to(DEVICE)
|
40 |
+
|
41 |
+
pidiNet_detector = PidiNetDetector.from_pretrained('lllyasviel/Annotators')
|
42 |
+
pidiNet_detector = pidiNet_detector.to(DEVICE)
|
43 |
+
|
44 |
+
hed_detector = HEDdetector.from_pretrained('lllyasviel/Annotators')
|
45 |
+
hed_detector = hed_detector.to(DEVICE)
|
46 |
+
|
47 |
+
controlnet = [
|
48 |
+
ControlNetModel.from_pretrained(
|
49 |
+
"lllyasviel/control_v11e_sd15_ip2p",
|
50 |
+
torch_dtype=torch.float16,
|
51 |
+
),
|
52 |
+
]
|
53 |
+
|
54 |
+
basepipeline = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
|
55 |
+
BASE_MODEL,
|
56 |
+
torch_dtype=torch.float16,
|
57 |
+
use_safetensors=True,
|
58 |
+
controlnet=controlnet,
|
59 |
+
)
|
60 |
+
|
61 |
+
basepipeline.scheduler = UniPCMultistepScheduler.from_config(basepipeline.scheduler.config)
|
62 |
+
|
63 |
+
basepipeline = basepipeline.to(DEVICE)
|
64 |
+
|
65 |
+
basepipeline.enable_model_cpu_offload()
|
66 |
+
|
67 |
+
@spaces.GPU(duration=30)
|
68 |
+
def image_to_image(
|
69 |
+
input_image: Image,
|
70 |
+
edit_prompt: str,
|
71 |
+
seed: int,
|
72 |
+
num_steps: int,
|
73 |
+
guidance_scale: float,
|
74 |
+
generate_size: int,
|
75 |
+
):
|
76 |
+
run_task_time = 0
|
77 |
+
time_cost_str = ''
|
78 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
79 |
+
|
80 |
+
# generator = torch.Generator(device=DEVICE).manual_seed(seed)
|
81 |
+
generated_image = basepipeline(
|
82 |
+
seeds=[seed],
|
83 |
+
# generator=generator,
|
84 |
+
prompt=edit_prompt,
|
85 |
+
negative_prompt=DEFAULT_NEGATIVE_PROMPT,
|
86 |
+
image=input_image,
|
87 |
+
height=generate_size,
|
88 |
+
width=generate_size,
|
89 |
+
guidance_scale=guidance_scale,
|
90 |
+
num_inference_steps=num_steps,
|
91 |
+
).images[0]
|
92 |
+
|
93 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
94 |
+
|
95 |
+
return generated_image, time_cost_str
|
96 |
+
|
97 |
+
def make_inpaint_condition(image, image_mask):
|
98 |
+
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
|
99 |
+
image_mask = np.array(image_mask.convert("L")).astype(np.float32) / 255.0
|
100 |
+
|
101 |
+
assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
|
102 |
+
image[image_mask > 0.5] = -1.0 # set as masked pixel
|
103 |
+
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
|
104 |
+
image = torch.from_numpy(image)
|
105 |
+
return image
|
106 |
+
|
107 |
+
def get_time_cost(run_task_time, time_cost_str):
|
108 |
+
now_time = int(time.time()*1000)
|
109 |
+
if run_task_time == 0:
|
110 |
+
time_cost_str = 'start'
|
111 |
+
else:
|
112 |
+
if time_cost_str != '':
|
113 |
+
time_cost_str += f'-->'
|
114 |
+
time_cost_str += f'{now_time - run_task_time}'
|
115 |
+
run_task_time = now_time
|
116 |
+
return run_task_time, time_cost_str
|
117 |
+
|
118 |
+
def create_demo() -> gr.Blocks:
|
119 |
+
with gr.Blocks() as demo:
|
120 |
+
croper = gr.State()
|
121 |
+
with gr.Row():
|
122 |
+
with gr.Column():
|
123 |
+
edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
|
124 |
+
generate_size = gr.Number(label="Generate Size", value=512)
|
125 |
+
with gr.Column():
|
126 |
+
num_steps = gr.Slider(minimum=1, maximum=100, value=25, step=1, label="Num Steps")
|
127 |
+
guidance_scale = gr.Slider(minimum=0, maximum=30, value=5, step=0.5, label="Guidance Scale")
|
128 |
+
with gr.Column():
|
129 |
+
with gr.Accordion("Advanced Options", open=False):
|
130 |
+
mask_expansion = gr.Number(label="Mask Expansion", value=50, visible=True)
|
131 |
+
mask_dilation = gr.Slider(minimum=0, maximum=10, value=2, step=1, label="Mask Dilation")
|
132 |
+
seed = gr.Number(label="Seed", value=8)
|
133 |
+
category = gr.Textbox(label="Category", value=DEFAULT_CATEGORY, visible=False)
|
134 |
+
g_btn = gr.Button("Edit Image")
|
135 |
+
|
136 |
+
with gr.Row():
|
137 |
+
with gr.Column():
|
138 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
139 |
+
with gr.Column():
|
140 |
+
restored_image = gr.Image(label="Restored Image", type="pil", interactive=False)
|
141 |
+
with gr.Column():
|
142 |
+
origin_area_image = gr.Image(label="Origin Area Image", type="pil", interactive=False)
|
143 |
+
generated_image = gr.Image(label="Generated Image", type="pil", interactive=False)
|
144 |
+
generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
145 |
+
|
146 |
+
g_btn.click(
|
147 |
+
fn=segment_image,
|
148 |
+
inputs=[input_image, category, generate_size, mask_expansion, mask_dilation],
|
149 |
+
outputs=[origin_area_image, croper],
|
150 |
+
).success(
|
151 |
+
fn=image_to_image,
|
152 |
+
inputs=[origin_area_image, edit_prompt,seed, num_steps, guidance_scale, generate_size],
|
153 |
+
outputs=[generated_image, generated_cost],
|
154 |
+
).success(
|
155 |
+
fn=restore_result,
|
156 |
+
inputs=[croper, category, generated_image],
|
157 |
+
outputs=[restored_image],
|
158 |
+
)
|
159 |
+
|
160 |
+
return demo
|