Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,67 +11,62 @@ from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
|
| 11 |
from PIL import Image, ImageDraw
|
| 12 |
import numpy as np
|
| 13 |
|
| 14 |
-
# Load VAE and ControlNet (shared components)
|
| 15 |
-
vae = AutoencoderKL.from_pretrained(
|
| 16 |
-
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 17 |
-
).to("cuda")
|
| 18 |
-
|
| 19 |
config_file = hf_hub_download(
|
| 20 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 21 |
filename="config_promax.json",
|
| 22 |
)
|
|
|
|
| 23 |
config = ControlNetModel_Union.load_config(config_file)
|
| 24 |
controlnet_model = ControlNetModel_Union.from_config(config)
|
| 25 |
model_file = hf_hub_download(
|
| 26 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 27 |
filename="diffusion_pytorch_model_promax.safetensors",
|
| 28 |
)
|
|
|
|
| 29 |
sstate_dict = load_state_dict(model_file)
|
| 30 |
-
|
| 31 |
controlnet_model, sstate_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# Define available models
|
| 36 |
-
models = {
|
| 37 |
-
"RealVisXL V5.0 Lightning": "SG161222/RealVisXL_V5.0_Lightning",
|
| 38 |
-
"RealVisXL V4.0 Lightning": "SG161222/RealVisXL_V4.0_Lightning",
|
| 39 |
-
}
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 44 |
-
models[default_model],
|
| 45 |
-
torch_dtype=torch.float16,
|
| 46 |
-
vae=vae,
|
| 47 |
-
controlnet=controlnet,
|
| 48 |
-
variant="fp16",
|
| 49 |
).to("cuda")
|
| 50 |
-
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
repo_id,
|
| 57 |
torch_dtype=torch.float16,
|
| 58 |
vae=vae,
|
| 59 |
-
controlnet=
|
| 60 |
variant="fp16",
|
| 61 |
-
).to("cuda")
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Prepare image and mask function (unchanged)
|
| 66 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
| 67 |
target_size = (width, height)
|
| 68 |
|
|
|
|
| 69 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
| 70 |
new_width = int(image.width * scale_factor)
|
| 71 |
new_height = int(image.height * scale_factor)
|
| 72 |
-
|
|
|
|
| 73 |
source = image.resize((new_width, new_height), Image.LANCZOS)
|
| 74 |
|
|
|
|
| 75 |
if resize_option == "Full":
|
| 76 |
resize_percentage = 100
|
| 77 |
elif resize_option == "50%":
|
|
@@ -83,21 +78,27 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 83 |
else: # Custom
|
| 84 |
resize_percentage = custom_resize_percentage
|
| 85 |
|
|
|
|
| 86 |
resize_factor = resize_percentage / 100
|
| 87 |
new_width = int(source.width * resize_factor)
|
| 88 |
new_height = int(source.height * resize_factor)
|
| 89 |
|
|
|
|
| 90 |
new_width = max(new_width, 64)
|
| 91 |
new_height = max(new_height, 64)
|
| 92 |
|
|
|
|
| 93 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 94 |
|
|
|
|
| 95 |
overlap_x = int(new_width * (overlap_percentage / 100))
|
| 96 |
overlap_y = int(new_height * (overlap_percentage / 100))
|
| 97 |
|
|
|
|
| 98 |
overlap_x = max(overlap_x, 1)
|
| 99 |
overlap_y = max(overlap_y, 1)
|
| 100 |
|
|
|
|
| 101 |
if alignment == "Middle":
|
| 102 |
margin_x = (target_size[0] - new_width) // 2
|
| 103 |
margin_y = (target_size[1] - new_height) // 2
|
|
@@ -114,22 +115,26 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 114 |
margin_x = (target_size[0] - new_width) // 2
|
| 115 |
margin_y = target_size[1] - new_height
|
| 116 |
|
|
|
|
| 117 |
margin_x = max(0, min(margin_x, target_size[0] - new_width))
|
| 118 |
margin_y = max(0, min(margin_y, target_size[1] - new_height))
|
| 119 |
|
|
|
|
| 120 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 121 |
background.paste(source, (margin_x, margin_y))
|
| 122 |
|
|
|
|
| 123 |
mask = Image.new('L', target_size, 255)
|
| 124 |
mask_draw = ImageDraw.Draw(mask)
|
| 125 |
|
|
|
|
| 126 |
white_gaps_patch = 2
|
| 127 |
|
| 128 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x + white_gaps_patch
|
| 129 |
right_overlap = margin_x + new_width - overlap_x if overlap_right else margin_x + new_width - white_gaps_patch
|
| 130 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y + white_gaps_patch
|
| 131 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height - white_gaps_patch
|
| 132 |
-
|
| 133 |
if alignment == "Left":
|
| 134 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x
|
| 135 |
elif alignment == "Right":
|
|
@@ -139,6 +144,7 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 139 |
elif alignment == "Bottom":
|
| 140 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height
|
| 141 |
|
|
|
|
| 142 |
mask_draw.rectangle([
|
| 143 |
(left_overlap, top_overlap),
|
| 144 |
(right_overlap, bottom_overlap)
|
|
@@ -146,24 +152,27 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 146 |
|
| 147 |
return background, mask
|
| 148 |
|
| 149 |
-
# Updated inference function to use selected pipeline
|
| 150 |
@spaces.GPU(duration=24)
|
| 151 |
-
def infer(
|
| 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 |
cnet_image = background.copy()
|
| 155 |
cnet_image.paste(0, (0, 0), mask)
|
| 156 |
|
| 157 |
final_prompt = f"{prompt_input} , high quality, 4k"
|
| 158 |
|
|
|
|
|
|
|
|
|
|
| 159 |
(
|
| 160 |
prompt_embeds,
|
| 161 |
negative_prompt_embeds,
|
| 162 |
pooled_prompt_embeds,
|
| 163 |
negative_pooled_prompt_embeds,
|
| 164 |
-
) =
|
| 165 |
|
| 166 |
-
|
|
|
|
| 167 |
prompt_embeds=prompt_embeds,
|
| 168 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 169 |
pooled_prompt_embeds=pooled_prompt_embeds,
|
|
@@ -171,28 +180,38 @@ def infer(pipeline, image, width, height, overlap_percentage, num_inference_step
|
|
| 171 |
image=cnet_image,
|
| 172 |
num_inference_steps=num_inference_steps
|
| 173 |
):
|
| 174 |
-
pass
|
| 175 |
-
generated_image = image
|
| 176 |
|
| 177 |
generated_image = generated_image.convert("RGBA")
|
| 178 |
cnet_image.paste(generated_image, (0, 0), mask)
|
| 179 |
|
| 180 |
return cnet_image
|
| 181 |
|
| 182 |
-
|
| 183 |
def clear_result():
|
|
|
|
| 184 |
return gr.update(value=None)
|
| 185 |
|
|
|
|
| 186 |
def preload_presets(target_ratio, ui_width, ui_height):
|
|
|
|
| 187 |
if target_ratio == "9:16":
|
| 188 |
-
|
|
|
|
|
|
|
| 189 |
elif target_ratio == "16:9":
|
| 190 |
-
|
|
|
|
|
|
|
| 191 |
elif target_ratio == "1:1":
|
| 192 |
-
|
|
|
|
|
|
|
| 193 |
elif target_ratio == "Custom":
|
| 194 |
return ui_width, ui_height, gr.update(open=True)
|
| 195 |
|
|
|
|
| 196 |
def select_the_right_preset(user_width, user_height):
|
| 197 |
if user_width == 720 and user_height == 1280:
|
| 198 |
return "9:16"
|
|
@@ -203,16 +222,20 @@ def select_the_right_preset(user_width, user_height):
|
|
| 203 |
else:
|
| 204 |
return "Custom"
|
| 205 |
|
|
|
|
| 206 |
def toggle_custom_resize_slider(resize_option):
|
| 207 |
return gr.update(visible=(resize_option == "Custom"))
|
| 208 |
|
|
|
|
| 209 |
def update_history(new_image, history):
|
|
|
|
| 210 |
if history is None:
|
| 211 |
history = []
|
| 212 |
history.insert(0, new_image)
|
| 213 |
return history
|
| 214 |
|
| 215 |
-
|
|
|
|
| 216 |
css = """
|
| 217 |
h1 {
|
| 218 |
text-align: center;
|
|
@@ -223,7 +246,6 @@ h1 {
|
|
| 223 |
title = """<h1 align="center">Diffusers Image Outpaint Lightning</h1>
|
| 224 |
"""
|
| 225 |
|
| 226 |
-
# Gradio interface with model selection
|
| 227 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 228 |
with gr.Column():
|
| 229 |
gr.HTML(title)
|
|
@@ -241,13 +263,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 241 |
with gr.Column(scale=1):
|
| 242 |
run_button = gr.Button("Generate")
|
| 243 |
|
| 244 |
-
with gr.Row():
|
| 245 |
-
model_selector = gr.Dropdown(
|
| 246 |
-
label="Select Model",
|
| 247 |
-
choices=list(models.keys()),
|
| 248 |
-
value="RealVisXL V5.0 Lightning",
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
with gr.Row():
|
| 252 |
target_ratio = gr.Radio(
|
| 253 |
label="Expected Ratio",
|
|
@@ -255,11 +270,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 255 |
value="9:16",
|
| 256 |
scale=2
|
| 257 |
)
|
|
|
|
| 258 |
alignment_dropdown = gr.Dropdown(
|
| 259 |
choices=["Middle", "Left", "Right", "Top", "Bottom"],
|
| 260 |
value="Middle",
|
| 261 |
label="Alignment"
|
| 262 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
| 265 |
with gr.Column():
|
|
@@ -278,7 +300,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 278 |
step=8,
|
| 279 |
value=1280,
|
| 280 |
)
|
| 281 |
-
|
| 282 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
| 283 |
with gr.Group():
|
| 284 |
overlap_percentage = gr.Slider(
|
|
@@ -308,7 +330,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 308 |
value=50,
|
| 309 |
visible=False
|
| 310 |
)
|
| 311 |
-
|
| 312 |
gr.Examples(
|
| 313 |
examples=[
|
| 314 |
["./examples/example_1.webp", 1280, 720, "Middle"],
|
|
@@ -327,74 +349,64 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 327 |
)
|
| 328 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
| 329 |
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
target_ratio
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
fn=infer,
|
| 390 |
-
inputs=[pipeline_state, input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 391 |
-
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 392 |
-
overlap_left, overlap_right, overlap_top, overlap_bottom],
|
| 393 |
-
outputs=result,
|
| 394 |
-
).then(
|
| 395 |
-
fn=lambda x, history: update_history(x, history),
|
| 396 |
-
inputs=[result, history_gallery],
|
| 397 |
-
outputs=history_gallery,
|
| 398 |
-
)
|
| 399 |
|
| 400 |
demo.queue(max_size=20).launch(share=False, ssr_mode=False, show_error=True)
|
|
|
|
| 11 |
from PIL import Image, ImageDraw
|
| 12 |
import numpy as np
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
config_file = hf_hub_download(
|
| 15 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 16 |
filename="config_promax.json",
|
| 17 |
)
|
| 18 |
+
|
| 19 |
config = ControlNetModel_Union.load_config(config_file)
|
| 20 |
controlnet_model = ControlNetModel_Union.from_config(config)
|
| 21 |
model_file = hf_hub_download(
|
| 22 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 23 |
filename="diffusion_pytorch_model_promax.safetensors",
|
| 24 |
)
|
| 25 |
+
|
| 26 |
sstate_dict = load_state_dict(model_file)
|
| 27 |
+
model, _, _, _, _ = ControlNetModel_Union._load_pretrained_model(
|
| 28 |
controlnet_model, sstate_dict, model_file, "xinsir/controlnet-union-sdxl-1.0"
|
| 29 |
)
|
| 30 |
+
model.to(device="cuda", dtype=torch.float16)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
vae = AutoencoderKL.from_pretrained(
|
| 33 |
+
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
).to("cuda")
|
|
|
|
| 35 |
|
| 36 |
+
# Initialize both pipelines and store them in a dictionary
|
| 37 |
+
pipelines = {
|
| 38 |
+
"RealVisXL V5.0 Lightning": StableDiffusionXLFillPipeline.from_pretrained(
|
| 39 |
+
"SG161222/RealVisXL_V5.0_Lightning",
|
|
|
|
| 40 |
torch_dtype=torch.float16,
|
| 41 |
vae=vae,
|
| 42 |
+
controlnet=model,
|
| 43 |
variant="fp16",
|
| 44 |
+
).to("cuda"),
|
| 45 |
+
"RealVisXL V4.0 Lightning": StableDiffusionXLFillPipeline.from_pretrained(
|
| 46 |
+
"SG161222/RealVisXL_V4.0_Lightning",
|
| 47 |
+
torch_dtype=torch.float16,
|
| 48 |
+
vae=vae,
|
| 49 |
+
controlnet=model,
|
| 50 |
+
variant="fp16",
|
| 51 |
+
).to("cuda"),
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
for pipe in pipelines.values():
|
| 55 |
+
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 56 |
+
|
| 57 |
|
|
|
|
| 58 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
| 59 |
target_size = (width, height)
|
| 60 |
|
| 61 |
+
# Calculate the scaling factor to fit the image within the target size
|
| 62 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
| 63 |
new_width = int(image.width * scale_factor)
|
| 64 |
new_height = int(image.height * scale_factor)
|
| 65 |
+
|
| 66 |
+
# Resize the source image to fit within target size
|
| 67 |
source = image.resize((new_width, new_height), Image.LANCZOS)
|
| 68 |
|
| 69 |
+
# Apply resize option using percentages
|
| 70 |
if resize_option == "Full":
|
| 71 |
resize_percentage = 100
|
| 72 |
elif resize_option == "50%":
|
|
|
|
| 78 |
else: # Custom
|
| 79 |
resize_percentage = custom_resize_percentage
|
| 80 |
|
| 81 |
+
# Calculate new dimensions based on percentage
|
| 82 |
resize_factor = resize_percentage / 100
|
| 83 |
new_width = int(source.width * resize_factor)
|
| 84 |
new_height = int(source.height * resize_factor)
|
| 85 |
|
| 86 |
+
# Ensure minimum size of 64 pixels
|
| 87 |
new_width = max(new_width, 64)
|
| 88 |
new_height = max(new_height, 64)
|
| 89 |
|
| 90 |
+
# Resize the image
|
| 91 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
| 92 |
|
| 93 |
+
# Calculate the overlap in pixels based on the percentage
|
| 94 |
overlap_x = int(new_width * (overlap_percentage / 100))
|
| 95 |
overlap_y = int(new_height * (overlap_percentage / 100))
|
| 96 |
|
| 97 |
+
# Ensure minimum overlap of 1 pixel
|
| 98 |
overlap_x = max(overlap_x, 1)
|
| 99 |
overlap_y = max(overlap_y, 1)
|
| 100 |
|
| 101 |
+
# Calculate margins based on alignment
|
| 102 |
if alignment == "Middle":
|
| 103 |
margin_x = (target_size[0] - new_width) // 2
|
| 104 |
margin_y = (target_size[1] - new_height) // 2
|
|
|
|
| 115 |
margin_x = (target_size[0] - new_width) // 2
|
| 116 |
margin_y = target_size[1] - new_height
|
| 117 |
|
| 118 |
+
# Adjust margins to eliminate gaps
|
| 119 |
margin_x = max(0, min(margin_x, target_size[0] - new_width))
|
| 120 |
margin_y = max(0, min(margin_y, target_size[1] - new_height))
|
| 121 |
|
| 122 |
+
# Create a new background image and paste the resized source image
|
| 123 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
| 124 |
background.paste(source, (margin_x, margin_y))
|
| 125 |
|
| 126 |
+
# Create the mask
|
| 127 |
mask = Image.new('L', target_size, 255)
|
| 128 |
mask_draw = ImageDraw.Draw(mask)
|
| 129 |
|
| 130 |
+
# Calculate overlap areas
|
| 131 |
white_gaps_patch = 2
|
| 132 |
|
| 133 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x + white_gaps_patch
|
| 134 |
right_overlap = margin_x + new_width - overlap_x if overlap_right else margin_x + new_width - white_gaps_patch
|
| 135 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y + white_gaps_patch
|
| 136 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height - white_gaps_patch
|
| 137 |
+
|
| 138 |
if alignment == "Left":
|
| 139 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x
|
| 140 |
elif alignment == "Right":
|
|
|
|
| 144 |
elif alignment == "Bottom":
|
| 145 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height
|
| 146 |
|
| 147 |
+
# Draw the mask
|
| 148 |
mask_draw.rectangle([
|
| 149 |
(left_overlap, top_overlap),
|
| 150 |
(right_overlap, bottom_overlap)
|
|
|
|
| 152 |
|
| 153 |
return background, mask
|
| 154 |
|
|
|
|
| 155 |
@spaces.GPU(duration=24)
|
| 156 |
+
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, selected_model):
|
| 157 |
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)
|
| 158 |
+
|
| 159 |
cnet_image = background.copy()
|
| 160 |
cnet_image.paste(0, (0, 0), mask)
|
| 161 |
|
| 162 |
final_prompt = f"{prompt_input} , high quality, 4k"
|
| 163 |
|
| 164 |
+
# Access the selected pipeline from the dictionary
|
| 165 |
+
pipe = pipelines[selected_model]
|
| 166 |
+
|
| 167 |
(
|
| 168 |
prompt_embeds,
|
| 169 |
negative_prompt_embeds,
|
| 170 |
pooled_prompt_embeds,
|
| 171 |
negative_pooled_prompt_embeds,
|
| 172 |
+
) = pipe.encode_prompt(final_prompt, "cuda", True)
|
| 173 |
|
| 174 |
+
# Generate the image
|
| 175 |
+
for image in pipe(
|
| 176 |
prompt_embeds=prompt_embeds,
|
| 177 |
negative_prompt_embeds=negative_prompt_embeds,
|
| 178 |
pooled_prompt_embeds=pooled_prompt_embeds,
|
|
|
|
| 180 |
image=cnet_image,
|
| 181 |
num_inference_steps=num_inference_steps
|
| 182 |
):
|
| 183 |
+
pass # Wait for the generation to complete
|
| 184 |
+
generated_image = image # Get the last image
|
| 185 |
|
| 186 |
generated_image = generated_image.convert("RGBA")
|
| 187 |
cnet_image.paste(generated_image, (0, 0), mask)
|
| 188 |
|
| 189 |
return cnet_image
|
| 190 |
|
| 191 |
+
|
| 192 |
def clear_result():
|
| 193 |
+
"""Clears the result Image."""
|
| 194 |
return gr.update(value=None)
|
| 195 |
|
| 196 |
+
|
| 197 |
def preload_presets(target_ratio, ui_width, ui_height):
|
| 198 |
+
"""Updates the width and height sliders based on the selected aspect ratio."""
|
| 199 |
if target_ratio == "9:16":
|
| 200 |
+
changed_width = 720
|
| 201 |
+
changed_height = 1280
|
| 202 |
+
return changed_width, changed_height, gr.update()
|
| 203 |
elif target_ratio == "16:9":
|
| 204 |
+
changed_width = 1280
|
| 205 |
+
changed_height = 720
|
| 206 |
+
return changed_width, changed_height, gr.update()
|
| 207 |
elif target_ratio == "1:1":
|
| 208 |
+
changed_width = 1024
|
| 209 |
+
changed_height = 1024
|
| 210 |
+
return changed_width, changed_height, gr.update()
|
| 211 |
elif target_ratio == "Custom":
|
| 212 |
return ui_width, ui_height, gr.update(open=True)
|
| 213 |
|
| 214 |
+
|
| 215 |
def select_the_right_preset(user_width, user_height):
|
| 216 |
if user_width == 720 and user_height == 1280:
|
| 217 |
return "9:16"
|
|
|
|
| 222 |
else:
|
| 223 |
return "Custom"
|
| 224 |
|
| 225 |
+
|
| 226 |
def toggle_custom_resize_slider(resize_option):
|
| 227 |
return gr.update(visible=(resize_option == "Custom"))
|
| 228 |
|
| 229 |
+
|
| 230 |
def update_history(new_image, history):
|
| 231 |
+
"""Updates the history gallery with the new image."""
|
| 232 |
if history is None:
|
| 233 |
history = []
|
| 234 |
history.insert(0, new_image)
|
| 235 |
return history
|
| 236 |
|
| 237 |
+
|
| 238 |
+
# --- CSS and Title (unchanged) ---
|
| 239 |
css = """
|
| 240 |
h1 {
|
| 241 |
text-align: center;
|
|
|
|
| 246 |
title = """<h1 align="center">Diffusers Image Outpaint Lightning</h1>
|
| 247 |
"""
|
| 248 |
|
|
|
|
| 249 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 250 |
with gr.Column():
|
| 251 |
gr.HTML(title)
|
|
|
|
| 263 |
with gr.Column(scale=1):
|
| 264 |
run_button = gr.Button("Generate")
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
with gr.Row():
|
| 267 |
target_ratio = gr.Radio(
|
| 268 |
label="Expected Ratio",
|
|
|
|
| 270 |
value="9:16",
|
| 271 |
scale=2
|
| 272 |
)
|
| 273 |
+
|
| 274 |
alignment_dropdown = gr.Dropdown(
|
| 275 |
choices=["Middle", "Left", "Right", "Top", "Bottom"],
|
| 276 |
value="Middle",
|
| 277 |
label="Alignment"
|
| 278 |
)
|
| 279 |
+
with gr.Row():
|
| 280 |
+
model_selector = gr.Dropdown(
|
| 281 |
+
label="Select Model",
|
| 282 |
+
choices=list(pipelines.keys()),
|
| 283 |
+
value="RealVisXL V5.0 Lightning",
|
| 284 |
+
)
|
| 285 |
|
| 286 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
| 287 |
with gr.Column():
|
|
|
|
| 300 |
step=8,
|
| 301 |
value=1280,
|
| 302 |
)
|
| 303 |
+
|
| 304 |
num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
|
| 305 |
with gr.Group():
|
| 306 |
overlap_percentage = gr.Slider(
|
|
|
|
| 330 |
value=50,
|
| 331 |
visible=False
|
| 332 |
)
|
| 333 |
+
|
| 334 |
gr.Examples(
|
| 335 |
examples=[
|
| 336 |
["./examples/example_1.webp", 1280, 720, "Middle"],
|
|
|
|
| 349 |
)
|
| 350 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
| 351 |
|
| 352 |
+
target_ratio.change(
|
| 353 |
+
fn=preload_presets,
|
| 354 |
+
inputs=[target_ratio, width_slider, height_slider],
|
| 355 |
+
outputs=[width_slider, height_slider, settings_panel],
|
| 356 |
+
queue=False
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
width_slider.change(
|
| 360 |
+
fn=select_the_right_preset,
|
| 361 |
+
inputs=[width_slider, height_slider],
|
| 362 |
+
outputs=[target_ratio],
|
| 363 |
+
queue=False
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
height_slider.change(
|
| 367 |
+
fn=select_the_right_preset,
|
| 368 |
+
inputs=[width_slider, height_slider],
|
| 369 |
+
outputs=[target_ratio],
|
| 370 |
+
queue=False
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
resize_option.change(
|
| 374 |
+
fn=toggle_custom_resize_slider,
|
| 375 |
+
inputs=[resize_option],
|
| 376 |
+
outputs=[custom_resize_percentage],
|
| 377 |
+
queue=False
|
| 378 |
+
)
|
| 379 |
+
|
| 380 |
+
run_button.click(
|
| 381 |
+
fn=clear_result,
|
| 382 |
+
inputs=None,
|
| 383 |
+
outputs=result,
|
| 384 |
+
).then(
|
| 385 |
+
fn=infer,
|
| 386 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 387 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 388 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom, model_selector],
|
| 389 |
+
outputs=result,
|
| 390 |
+
).then(
|
| 391 |
+
fn=lambda x, history: update_history(x, history),
|
| 392 |
+
inputs=[result, history_gallery],
|
| 393 |
+
outputs=history_gallery,
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
prompt_input.submit(
|
| 397 |
+
fn=clear_result,
|
| 398 |
+
inputs=None,
|
| 399 |
+
outputs=result,
|
| 400 |
+
).then(
|
| 401 |
+
fn=infer,
|
| 402 |
+
inputs=[input_image, width_slider, height_slider, overlap_percentage, num_inference_steps,
|
| 403 |
+
resize_option, custom_resize_percentage, prompt_input, alignment_dropdown,
|
| 404 |
+
overlap_left, overlap_right, overlap_top, overlap_bottom, model_selector],
|
| 405 |
+
outputs=result,
|
| 406 |
+
).then(
|
| 407 |
+
fn=lambda x, history: update_history(x, history),
|
| 408 |
+
inputs=[result, history_gallery],
|
| 409 |
+
outputs=history_gallery,
|
| 410 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 411 |
|
| 412 |
demo.queue(max_size=20).launch(share=False, ssr_mode=False, show_error=True)
|