Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,13 +10,12 @@ from controlnet_union import ControlNetModel_Union
|
|
| 10 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
| 11 |
|
| 12 |
from PIL import Image, ImageDraw
|
| 13 |
-
import numpy as np
|
| 14 |
|
|
|
|
| 15 |
config_file = hf_hub_download(
|
| 16 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 17 |
filename="config_promax.json",
|
| 18 |
)
|
| 19 |
-
|
| 20 |
config = ControlNetModel_Union.load_config(config_file)
|
| 21 |
controlnet_model = ControlNetModel_Union.from_config(config)
|
| 22 |
|
|
@@ -25,15 +24,12 @@ model_file = hf_hub_download(
|
|
| 25 |
filename="diffusion_pytorch_model_promax.safetensors",
|
| 26 |
)
|
| 27 |
state_dict = load_state_dict(model_file)
|
| 28 |
-
|
| 29 |
loaded_keys = list(state_dict.keys())
|
| 30 |
|
| 31 |
result = ControlNetModel_Union._load_pretrained_model(
|
| 32 |
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0", loaded_keys
|
| 33 |
)
|
| 34 |
-
|
| 35 |
-
model = result[0]
|
| 36 |
-
model = model.to(device="cuda", dtype=torch.float16)
|
| 37 |
|
| 38 |
vae = AutoencoderKL.from_pretrained(
|
| 39 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
|
@@ -49,7 +45,7 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
| 49 |
|
| 50 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 51 |
|
| 52 |
-
|
| 53 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
| 54 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
| 55 |
return False
|
|
@@ -59,7 +55,6 @@ def can_expand(source_width, source_height, target_width, target_height, alignme
|
|
| 59 |
|
| 60 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
| 61 |
target_size = (width, height)
|
| 62 |
-
|
| 63 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
| 64 |
new_width = int(image.width * scale_factor)
|
| 65 |
new_height = int(image.height * scale_factor)
|
|
@@ -77,16 +72,12 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 77 |
resize_percentage = custom_resize_percentage
|
| 78 |
|
| 79 |
resize_factor = resize_percentage / 100
|
| 80 |
-
new_width = int(source.width * resize_factor)
|
| 81 |
-
new_height = int(source.height * resize_factor)
|
| 82 |
-
new_width = max(new_width, 64)
|
| 83 |
-
new_height = max(new_height, 64)
|
| 84 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 85 |
|
| 86 |
-
overlap_x = int(new_width * (overlap_percentage / 100))
|
| 87 |
-
overlap_y = int(new_height * (overlap_percentage / 100))
|
| 88 |
-
overlap_x = max(overlap_x, 1)
|
| 89 |
-
overlap_y = max(overlap_y, 1)
|
| 90 |
|
| 91 |
if alignment == "Middle":
|
| 92 |
margin_x = (target_size[0] - new_width) // 2
|
|
@@ -133,8 +124,7 @@ def preview_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 133 |
red_overlay = Image.new('RGBA', background.size, (255, 0, 0, 64))
|
| 134 |
red_mask = Image.new('RGBA', background.size, (0, 0, 0, 0))
|
| 135 |
red_mask.paste(red_overlay, (0, 0), mask)
|
| 136 |
-
|
| 137 |
-
return preview
|
| 138 |
|
| 139 |
@spaces.GPU(duration=24)
|
| 140 |
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):
|
|
@@ -167,7 +157,6 @@ def infer(image, width, height, overlap_percentage, num_inference_steps, resize_
|
|
| 167 |
|
| 168 |
image = image.convert("RGBA")
|
| 169 |
cnet_image.paste(image, (0, 0), mask)
|
| 170 |
-
|
| 171 |
yield background, cnet_image
|
| 172 |
|
| 173 |
def clear_result():
|
|
@@ -241,30 +230,12 @@ with gr.Blocks(theme="soft", css=css) as demo:
|
|
| 241 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
| 242 |
with gr.Column():
|
| 243 |
with gr.Row():
|
| 244 |
-
width_slider = gr.Slider(
|
| 245 |
-
|
| 246 |
-
minimum=720,
|
| 247 |
-
maximum=1536,
|
| 248 |
-
step=8,
|
| 249 |
-
value=720,
|
| 250 |
-
)
|
| 251 |
-
height_slider = gr.Slider(
|
| 252 |
-
label="Target Height",
|
| 253 |
-
minimum=720,
|
| 254 |
-
maximum=1536,
|
| 255 |
-
step=8,
|
| 256 |
-
value=1280,
|
| 257 |
-
)
|
| 258 |
|
| 259 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
| 260 |
with gr.Group():
|
| 261 |
-
overlap_percentage = gr.Slider(
|
| 262 |
-
label="Mask overlap (%)",
|
| 263 |
-
minimum=1,
|
| 264 |
-
maximum=50,
|
| 265 |
-
value=10,
|
| 266 |
-
step=1
|
| 267 |
-
)
|
| 268 |
with gr.Row():
|
| 269 |
overlap_top = gr.Checkbox(label="Overlap Top", value=True)
|
| 270 |
overlap_right = gr.Checkbox(label="Overlap Right", value=True)
|
|
@@ -272,20 +243,8 @@ with gr.Blocks(theme="soft", css=css) as demo:
|
|
| 272 |
overlap_left = gr.Checkbox(label="Overlap Left", value=True)
|
| 273 |
overlap_bottom = gr.Checkbox(label="Overlap Bottom", value=True)
|
| 274 |
with gr.Row():
|
| 275 |
-
resize_option = gr.Radio(
|
| 276 |
-
|
| 277 |
-
choices=["Full", "50%", "33%", "25%", "Custom"],
|
| 278 |
-
value="Full"
|
| 279 |
-
)
|
| 280 |
-
custom_resize_percentage = gr.Slider(
|
| 281 |
-
label="Custom resize (%)",
|
| 282 |
-
minimum=1,
|
| 283 |
-
maximum=100,
|
| 284 |
-
step=1,
|
| 285 |
-
value=50,
|
| 286 |
-
visible=False
|
| 287 |
-
)
|
| 288 |
-
|
| 289 |
with gr.Column():
|
| 290 |
preview_button = gr.Button("Preview alignment and mask")
|
| 291 |
|
|
@@ -299,90 +258,100 @@ with gr.Blocks(theme="soft", css=css) as demo:
|
|
| 299 |
)
|
| 300 |
|
| 301 |
with gr.Column():
|
| 302 |
-
result = ImageSlider(
|
| 303 |
-
interactive=False,
|
| 304 |
-
label="Generated Image",
|
| 305 |
-
)
|
| 306 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
| 307 |
-
|
| 308 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
| 309 |
preview_image = gr.Image(label="Preview")
|
| 310 |
|
|
|
|
| 311 |
def use_output_as_input(output_image):
|
| 312 |
return gr.update(value=output_image[1])
|
| 313 |
|
| 314 |
use_as_input_button.click(
|
| 315 |
fn=use_output_as_input,
|
| 316 |
inputs=[result],
|
| 317 |
-
outputs=[input_image]
|
|
|
|
| 318 |
)
|
| 319 |
|
| 320 |
target_ratio.change(
|
| 321 |
fn=preload_presets,
|
| 322 |
inputs=[target_ratio, width_slider, height_slider],
|
| 323 |
outputs=[width_slider, height_slider, settings_panel],
|
| 324 |
-
queue=False
|
|
|
|
| 325 |
)
|
| 326 |
|
| 327 |
width_slider.change(
|
| 328 |
fn=select_the_right_preset,
|
| 329 |
inputs=[width_slider, height_slider],
|
| 330 |
outputs=[target_ratio],
|
| 331 |
-
queue=False
|
|
|
|
| 332 |
)
|
| 333 |
|
| 334 |
height_slider.change(
|
| 335 |
fn=select_the_right_preset,
|
| 336 |
inputs=[width_slider, height_slider],
|
| 337 |
outputs=[target_ratio],
|
| 338 |
-
queue=False
|
|
|
|
| 339 |
)
|
| 340 |
|
| 341 |
resize_option.change(
|
| 342 |
fn=toggle_custom_resize_slider,
|
| 343 |
inputs=[resize_option],
|
| 344 |
outputs=[custom_resize_percentage],
|
| 345 |
-
queue=False
|
|
|
|
| 346 |
)
|
| 347 |
|
| 348 |
run_button.click(
|
| 349 |
fn=clear_result,
|
| 350 |
inputs=None,
|
| 351 |
outputs=result,
|
|
|
|
| 352 |
).then(
|
| 353 |
fn=infer,
|
| 354 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 355 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 356 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 357 |
outputs=result,
|
|
|
|
| 358 |
).then(
|
| 359 |
fn=lambda x, history: update_history(x[1], history) if x else history,
|
| 360 |
inputs=[result, history_gallery],
|
| 361 |
outputs=history_gallery,
|
|
|
|
| 362 |
).then(
|
| 363 |
fn=lambda: gr.update(visible=True),
|
| 364 |
inputs=None,
|
| 365 |
outputs=use_as_input_button,
|
|
|
|
| 366 |
)
|
| 367 |
|
| 368 |
prompt_input.submit(
|
| 369 |
fn=clear_result,
|
| 370 |
inputs=None,
|
| 371 |
outputs=result,
|
|
|
|
| 372 |
).then(
|
| 373 |
fn=infer,
|
| 374 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 375 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 376 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 377 |
outputs=result,
|
|
|
|
| 378 |
).then(
|
| 379 |
fn=lambda x, history: update_history(x[1], history) if x else history,
|
| 380 |
inputs=[result, history_gallery],
|
| 381 |
outputs=history_gallery,
|
|
|
|
| 382 |
).then(
|
| 383 |
fn=lambda: gr.update(visible=True),
|
| 384 |
inputs=None,
|
| 385 |
outputs=use_as_input_button,
|
|
|
|
| 386 |
)
|
| 387 |
|
| 388 |
preview_button.click(
|
|
@@ -390,7 +359,9 @@ with gr.Blocks(theme="soft", css=css) as demo:
|
|
| 390 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, resize_option, custom_resize_percentage, alignment_dropdown,
|
| 391 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 392 |
outputs=preview_image,
|
| 393 |
-
queue=False
|
|
|
|
| 394 |
)
|
| 395 |
|
| 396 |
-
|
|
|
|
|
|
| 10 |
from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
| 11 |
|
| 12 |
from PIL import Image, ImageDraw
|
|
|
|
| 13 |
|
| 14 |
+
# ===== Load models (same as your original) =====
|
| 15 |
config_file = hf_hub_download(
|
| 16 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 17 |
filename="config_promax.json",
|
| 18 |
)
|
|
|
|
| 19 |
config = ControlNetModel_Union.load_config(config_file)
|
| 20 |
controlnet_model = ControlNetModel_Union.from_config(config)
|
| 21 |
|
|
|
|
| 24 |
filename="diffusion_pytorch_model_promax.safetensors",
|
| 25 |
)
|
| 26 |
state_dict = load_state_dict(model_file)
|
|
|
|
| 27 |
loaded_keys = list(state_dict.keys())
|
| 28 |
|
| 29 |
result = ControlNetModel_Union._load_pretrained_model(
|
| 30 |
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0", loaded_keys
|
| 31 |
)
|
| 32 |
+
model = result[0].to(device="cuda", dtype=torch.float16)
|
|
|
|
|
|
|
| 33 |
|
| 34 |
vae = AutoencoderKL.from_pretrained(
|
| 35 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
|
|
|
| 45 |
|
| 46 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 47 |
|
| 48 |
+
# ===== Helpers (same as your original) =====
|
| 49 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
| 50 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
| 51 |
return False
|
|
|
|
| 55 |
|
| 56 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
| 57 |
target_size = (width, height)
|
|
|
|
| 58 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
| 59 |
new_width = int(image.width * scale_factor)
|
| 60 |
new_height = int(image.height * scale_factor)
|
|
|
|
| 72 |
resize_percentage = custom_resize_percentage
|
| 73 |
|
| 74 |
resize_factor = resize_percentage / 100
|
| 75 |
+
new_width = max(int(source.width * resize_factor), 64)
|
| 76 |
+
new_height = max(int(source.height * resize_factor), 64)
|
|
|
|
|
|
|
| 77 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 78 |
|
| 79 |
+
overlap_x = max(int(new_width * (overlap_percentage / 100)), 1)
|
| 80 |
+
overlap_y = max(int(new_height * (overlap_percentage / 100)), 1)
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if alignment == "Middle":
|
| 83 |
margin_x = (target_size[0] - new_width) // 2
|
|
|
|
| 124 |
red_overlay = Image.new('RGBA', background.size, (255, 0, 0, 64))
|
| 125 |
red_mask = Image.new('RGBA', background.size, (0, 0, 0, 0))
|
| 126 |
red_mask.paste(red_overlay, (0, 0), mask)
|
| 127 |
+
return Image.alpha_composite(preview, red_mask)
|
|
|
|
| 128 |
|
| 129 |
@spaces.GPU(duration=24)
|
| 130 |
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):
|
|
|
|
| 157 |
|
| 158 |
image = image.convert("RGBA")
|
| 159 |
cnet_image.paste(image, (0, 0), mask)
|
|
|
|
| 160 |
yield background, cnet_image
|
| 161 |
|
| 162 |
def clear_result():
|
|
|
|
| 230 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
| 231 |
with gr.Column():
|
| 232 |
with gr.Row():
|
| 233 |
+
width_slider = gr.Slider(label="Target Width", minimum=720, maximum=1536, step=8, value=720)
|
| 234 |
+
height_slider = gr.Slider(label="Target Height", minimum=720, maximum=1536, step=8, value=1280)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
| 237 |
with gr.Group():
|
| 238 |
+
overlap_percentage = gr.Slider(label="Mask overlap (%)", minimum=1, maximum=50, value=10, step=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
with gr.Row():
|
| 240 |
overlap_top = gr.Checkbox(label="Overlap Top", value=True)
|
| 241 |
overlap_right = gr.Checkbox(label="Overlap Right", value=True)
|
|
|
|
| 243 |
overlap_left = gr.Checkbox(label="Overlap Left", value=True)
|
| 244 |
overlap_bottom = gr.Checkbox(label="Overlap Bottom", value=True)
|
| 245 |
with gr.Row():
|
| 246 |
+
resize_option = gr.Radio(label="Resize input image", choices=["Full", "50%", "33%", "25%", "Custom"], value="Full")
|
| 247 |
+
custom_resize_percentage = gr.Slider(label="Custom resize (%)", minimum=1, maximum=100, step=1, value=50, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
with gr.Column():
|
| 249 |
preview_button = gr.Button("Preview alignment and mask")
|
| 250 |
|
|
|
|
| 258 |
)
|
| 259 |
|
| 260 |
with gr.Column():
|
| 261 |
+
result = ImageSlider(interactive=False, label="Generated Image")
|
|
|
|
|
|
|
|
|
|
| 262 |
use_as_input_button = gr.Button("Use as Input Image", visible=False)
|
|
|
|
| 263 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
| 264 |
preview_image = gr.Image(label="Preview")
|
| 265 |
|
| 266 |
+
# === Named API endpoints (critical for REST) ===
|
| 267 |
def use_output_as_input(output_image):
|
| 268 |
return gr.update(value=output_image[1])
|
| 269 |
|
| 270 |
use_as_input_button.click(
|
| 271 |
fn=use_output_as_input,
|
| 272 |
inputs=[result],
|
| 273 |
+
outputs=[input_image],
|
| 274 |
+
api_name="use_output_as_input"
|
| 275 |
)
|
| 276 |
|
| 277 |
target_ratio.change(
|
| 278 |
fn=preload_presets,
|
| 279 |
inputs=[target_ratio, width_slider, height_slider],
|
| 280 |
outputs=[width_slider, height_slider, settings_panel],
|
| 281 |
+
queue=False,
|
| 282 |
+
api_name="preload_presets"
|
| 283 |
)
|
| 284 |
|
| 285 |
width_slider.change(
|
| 286 |
fn=select_the_right_preset,
|
| 287 |
inputs=[width_slider, height_slider],
|
| 288 |
outputs=[target_ratio],
|
| 289 |
+
queue=False,
|
| 290 |
+
api_name="select_the_right_preset"
|
| 291 |
)
|
| 292 |
|
| 293 |
height_slider.change(
|
| 294 |
fn=select_the_right_preset,
|
| 295 |
inputs=[width_slider, height_slider],
|
| 296 |
outputs=[target_ratio],
|
| 297 |
+
queue=False,
|
| 298 |
+
api_name="select_the_right_preset_1"
|
| 299 |
)
|
| 300 |
|
| 301 |
resize_option.change(
|
| 302 |
fn=toggle_custom_resize_slider,
|
| 303 |
inputs=[resize_option],
|
| 304 |
outputs=[custom_resize_percentage],
|
| 305 |
+
queue=False,
|
| 306 |
+
api_name="toggle_custom_resize_slider"
|
| 307 |
)
|
| 308 |
|
| 309 |
run_button.click(
|
| 310 |
fn=clear_result,
|
| 311 |
inputs=None,
|
| 312 |
outputs=result,
|
| 313 |
+
api_name="clear_result"
|
| 314 |
).then(
|
| 315 |
fn=infer,
|
| 316 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 317 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 318 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 319 |
outputs=result,
|
| 320 |
+
api_name="infer"
|
| 321 |
).then(
|
| 322 |
fn=lambda x, history: update_history(x[1], history) if x else history,
|
| 323 |
inputs=[result, history_gallery],
|
| 324 |
outputs=history_gallery,
|
| 325 |
+
api_name="lambda"
|
| 326 |
).then(
|
| 327 |
fn=lambda: gr.update(visible=True),
|
| 328 |
inputs=None,
|
| 329 |
outputs=use_as_input_button,
|
| 330 |
+
api_name="lambda_1"
|
| 331 |
)
|
| 332 |
|
| 333 |
prompt_input.submit(
|
| 334 |
fn=clear_result,
|
| 335 |
inputs=None,
|
| 336 |
outputs=result,
|
| 337 |
+
api_name="clear_result_1"
|
| 338 |
).then(
|
| 339 |
fn=infer,
|
| 340 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 341 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 342 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 343 |
outputs=result,
|
| 344 |
+
api_name="infer_1"
|
| 345 |
).then(
|
| 346 |
fn=lambda x, history: update_history(x[1], history) if x else history,
|
| 347 |
inputs=[result, history_gallery],
|
| 348 |
outputs=history_gallery,
|
| 349 |
+
api_name="lambda_2"
|
| 350 |
).then(
|
| 351 |
fn=lambda: gr.update(visible=True),
|
| 352 |
inputs=None,
|
| 353 |
outputs=use_as_input_button,
|
| 354 |
+
api_name="lambda_3"
|
| 355 |
)
|
| 356 |
|
| 357 |
preview_button.click(
|
|
|
|
| 359 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, resize_option, custom_resize_percentage, alignment_dropdown,
|
| 360 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 361 |
outputs=preview_image,
|
| 362 |
+
queue=False,
|
| 363 |
+
api_name="preview_image_and_mask"
|
| 364 |
)
|
| 365 |
|
| 366 |
+
# IMPORTANT: open REST API
|
| 367 |
+
demo.queue(max_size=12, api_open=True).launch(share=False)
|