Spaces:
Paused
Paused
Update app.py (#4)
Browse files- Update app.py (01fee4fd5c0f61382e1729dd6799110f96a48a05)
Co-authored-by: Aditya Deshmukh <[email protected]>
app.py
CHANGED
|
@@ -231,236 +231,61 @@ def predict(
|
|
| 231 |
return dict_out, dict_res
|
| 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 |
if task == "text-guided":
|
| 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 |
-
return predict(input_image, prompt, fitting_degree, ddim_steps, scale, seed, negative_prompt, task, None, None)
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
def select_tab_text_guided():
|
| 294 |
-
return "text-guided"
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
def select_tab_object_removal():
|
| 298 |
-
return "object-removal"
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
def select_tab_context_aware():
|
| 302 |
-
return "context-aware"
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
def select_tab_image_outpainting():
|
| 306 |
-
return "image-outpainting"
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
def select_tab_shape_guided():
|
| 310 |
-
return "shape-guided"
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
with gr.Blocks(css="style.css") as demo:
|
| 314 |
-
with gr.Row():
|
| 315 |
-
gr.Markdown(
|
| 316 |
-
"<div align='center'><font size='18'>PowerPaint: High-Quality Versatile Image Inpainting</font></div>" # noqa
|
| 317 |
-
)
|
| 318 |
-
with gr.Row():
|
| 319 |
-
gr.Markdown(
|
| 320 |
-
"<div align='center'><font size='5'><a href='https://powerpaint.github.io/'>Project Page</a>  " # noqa
|
| 321 |
-
"<a href='https://arxiv.org/abs/2312.03594/'>Paper</a>  "
|
| 322 |
-
"<a href='https://github.com/zhuang2002/PowerPaint'>Code</a> </font></div>" # noqa
|
| 323 |
-
)
|
| 324 |
-
with gr.Row():
|
| 325 |
-
gr.Markdown(
|
| 326 |
-
"**Note:** Due to network-related factors, the page may experience occasional bugs! If the inpainting results deviate significantly from expectations, consider toggling between task options to refresh the content." # noqa
|
| 327 |
-
)
|
| 328 |
-
# Attention: Due to network-related factors, the page may experience occasional bugs. If the inpainting results deviate significantly from expectations, consider toggling between task options to refresh the content.
|
| 329 |
-
with gr.Row():
|
| 330 |
-
with gr.Column():
|
| 331 |
-
gr.Markdown("### Input image and draw mask")
|
| 332 |
-
input_image = gr.Image(label="Input Image", type="pil")
|
| 333 |
-
|
| 334 |
-
task = gr.Radio(
|
| 335 |
-
["text-guided", "object-removal", "shape-guided", "image-outpainting"], show_label=False, visible=False
|
| 336 |
-
)
|
| 337 |
-
|
| 338 |
-
# Text-guided object inpainting
|
| 339 |
-
with gr.Tab("Text-guided object inpainting") as tab_text_guided:
|
| 340 |
-
enable_text_guided = gr.Checkbox(
|
| 341 |
-
label="Enable text-guided object inpainting", value=True, interactive=False
|
| 342 |
-
)
|
| 343 |
-
text_guided_prompt = gr.Textbox(label="Prompt")
|
| 344 |
-
text_guided_negative_prompt = gr.Textbox(label="negative_prompt")
|
| 345 |
-
tab_text_guided.select(fn=select_tab_text_guided, inputs=None, outputs=task)
|
| 346 |
-
|
| 347 |
-
# Object removal inpainting
|
| 348 |
-
with gr.Tab("Object removal inpainting") as tab_object_removal:
|
| 349 |
-
enable_object_removal = gr.Checkbox(
|
| 350 |
-
label="Enable object removal inpainting",
|
| 351 |
-
value=True,
|
| 352 |
-
info="The recommended configuration for the Guidance Scale is 10 or higher. \
|
| 353 |
-
If undesired objects appear in the masked area, \
|
| 354 |
-
you can address this by specifically increasing the Guidance Scale.",
|
| 355 |
-
interactive=False,
|
| 356 |
-
)
|
| 357 |
-
removal_prompt = gr.Textbox(label="Prompt")
|
| 358 |
-
removal_negative_prompt = gr.Textbox(label="negative_prompt")
|
| 359 |
-
context_prompt = removal_prompt
|
| 360 |
-
context_negative_prompt = removal_negative_prompt
|
| 361 |
-
tab_object_removal.select(fn=select_tab_object_removal, inputs=None, outputs=task)
|
| 362 |
-
|
| 363 |
-
# Object image outpainting
|
| 364 |
-
with gr.Tab("Image outpainting") as tab_image_outpainting:
|
| 365 |
-
enable_object_removal = gr.Checkbox(
|
| 366 |
-
label="Enable image outpainting",
|
| 367 |
-
value=True,
|
| 368 |
-
info="The recommended configuration for the Guidance Scale is 15 or higher. \
|
| 369 |
-
If unwanted random objects appear in the extended image region, \
|
| 370 |
-
you can enhance the cleanliness of the extension area by increasing the Guidance Scale.",
|
| 371 |
-
interactive=False,
|
| 372 |
-
)
|
| 373 |
-
outpaint_prompt = gr.Textbox(label="Outpainting_prompt")
|
| 374 |
-
outpaint_negative_prompt = gr.Textbox(label="Outpainting_negative_prompt")
|
| 375 |
-
left_expansion_ratio = gr.Slider(
|
| 376 |
-
label="left expansion ratio",
|
| 377 |
-
minimum=0,
|
| 378 |
-
maximum=4,
|
| 379 |
-
step=0.05,
|
| 380 |
-
value=1,
|
| 381 |
-
)
|
| 382 |
-
right_expansion_ratio = gr.Slider(
|
| 383 |
-
label="right expansion ratio",
|
| 384 |
-
minimum=0,
|
| 385 |
-
maximum=4,
|
| 386 |
-
step=0.05,
|
| 387 |
-
value=1,
|
| 388 |
-
)
|
| 389 |
-
top_expansion_ratio = gr.Slider(
|
| 390 |
-
label="top expansion ratio",
|
| 391 |
-
minimum=0,
|
| 392 |
-
maximum=4,
|
| 393 |
-
step=0.05,
|
| 394 |
-
value=1,
|
| 395 |
-
)
|
| 396 |
-
bottom_expansion_ratio = gr.Slider(
|
| 397 |
-
label="bottom expansion ratio",
|
| 398 |
-
minimum=0,
|
| 399 |
-
maximum=4,
|
| 400 |
-
step=0.05,
|
| 401 |
-
value=1,
|
| 402 |
-
)
|
| 403 |
-
tab_image_outpainting.select(fn=select_tab_image_outpainting, inputs=None, outputs=task)
|
| 404 |
-
|
| 405 |
-
# Shape-guided object inpainting
|
| 406 |
-
with gr.Tab("Shape-guided object inpainting") as tab_shape_guided:
|
| 407 |
-
enable_shape_guided = gr.Checkbox(
|
| 408 |
-
label="Enable shape-guided object inpainting", value=True, interactive=False
|
| 409 |
-
)
|
| 410 |
-
shape_guided_prompt = gr.Textbox(label="shape_guided_prompt")
|
| 411 |
-
shape_guided_negative_prompt = gr.Textbox(label="shape_guided_negative_prompt")
|
| 412 |
-
fitting_degree = gr.Slider(
|
| 413 |
-
label="fitting degree",
|
| 414 |
-
minimum=0.3,
|
| 415 |
-
maximum=1,
|
| 416 |
-
step=0.05,
|
| 417 |
-
value=1,
|
| 418 |
-
)
|
| 419 |
-
tab_shape_guided.select(fn=select_tab_shape_guided, inputs=None, outputs=task)
|
| 420 |
-
|
| 421 |
-
run_button = gr.Button(label="Run")
|
| 422 |
-
with gr.Accordion("Advanced options", open=False):
|
| 423 |
-
ddim_steps = gr.Slider(label="Steps", minimum=1, maximum=50, value=50, step=1)
|
| 424 |
-
scale = gr.Slider(label="Guidance Scale", minimum=0.1, maximum=45.0, value=12, step=0.1)
|
| 425 |
-
seed = gr.Slider(
|
| 426 |
-
label="Seed",
|
| 427 |
-
minimum=0,
|
| 428 |
-
maximum=2147483647,
|
| 429 |
-
step=1,
|
| 430 |
-
randomize=True,
|
| 431 |
-
)
|
| 432 |
-
with gr.Column():
|
| 433 |
-
gr.Markdown("### Inpainting result")
|
| 434 |
-
inpaint_result = gr.Gallery(label="Generated images", show_label=False, columns=2)
|
| 435 |
-
gr.Markdown("### Mask")
|
| 436 |
-
gallery = gr.Gallery(label="Generated masks", show_label=False, columns=2)
|
| 437 |
-
|
| 438 |
-
run_button.click(
|
| 439 |
-
fn=infer,
|
| 440 |
-
inputs=[
|
| 441 |
-
input_image,
|
| 442 |
-
text_guided_prompt,
|
| 443 |
-
text_guided_negative_prompt,
|
| 444 |
-
shape_guided_prompt,
|
| 445 |
-
shape_guided_negative_prompt,
|
| 446 |
-
fitting_degree,
|
| 447 |
-
ddim_steps,
|
| 448 |
-
scale,
|
| 449 |
-
seed,
|
| 450 |
-
task,
|
| 451 |
-
left_expansion_ratio,
|
| 452 |
-
right_expansion_ratio,
|
| 453 |
-
top_expansion_ratio,
|
| 454 |
-
bottom_expansion_ratio,
|
| 455 |
-
outpaint_prompt,
|
| 456 |
-
outpaint_negative_prompt,
|
| 457 |
-
removal_prompt,
|
| 458 |
-
removal_negative_prompt,
|
| 459 |
-
context_prompt,
|
| 460 |
-
context_negative_prompt,
|
| 461 |
-
],
|
| 462 |
-
outputs=[inpaint_result, gallery],
|
| 463 |
-
)
|
| 464 |
|
| 465 |
demo.queue(concurrency_count=1, max_size=1, api_open=True)
|
| 466 |
-
demo.launch(show_api=True
|
|
|
|
| 231 |
return dict_out, dict_res
|
| 232 |
|
| 233 |
|
| 234 |
+
|
| 235 |
+
import gradio as gr
|
| 236 |
+
|
| 237 |
+
def custom_infer(input_image_path,
|
| 238 |
+
input_mask_path=None,
|
| 239 |
+
prompt="",
|
| 240 |
+
fitting_degree=0.5,
|
| 241 |
+
ddim_steps=20,
|
| 242 |
+
scale=5,
|
| 243 |
+
seed=143,
|
| 244 |
+
negative_prompt="",
|
| 245 |
+
task="text-guided",
|
| 246 |
+
left_expansion_ratio=0.2,
|
| 247 |
+
right_expansion_ratio=0.2,
|
| 248 |
+
top_expansion_ratio=0.2,
|
| 249 |
+
bottom_expansion_ratio=0.2):
|
| 250 |
+
|
| 251 |
+
image = Image.open(input_image_path)
|
| 252 |
+
if input_mask_path:
|
| 253 |
+
mask = Image.open(input_mask_path)
|
| 254 |
+
|
|
|
|
| 255 |
if task == "text-guided":
|
| 256 |
+
input_dict = {"image": image, "mask": mask}
|
| 257 |
+
a, b = predict(input_dict, prompt, fitting_degree, ddim_steps, scale, seed, negative_prompt, task, None, None, None, None)
|
| 258 |
+
|
| 259 |
+
if task == "image-outpainting":
|
| 260 |
+
input_dict = {"image": image}
|
| 261 |
+
a, b = predict(input_dict, prompt, fitting_degree, ddim_steps, scale, seed, negative_prompt, task, left_expansion_ratio, right_expansion_ratio, top_expansion_ratio, bottom_expansion_ratio)
|
| 262 |
+
|
| 263 |
+
return a, b
|
| 264 |
+
|
| 265 |
+
# Create the Gradio interface
|
| 266 |
+
inputs = [
|
| 267 |
+
gr.inputs.Image(label="Input Image", type="filepath"),
|
| 268 |
+
gr.inputs.Image(label="Input Mask (optional)", type="filepath"),
|
| 269 |
+
gr.inputs.Textbox(label="Prompt", default="A beautiful landscape"),
|
| 270 |
+
gr.inputs.Slider(label="Fitting Degree", minimum=1, maximum=20, value=7, step=1),
|
| 271 |
+
gr.inputs.Slider(label="DDIM Steps", minimum=10, maximum=100, value=20, step=1),
|
| 272 |
+
gr.inputs.Slider(label="Scale", minimum=1, maximum=20, value=7.5, step=0.1),
|
| 273 |
+
gr.inputs.Slider(label="Use Seed", minimum=0, maximum=1300000,value=143,step=1),
|
| 274 |
+
gr.inputs.Textbox(label="Negative Prompt", default="blur, low quality"),
|
| 275 |
+
gr.inputs.Radio(label="Task", choices=["text-guided", "image-outpainting"], value="image-outpainting"),
|
| 276 |
+
gr.inputs.Slider(label="Left Expansion Ratio", minimum=0, maximum=2, value=0.2, step=0.01),
|
| 277 |
+
gr.inputs.Slider(label="Right Expansion Ratio", minimum=0, maximum=2, value=0.2, step=0.01),
|
| 278 |
+
gr.inputs.Slider(label="Top Expansion Ratio", minimum=0, maximum=2, value=0.2, step=0.01),
|
| 279 |
+
gr.inputs.Slider(label="Bottom Expansion Ratio", minimum=0, maximum=2, value=0.2, step=0.01)
|
| 280 |
+
]
|
| 281 |
+
|
| 282 |
+
outputs = [
|
| 283 |
+
gr.outputs.Image(label="Output Image"),
|
| 284 |
+
gr.outputs.Textbox(label="Output Text")
|
| 285 |
+
]
|
| 286 |
+
|
| 287 |
+
# Create the Gradio interface
|
| 288 |
+
demo = gr.Interface(fn=custom_infer, inputs=inputs, outputs=outputs, title="Custom Infer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
demo.queue(concurrency_count=1, max_size=1, api_open=True)
|
| 291 |
+
demo.launch(show_api=True)
|