Update app.py
Browse files
app.py
CHANGED
|
@@ -28,24 +28,27 @@ state_dict = load_state_dict(model_file)
|
|
| 28 |
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
|
| 29 |
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
|
| 30 |
)
|
| 31 |
-
|
|
|
|
| 32 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 33 |
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 34 |
|
| 35 |
-
# 修改
|
| 36 |
model.to(device=device, dtype=dtype)
|
| 37 |
|
|
|
|
| 38 |
vae = AutoencoderKL.from_pretrained(
|
| 39 |
-
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=
|
| 40 |
-
).to(
|
| 41 |
|
|
|
|
| 42 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 43 |
"SG161222/RealVisXL_V5.0_Lightning",
|
| 44 |
-
torch_dtype=
|
| 45 |
vae=vae,
|
| 46 |
controlnet=model,
|
| 47 |
-
variant="fp16",
|
| 48 |
-
).to(
|
| 49 |
|
| 50 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 51 |
|
|
@@ -190,35 +193,6 @@ def infer(
|
|
| 190 |
overlap_top,
|
| 191 |
overlap_bottom
|
| 192 |
):
|
| 193 |
-
"""
|
| 194 |
-
Generate an outpainted image using Stable Diffusion XL with ControlNet guidance.
|
| 195 |
-
|
| 196 |
-
This function performs intelligent image outpainting by expanding the input image
|
| 197 |
-
according to the specified target dimensions and alignment, generating new content
|
| 198 |
-
guided by a textual prompt. It uses a ControlNet-enabled diffusion pipeline to ensure
|
| 199 |
-
coherent image extension.
|
| 200 |
-
|
| 201 |
-
Args:
|
| 202 |
-
image (PIL.Image): The input image to be outpainted.
|
| 203 |
-
width (int): The target width of the output image.
|
| 204 |
-
height (int): The target height of the output image.
|
| 205 |
-
overlap_percentage (int): Percentage of overlap between original and outpainted regions for seamless blending.
|
| 206 |
-
num_inference_steps (int): Number of inference steps for image generation. Higher values yield better results.
|
| 207 |
-
resize_option (str): Predefined or custom percentage to resize the input image ("Full", "50%", "33%", "25%", or "Custom").
|
| 208 |
-
custom_resize_percentage (int): Custom resize percentage if resize_option is "Custom".
|
| 209 |
-
prompt_input (str): A text prompt describing desired content for the generated region.
|
| 210 |
-
alignment (str): Alignment of the original image within the canvas ("Middle", "Left", "Right", "Top", "Bottom").
|
| 211 |
-
overlap_left (bool): Whether to allow blending on the left edge.
|
| 212 |
-
overlap_right (bool): Whether to allow blending on the right edge.
|
| 213 |
-
overlap_top (bool): Whether to allow blending on the top edge.
|
| 214 |
-
overlap_bottom (bool): Whether to allow blending on the bottom edge.
|
| 215 |
-
|
| 216 |
-
Yields:
|
| 217 |
-
Tuple[PIL.Image, PIL.Image]:
|
| 218 |
-
- The intermediate ControlNet input image (showing the masked area).
|
| 219 |
-
- The final generated image with the inpainted region.
|
| 220 |
-
"""
|
| 221 |
-
#gr.Info("10 seconds will be used from your daily ZeroGPU time credits.")
|
| 222 |
background, mask = prepare_image_and_mask(
|
| 223 |
image, width, height, overlap_percentage,
|
| 224 |
resize_option, custom_resize_percentage, alignment,
|
|
@@ -233,12 +207,13 @@ def infer(
|
|
| 233 |
|
| 234 |
final_prompt = f"{prompt_input} , high quality, 4k"
|
| 235 |
|
|
|
|
| 236 |
(
|
| 237 |
prompt_embeds,
|
| 238 |
negative_prompt_embeds,
|
| 239 |
pooled_prompt_embeds,
|
| 240 |
negative_pooled_prompt_embeds,
|
| 241 |
-
) = pipe.encode_prompt(final_prompt,
|
| 242 |
|
| 243 |
for image in pipe(
|
| 244 |
prompt_embeds=prompt_embeds,
|
|
@@ -250,12 +225,6 @@ def infer(
|
|
| 250 |
):
|
| 251 |
yield cnet_image, image
|
| 252 |
|
| 253 |
-
#time.sleep(1)
|
| 254 |
-
#image = image.convert("RGBA")
|
| 255 |
-
#cnet_image.paste(image, (0, 0), mask)
|
| 256 |
-
|
| 257 |
-
#return background, cnet_image
|
| 258 |
-
|
| 259 |
|
| 260 |
def clear_result():
|
| 261 |
"""Clears the result ImageSlider."""
|
|
@@ -356,14 +325,14 @@ with gr.Blocks(css=css) as demo:
|
|
| 356 |
minimum=720,
|
| 357 |
maximum=1536,
|
| 358 |
step=8,
|
| 359 |
-
value=720,
|
| 360 |
)
|
| 361 |
height_slider = gr.Slider(
|
| 362 |
label="Target Height",
|
| 363 |
minimum=720,
|
| 364 |
maximum=1536,
|
| 365 |
step=8,
|
| 366 |
-
value=1280,
|
| 367 |
)
|
| 368 |
|
| 369 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
|
@@ -467,47 +436,47 @@ with gr.Blocks(css=css) as demo:
|
|
| 467 |
api_visibility="private"
|
| 468 |
)
|
| 469 |
|
| 470 |
-
run_button.click(
|
| 471 |
fn=clear_result,
|
| 472 |
inputs=None,
|
| 473 |
outputs=result,
|
| 474 |
api_visibility="private"
|
| 475 |
-
).then(
|
| 476 |
fn=infer,
|
| 477 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 478 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 479 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 480 |
outputs=result,
|
| 481 |
-
).then(
|
| 482 |
fn=lambda: gr.update(visible=True),
|
| 483 |
inputs=None,
|
| 484 |
outputs=use_as_input_button,
|
| 485 |
api_visibility="private"
|
| 486 |
-
).then(
|
| 487 |
fn=lambda x, history: update_history(x[1], history),
|
| 488 |
inputs=[result, history_gallery],
|
| 489 |
outputs=history_gallery,
|
| 490 |
api_visibility="private"
|
| 491 |
)
|
| 492 |
|
| 493 |
-
prompt_input.submit(
|
| 494 |
fn=clear_result,
|
| 495 |
inputs=None,
|
| 496 |
outputs=result,
|
| 497 |
api_visibility="private"
|
| 498 |
-
).then(
|
| 499 |
fn=infer,
|
| 500 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 501 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 502 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 503 |
outputs=result,
|
| 504 |
api_visibility="private"
|
| 505 |
-
).then(
|
| 506 |
fn=lambda x, history: update_history(x[1], history),
|
| 507 |
inputs=[result, history_gallery],
|
| 508 |
outputs=history_gallery,
|
| 509 |
api_visibility="private"
|
| 510 |
-
).then(
|
| 511 |
fn=lambda: gr.update(visible=True),
|
| 512 |
inputs=None,
|
| 513 |
outputs=use_as_input_button,
|
|
|
|
| 28 |
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
|
| 29 |
controlnet_model, state_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
|
| 30 |
)
|
| 31 |
+
|
| 32 |
+
# 【核心修改 1】自动检测是否有可用的 GPU,否则使用 CPU
|
| 33 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 34 |
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 35 |
|
| 36 |
+
# 修改模型加载代码,使用动态变量
|
| 37 |
model.to(device=device, dtype=dtype)
|
| 38 |
|
| 39 |
+
# 【核心修改 2】将 .to("cuda") 修改为动态适配变量,并根据设备调整精度
|
| 40 |
vae = AutoencoderKL.from_pretrained(
|
| 41 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=dtype
|
| 42 |
+
).to(device)
|
| 43 |
|
| 44 |
+
# 【核心修改 3】将 .to("cuda") 修改为动态适配变量,并根据设备调整精度与 variant
|
| 45 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 46 |
"SG161222/RealVisXL_V5.0_Lightning",
|
| 47 |
+
torch_dtype=dtype,
|
| 48 |
vae=vae,
|
| 49 |
controlnet=model,
|
| 50 |
+
variant="fp16" if device == "cuda" else None,
|
| 51 |
+
).to(device)
|
| 52 |
|
| 53 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 54 |
|
|
|
|
| 193 |
overlap_top,
|
| 194 |
overlap_bottom
|
| 195 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
background, mask = prepare_image_and_mask(
|
| 197 |
image, width, height, overlap_percentage,
|
| 198 |
resize_option, custom_resize_percentage, alignment,
|
|
|
|
| 207 |
|
| 208 |
final_prompt = f"{prompt_input} , high quality, 4k"
|
| 209 |
|
| 210 |
+
# 【核心修改 4】将这里的编码推理设备也改用动态变量
|
| 211 |
(
|
| 212 |
prompt_embeds,
|
| 213 |
negative_prompt_embeds,
|
| 214 |
pooled_prompt_embeds,
|
| 215 |
negative_pooled_prompt_embeds,
|
| 216 |
+
) = pipe.encode_prompt(final_prompt, device, True)
|
| 217 |
|
| 218 |
for image in pipe(
|
| 219 |
prompt_embeds=prompt_embeds,
|
|
|
|
| 225 |
):
|
| 226 |
yield cnet_image, image
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def clear_result():
|
| 230 |
"""Clears the result ImageSlider."""
|
|
|
|
| 325 |
minimum=720,
|
| 326 |
maximum=1536,
|
| 327 |
step=8,
|
| 328 |
+
value=720,
|
| 329 |
)
|
| 330 |
height_slider = gr.Slider(
|
| 331 |
label="Target Height",
|
| 332 |
minimum=720,
|
| 333 |
maximum=1536,
|
| 334 |
step=8,
|
| 335 |
+
value=1280,
|
| 336 |
)
|
| 337 |
|
| 338 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
|
|
|
| 436 |
api_visibility="private"
|
| 437 |
)
|
| 438 |
|
| 439 |
+
run_button.click(
|
| 440 |
fn=clear_result,
|
| 441 |
inputs=None,
|
| 442 |
outputs=result,
|
| 443 |
api_visibility="private"
|
| 444 |
+
).then(
|
| 445 |
fn=infer,
|
| 446 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 447 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 448 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 449 |
outputs=result,
|
| 450 |
+
).then(
|
| 451 |
fn=lambda: gr.update(visible=True),
|
| 452 |
inputs=None,
|
| 453 |
outputs=use_as_input_button,
|
| 454 |
api_visibility="private"
|
| 455 |
+
).then(
|
| 456 |
fn=lambda x, history: update_history(x[1], history),
|
| 457 |
inputs=[result, history_gallery],
|
| 458 |
outputs=history_gallery,
|
| 459 |
api_visibility="private"
|
| 460 |
)
|
| 461 |
|
| 462 |
+
prompt_input.submit(
|
| 463 |
fn=clear_result,
|
| 464 |
inputs=None,
|
| 465 |
outputs=result,
|
| 466 |
api_visibility="private"
|
| 467 |
+
).then(
|
| 468 |
fn=infer,
|
| 469 |
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 470 |
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 471 |
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 472 |
outputs=result,
|
| 473 |
api_visibility="private"
|
| 474 |
+
).then(
|
| 475 |
fn=lambda x, history: update_history(x[1], history),
|
| 476 |
inputs=[result, history_gallery],
|
| 477 |
outputs=history_gallery,
|
| 478 |
api_visibility="private"
|
| 479 |
+
).then(
|
| 480 |
fn=lambda: gr.update(visible=True),
|
| 481 |
inputs=None,
|
| 482 |
outputs=use_as_input_button,
|