Spaces:
Running
on
Zero
Running
on
Zero
Update app_quant_latent.py
Browse files- app_quant_latent.py +21 -15
app_quant_latent.py
CHANGED
|
@@ -246,26 +246,31 @@ log_system_stats("AFTER PIPELINE BUILD")
|
|
| 246 |
|
| 247 |
|
| 248 |
|
|
|
|
|
|
|
| 249 |
@spaces.GPU
|
| 250 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
|
| 251 |
"""
|
| 252 |
-
Robust dual pipeline
|
| 253 |
-
-
|
| 254 |
-
-
|
| 255 |
-
- Always returns
|
| 256 |
-
-
|
| 257 |
-
- Logs progress and errors
|
| 258 |
"""
|
| 259 |
|
| 260 |
LOGS = []
|
| 261 |
image = None
|
| 262 |
latents = None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
try:
|
| 265 |
generator = torch.Generator(device).manual_seed(int(seed))
|
| 266 |
|
| 267 |
# -------------------------------
|
| 268 |
-
#
|
| 269 |
# -------------------------------
|
| 270 |
try:
|
| 271 |
batch_size = 1
|
|
@@ -294,6 +299,8 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
|
|
| 294 |
latents=latents
|
| 295 |
)
|
| 296 |
image = output.images[0]
|
|
|
|
|
|
|
| 297 |
LOGS.append("β
Advanced latent generation succeeded.")
|
| 298 |
|
| 299 |
# -------------------------------
|
|
@@ -302,6 +309,7 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
|
|
| 302 |
except Exception as e_latent:
|
| 303 |
LOGS.append(f"β οΈ Advanced latent generation failed: {e_latent}")
|
| 304 |
LOGS.append("π Falling back to standard pipeline...")
|
|
|
|
| 305 |
try:
|
| 306 |
output = pipe(
|
| 307 |
prompt=prompt,
|
|
@@ -312,23 +320,21 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
|
|
| 312 |
generator=generator
|
| 313 |
)
|
| 314 |
image = output.images[0]
|
|
|
|
| 315 |
LOGS.append("β
Standard pipeline generation succeeded.")
|
| 316 |
except Exception as e_standard:
|
| 317 |
LOGS.append(f"β Standard pipeline generation failed: {e_standard}")
|
| 318 |
-
#
|
|
|
|
| 319 |
|
| 320 |
# -------------------------------
|
| 321 |
-
# Return
|
| 322 |
# -------------------------------
|
| 323 |
-
|
| 324 |
-
return image, latents, LOGS
|
| 325 |
-
else:
|
| 326 |
-
return image, image, LOGS
|
| 327 |
|
| 328 |
except Exception as e:
|
| 329 |
LOGS.append(f"β Inference failed entirely: {e}")
|
| 330 |
-
return
|
| 331 |
-
|
| 332 |
|
| 333 |
# ============================================================
|
| 334 |
# UI
|
|
|
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
+
from PIL import Image
|
| 250 |
+
|
| 251 |
@spaces.GPU
|
| 252 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
|
| 253 |
"""
|
| 254 |
+
Robust dual pipeline:
|
| 255 |
+
- Advanced latent generation first
|
| 256 |
+
- Fallback to standard pipeline if latent fails
|
| 257 |
+
- Always returns final image
|
| 258 |
+
- Returns gallery (latents or final image) and logs
|
|
|
|
| 259 |
"""
|
| 260 |
|
| 261 |
LOGS = []
|
| 262 |
image = None
|
| 263 |
latents = None
|
| 264 |
+
gallery = []
|
| 265 |
+
|
| 266 |
+
# Keep a placeholder original image (white) in case everything fails
|
| 267 |
+
original_image = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 268 |
|
| 269 |
try:
|
| 270 |
generator = torch.Generator(device).manual_seed(int(seed))
|
| 271 |
|
| 272 |
# -------------------------------
|
| 273 |
+
# Try advanced latent generation
|
| 274 |
# -------------------------------
|
| 275 |
try:
|
| 276 |
batch_size = 1
|
|
|
|
| 299 |
latents=latents
|
| 300 |
)
|
| 301 |
image = output.images[0]
|
| 302 |
+
gallery = [image] if image else []
|
| 303 |
+
|
| 304 |
LOGS.append("β
Advanced latent generation succeeded.")
|
| 305 |
|
| 306 |
# -------------------------------
|
|
|
|
| 309 |
except Exception as e_latent:
|
| 310 |
LOGS.append(f"β οΈ Advanced latent generation failed: {e_latent}")
|
| 311 |
LOGS.append("π Falling back to standard pipeline...")
|
| 312 |
+
|
| 313 |
try:
|
| 314 |
output = pipe(
|
| 315 |
prompt=prompt,
|
|
|
|
| 320 |
generator=generator
|
| 321 |
)
|
| 322 |
image = output.images[0]
|
| 323 |
+
gallery = [image] if image else []
|
| 324 |
LOGS.append("β
Standard pipeline generation succeeded.")
|
| 325 |
except Exception as e_standard:
|
| 326 |
LOGS.append(f"β Standard pipeline generation failed: {e_standard}")
|
| 327 |
+
image = original_image # Always return some image
|
| 328 |
+
gallery = [image]
|
| 329 |
|
| 330 |
# -------------------------------
|
| 331 |
+
# Return all 3 outputs
|
| 332 |
# -------------------------------
|
| 333 |
+
return image, gallery, LOGS
|
|
|
|
|
|
|
|
|
|
| 334 |
|
| 335 |
except Exception as e:
|
| 336 |
LOGS.append(f"β Inference failed entirely: {e}")
|
| 337 |
+
return original_image, [original_image], LOGS
|
|
|
|
| 338 |
|
| 339 |
# ============================================================
|
| 340 |
# UI
|