Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -43,14 +43,6 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
43 |
|
44 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
45 |
|
46 |
-
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
47 |
-
"""Checks if the image can be expanded based on the alignment."""
|
48 |
-
if alignment in ("Left", "Right") and source_width >= target_width:
|
49 |
-
return False
|
50 |
-
if alignment in ("Top", "Bottom") and source_height >= target_height:
|
51 |
-
return False
|
52 |
-
return True
|
53 |
-
|
54 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
55 |
target_size = (width, height)
|
56 |
|
@@ -148,31 +140,10 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
148 |
|
149 |
return background, mask
|
150 |
|
151 |
-
def preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
152 |
-
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
153 |
-
|
154 |
-
# Create a preview image showing the mask
|
155 |
-
preview = background.copy().convert('RGBA')
|
156 |
-
|
157 |
-
# Create a semi-transparent red overlay
|
158 |
-
red_overlay = Image.new('RGBA', background.size, (255, 0, 0, 64)) # Reduced alpha to 64 (25% opacity)
|
159 |
-
|
160 |
-
# Convert black pixels in the mask to semi-transparent red
|
161 |
-
red_mask = Image.new('RGBA', background.size, (0, 0, 0, 0))
|
162 |
-
red_mask.paste(red_overlay, (0, 0), mask)
|
163 |
-
|
164 |
-
# Overlay the red mask on the background
|
165 |
-
preview = Image.alpha_composite(preview, red_mask)
|
166 |
-
|
167 |
-
return preview
|
168 |
-
|
169 |
@spaces.GPU(duration=24)
|
170 |
def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
171 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
172 |
|
173 |
-
if not can_expand(background.width, background.height, width, height, alignment):
|
174 |
-
alignment = "Middle"
|
175 |
-
|
176 |
cnet_image = background.copy()
|
177 |
cnet_image.paste(0, (0, 0), mask)
|
178 |
|
@@ -332,9 +303,6 @@ with gr.Blocks(css=css) as demo:
|
|
332 |
value=50,
|
333 |
visible=False
|
334 |
)
|
335 |
-
|
336 |
-
with gr.Column():
|
337 |
-
preview_button = gr.Button("Preview alignment and mask")
|
338 |
|
339 |
gr.Examples(
|
340 |
examples=[
|
@@ -348,7 +316,66 @@ with gr.Blocks(css=css) as demo:
|
|
348 |
|
349 |
with gr.Column():
|
350 |
result = gr.Image(label="Generated Image", type="pil")
|
351 |
-
|
352 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
demo.queue(max_size=20).launch(share=False, ssr_mode=False, show_error=True)
|
|
|
43 |
|
44 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
47 |
target_size = (width, height)
|
48 |
|
|
|
140 |
|
141 |
return background, mask
|
142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
@spaces.GPU(duration=24)
|
144 |
def infer(image, width, height, overlap_percentage, num_inference_steps, resize_option, custom_resize_percentage, prompt_input, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
145 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
146 |
|
|
|
|
|
|
|
147 |
cnet_image = background.copy()
|
148 |
cnet_image.paste(0, (0, 0), mask)
|
149 |
|
|
|
303 |
value=50,
|
304 |
visible=False
|
305 |
)
|
|
|
|
|
|
|
306 |
|
307 |
gr.Examples(
|
308 |
examples=[
|
|
|
316 |
|
317 |
with gr.Column():
|
318 |
result = gr.Image(label="Generated Image", type="pil")
|
|
|
319 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
320 |
|
321 |
+
target_ratio.change(
|
322 |
+
fn=preload_presets,
|
323 |
+
inputs=[target_ratio, width_slider, height_slider],
|
324 |
+
outputs=[width_slider, height_slider, settings_panel],
|
325 |
+
queue=False
|
326 |
+
)
|
327 |
+
|
328 |
+
width_slider.change(
|
329 |
+
fn=select_the_right_preset,
|
330 |
+
inputs=[width_slider, height_slider],
|
331 |
+
outputs=[target_ratio],
|
332 |
+
queue=False
|
333 |
+
)
|
334 |
+
|
335 |
+
height_slider.change(
|
336 |
+
fn=select_the_right_preset,
|
337 |
+
inputs=[width_slider, height_slider],
|
338 |
+
outputs=[target_ratio],
|
339 |
+
queue=False
|
340 |
+
)
|
341 |
+
|
342 |
+
resize_option.change(
|
343 |
+
fn=toggle_custom_resize_slider,
|
344 |
+
inputs=[resize_option],
|
345 |
+
outputs=[custom_resize_percentage],
|
346 |
+
queue=False
|
347 |
+
)
|
348 |
+
|
349 |
+
run_button.click(
|
350 |
+
fn=clear_result,
|
351 |
+
inputs=None,
|
352 |
+
outputs=result,
|
353 |
+
).then(
|
354 |
+
fn=infer,
|
355 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
356 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
357 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
358 |
+
outputs=result,
|
359 |
+
).then(
|
360 |
+
fn=lambda x, history: update_history(x, history),
|
361 |
+
inputs=[result, history_gallery],
|
362 |
+
outputs=history_gallery,
|
363 |
+
)
|
364 |
+
|
365 |
+
prompt_input.submit(
|
366 |
+
fn=clear_result,
|
367 |
+
inputs=None,
|
368 |
+
outputs=result,
|
369 |
+
).then(
|
370 |
+
fn=infer,
|
371 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
372 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
373 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
374 |
+
outputs=result,
|
375 |
+
).then(
|
376 |
+
fn=lambda x, history: update_history(x, history),
|
377 |
+
inputs=[result, history_gallery],
|
378 |
+
outputs=history_gallery,
|
379 |
+
)
|
380 |
+
|
381 |
demo.queue(max_size=20).launch(share=False, ssr_mode=False, show_error=True)
|