rahul7star commited on
Commit
99363bd
Β·
verified Β·
1 Parent(s): 81fb0d6

Tesring latent

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +61 -37
app_quant_latent.py CHANGED
@@ -560,71 +560,91 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
560
  device = "cuda"
561
  generator = torch.Generator(device).manual_seed(int(seed))
562
 
563
- # placeholders
564
  placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
565
  latent_gallery = []
566
  final_gallery = []
567
 
568
  try:
569
- # --- Try advanced latent mode ---
 
 
570
  try:
571
  latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
572
 
573
  for i, t in enumerate(pipe.scheduler.timesteps):
574
- # Step-wise denoising
575
  with torch.no_grad():
576
- noise_pred = pipe.unet(latents, t, encoder_hidden_states=pipe.get_text_embeddings(prompt))["sample"]
 
 
 
 
577
  latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"]
578
 
579
- # Convert latent to preview image
580
  try:
581
- latent_img = latent_to_image(latents) # returns single PIL image
582
  except Exception:
583
  latent_img = placeholder
584
 
585
  latent_gallery.append(latent_img)
586
 
587
- # Yield intermediate update: final gallery empty for now
588
- yield None, latent_gallery, final_gallery, LOGS
 
 
 
 
589
 
590
- # decode final image after all timesteps
 
 
591
  final_img = pipe.decode_latents(latents)[0]
592
  final_gallery.append(final_img)
593
  LOGS.append("βœ… Advanced latent pipeline succeeded.")
594
- yield final_img, latent_gallery, final_gallery, LOGS
595
 
 
 
 
 
 
 
 
 
 
 
596
  except Exception as e:
597
  LOGS.append(f"⚠️ Advanced latent mode failed: {e}")
598
  LOGS.append("πŸ” Switching to standard pipeline...")
599
 
600
- # Standard pipeline fallback
601
- try:
602
- output = pipe(
603
- prompt=prompt,
604
- height=height,
605
- width=width,
606
- num_inference_steps=steps,
607
- guidance_scale=guidance_scale,
608
- generator=generator,
609
- )
610
- final_img = output.images[0]
611
- final_gallery.append(final_img)
612
- latent_gallery.append(final_img) # optionally show in latent gallery as last step
613
- LOGS.append("βœ… Standard pipeline succeeded.")
614
- yield final_img, latent_gallery, final_gallery, LOGS
615
-
616
- except Exception as e2:
617
- LOGS.append(f"❌ Standard pipeline failed: {e2}")
618
- final_gallery.append(placeholder)
619
- latent_gallery.append(placeholder)
620
- yield placeholder, latent_gallery, final_gallery, LOGS
621
 
622
  except Exception as e:
623
  LOGS.append(f"❌ Total failure: {e}")
624
- final_gallery.append(placeholder)
625
- latent_gallery.append(placeholder)
626
- yield placeholder, latent_gallery, final_gallery, LOGS
627
 
 
 
 
 
 
628
 
629
 
630
 
@@ -941,9 +961,13 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
941
 
942
  # Wire the button AFTER all components exist
943
  run_btn.click(
944
- generate_image,
945
- inputs=[prompt, height, width, steps, seed],
946
- outputs=[final_image, latent_gallery, logs_box]
 
 
 
 
947
  )
948
 
949
  demo.launch()
 
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
570
+ # ==========================================================
571
  try:
572
  latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
573
 
574
  for i, t in enumerate(pipe.scheduler.timesteps):
 
575
  with torch.no_grad():
576
+ noise_pred = pipe.unet(
577
+ latents, t,
578
+ encoder_hidden_states=pipe.get_text_embeddings(prompt)
579
+ )["sample"]
580
+
581
  latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"]
582
 
583
+ # Convert latents to image
584
  try:
585
+ latent_img = latent_to_image(latents)
586
  except Exception:
587
  latent_img = placeholder
588
 
589
  latent_gallery.append(latent_img)
590
 
591
+ # πŸ”₯ STREAM UPDATE: (final_image=None for now)
592
+ yield {
593
+ "final_image": None,
594
+ "latent_gallery": latent_gallery,
595
+ "logs_box": "\n".join(LOGS),
596
+ }
597
 
598
+ # ----------------------
599
+ # Final decode
600
+ # ----------------------
601
  final_img = pipe.decode_latents(latents)[0]
602
  final_gallery.append(final_img)
603
  LOGS.append("βœ… Advanced latent pipeline succeeded.")
 
604
 
605
+ # πŸ”₯ Final output
606
+ yield {
607
+ "final_image": final_img,
608
+ "latent_gallery": latent_gallery,
609
+ "logs_box": "\n".join(LOGS),
610
+ }
611
+
612
+ # ==========================================================
613
+ # STANDARD PIPELINE (fallback)
614
+ # ==========================================================
615
  except Exception as e:
616
  LOGS.append(f"⚠️ Advanced latent mode failed: {e}")
617
  LOGS.append("πŸ” Switching to standard pipeline...")
618
 
619
+ output = pipe(
620
+ prompt=prompt,
621
+ height=height,
622
+ width=width,
623
+ num_inference_steps=steps,
624
+ guidance_scale=guidance_scale,
625
+ generator=generator,
626
+ )
627
+
628
+ final_img = output.images[0]
629
+ final_gallery.append(final_img)
630
+ latent_gallery.append(final_img)
631
+ LOGS.append("βœ… Standard pipeline succeeded.")
632
+
633
+ yield {
634
+ "final_image": final_img,
635
+ "latent_gallery": latent_gallery,
636
+ "logs_box": "\n".join(LOGS),
637
+ }
 
 
638
 
639
  except Exception as e:
640
  LOGS.append(f"❌ Total failure: {e}")
641
+ placeholder_img = placeholder
 
 
642
 
643
+ yield {
644
+ "final_image": placeholder_img,
645
+ "latent_gallery": [placeholder_img],
646
+ "logs_box": "\n".join(LOGS),
647
+ }
648
 
649
 
650
 
 
961
 
962
  # Wire the button AFTER all components exist
963
  run_btn.click(
964
+ fn=generate_image,
965
+ inputs=[prompt, height, width, steps, seed],
966
+ outputs={
967
+ "final_image": final_image,
968
+ "latent_gallery": latent_gallery,
969
+ "logs_box": logs_box,
970
+ }
971
  )
972
 
973
  demo.launch()