Spaces:
Paused
Paused
Tesring latent
Browse files- 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 |
-
#
|
|
|
|
|
|
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 577 |
latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"]
|
| 578 |
|
| 579 |
-
# Convert
|
| 580 |
try:
|
| 581 |
-
latent_img = latent_to_image(latents)
|
| 582 |
except Exception:
|
| 583 |
latent_img = placeholder
|
| 584 |
|
| 585 |
latent_gallery.append(latent_img)
|
| 586 |
|
| 587 |
-
#
|
| 588 |
-
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
|
| 590 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 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 |
-
|
| 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 |
-
|
| 945 |
-
|
| 946 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|