Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -44,7 +44,6 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
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:
|
@@ -52,36 +51,30 @@ def can_expand(source_width, source_height, target_width, target_height, alignme
|
|
52 |
return True
|
53 |
|
54 |
@spaces.GPU
|
55 |
-
def infer(image, width, height, overlap_width, num_inference_steps,
|
56 |
source = image
|
57 |
target_size = (width, height)
|
58 |
overlap = overlap_width
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
if source.width > target_size[0] or source.height > target_size[1]:
|
76 |
-
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
77 |
-
new_width = int(source.width * scale_factor)
|
78 |
-
new_height = int(source.height * scale_factor)
|
79 |
-
source = source.resize((new_width, new_height), Image.LANCZOS)
|
80 |
|
81 |
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
82 |
alignment = "Middle"
|
83 |
|
84 |
-
# Calculate margins based on alignment
|
85 |
if alignment == "Middle":
|
86 |
margin_x = (target_size[0] - source.width) // 2
|
87 |
margin_y = (target_size[1] - source.height) // 2
|
@@ -104,7 +97,6 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_size,
|
|
104 |
mask = Image.new('L', target_size, 255)
|
105 |
mask_draw = ImageDraw.Draw(mask)
|
106 |
|
107 |
-
# Adjust mask generation based on alignment
|
108 |
if alignment == "Middle":
|
109 |
mask_draw.rectangle([
|
110 |
(margin_x + overlap, margin_y + overlap),
|
@@ -159,11 +151,9 @@ def infer(image, width, height, overlap_width, num_inference_steps, resize_size,
|
|
159 |
yield background, cnet_image
|
160 |
|
161 |
def clear_result():
|
162 |
-
"""Clears the result ImageSlider."""
|
163 |
return gr.update(value=None)
|
164 |
|
165 |
def preload_presets(target_ratio, ui_width, ui_height):
|
166 |
-
"""Updates the width and height sliders based on the selected aspect ratio."""
|
167 |
if target_ratio == "9:16":
|
168 |
changed_width = 720
|
169 |
changed_height = 1280
|
@@ -183,6 +173,9 @@ def select_the_right_preset(user_width, user_height):
|
|
183 |
else:
|
184 |
return "Custom"
|
185 |
|
|
|
|
|
|
|
186 |
css = """
|
187 |
.gradio-container {
|
188 |
width: 1200px !important;
|
@@ -257,11 +250,20 @@ with gr.Blocks(css=css) as demo:
|
|
257 |
value=42,
|
258 |
step=1
|
259 |
)
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
|
266 |
gr.Examples(
|
267 |
examples=[
|
@@ -281,7 +283,6 @@ with gr.Blocks(css=css) as demo:
|
|
281 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
282 |
|
283 |
def use_output_as_input(output_image):
|
284 |
-
"""Sets the generated output as the new input image."""
|
285 |
return gr.update(value=output_image[1])
|
286 |
|
287 |
use_as_input_button.click(
|
@@ -310,6 +311,13 @@ with gr.Blocks(css=css) as demo:
|
|
310 |
outputs=[target_ratio],
|
311 |
queue=False
|
312 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
|
314 |
run_button.click(
|
315 |
fn=clear_result,
|
@@ -318,7 +326,7 @@ with gr.Blocks(css=css) as demo:
|
|
318 |
).then(
|
319 |
fn=infer,
|
320 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
321 |
-
|
322 |
outputs=result,
|
323 |
).then(
|
324 |
fn=lambda: gr.update(visible=True),
|
@@ -333,7 +341,7 @@ with gr.Blocks(css=css) as demo:
|
|
333 |
).then(
|
334 |
fn=infer,
|
335 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
336 |
-
|
337 |
outputs=result,
|
338 |
).then(
|
339 |
fn=lambda: gr.update(visible=True),
|
|
|
44 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
45 |
|
46 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
|
|
47 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
48 |
return False
|
49 |
if alignment in ("Top", "Bottom") and source_height >= target_height:
|
|
|
51 |
return True
|
52 |
|
53 |
@spaces.GPU
|
54 |
+
def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_size, prompt_input=None, alignment="Middle"):
|
55 |
source = image
|
56 |
target_size = (width, height)
|
57 |
overlap = overlap_width
|
58 |
|
59 |
+
if resize_option == "Full":
|
60 |
+
resize_size = max(source.width, source.height)
|
61 |
+
elif resize_option == "1/2":
|
62 |
+
resize_size = max(source.width, source.height) // 2
|
63 |
+
elif resize_option == "1/3":
|
64 |
+
resize_size = max(source.width, source.height) // 3
|
65 |
+
elif resize_option == "1/4":
|
66 |
+
resize_size = max(source.width, source.height) // 4
|
67 |
+
else: # Custom
|
68 |
+
resize_size = custom_resize_size
|
69 |
+
|
70 |
+
aspect_ratio = source.height / source.width
|
71 |
+
new_width = resize_size
|
72 |
+
new_height = int(resize_size * aspect_ratio)
|
73 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
76 |
alignment = "Middle"
|
77 |
|
|
|
78 |
if alignment == "Middle":
|
79 |
margin_x = (target_size[0] - source.width) // 2
|
80 |
margin_y = (target_size[1] - source.height) // 2
|
|
|
97 |
mask = Image.new('L', target_size, 255)
|
98 |
mask_draw = ImageDraw.Draw(mask)
|
99 |
|
|
|
100 |
if alignment == "Middle":
|
101 |
mask_draw.rectangle([
|
102 |
(margin_x + overlap, margin_y + overlap),
|
|
|
151 |
yield background, cnet_image
|
152 |
|
153 |
def clear_result():
|
|
|
154 |
return gr.update(value=None)
|
155 |
|
156 |
def preload_presets(target_ratio, ui_width, ui_height):
|
|
|
157 |
if target_ratio == "9:16":
|
158 |
changed_width = 720
|
159 |
changed_height = 1280
|
|
|
173 |
else:
|
174 |
return "Custom"
|
175 |
|
176 |
+
def toggle_custom_resize_slider(resize_option):
|
177 |
+
return gr.update(visible=(resize_option == "Custom"))
|
178 |
+
|
179 |
css = """
|
180 |
.gradio-container {
|
181 |
width: 1200px !important;
|
|
|
250 |
value=42,
|
251 |
step=1
|
252 |
)
|
253 |
+
with gr.Row():
|
254 |
+
resize_option = gr.Radio(
|
255 |
+
label="Resize input image",
|
256 |
+
choices=["Full", "1/2", "1/3", "1/4", "Custom"],
|
257 |
+
value="Full"
|
258 |
+
)
|
259 |
+
custom_resize_size = gr.Slider(
|
260 |
+
label="Custom resize size",
|
261 |
+
minimum=64,
|
262 |
+
maximum=1024,
|
263 |
+
step=8,
|
264 |
+
value=512,
|
265 |
+
visible=False
|
266 |
+
)
|
267 |
|
268 |
gr.Examples(
|
269 |
examples=[
|
|
|
283 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
284 |
|
285 |
def use_output_as_input(output_image):
|
|
|
286 |
return gr.update(value=output_image[1])
|
287 |
|
288 |
use_as_input_button.click(
|
|
|
311 |
outputs=[target_ratio],
|
312 |
queue=False
|
313 |
)
|
314 |
+
|
315 |
+
resize_option.change(
|
316 |
+
fn=toggle_custom_resize_slider,
|
317 |
+
inputs=[resize_option],
|
318 |
+
outputs=[custom_resize_size],
|
319 |
+
queue=False
|
320 |
+
)
|
321 |
|
322 |
run_button.click(
|
323 |
fn=clear_result,
|
|
|
326 |
).then(
|
327 |
fn=infer,
|
328 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
329 |
+
resize_option, custom_resize_size, prompt_input, alignment_dropdown],
|
330 |
outputs=result,
|
331 |
).then(
|
332 |
fn=lambda: gr.update(visible=True),
|
|
|
341 |
).then(
|
342 |
fn=infer,
|
343 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
344 |
+
resize_option, custom_resize_size, prompt_input, alignment_dropdown],
|
345 |
outputs=result,
|
346 |
).then(
|
347 |
fn=lambda: gr.update(visible=True),
|