Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +1 -84
app_quant_latent.py
CHANGED
|
@@ -555,90 +555,7 @@ def safe_get_latents(pipe, height, width, generator, device, LOGS):
|
|
| 555 |
# Main generation function (kept exactly as your logic)
|
| 556 |
# --------------------------
|
| 557 |
@spaces.GPU
|
| 558 |
-
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 559 |
-
LOGS = []
|
| 560 |
-
device = "cuda"
|
| 561 |
-
generator = torch.Generator(device).manual_seed(int(seed))
|
| 562 |
-
|
| 563 |
-
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 564 |
-
latent_gallery = []
|
| 565 |
-
final_gallery = []
|
| 566 |
-
|
| 567 |
-
try:
|
| 568 |
-
# ==========================================================
|
| 569 |
-
# ADVANCED LATENT MODE (hack using latent tensor)
|
| 570 |
-
# ==========================================================
|
| 571 |
-
try:
|
| 572 |
-
# Get initial latent tensor
|
| 573 |
-
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 574 |
-
|
| 575 |
-
# Fake step-wise preview by slicing latent channels / noise
|
| 576 |
-
num_preview_steps = min(6, latents.shape[1]) # e.g. 6 slices
|
| 577 |
-
for i in range(num_preview_steps):
|
| 578 |
-
# Take a slice of latent channels to simulate intermediate step
|
| 579 |
-
step_latent = latents[:, : (i + 1), :, :]
|
| 580 |
-
|
| 581 |
-
# Convert step latent to PIL
|
| 582 |
-
try:
|
| 583 |
-
latent_img = latent_to_image(step_latent)
|
| 584 |
-
except Exception:
|
| 585 |
-
latent_img = placeholder
|
| 586 |
-
|
| 587 |
-
latent_gallery.append(latent_img)
|
| 588 |
-
|
| 589 |
-
# Stream intermediate latent preview to Gradio
|
| 590 |
-
yield (
|
| 591 |
-
None, # final image not ready
|
| 592 |
-
latent_gallery, # gallery updates live
|
| 593 |
-
"\n".join(LOGS),
|
| 594 |
-
)
|
| 595 |
-
|
| 596 |
-
# Decode final latent tensor into final image
|
| 597 |
-
final_img = pipe.decode_latents(latents)[0]
|
| 598 |
-
final_gallery.append(final_img)
|
| 599 |
-
LOGS.append("β
Advanced latent pipeline succeeded.")
|
| 600 |
-
|
| 601 |
-
yield (
|
| 602 |
-
final_img,
|
| 603 |
-
latent_gallery,
|
| 604 |
-
"\n".join(LOGS),
|
| 605 |
-
)
|
| 606 |
-
|
| 607 |
-
# ==========================================================
|
| 608 |
-
# FALLBACK STANDARD PIPELINE
|
| 609 |
-
# ==========================================================
|
| 610 |
-
except Exception as e:
|
| 611 |
-
LOGS.append(f"β οΈ Advanced latent mode failed: {e}")
|
| 612 |
-
LOGS.append("π Switching to standard pipeline...")
|
| 613 |
-
|
| 614 |
-
output = pipe(
|
| 615 |
-
prompt=prompt,
|
| 616 |
-
height=height,
|
| 617 |
-
width=width,
|
| 618 |
-
num_inference_steps=steps,
|
| 619 |
-
guidance_scale=guidance_scale,
|
| 620 |
-
generator=generator,
|
| 621 |
-
)
|
| 622 |
-
|
| 623 |
-
final_img = output.images[0]
|
| 624 |
-
latent_gallery.append(final_img) # last step in gallery
|
| 625 |
-
LOGS.append("β
Standard pipeline succeeded.")
|
| 626 |
-
|
| 627 |
-
yield (
|
| 628 |
-
final_img,
|
| 629 |
-
latent_gallery,
|
| 630 |
-
"\n".join(LOGS),
|
| 631 |
-
)
|
| 632 |
-
|
| 633 |
-
except Exception as e:
|
| 634 |
-
LOGS.append(f"β Total failure: {e}")
|
| 635 |
-
placeholder_img = placeholder
|
| 636 |
-
yield (
|
| 637 |
-
placeholder_img,
|
| 638 |
-
[placeholder_img],
|
| 639 |
-
"\n".join(LOGS),
|
| 640 |
-
)
|
| 641 |
-
|
| 642 |
|
| 643 |
@spaces.GPU
|
| 644 |
def generate_image_backup(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
|
|
|
|
| 555 |
# Main generation function (kept exactly as your logic)
|
| 556 |
# --------------------------
|
| 557 |
@spaces.GPU
|
| 558 |
+
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0): LOGS = [] device = "cuda" generator = torch.Generator(device).manual_seed(int(seed)) # placeholders placeholder = Image.new("RGB", (width, height), color=(255, 255, 255)) latent_gallery = [] final_gallery = [] try: # --- Try advanced latent mode --- try: latents = safe_get_latents(pipe, height, width, generator, device, LOGS) for i, t in enumerate(pipe.scheduler.timesteps): # Step-wise denoising with torch.no_grad(): noise_pred = pipe.unet(latents, t, encoder_hidden_states=pipe.get_text_embeddings(prompt))["sample"] latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"] # Convert latent to preview image try: latent_img = latent_to_image(latents, pipe.vae)[0] except Exception: latent_img = placeholder latent_gallery.append(latent_img) # Yield intermediate update: latents updated, final gallery empty yield None, latent_gallery, final_gallery, LOGS # decode final image final_img = pipe.decode_latents(latents)[0] final_gallery.append(final_img) LOGS.append("β
Advanced latent pipeline succeeded.") yield final_img, latent_gallery, final_gallery, LOGS except Exception as e: LOGS.append(f"β οΈ Advanced latent mode failed: {e}") LOGS.append("π Switching to standard pipeline...") # Standard pipeline fallback try: output = pipe( prompt=prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=guidance_scale, generator=generator, ) final_img = output.images[0] final_gallery.append(final_img) latent_gallery.append(final_img) # optionally show in latent gallery as last step LOGS.append("β
Standard pipeline succeeded.") yield final_img, latent_gallery, final_gallery, LOGS except Exception as e2: LOGS.append(f"β Standard pipeline failed: {e2}") final_gallery.append(placeholder) latent_gallery.append(placeholder) yield placeholder, latent_gallery, final_gallery, LOGS except Exception as e: LOGS.append(f"β Total failure: {e}") final_gallery.append(placeholder) latent_gallery.append(placeholder) yield placeholder, latent_gallery, final_gallery, LOGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
@spaces.GPU
|
| 561 |
def generate_image_backup(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
|