Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -254,63 +254,195 @@ footer { visibility: hidden; }
|
|
254 |
|
255 |
title = """<h1 align="center">Diffusers Image Outpaint Lightning</h1>
|
256 |
"""
|
|
|
257 |
with gr.Blocks(css=css) as demo:
|
258 |
-
gr.
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
|
255 |
title = """<h1 align="center">Diffusers Image Outpaint Lightning</h1>
|
256 |
"""
|
257 |
+
|
258 |
with gr.Blocks(css=css) as demo:
|
259 |
+
with gr.Column():
|
260 |
+
gr.HTML(title)
|
261 |
+
|
262 |
+
with gr.Row():
|
263 |
+
with gr.Column():
|
264 |
+
input_image = gr.Image(
|
265 |
+
type="pil",
|
266 |
+
label="Input Image"
|
267 |
+
)
|
268 |
+
|
269 |
+
with gr.Row():
|
270 |
+
with gr.Column(scale=2):
|
271 |
+
prompt_input = gr.Textbox(label="Prompt (Optional)")
|
272 |
+
with gr.Column(scale=1):
|
273 |
+
run_button = gr.Button("Generate")
|
274 |
+
|
275 |
+
with gr.Row():
|
276 |
+
target_ratio = gr.Radio(
|
277 |
+
label="Expected Ratio",
|
278 |
+
choices=["9:16", "16:9", "1:1", "Custom"],
|
279 |
+
value="9:16",
|
280 |
+
scale=2
|
281 |
+
)
|
282 |
+
|
283 |
+
alignment_dropdown = gr.Dropdown(
|
284 |
+
choices=["Middle", "Left", "Right", "Top", "Bottom"],
|
285 |
+
value="Middle",
|
286 |
+
label="Alignment"
|
287 |
+
)
|
288 |
+
|
289 |
+
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
290 |
+
with gr.Column():
|
291 |
+
with gr.Row():
|
292 |
+
width_slider = gr.Slider(
|
293 |
+
label="Target Width",
|
294 |
+
minimum=720,
|
295 |
+
maximum=1536,
|
296 |
+
step=8,
|
297 |
+
value=720, # Set a default value
|
298 |
+
)
|
299 |
+
height_slider = gr.Slider(
|
300 |
+
label="Target Height",
|
301 |
+
minimum=720,
|
302 |
+
maximum=1536,
|
303 |
+
step=8,
|
304 |
+
value=1280, # Set a default value
|
305 |
+
)
|
306 |
+
|
307 |
+
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
308 |
+
with gr.Group():
|
309 |
+
overlap_percentage = gr.Slider(
|
310 |
+
label="Mask overlap (%)",
|
311 |
+
minimum=1,
|
312 |
+
maximum=50,
|
313 |
+
value=10,
|
314 |
+
step=1
|
315 |
+
)
|
316 |
+
with gr.Row():
|
317 |
+
overlap_top = gr.Checkbox(label="Overlap Top", value=True)
|
318 |
+
overlap_right = gr.Checkbox(label="Overlap Right", value=True)
|
319 |
+
with gr.Row():
|
320 |
+
overlap_left = gr.Checkbox(label="Overlap Left", value=True)
|
321 |
+
overlap_bottom = gr.Checkbox(label="Overlap Bottom", value=True)
|
322 |
+
with gr.Row():
|
323 |
+
resize_option = gr.Radio(
|
324 |
+
label="Resize input image",
|
325 |
+
choices=["Full", "50%", "33%", "25%", "Custom"],
|
326 |
+
value="Full"
|
327 |
+
)
|
328 |
+
custom_resize_percentage = gr.Slider(
|
329 |
+
label="Custom resize (%)",
|
330 |
+
minimum=1,
|
331 |
+
maximum=100,
|
332 |
+
step=1,
|
333 |
+
value=50,
|
334 |
+
visible=False
|
335 |
+
)
|
336 |
+
|
337 |
+
with gr.Column():
|
338 |
+
preview_button = gr.Button("Preview alignment and mask")
|
339 |
+
|
340 |
+
|
341 |
+
gr.Examples(
|
342 |
+
examples=[
|
343 |
+
["./examples/example_1.webp", 1280, 720, "Middle"],
|
344 |
+
["./examples/example_2.jpg", 1440, 810, "Left"],
|
345 |
+
["./examples/example_3.jpg", 1024, 1024, "Top"],
|
346 |
+
["./examples/example_3.jpg", 1024, 1024, "Bottom"],
|
347 |
+
],
|
348 |
+
inputs=[input_image, width_slider, height_slider, alignment_dropdown],
|
349 |
+
)
|
350 |
+
|
351 |
+
|
352 |
+
|
353 |
+
with gr.Column():
|
354 |
+
result = ImageSlider(label="Generated Image", interactive=False, type="pil", slider_color="pink")
|
355 |
+
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
356 |
+
|
357 |
+
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
358 |
+
preview_image = gr.Image(label="Preview")
|
359 |
+
|
360 |
+
|
361 |
+
|
362 |
+
def use_output_as_input(output_image):
|
363 |
+
"""Sets the generated output as the new input image."""
|
364 |
+
return gr.update(value=output_image[1])
|
365 |
+
|
366 |
+
use_as_input_button.click(
|
367 |
+
fn=use_output_as_input,
|
368 |
+
inputs=[result],
|
369 |
+
outputs=[input_image]
|
370 |
+
)
|
371 |
+
|
372 |
+
target_ratio.change(
|
373 |
+
fn=preload_presets,
|
374 |
+
inputs=[target_ratio, width_slider, height_slider],
|
375 |
+
outputs=[width_slider, height_slider, settings_panel],
|
376 |
+
queue=False
|
377 |
+
)
|
378 |
+
|
379 |
+
width_slider.change(
|
380 |
+
fn=select_the_right_preset,
|
381 |
+
inputs=[width_slider, height_slider],
|
382 |
+
outputs=[target_ratio],
|
383 |
+
queue=False
|
384 |
+
)
|
385 |
+
|
386 |
+
height_slider.change(
|
387 |
+
fn=select_the_right_preset,
|
388 |
+
inputs=[width_slider, height_slider],
|
389 |
+
outputs=[target_ratio],
|
390 |
+
queue=False
|
391 |
+
)
|
392 |
+
|
393 |
+
resize_option.change(
|
394 |
+
fn=toggle_custom_resize_slider,
|
395 |
+
inputs=[resize_option],
|
396 |
+
outputs=[custom_resize_percentage],
|
397 |
+
queue=False
|
398 |
+
)
|
399 |
+
|
400 |
+
run_button.click( # Clear the result
|
401 |
+
fn=clear_result,
|
402 |
+
inputs=None,
|
403 |
+
outputs=result,
|
404 |
+
).then( # Generate the new image
|
405 |
+
fn=infer,
|
406 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
407 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
408 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
409 |
+
outputs=result,
|
410 |
+
).then( # Update the history gallery
|
411 |
+
fn=lambda x, history: update_history(x[1], history),
|
412 |
+
inputs=[result, history_gallery],
|
413 |
+
outputs=history_gallery,
|
414 |
+
).then( # Show the "Use as Input Image" button
|
415 |
+
fn=lambda: gr.update(visible=True),
|
416 |
+
inputs=None,
|
417 |
+
outputs=use_as_input_button,
|
418 |
+
)
|
419 |
+
|
420 |
+
prompt_input.submit( # Clear the result
|
421 |
+
fn=clear_result,
|
422 |
+
inputs=None,
|
423 |
+
outputs=result,
|
424 |
+
).then( # Generate the new image
|
425 |
+
fn=infer,
|
426 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
427 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
428 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
429 |
+
outputs=result,
|
430 |
+
).then( # Update the history gallery
|
431 |
+
fn=lambda x, history: update_history(x[1], history),
|
432 |
+
inputs=[result, history_gallery],
|
433 |
+
outputs=history_gallery,
|
434 |
+
).then( # Show the "Use as Input Image" button
|
435 |
+
fn=lambda: gr.update(visible=True),
|
436 |
+
inputs=None,
|
437 |
+
outputs=use_as_input_button,
|
438 |
+
)
|
439 |
+
|
440 |
+
preview_button.click(
|
441 |
+
fn=preview_image_and_mask,
|
442 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, resize_option, custom_resize_percentage, alignment_dropdown,
|
443 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
444 |
+
outputs=preview_image,
|
445 |
+
queue=False
|
446 |
+
)
|
447 |
+
|
448 |
+
demo.queue(max_size=20).launch(share=False, show_error=True)
|