prithivMLmods commited on
Commit
108256c
·
verified ·
1 Parent(s): 04cce22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -59
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.HTML(title)
259
- with gr.Row():
260
- with gr.Column():
261
- input_image = gr.Image(type="pil", label="Input Image")
262
- prompt_input = gr.Textbox(label="Prompt (Optional)")
263
- run_button = gr.Button("Generate")
264
-
265
- target_ratio = gr.Radio(["9:16","16:9","1:1","Custom"], value="9:16", label="Expected Ratio")
266
- alignment_dropdown = gr.Dropdown(["Middle","Left","Right","Top","Bottom"], value="Middle", label="Alignment")
267
-
268
- with gr.Accordion("Advanced settings", open=False) as adv:
269
- width_slider = gr.Slider(720,1536,step=8, value=720, label="Target Width")
270
- height_slider = gr.Slider(720,1536,step=8, value=1280, label="Target Height")
271
- num_steps = gr.Slider(4,12,step=1, value=8, label="Steps")
272
- overlap_pct = gr.Slider(1,50,step=1, value=10, label="Mask overlap (%)")
273
- overlap_top = gr.Checkbox(label="Overlap Top", value=True)
274
- overlap_right = gr.Checkbox(label="Overlap Right", value=True)
275
- overlap_left = gr.Checkbox(label="Overlap Left", value=True)
276
- overlap_bottom= gr.Checkbox(label="Overlap Bottom", value=True)
277
- resize_opt = gr.Radio(["Full","50%","33%","25%","Custom"], value="Full", label="Resize input image")
278
- custom_resize = gr.Slider(1,100,step=1, value=50, visible=False, label="Custom resize (%)")
279
- preview_btn = gr.Button("Preview alignment and mask")
280
-
281
- gr.Examples(
282
- examples=[
283
- ["./examples/example_1.webp",1280,720,"Middle"],
284
- ["./examples/example_2.jpg",1440,810,"Left"],
285
- ["./examples/example_3.jpg",1024,1024,"Top"],
286
- ["./examples/example_3.jpg",1024,1024,"Bottom"]
287
- ],
288
- inputs=[input_image,width_slider,height_slider,alignment_dropdown]
289
- )
290
-
291
- with gr.Column():
292
- result = ImageSlider(label="Comparison", interactive=False, type="pil", slider_color="pink")
293
- history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain")
294
- preview_image = gr.Image(label="Preview")
295
-
296
- # Callbacks
297
- run_button.click(clear_result, None, result)
298
- run_button.click(
299
- infer,
300
- inputs=[ input_image, width_slider, height_slider, overlap_pct, num_steps,
301
- resize_opt, custom_resize, prompt_input, alignment_dropdown,
302
- overlap_left, overlap_right, overlap_top, overlap_bottom],
303
- outputs=result
304
- ).then(update_history, inputs=[result, history_gallery], outputs=history_gallery)
305
-
306
- target_ratio.change(preload_presets, [target_ratio, width_slider, height_slider], [width_slider, height_slider, adv])
307
- width_slider.change(select_the_right_preset, [width_slider, height_slider], target_ratio)
308
- height_slider.change(select_the_right_preset, [width_slider, height_slider], target_ratio)
309
- resize_opt.change(toggle_custom_resize_slider, resize_opt, custom_resize)
310
- preview_btn.click(preview_image_and_mask,
311
- [input_image, width_slider, height_slider, overlap_pct, resize_opt, custom_resize, alignment_dropdown,
312
- overlap_left, overlap_right, overlap_top, overlap_bottom],
313
- preview_image)
314
-
315
-
316
- demo.queue(max_size=20).launch(share=False, ssr_mode=False, show_error=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)