Spaces:
Sleeping
Sleeping
zhiweili
commited on
Commit
•
61a236c
1
Parent(s):
10feba2
add app_diffedit
Browse files- app.py +6 -3
- app_diffedit.py +98 -0
app.py
CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
|
|
2 |
|
3 |
# from app_base import create_demo as create_demo_face
|
4 |
# from app_haircolor import create_demo as create_demo_haircolor
|
5 |
-
from app_makeup import create_demo as create_demo_makeup
|
|
|
6 |
|
7 |
with gr.Blocks(css="style.css") as demo:
|
8 |
with gr.Tabs():
|
@@ -10,7 +11,9 @@ with gr.Blocks(css="style.css") as demo:
|
|
10 |
# create_demo_face()
|
11 |
# with gr.Tab(label="Hair Color"):
|
12 |
# create_demo_haircolor()
|
13 |
-
with gr.Tab(label="Face Makeup"):
|
14 |
-
|
|
|
|
|
15 |
|
16 |
demo.launch()
|
|
|
2 |
|
3 |
# from app_base import create_demo as create_demo_face
|
4 |
# from app_haircolor import create_demo as create_demo_haircolor
|
5 |
+
# from app_makeup import create_demo as create_demo_makeup
|
6 |
+
from app_diffedit import create_demo as create_demo_diffedit
|
7 |
|
8 |
with gr.Blocks(css="style.css") as demo:
|
9 |
with gr.Tabs():
|
|
|
11 |
# create_demo_face()
|
12 |
# with gr.Tab(label="Hair Color"):
|
13 |
# create_demo_haircolor()
|
14 |
+
# with gr.Tab(label="Face Makeup"):
|
15 |
+
# create_demo_makeup()
|
16 |
+
with gr.Tab(label="Diff Edit"):
|
17 |
+
create_demo_diffedit()
|
18 |
|
19 |
demo.launch()
|
app_diffedit.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import spaces
|
3 |
+
import time
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
from diffusers import (
|
8 |
+
DDIMScheduler,
|
9 |
+
DDIMInverseScheduler,
|
10 |
+
StableDiffusionDiffEditPipeline,
|
11 |
+
)
|
12 |
+
|
13 |
+
DEFAULT_SRC_PROMPT = "a woman"
|
14 |
+
DEFAULT_EDIT_PROMPT = "a woman, with red lips, 8k, high quality"
|
15 |
+
|
16 |
+
basepipeline = StableDiffusionDiffEditPipeline.from_pretrained(
|
17 |
+
"stabilityai/stable-diffusion-2-1",
|
18 |
+
torch_dtype=torch.float16,
|
19 |
+
safety_checker=None,
|
20 |
+
use_safetensors=True,
|
21 |
+
)
|
22 |
+
basepipeline.scheduler = DDIMScheduler.from_config(basepipeline.scheduler.config)
|
23 |
+
basepipeline.inverse_scheduler = DDIMInverseScheduler.from_config(basepipeline.scheduler.config)
|
24 |
+
basepipeline.enable_model_cpu_offload()
|
25 |
+
basepipeline.enable_vae_slicing()
|
26 |
+
|
27 |
+
@spaces.GPU(duration=30)
|
28 |
+
def image_to_image(
|
29 |
+
input_image: Image,
|
30 |
+
source_prompt: str,
|
31 |
+
target_prompt: str,
|
32 |
+
):
|
33 |
+
run_task_time = 0
|
34 |
+
time_cost_str = ''
|
35 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
36 |
+
input_image = input_image.resize((768, 768), Image.LANCZOS)
|
37 |
+
|
38 |
+
mask_image = basepipeline.generate_mask(
|
39 |
+
image=input_image,
|
40 |
+
source_prompt=source_prompt,
|
41 |
+
target_prompt=target_prompt,
|
42 |
+
)
|
43 |
+
|
44 |
+
inv_latents = basepipeline.invert(prompt=source_prompt, image=input_image).latents
|
45 |
+
|
46 |
+
output_image = basepipeline(
|
47 |
+
prompt=target_prompt,
|
48 |
+
mask_image=mask_image,
|
49 |
+
image_latents=inv_latents,
|
50 |
+
negative_prompt=source_prompt,
|
51 |
+
).images[0]
|
52 |
+
mask_image = Image.fromarray((mask_image.squeeze()*255).astype("uint8"), "L")
|
53 |
+
|
54 |
+
|
55 |
+
run_task_time, time_cost_str = get_time_cost(run_task_time, time_cost_str)
|
56 |
+
|
57 |
+
return output_image, mask_image, time_cost_str
|
58 |
+
|
59 |
+
def get_time_cost(run_task_time, time_cost_str):
|
60 |
+
now_time = int(time.time()*1000)
|
61 |
+
if run_task_time == 0:
|
62 |
+
time_cost_str = 'start'
|
63 |
+
else:
|
64 |
+
if time_cost_str != '':
|
65 |
+
time_cost_str += f'-->'
|
66 |
+
time_cost_str += f'{now_time - run_task_time}'
|
67 |
+
run_task_time = now_time
|
68 |
+
return run_task_time, time_cost_str
|
69 |
+
|
70 |
+
def create_demo() -> gr.Blocks:
|
71 |
+
with gr.Blocks() as demo:
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
input_image_prompt = gr.Textbox(lines=1, label="Input Image Prompt", value=DEFAULT_SRC_PROMPT)
|
75 |
+
edit_prompt = gr.Textbox(lines=1, label="Edit Prompt", value=DEFAULT_EDIT_PROMPT)
|
76 |
+
with gr.Column():
|
77 |
+
num_steps = gr.Slider(minimum=1, maximum=100, value=20, step=1, label="Num Steps")
|
78 |
+
start_step = gr.Slider(minimum=1, maximum=100, value=15, step=1, label="Start Step")
|
79 |
+
with gr.Column():
|
80 |
+
seed = gr.Number(label="Seed", value=8)
|
81 |
+
g_btn = gr.Button("Edit Image")
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
with gr.Column():
|
85 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
86 |
+
with gr.Column():
|
87 |
+
output_image = gr.Image(label="Output Image", type="pil", interactive=False)
|
88 |
+
with gr.Column():
|
89 |
+
mask_image = gr.Image(label="Mask Image", type="pil", interactive=False)
|
90 |
+
generated_cost = gr.Textbox(label="Time cost by step (ms):", visible=True, interactive=False)
|
91 |
+
|
92 |
+
g_btn.click(
|
93 |
+
fn=image_to_image,
|
94 |
+
inputs=[input_image_prompt, input_image_prompt, edit_prompt],
|
95 |
+
outputs=[output_image, mask_image, generated_cost],
|
96 |
+
)
|
97 |
+
|
98 |
+
return demo
|