Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +43 -64
app_quant_latent.py
CHANGED
|
@@ -589,75 +589,54 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 589 |
latent_gallery = []
|
| 590 |
final_gallery = []
|
| 591 |
|
|
|
|
| 592 |
try:
|
| 593 |
-
|
| 594 |
-
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 595 |
-
|
| 596 |
-
# --- Latent preview only ---
|
| 597 |
-
try:
|
| 598 |
-
with torch.no_grad():
|
| 599 |
-
latent_img_tensor = pipe.vae.decode(latents).sample # [1, 3, H, W]
|
| 600 |
-
latent_img_tensor = (latent_img_tensor / 2 + 0.5).clamp(0, 1) # normalize
|
| 601 |
-
latent_img_tensor = latent_img_tensor.cpu().permute(0, 2, 3, 1)[0] # HWC
|
| 602 |
-
latent_img = Image.fromarray((latent_img_tensor.numpy() * 255).astype('uint8'))
|
| 603 |
-
except Exception:
|
| 604 |
-
latent_img = placeholder
|
| 605 |
-
|
| 606 |
-
latent_gallery.append(latent_img)
|
| 607 |
-
|
| 608 |
-
# Yield latent preview immediately
|
| 609 |
-
yield None, latent_gallery, LOGS
|
| 610 |
-
|
| 611 |
-
# --- Final image decoding ---
|
| 612 |
-
try:
|
| 613 |
-
with torch.no_grad():
|
| 614 |
-
final_img_tensor = pipe.vae.decode(latents).sample
|
| 615 |
-
final_img_tensor = (final_img_tensor / 2 + 0.5).clamp(0, 1)
|
| 616 |
-
final_img_tensor = final_img_tensor.cpu().permute(0, 2, 3, 1)[0]
|
| 617 |
-
final_img = Image.fromarray((final_img_tensor.numpy() * 255).astype('uint8'))
|
| 618 |
-
except Exception:
|
| 619 |
-
final_img = placeholder
|
| 620 |
-
|
| 621 |
-
final_gallery.append(final_img)
|
| 622 |
-
LOGS.append("β
Advanced latent pipeline succeeded.")
|
| 623 |
-
|
| 624 |
-
# Save latents to dict and upload to Hugging Face
|
| 625 |
-
latent_dict = {"latents": latents.cpu(), "prompt": prompt, "seed": seed}
|
| 626 |
-
try:
|
| 627 |
-
hf_url = upload_latents_to_hf(latent_dict, filename=f"latents_{seed}.pt")
|
| 628 |
-
LOGS.append(f"πΉ Latents uploaded: {hf_url}")
|
| 629 |
-
except Exception as e:
|
| 630 |
-
LOGS.append(f"β οΈ Failed to upload latents: {e}")
|
| 631 |
-
|
| 632 |
-
yield final_img, latent_gallery, LOGS
|
| 633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
except Exception as e:
|
| 635 |
-
LOGS.append(f"β οΈ
|
| 636 |
-
LOGS.append("π Switching to standard pipeline...")
|
| 637 |
-
|
| 638 |
-
try:
|
| 639 |
-
output = pipe(
|
| 640 |
-
prompt=prompt,
|
| 641 |
-
height=height,
|
| 642 |
-
width=width,
|
| 643 |
-
num_inference_steps=steps,
|
| 644 |
-
guidance_scale=guidance_scale,
|
| 645 |
-
generator=generator,
|
| 646 |
-
)
|
| 647 |
-
final_img = output.images[0]
|
| 648 |
-
final_gallery.append(final_img)
|
| 649 |
-
latent_gallery.append(final_img) # fallback preview
|
| 650 |
-
LOGS.append("β
Standard pipeline succeeded.")
|
| 651 |
-
yield final_img, latent_gallery, LOGS
|
| 652 |
-
|
| 653 |
-
except Exception as e2:
|
| 654 |
-
LOGS.append(f"β Standard pipeline failed: {e2}")
|
| 655 |
-
final_gallery.append(placeholder)
|
| 656 |
-
latent_gallery.append(placeholder)
|
| 657 |
-
yield placeholder, latent_gallery, LOGS
|
| 658 |
|
| 659 |
except Exception as e:
|
| 660 |
-
LOGS.append(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 661 |
final_gallery.append(placeholder)
|
| 662 |
latent_gallery.append(placeholder)
|
| 663 |
yield placeholder, latent_gallery, LOGS
|
|
|
|
| 589 |
latent_gallery = []
|
| 590 |
final_gallery = []
|
| 591 |
|
| 592 |
+
# --- Try generating latent previews ---
|
| 593 |
try:
|
| 594 |
+
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
+
# Decode latent tensor to PIL for preview
|
| 597 |
+
try:
|
| 598 |
+
with torch.no_grad():
|
| 599 |
+
latent_img_tensor = pipe.vae.decode(latents).sample # [1, 3, H, W]
|
| 600 |
+
latent_img_tensor = (latent_img_tensor / 2 + 0.5).clamp(0, 1)
|
| 601 |
+
latent_img_tensor = latent_img_tensor.cpu().permute(0, 2, 3, 1)[0] # HWC
|
| 602 |
+
latent_img = Image.fromarray((latent_img_tensor.numpy() * 255).astype('uint8'))
|
| 603 |
+
except Exception:
|
| 604 |
+
latent_img = placeholder
|
| 605 |
+
|
| 606 |
+
latent_gallery.append(latent_img)
|
| 607 |
+
yield None, latent_gallery, LOGS # show preview immediately
|
| 608 |
+
|
| 609 |
+
# Save latents to HF for later testing
|
| 610 |
+
latent_dict = {"latents": latents.cpu(), "prompt": prompt, "seed": seed}
|
| 611 |
+
try:
|
| 612 |
+
hf_url = upload_latents_to_hf(latent_dict, filename=f"latents_{seed}.pt")
|
| 613 |
+
LOGS.append(f"πΉ Latents uploaded: {hf_url}")
|
| 614 |
except Exception as e:
|
| 615 |
+
LOGS.append(f"β οΈ Failed to upload latents: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
except Exception as e:
|
| 618 |
+
LOGS.append(f"β οΈ Latent generation failed: {e}")
|
| 619 |
+
latent_gallery.append(placeholder)
|
| 620 |
+
yield None, latent_gallery, LOGS
|
| 621 |
+
|
| 622 |
+
# --- Final image: completely untouched, uses standard pipeline ---
|
| 623 |
+
try:
|
| 624 |
+
output = pipe(
|
| 625 |
+
prompt=prompt,
|
| 626 |
+
height=height,
|
| 627 |
+
width=width,
|
| 628 |
+
num_inference_steps=steps,
|
| 629 |
+
guidance_scale=guidance_scale,
|
| 630 |
+
generator=generator,
|
| 631 |
+
)
|
| 632 |
+
final_img = output.images[0]
|
| 633 |
+
final_gallery.append(final_img)
|
| 634 |
+
latent_gallery.append(final_img) # fallback preview if needed
|
| 635 |
+
LOGS.append("β
Standard pipeline succeeded.")
|
| 636 |
+
yield final_img, latent_gallery, LOGS
|
| 637 |
+
|
| 638 |
+
except Exception as e2:
|
| 639 |
+
LOGS.append(f"β Standard pipeline failed: {e2}")
|
| 640 |
final_gallery.append(placeholder)
|
| 641 |
latent_gallery.append(placeholder)
|
| 642 |
yield placeholder, latent_gallery, LOGS
|