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