-
Notifications
You must be signed in to change notification settings - Fork 190
/
editany_demo.py
466 lines (454 loc) · 17.9 KB
/
editany_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# Edit Anything trained with Stable Diffusion + ControlNet + SAM + BLIP2
import gradio as gr
import numpy as np
import cv2
from cv2 import imencode
import base64
def create_demo_template(
process,
process_image_click=None,
examples=None,
INFO="EditAnything https://github.com/sail-sg/EditAnything",
WARNING_INFO=None,
enable_auto_prompt_default=False,
):
print("The GUI is not fully tested yet. Please open an issue if you find bugs.")
block = gr.Blocks()
with block as demo:
clicked_points = gr.State([])
origin_image = gr.State(None)
click_mask = gr.State(None)
ref_clicked_points = gr.State([])
ref_origin_image = gr.State(None)
ref_click_mask = gr.State(None)
with gr.Row():
gr.Markdown(INFO)
with gr.Row(equal_height=False):
with gr.Column():
with gr.Tab("Click🖱"):
source_image_click = gr.Image(
type="pil",
interactive=True,
label="Image: Upload an image and click the region you want to edit.",
)
with gr.Column():
with gr.Row():
point_prompt = gr.Radio(
choices=["Foreground Point",
"Background Point"],
value="Foreground Point",
label="Point Label",
interactive=True,
show_label=False,
)
with gr.Row():
clear_button_click = gr.Button(
value="Clear Points", interactive=True
)
clear_button_image = gr.Button(
value="Reset Image", interactive=True
)
with gr.Row():
run_button_click = gr.Button(
label="Run EditAnying", interactive=True
)
with gr.Tab("Brush🖌️"):
source_image_brush = gr.Image(
source="upload",
label="Image: Upload an image and cover the region you want to edit with sketch",
type="numpy",
tool="sketch",
brush_color="#00FFBF"
)
run_button = gr.Button(
label="Run EditAnying", interactive=True)
with gr.Tab("All region"):
source_image_clean = gr.Image(
source="upload",
label="Image: Upload an image",
type="numpy",
)
run_button_allregion = gr.Button(
label="Run EditAnying", interactive=True)
with gr.Row():
# enable_all_generate = gr.Checkbox(
# label="All Region Generation", value=False
# )
control_scale = gr.Slider(
label="SAM Mask Alignment Strength",
# info="Large value -> strict alignment with SAM mask",
minimum=0,
maximum=1,
value=0.5,
step=0.1,
)
with gr.Row():
num_samples = gr.Slider(
label="Images", minimum=1, maximum=12, value=2, step=1
)
seed = gr.Slider(
label="Seed",
minimum=-1,
maximum=2147483647,
step=1,
randomize=True,
)
with gr.Column():
with gr.Row():
enable_auto_prompt = gr.Checkbox(
label="Prompt Auto Generation (Enable this may makes your prompt not working)",
# info="",
value=enable_auto_prompt_default,
)
with gr.Row():
a_prompt = gr.Textbox(
label="Positive Prompt",
info="Text in the expected things of edited region",
value="best quality, extremely detailed,",
)
n_prompt = gr.Textbox(
label="Negative Prompt",
value="longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, NSFW",
)
with gr.Row():
enable_tile = gr.Checkbox(
label="High-resolution Refinement",
info="Slow inference",
value=True,
)
refine_alignment_ratio = gr.Slider(
label="Similarity with Initial Results",
# info="Large value -> strict alignment with input image. Small value -> strong global consistency",
minimum=0.0,
maximum=1.0,
value=0.95,
step=0.05,
)
with gr.Accordion("Cross-image Drag Options", open=False):
# ref_image = gr.Image(
# source='upload', label="Upload a reference image", type="pil", value=None)
ref_image = gr.Image(
source="upload",
label="Upload a reference image and cover the region you want to use with sketch",
type="pil",
tool="sketch",
brush_color="#00FFBF",
)
with gr.Row():
ref_auto_prompt = gr.Checkbox(
label="Ref. Auto Prompt", value=True
)
ref_prompt = gr.Textbox(
label="Prompt",
info="Text in the prompt of edited region",
value="best quality, extremely detailed, ",
)
# ref_image = gr.Image(
# type="pil", interactive=True,
# label="Image: Upload an image and click the region you want to use as reference.",
# )
# with gr.Column():
# with gr.Row():
# ref_point_prompt = gr.Radio(
# choices=["Foreground Point", "Background Point"],
# value="Foreground Point",
# label="Point Label",
# interactive=True, show_label=False)
# ref_clear_button_click = gr.Button(
# value="Clear Click Points", interactive=True)
# ref_clear_button_image = gr.Button(
# value="Clear Image", interactive=True)
with gr.Row():
reference_attn = gr.Checkbox(
label="reference_attn", value=True)
reference_adain = gr.Checkbox(
label="reference_adain", value=True
)
with gr.Row():
ref_sam_scale = gr.Slider(
label="Pos Control Scale",
minimum=0,
maximum=1.0,
value=0.3,
step=0.1,
)
ref_inpaint_scale = gr.Slider(
label="Content Control Scale",
minimum=0,
maximum=1.0,
value=0.2,
step=0.1,
)
with gr.Row():
ref_textinv = gr.Checkbox(
label="Use textual inversion token", value=False
)
ref_textinv_path = gr.Textbox(
label="textual inversion token path",
info="Text in the inversion token path",
value=None,
)
with gr.Accordion("Advanced options", open=False):
style_fidelity = gr.Slider(
label="Style fidelity",
minimum=0,
maximum=1.,
value=0.,
step=0.1,
)
attention_auto_machine_weight = gr.Slider(
label="Attention Reference Weight",
minimum=0,
maximum=1.0,
value=1.0,
step=0.01,
)
gn_auto_machine_weight = gr.Slider(
label="GroupNorm Reference Weight",
minimum=0,
maximum=1.0,
value=1.0,
step=0.01,
)
ref_scale = gr.Slider(
label="Frequency Reference Guidance Scale",
minimum=0,
maximum=1.0,
value=0.0,
step=0.1,
)
with gr.Accordion("Advanced Options", open=False):
mask_image = gr.Image(
source="upload",
label="Upload a predefined mask of edit region: Switch to Brush mode when using this!",
type="numpy",
value=None,
)
image_resolution = gr.Slider(
label="Image Resolution",
minimum=256,
maximum=768,
value=512,
step=64,
)
refine_image_resolution = gr.Slider(
label="Image Resolution",
minimum=256,
maximum=8192,
value=1024,
step=64,
)
guess_mode = gr.Checkbox(label="Guess Mode", value=False)
detect_resolution = gr.Slider(
label="SAM Resolution",
minimum=128,
maximum=2048,
value=1024,
step=1,
)
ddim_steps = gr.Slider(
label="Steps", minimum=1, maximum=100, value=30, step=1
)
scale = gr.Slider(
label="Guidance Scale", minimum=0.1, maximum=30.0, value=9.0, step=0.1)
alpha_weight = gr.Slider(
label="Alpha weight", info="Alpha mixing with original image", minimum=0,
maximum=1, value=0.0, step=0.1)
use_scale_map = gr.Checkbox(
label='Use scale map', value=False)
eta = gr.Number(label="eta (DDIM)", value=0.0)
condition_model = gr.Textbox(
label="Condition model path",
info="Text in the Controlnet model path in hugglingface",
value="EditAnything",
)
with gr.Column():
result_gallery_refine = gr.Gallery(
label="Output High quality", show_label=True, elem_id="gallery", preview=False)
result_gallery_init = gr.Gallery(
label="Output Low quality", show_label=True, elem_id="gallery", height="auto")
result_gallery_ref = gr.Gallery(
label="Output Ref", show_label=False, elem_id="gallery", height="auto")
result_text = gr.Text(label="ALL Prompt Text")
ips = [
source_image_brush,
gr.State(False), # enable_all_generate
mask_image,
control_scale,
enable_auto_prompt,
a_prompt,
n_prompt,
num_samples,
image_resolution,
detect_resolution,
ddim_steps,
guess_mode,
scale,
seed,
eta,
enable_tile,
refine_alignment_ratio,
refine_image_resolution,
alpha_weight,
use_scale_map,
condition_model,
ref_image,
attention_auto_machine_weight,
gn_auto_machine_weight,
style_fidelity,
reference_attn,
reference_adain,
ref_prompt,
ref_sam_scale,
ref_inpaint_scale,
ref_auto_prompt,
ref_textinv,
ref_textinv_path,
ref_scale,
]
run_button.click(
fn=process,
inputs=ips,
outputs=[
result_gallery_refine,
result_gallery_init,
result_gallery_ref,
result_text,
],
)
ips_allregion = [
source_image_clean,
gr.State(True), # enable_all_generate
mask_image,
control_scale,
enable_auto_prompt,
a_prompt,
n_prompt,
num_samples,
image_resolution,
detect_resolution,
ddim_steps,
guess_mode,
scale,
seed,
eta,
enable_tile,
refine_alignment_ratio,
refine_image_resolution,
alpha_weight,
use_scale_map,
condition_model,
ref_image,
attention_auto_machine_weight,
gn_auto_machine_weight,
style_fidelity,
reference_attn,
reference_adain,
ref_prompt,
ref_sam_scale,
ref_inpaint_scale,
ref_auto_prompt,
ref_textinv,
ref_textinv_path,
ref_scale,
]
run_button_allregion.click(
fn=process,
inputs=ips_allregion,
outputs=[
result_gallery_refine,
result_gallery_init,
result_gallery_ref,
result_text,
],
)
ip_click = [
origin_image,
gr.State(False), # enable_all_generate
click_mask,
control_scale,
enable_auto_prompt,
a_prompt,
n_prompt,
num_samples,
image_resolution,
detect_resolution,
ddim_steps,
guess_mode,
scale,
seed,
eta,
enable_tile,
refine_alignment_ratio,
refine_image_resolution,
alpha_weight,
use_scale_map,
condition_model,
ref_image,
attention_auto_machine_weight,
gn_auto_machine_weight,
style_fidelity,
reference_attn,
reference_adain,
ref_prompt,
ref_sam_scale,
ref_inpaint_scale,
ref_auto_prompt,
ref_textinv,
ref_textinv_path,
ref_scale,
]
run_button_click.click(
fn=process,
inputs=ip_click,
outputs=[
result_gallery_refine,
result_gallery_init,
result_gallery_ref,
result_text,
],
)
source_image_click.upload(
lambda image: image.copy() if image is not None else None,
inputs=[source_image_click],
outputs=[origin_image],
)
source_image_click.select(
process_image_click,
inputs=[origin_image, point_prompt,
clicked_points, image_resolution],
outputs=[source_image_click, clicked_points, click_mask],
show_progress=True,
queue=True,
)
clear_button_click.click(
fn=lambda original_image: (original_image.copy(), [], None)
if original_image is not None
else (None, [], None),
inputs=[origin_image],
outputs=[source_image_click, clicked_points, click_mask],
)
clear_button_image.click(
fn=lambda: (None, [], None, None, None),
inputs=[],
outputs=[
source_image_click,
clicked_points,
click_mask,
result_gallery_init,
result_text,
],
)
if examples is not None:
with gr.Row():
ex = gr.Examples(
examples=examples,
fn=process,
inputs=[a_prompt, n_prompt, scale],
outputs=[result_gallery_init],
cache_examples=False,
)
if WARNING_INFO is not None:
with gr.Row():
gr.Markdown(WARNING_INFO)
return demo