Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files- app_quant_latent.py +35 -39
app_quant_latent.py
CHANGED
|
@@ -560,70 +560,66 @@ 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 |
-
|
| 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 =
|
| 584 |
|
| 585 |
latent_gallery.append(latent_img)
|
| 586 |
|
| 587 |
-
#
|
| 588 |
-
yield latent_gallery, None, LOGS
|
| 589 |
|
| 590 |
-
# decode
|
| 591 |
-
|
| 592 |
-
|
| 593 |
LOGS.append("β
Advanced latent pipeline succeeded.")
|
| 594 |
-
|
|
|
|
|
|
|
| 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 |
-
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 |
-
|
| 625 |
-
|
| 626 |
-
yield placeholder, latent_gallery, final_gallery, LOGS
|
| 627 |
|
| 628 |
|
| 629 |
|
|
|
|
| 560 |
device = "cuda"
|
| 561 |
generator = torch.Generator(device).manual_seed(int(seed))
|
| 562 |
|
|
|
|
|
|
|
| 563 |
latent_gallery = []
|
| 564 |
+
final_image = None
|
| 565 |
|
| 566 |
try:
|
| 567 |
+
# Try advanced latent mode
|
| 568 |
try:
|
| 569 |
latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
|
| 570 |
|
| 571 |
for i, t in enumerate(pipe.scheduler.timesteps):
|
|
|
|
| 572 |
with torch.no_grad():
|
| 573 |
+
noise_pred = pipe.unet(
|
| 574 |
+
latents,
|
| 575 |
+
t,
|
| 576 |
+
encoder_hidden_states=pipe.get_text_embeddings(prompt)
|
| 577 |
+
)["sample"]
|
| 578 |
+
|
| 579 |
latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"]
|
| 580 |
|
| 581 |
+
# Convert to preview
|
| 582 |
try:
|
| 583 |
+
latent_img = latent_to_image(latents)
|
| 584 |
except Exception:
|
| 585 |
+
latent_img = Image.new("RGB", (width, height), "white")
|
| 586 |
|
| 587 |
latent_gallery.append(latent_img)
|
| 588 |
|
| 589 |
+
# ---- LIVE UPDATE ----
|
| 590 |
+
yield latent_gallery, None, "\n".join(LOGS)
|
| 591 |
|
| 592 |
+
# Final decode
|
| 593 |
+
final = pipe.decode_latents(latents)[0]
|
| 594 |
+
final_image = final
|
| 595 |
LOGS.append("β
Advanced latent pipeline succeeded.")
|
| 596 |
+
|
| 597 |
+
# ---- FINAL OUTPUT ----
|
| 598 |
+
yield latent_gallery, final_image, "\n".join(LOGS)
|
| 599 |
|
| 600 |
except Exception as e:
|
| 601 |
LOGS.append(f"β οΈ Advanced latent mode failed: {e}")
|
| 602 |
LOGS.append("π Switching to standard pipeline...")
|
| 603 |
|
| 604 |
+
output = pipe(
|
| 605 |
+
prompt=prompt,
|
| 606 |
+
height=height,
|
| 607 |
+
width=width,
|
| 608 |
+
num_inference_steps=steps,
|
| 609 |
+
guidance_scale=guidance_scale,
|
| 610 |
+
generator=generator,
|
| 611 |
+
)
|
| 612 |
+
|
| 613 |
+
final_image = output.images[0]
|
| 614 |
+
latent_gallery.append(final_image)
|
| 615 |
+
LOGS.append("β
Standard pipeline succeeded.")
|
| 616 |
+
|
| 617 |
+
yield latent_gallery, final_image, "\n".join(LOGS)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 618 |
|
| 619 |
except Exception as e:
|
| 620 |
LOGS.append(f"β Total failure: {e}")
|
| 621 |
+
placeholder = Image.new("RGB", (width, height), "white")
|
| 622 |
+
yield [placeholder], placeholder, "\n".join(LOGS)
|
|
|
|
| 623 |
|
| 624 |
|
| 625 |
|