Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,9 @@ from pipeline_fill_sd_xl import StableDiffusionXLFillPipeline
|
|
| 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",
|
|
@@ -32,11 +35,12 @@ result = ControlNetModel_Union._load_pretrained_model(
|
|
| 32 |
)
|
| 33 |
|
| 34 |
model = result[0]
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
vae = AutoencoderKL.from_pretrained(
|
| 38 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 39 |
-
).to("cuda")
|
| 40 |
|
| 41 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 42 |
"SG161222/RealVisXL_V5.0_Lightning",
|
|
@@ -44,11 +48,14 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
| 44 |
vae=vae,
|
| 45 |
controlnet=model,
|
| 46 |
variant="fp16",
|
| 47 |
-
).to("cuda")
|
| 48 |
|
| 49 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 50 |
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
| 53 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
| 54 |
return False
|
|
@@ -142,8 +149,15 @@ def preview_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
| 142 |
preview = Image.alpha_composite(preview, red_mask)
|
| 143 |
return preview
|
| 144 |
|
|
|
|
|
|
|
|
|
|
| 145 |
@spaces.GPU(duration=24)
|
| 146 |
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):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
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)
|
| 148 |
|
| 149 |
if not can_expand(background.width, background.height, width, height, alignment):
|
|
@@ -154,7 +168,6 @@ def infer(image, width, height, overlap_percentage, num_inference_steps, resize_
|
|
| 154 |
|
| 155 |
final_prompt = f"{prompt_input} , high quality, 4k" if prompt_input else "high quality, 4k"
|
| 156 |
|
| 157 |
-
# --- CHANGED HERE: We run generation but DO NOT yield inside the loop ---
|
| 158 |
with torch.autocast(device_type="cuda", dtype=torch.float16):
|
| 159 |
(
|
| 160 |
prompt_embeds,
|
|
@@ -163,7 +176,7 @@ def infer(image, width, height, overlap_percentage, num_inference_steps, resize_
|
|
| 163 |
negative_pooled_prompt_embeds,
|
| 164 |
) = pipe.encode_prompt(final_prompt, "cuda", True)
|
| 165 |
|
| 166 |
-
# Iterate
|
| 167 |
for _ in pipe(
|
| 168 |
prompt_embeds=prompt_embeds,
|
| 169 |
negative_prompt_embeds=negative_prompt_embeds,
|
|
@@ -172,15 +185,18 @@ def infer(image, width, height, overlap_percentage, num_inference_steps, resize_
|
|
| 172 |
image=cnet_image,
|
| 173 |
num_inference_steps=num_inference_steps
|
| 174 |
):
|
| 175 |
-
pass
|
| 176 |
|
| 177 |
-
#
|
| 178 |
-
image = _
|
| 179 |
image = image.convert("RGBA")
|
| 180 |
cnet_image.paste(image, (0, 0), mask)
|
| 181 |
|
| 182 |
yield background, cnet_image
|
| 183 |
|
|
|
|
|
|
|
|
|
|
| 184 |
def clear_result():
|
| 185 |
return gr.update(value=None)
|
| 186 |
|
|
|
|
| 12 |
from PIL import Image, ImageDraw
|
| 13 |
import numpy as np
|
| 14 |
|
| 15 |
+
# ---------------------------------------------------------
|
| 16 |
+
# 1. LOAD MODELS GLOBALLY (TO CPU/RAM ONLY)
|
| 17 |
+
# ---------------------------------------------------------
|
| 18 |
config_file = hf_hub_download(
|
| 19 |
"xinsir/controlnet-union-sdxl-1.0",
|
| 20 |
filename="config_promax.json",
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
model = result[0]
|
| 38 |
+
# ERROR FIX: Do not move to CUDA yet. Keep on CPU.
|
| 39 |
+
model = model.to(dtype=torch.float16)
|
| 40 |
|
| 41 |
vae = AutoencoderKL.from_pretrained(
|
| 42 |
"madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16
|
| 43 |
+
) # ERROR FIX: Removed .to("cuda")
|
| 44 |
|
| 45 |
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
| 46 |
"SG161222/RealVisXL_V5.0_Lightning",
|
|
|
|
| 48 |
vae=vae,
|
| 49 |
controlnet=model,
|
| 50 |
variant="fp16",
|
| 51 |
+
) # ERROR FIX: Removed .to("cuda")
|
| 52 |
|
| 53 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
| 54 |
|
| 55 |
|
| 56 |
+
# ---------------------------------------------------------
|
| 57 |
+
# HELPER FUNCTIONS
|
| 58 |
+
# ---------------------------------------------------------
|
| 59 |
def can_expand(source_width, source_height, target_width, target_height, alignment):
|
| 60 |
if alignment in ("Left", "Right") and source_width >= target_width:
|
| 61 |
return False
|
|
|
|
| 149 |
preview = Image.alpha_composite(preview, red_mask)
|
| 150 |
return preview
|
| 151 |
|
| 152 |
+
# ---------------------------------------------------------
|
| 153 |
+
# 2. INFERENCE (MOVE TO GPU HERE)
|
| 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):
|
| 157 |
+
# ERROR FIX: Move pipe to CUDA *inside* the decorated function
|
| 158 |
+
# This automatically handles the VAE and ControlNet inside the pipe
|
| 159 |
+
pipe.to("cuda")
|
| 160 |
+
|
| 161 |
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)
|
| 162 |
|
| 163 |
if not can_expand(background.width, background.height, width, height, alignment):
|
|
|
|
| 168 |
|
| 169 |
final_prompt = f"{prompt_input} , high quality, 4k" if prompt_input else "high quality, 4k"
|
| 170 |
|
|
|
|
| 171 |
with torch.autocast(device_type="cuda", dtype=torch.float16):
|
| 172 |
(
|
| 173 |
prompt_embeds,
|
|
|
|
| 176 |
negative_pooled_prompt_embeds,
|
| 177 |
) = pipe.encode_prompt(final_prompt, "cuda", True)
|
| 178 |
|
| 179 |
+
# Iterate but DO NOT YIELD yet (prevents blurry lambda response)
|
| 180 |
for _ in pipe(
|
| 181 |
prompt_embeds=prompt_embeds,
|
| 182 |
negative_prompt_embeds=negative_prompt_embeds,
|
|
|
|
| 185 |
image=cnet_image,
|
| 186 |
num_inference_steps=num_inference_steps
|
| 187 |
):
|
| 188 |
+
pass
|
| 189 |
|
| 190 |
+
# Retrieve last generated image
|
| 191 |
+
image = _
|
| 192 |
image = image.convert("RGBA")
|
| 193 |
cnet_image.paste(image, (0, 0), mask)
|
| 194 |
|
| 195 |
yield background, cnet_image
|
| 196 |
|
| 197 |
+
# ---------------------------------------------------------
|
| 198 |
+
# UI SETUP
|
| 199 |
+
# ---------------------------------------------------------
|
| 200 |
def clear_result():
|
| 201 |
return gr.update(value=None)
|
| 202 |
|