Spaces:
Running
on
Zero
Running
on
Zero
Update app_lora.py
Browse files- app_lora.py +38 -76
app_lora.py
CHANGED
|
@@ -754,90 +754,52 @@ def generate_imagenegative(prompt, height, width, steps, seed, guidance_scale=7.
|
|
| 754 |
yield placeholder, latent_gallery, LOGS
|
| 755 |
|
| 756 |
|
|
|
|
| 757 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
generator = torch.Generator(device).manual_seed(int(seed))
|
| 762 |
|
| 763 |
-
|
| 764 |
-
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 768 |
|
|
|
|
| 769 |
try:
|
| 770 |
-
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
|
| 774 |
-
num_previews = min(10, steps)
|
| 775 |
-
preview_indices = torch.linspace(0, steps - 1, num_previews).long()
|
| 776 |
-
|
| 777 |
-
for i, step_idx in enumerate(preview_indices):
|
| 778 |
-
try:
|
| 779 |
-
with torch.no_grad():
|
| 780 |
-
# --- Z-Image Turbo-style denoise simulation ---
|
| 781 |
-
t = 1.0 - (i / num_previews) # linear decay [1.0 -> 0.0]
|
| 782 |
-
noise_scale = t ** 0.5 # reduce noise over steps (sqrt for smoother)
|
| 783 |
-
denoise_latent = latents * t + torch.randn_like(latents) * noise_scale
|
| 784 |
-
|
| 785 |
-
# Move to VAE device & dtype
|
| 786 |
-
denoise_latent = denoise_latent.to(pipe.vae.device).to(pipe.vae.dtype)
|
| 787 |
-
|
| 788 |
-
# Decode latent to image
|
| 789 |
-
decoded = pipe.vae.decode(denoise_latent, return_dict=False)[0]
|
| 790 |
-
decoded = (decoded / 2 + 0.5).clamp(0, 1)
|
| 791 |
-
decoded = decoded.cpu().permute(0, 2, 3, 1).float().numpy()
|
| 792 |
-
decoded = (decoded * 255).round().astype("uint8")
|
| 793 |
-
latent_img = Image.fromarray(decoded[0])
|
| 794 |
-
|
| 795 |
-
except Exception as e:
|
| 796 |
-
LOGS.append(f"⚠️ Latent preview decode failed: {e}")
|
| 797 |
-
latent_img = placeholder
|
| 798 |
-
|
| 799 |
-
latent_gallery.append(latent_img)
|
| 800 |
-
|
| 801 |
-
# Keep last 5 latents only
|
| 802 |
-
last_latents.append(denoise_latent.cpu().clone())
|
| 803 |
-
if len(last_latents) > 5:
|
| 804 |
-
last_latents.pop(0)
|
| 805 |
-
|
| 806 |
-
# Show only last 5 previews in UI
|
| 807 |
-
yield None, latent_gallery[-5:], LOGS
|
| 808 |
-
|
| 809 |
-
# Optionally: upload last 5 latents
|
| 810 |
-
# latent_dict = {"latents": last_latents, "prompt": prompt, "seed": seed}
|
| 811 |
-
# hf_url = upload_latents_to_hf(latent_dict, filename=f"latents_last5_{seed}.pt")
|
| 812 |
-
# LOGS.append(f"🔹 Last 5 latents uploaded: {hf_url}")
|
| 813 |
|
| 814 |
except Exception as e:
|
| 815 |
-
|
| 816 |
-
latent_gallery.append(placeholder)
|
| 817 |
-
yield None, latent_gallery[-5:], LOGS
|
| 818 |
|
| 819 |
-
|
| 820 |
-
|
| 821 |
-
output = pipe(
|
| 822 |
-
prompt=prompt,
|
| 823 |
-
height=height,
|
| 824 |
-
width=width,
|
| 825 |
-
num_inference_steps=steps,
|
| 826 |
-
guidance_scale=guidance_scale,
|
| 827 |
-
generator=generator,
|
| 828 |
-
)
|
| 829 |
-
final_img = output.images[0]
|
| 830 |
-
final_gallery.append(final_img)
|
| 831 |
-
latent_gallery.append(final_img)
|
| 832 |
-
LOGS.append("✅ Standard pipeline succeeded.")
|
| 833 |
-
yield final_img, latent_gallery[-5:] + [final_img], LOGS # last 5 previews + final
|
| 834 |
-
|
| 835 |
-
except Exception as e2:
|
| 836 |
-
LOGS.append(f"❌ Standard pipeline failed: {e2}")
|
| 837 |
-
final_gallery.append(placeholder)
|
| 838 |
-
latent_gallery.append(placeholder)
|
| 839 |
-
yield placeholder, latent_gallery[-5:] + [placeholder], LOGS
|
| 840 |
|
|
|
|
| 841 |
|
| 842 |
|
| 843 |
# this is astable vesopn tha can gen final and a noise to latent
|
|
|
|
| 754 |
yield placeholder, latent_gallery, LOGS
|
| 755 |
|
| 756 |
|
| 757 |
+
@spaces.GPU
|
| 758 |
def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 759 |
+
|
| 760 |
+
print(prompt)
|
| 761 |
+
|
|
|
|
| 762 |
|
| 763 |
+
if randomize_seed:
|
| 764 |
+
seed = torch.randint(0, 2**32 - 1, (1,)).item()
|
| 765 |
+
log(f"Randomized Seed → {seed}")
|
| 766 |
+
else:
|
| 767 |
+
log(f"Seed: {seed}")
|
| 768 |
+
|
| 769 |
+
|
| 770 |
+
|
| 771 |
+
# Debug pipe info
|
| 772 |
+
log(pipeline_debug_info(pipe))
|
| 773 |
+
|
| 774 |
+
generator = torch.Generator().manual_seed(seed)
|
| 775 |
+
|
| 776 |
+
log("Running pipeline forward()...")
|
| 777 |
+
result = pipe(
|
| 778 |
+
prompt=prompt,
|
| 779 |
+
height=int(height),
|
| 780 |
+
width=int(width),
|
| 781 |
+
num_inference_steps=int(num_inference_steps),
|
| 782 |
+
guidance_scale=0.0,
|
| 783 |
+
generator=generator,
|
| 784 |
+
max_sequence_length=1024,
|
| 785 |
+
num_images_per_prompt=2,
|
| 786 |
+
output_type="pil",
|
| 787 |
+
)
|
| 788 |
|
| 789 |
+
# Correct latent diagnostics (Z-Image uses VAE + Transformer)
|
| 790 |
try:
|
| 791 |
+
log(f"VAE latent channels: {pipe.vae.config.latent_channels}")
|
| 792 |
+
log(f"VAE scaling factor: {pipe.vae.config.scaling_factor}")
|
| 793 |
+
log(f"Transformer latent size: {pipe.transformer.config.sample_size}")
|
| 794 |
+
log(latent_shape_info(height, width, pipe))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
|
| 796 |
except Exception as e:
|
| 797 |
+
log(f"Latent diagnostics error: {e}")
|
|
|
|
|
|
|
| 798 |
|
| 799 |
+
log("Pipeline finished.")
|
| 800 |
+
log("Returning images...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 801 |
|
| 802 |
+
return result.images, seed, log_buffer.getvalue()
|
| 803 |
|
| 804 |
|
| 805 |
# this is astable vesopn tha can gen final and a noise to latent
|