rahul7star commited on
Commit
342566d
·
verified ·
1 Parent(s): 9c8674b

Update app_quant_latent.py

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +79 -1
app_quant_latent.py CHANGED
@@ -687,7 +687,6 @@ def generate_image_all_latents(prompt, height, width, steps, seed, guidance_scal
687
  LOGS.append(f"❌ Standard pipeline failed: {e2}")
688
  yield placeholder, latent_gallery, LOGS
689
 
690
- # this is astable vesopn tha can gen final and a noise to latent
691
  @spaces.GPU
692
  def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
693
  LOGS = []
@@ -698,6 +697,85 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
698
  latent_gallery = []
699
  final_gallery = []
700
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
701
  # --- Generate latent previews ---
702
  try:
703
  latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
 
687
  LOGS.append(f"❌ Standard pipeline failed: {e2}")
688
  yield placeholder, latent_gallery, LOGS
689
 
 
690
  @spaces.GPU
691
  def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
692
  LOGS = []
 
697
  latent_gallery = []
698
  final_gallery = []
699
 
700
+ last_latents = [] # store last 5 preview latents
701
+
702
+ # --- Generate latent previews ---
703
+ try:
704
+ latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
705
+ latents = latents.float()
706
+
707
+ num_previews = min(10, steps)
708
+ preview_steps = torch.linspace(0, 1, num_previews)
709
+
710
+ for alpha in preview_steps:
711
+ try:
712
+ with torch.no_grad():
713
+ preview_latent = latents * alpha + latents * 0 # simple progression
714
+ preview_latent = preview_latent.to(pipe.vae.device).to(pipe.vae.dtype)
715
+
716
+ decoded = pipe.vae.decode(preview_latent, return_dict=False)[0]
717
+ decoded = (decoded / 2 + 0.5).clamp(0, 1)
718
+ decoded = decoded.cpu().permute(0, 2, 3, 1).float().numpy()
719
+ decoded = (decoded * 255).round().astype("uint8")
720
+ latent_img = Image.fromarray(decoded[0])
721
+
722
+ except Exception as e:
723
+ LOGS.append(f"⚠️ Latent preview decode failed: {e}")
724
+ latent_img = placeholder
725
+
726
+ latent_gallery.append(latent_img)
727
+
728
+ # Keep last 5 latents
729
+ last_latents.append(preview_latent.cpu().clone())
730
+ if len(last_latents) > 5:
731
+ last_latents.pop(0)
732
+
733
+ yield None, latent_gallery, LOGS
734
+
735
+ # Optionally: save only last 5 latents
736
+ # latent_dict = {"latents": last_latents, "prompt": prompt, "seed": seed}
737
+ # hf_url = upload_latents_to_hf(latent_dict, filename=f"latents_last5_{seed}.pt")
738
+ # LOGS.append(f"🔹 Last 5 latents uploaded: {hf_url}")
739
+
740
+ except Exception as e:
741
+ LOGS.append(f"⚠️ Latent generation failed: {e}")
742
+ latent_gallery.append(placeholder)
743
+ yield None, latent_gallery, LOGS
744
+
745
+ # --- Final image: standard pipeline ---
746
+ try:
747
+ output = pipe(
748
+ prompt=prompt,
749
+ height=height,
750
+ width=width,
751
+ num_inference_steps=steps,
752
+ guidance_scale=guidance_scale,
753
+ generator=generator,
754
+ )
755
+ final_img = output.images[0]
756
+ final_gallery.append(final_img)
757
+ latent_gallery.append(final_img) # fallback preview
758
+ LOGS.append("✅ Standard pipeline succeeded.")
759
+ yield final_img, latent_gallery, LOGS
760
+
761
+ except Exception as e2:
762
+ LOGS.append(f"❌ Standard pipeline failed: {e2}")
763
+ final_gallery.append(placeholder)
764
+ latent_gallery.append(placeholder)
765
+ yield placeholder, latent_gallery, LOGS
766
+
767
+
768
+ # this is astable vesopn tha can gen final and a noise to latent
769
+ @spaces.GPU
770
+ def generate_image_verygood_realnoise(prompt, height, width, steps, seed, guidance_scale=0.0):
771
+ LOGS = []
772
+ device = "cuda"
773
+ generator = torch.Generator(device).manual_seed(int(seed))
774
+
775
+ placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
776
+ latent_gallery = []
777
+ final_gallery = []
778
+
779
  # --- Generate latent previews ---
780
  try:
781
  latents = safe_get_latents(pipe, height, width, generator, device, LOGS)