Spaces:
Running
on
Zero
Running
on
Zero
Add resize options
#14
by
multimodalart
HF Staff
- opened
app.py
CHANGED
|
@@ -52,10 +52,8 @@ def can_expand(source_width, source_height, target_width, target_height, alignme
|
|
| 52 |
return False
|
| 53 |
return True
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def infer(image, width, height, overlap_width, num_inference_steps, prompt_input=None, alignment="Middle"):
|
| 58 |
-
|
| 59 |
source = image
|
| 60 |
target_size = (width, height)
|
| 61 |
overlap = overlap_width
|
|
@@ -72,6 +70,22 @@ def infer(image, width, height, overlap_width, num_inference_steps, prompt_input
|
|
| 72 |
new_width = int(source.width * scale_factor)
|
| 73 |
new_height = int(source.height * scale_factor)
|
| 74 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
| 77 |
alignment = "Middle"
|
|
@@ -153,12 +167,10 @@ def infer(image, width, height, overlap_width, num_inference_steps, prompt_input
|
|
| 153 |
|
| 154 |
yield background, cnet_image
|
| 155 |
|
| 156 |
-
|
| 157 |
def clear_result():
|
| 158 |
"""Clears the result ImageSlider."""
|
| 159 |
return gr.update(value=None)
|
| 160 |
|
| 161 |
-
|
| 162 |
def preload_presets(target_ratio, ui_width, ui_height):
|
| 163 |
"""Updates the width and height sliders based on the selected aspect ratio."""
|
| 164 |
if target_ratio == "9:16":
|
|
@@ -169,6 +181,10 @@ def preload_presets(target_ratio, ui_width, ui_height):
|
|
| 169 |
changed_width = 1280
|
| 170 |
changed_height = 720
|
| 171 |
return changed_width, changed_height, gr.update(open=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
elif target_ratio == "Custom":
|
| 173 |
return ui_width, ui_height, gr.update(open=True)
|
| 174 |
|
|
@@ -177,9 +193,14 @@ def select_the_right_preset(user_width, user_height):
|
|
| 177 |
return "9:16"
|
| 178 |
elif user_width == 1280 and user_height == 720:
|
| 179 |
return "16:9"
|
|
|
|
|
|
|
| 180 |
else:
|
| 181 |
return "Custom"
|
| 182 |
|
|
|
|
|
|
|
|
|
|
| 183 |
css = """
|
| 184 |
.gradio-container {
|
| 185 |
width: 1200px !important;
|
|
@@ -218,7 +239,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 218 |
with gr.Row():
|
| 219 |
target_ratio = gr.Radio(
|
| 220 |
label="Expected Ratio",
|
| 221 |
-
choices=["9:16", "16:9", "Custom"],
|
| 222 |
value="9:16",
|
| 223 |
scale=2
|
| 224 |
)
|
|
@@ -255,6 +276,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 255 |
value=42,
|
| 256 |
step=1
|
| 257 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
gr.Examples(
|
| 260 |
examples=[
|
|
@@ -291,17 +326,24 @@ with gr.Blocks(css=css) as demo:
|
|
| 291 |
)
|
| 292 |
|
| 293 |
width_slider.change(
|
| 294 |
-
fn
|
| 295 |
-
inputs
|
| 296 |
-
outputs
|
| 297 |
-
queue
|
| 298 |
)
|
| 299 |
|
| 300 |
height_slider.change(
|
| 301 |
-
fn
|
| 302 |
-
inputs
|
| 303 |
-
outputs
|
| 304 |
-
queue
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 305 |
)
|
| 306 |
|
| 307 |
run_button.click(
|
|
@@ -311,7 +353,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 311 |
).then(
|
| 312 |
fn=infer,
|
| 313 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
| 314 |
-
prompt_input, alignment_dropdown],
|
| 315 |
outputs=result,
|
| 316 |
).then(
|
| 317 |
fn=lambda: gr.update(visible=True),
|
|
@@ -326,7 +368,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 326 |
).then(
|
| 327 |
fn=infer,
|
| 328 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
| 329 |
-
prompt_input, alignment_dropdown],
|
| 330 |
outputs=result,
|
| 331 |
).then(
|
| 332 |
fn=lambda: gr.update(visible=True),
|
|
|
|
| 52 |
return False
|
| 53 |
return True
|
| 54 |
|
| 55 |
+
@spaces.GPU(duration=24)
|
| 56 |
+
def infer(image, width, height, overlap_width, num_inference_steps, resize_option, custom_resize_size, prompt_input=None, alignment="Middle"):
|
|
|
|
|
|
|
| 57 |
source = image
|
| 58 |
target_size = (width, height)
|
| 59 |
overlap = overlap_width
|
|
|
|
| 70 |
new_width = int(source.width * scale_factor)
|
| 71 |
new_height = int(source.height * scale_factor)
|
| 72 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 73 |
+
|
| 74 |
+
if resize_option == "Full":
|
| 75 |
+
resize_size = max(source.width, source.height)
|
| 76 |
+
elif resize_option == "1/2":
|
| 77 |
+
resize_size = max(source.width, source.height) // 2
|
| 78 |
+
elif resize_option == "1/3":
|
| 79 |
+
resize_size = max(source.width, source.height) // 3
|
| 80 |
+
elif resize_option == "1/4":
|
| 81 |
+
resize_size = max(source.width, source.height) // 4
|
| 82 |
+
else: # Custom
|
| 83 |
+
resize_size = custom_resize_size
|
| 84 |
+
|
| 85 |
+
aspect_ratio = source.height / source.width
|
| 86 |
+
new_width = resize_size
|
| 87 |
+
new_height = int(resize_size * aspect_ratio)
|
| 88 |
+
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 89 |
|
| 90 |
if not can_expand(source.width, source.height, target_size[0], target_size[1], alignment):
|
| 91 |
alignment = "Middle"
|
|
|
|
| 167 |
|
| 168 |
yield background, cnet_image
|
| 169 |
|
|
|
|
| 170 |
def clear_result():
|
| 171 |
"""Clears the result ImageSlider."""
|
| 172 |
return gr.update(value=None)
|
| 173 |
|
|
|
|
| 174 |
def preload_presets(target_ratio, ui_width, ui_height):
|
| 175 |
"""Updates the width and height sliders based on the selected aspect ratio."""
|
| 176 |
if target_ratio == "9:16":
|
|
|
|
| 181 |
changed_width = 1280
|
| 182 |
changed_height = 720
|
| 183 |
return changed_width, changed_height, gr.update(open=False)
|
| 184 |
+
elif target_ratio == "1:1":
|
| 185 |
+
changed_width = 1024
|
| 186 |
+
changed_height = 1024
|
| 187 |
+
return changed_width, changed_height, gr.update(open=False)
|
| 188 |
elif target_ratio == "Custom":
|
| 189 |
return ui_width, ui_height, gr.update(open=True)
|
| 190 |
|
|
|
|
| 193 |
return "9:16"
|
| 194 |
elif user_width == 1280 and user_height == 720:
|
| 195 |
return "16:9"
|
| 196 |
+
elif user_width == 1024 and user_height == 1024:
|
| 197 |
+
return "1:1"
|
| 198 |
else:
|
| 199 |
return "Custom"
|
| 200 |
|
| 201 |
+
def toggle_custom_resize_slider(resize_option):
|
| 202 |
+
return gr.update(visible=(resize_option == "Custom"))
|
| 203 |
+
|
| 204 |
css = """
|
| 205 |
.gradio-container {
|
| 206 |
width: 1200px !important;
|
|
|
|
| 239 |
with gr.Row():
|
| 240 |
target_ratio = gr.Radio(
|
| 241 |
label="Expected Ratio",
|
| 242 |
+
choices=["9:16", "16:9", "1:1", "Custom"],
|
| 243 |
value="9:16",
|
| 244 |
scale=2
|
| 245 |
)
|
|
|
|
| 276 |
value=42,
|
| 277 |
step=1
|
| 278 |
)
|
| 279 |
+
with gr.Row():
|
| 280 |
+
resize_option = gr.Radio(
|
| 281 |
+
label="Resize input image",
|
| 282 |
+
choices=["Full", "1/2", "1/3", "1/4", "Custom"],
|
| 283 |
+
value="Full"
|
| 284 |
+
)
|
| 285 |
+
custom_resize_size = gr.Slider(
|
| 286 |
+
label="Custom resize size",
|
| 287 |
+
minimum=64,
|
| 288 |
+
maximum=1024,
|
| 289 |
+
step=8,
|
| 290 |
+
value=512,
|
| 291 |
+
visible=False
|
| 292 |
+
)
|
| 293 |
|
| 294 |
gr.Examples(
|
| 295 |
examples=[
|
|
|
|
| 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_size],
|
| 346 |
+
queue=False
|
| 347 |
)
|
| 348 |
|
| 349 |
run_button.click(
|
|
|
|
| 353 |
).then(
|
| 354 |
fn=infer,
|
| 355 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
| 356 |
+
resize_option, custom_resize_size, prompt_input, alignment_dropdown],
|
| 357 |
outputs=result,
|
| 358 |
).then(
|
| 359 |
fn=lambda: gr.update(visible=True),
|
|
|
|
| 368 |
).then(
|
| 369 |
fn=infer,
|
| 370 |
inputs=[input_image, width_slider, height_slider, overlap_width, num_inference_steps,
|
| 371 |
+
resize_option, custom_resize_size, prompt_input, alignment_dropdown],
|
| 372 |
outputs=result,
|
| 373 |
).then(
|
| 374 |
fn=lambda: gr.update(visible=True),
|