Spaces:
Paused
Paused
Update app_quant_latent1.py
Browse files- app_quant_latent1.py +5 -82
app_quant_latent1.py
CHANGED
|
@@ -373,88 +373,11 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
|
|
| 373 |
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 374 |
print(prompt)
|
| 375 |
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
#
|
| 380 |
-
|
| 381 |
-
# -------------------------------
|
| 382 |
-
try:
|
| 383 |
-
latents, latent_err = safe_generate_with_latents(
|
| 384 |
-
transformer=transformer,
|
| 385 |
-
vae=vae,
|
| 386 |
-
text_encoder=text_encoder,
|
| 387 |
-
tokenizer=tokenizer,
|
| 388 |
-
scheduler=scheduler,
|
| 389 |
-
pipe=pipe,
|
| 390 |
-
prompt=prompt,
|
| 391 |
-
height=height,
|
| 392 |
-
width=width,
|
| 393 |
-
steps=steps,
|
| 394 |
-
guidance_scale=guidance_scale,
|
| 395 |
-
negative_prompt="",
|
| 396 |
-
num_images_per_prompt=1,
|
| 397 |
-
generator=generator,
|
| 398 |
-
cfg_normalization=False,
|
| 399 |
-
cfg_truncation=1.0,
|
| 400 |
-
max_sequence_length=4096,
|
| 401 |
-
)
|
| 402 |
-
|
| 403 |
-
if latent_err is None:
|
| 404 |
-
log("β
Latent generator succeeded.")
|
| 405 |
-
try:
|
| 406 |
-
# Decode latents to image
|
| 407 |
-
shift_factor = getattr(vae.config, "shift_factor", 0.0) or 0.0
|
| 408 |
-
dec = (latents.to(vae.dtype) / vae.config.scaling_factor) + shift_factor
|
| 409 |
-
image = vae.decode(dec, return_dict=False)[0]
|
| 410 |
-
|
| 411 |
-
image = (image / 2 + 0.5).clamp(0, 1)
|
| 412 |
-
image = image.cpu().permute(0, 2, 3, 1).numpy()
|
| 413 |
-
image = (image * 255).round().astype("uint8")
|
| 414 |
-
from PIL import Image
|
| 415 |
-
image = Image.fromarray(image[0])
|
| 416 |
-
|
| 417 |
-
log("π’ Final image decoded from latent generator.")
|
| 418 |
-
return image, latents, LOGS
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
except Exception as decode_error:
|
| 422 |
-
log(f"β οΈ Latent decode failed: {decode_error}")
|
| 423 |
-
log("π Falling back to standard pipeline...")
|
| 424 |
-
|
| 425 |
-
image = output.images[0]
|
| 426 |
-
gallery = [image]
|
| 427 |
-
LOGS.append("β
Advanced latent pipeline succeeded.")
|
| 428 |
-
|
| 429 |
-
except Exception as e:
|
| 430 |
-
LOGS.append(f"β οΈ Latent mode failed: {e}")
|
| 431 |
-
LOGS.append("π Switching to standard pipeline...")
|
| 432 |
-
image = placeholder
|
| 433 |
-
gallery = [image]
|
| 434 |
-
# ========================================================== # π© STANDARD PIPELINE FALLBACK (Never fails) # ==========================================================
|
| 435 |
-
try:
|
| 436 |
-
output = pipe(
|
| 437 |
-
prompt=prompt,
|
| 438 |
-
height=height,
|
| 439 |
-
width=width,
|
| 440 |
-
num_inference_steps=steps,
|
| 441 |
-
guidance_scale=guidance_scale,
|
| 442 |
-
generator=generator,
|
| 443 |
-
)
|
| 444 |
-
image = output.images[0]
|
| 445 |
-
gallery = [image]
|
| 446 |
-
LOGS.append("β
Standard pipeline succeeded.")
|
| 447 |
-
|
| 448 |
-
except Exception as e2:
|
| 449 |
-
LOGS.append(f"β Standard pipeline failed: {e2}")
|
| 450 |
-
image = placeholder
|
| 451 |
-
gallery = [image]
|
| 452 |
-
|
| 453 |
-
return image, gallery, LOGS
|
| 454 |
-
|
| 455 |
-
except Exception as e:
|
| 456 |
-
LOGS.append(f"β Total failure: {e}")
|
| 457 |
-
return placeholder, [placeholder], LOGS
|
| 458 |
|
| 459 |
|
| 460 |
|
|
|
|
| 373 |
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 374 |
print(prompt)
|
| 375 |
|
| 376 |
+
latents, latent_err = safe_generate_with_latents( transformer=transformer, vae=vae, text_encoder=text_encoder, tokenizer=tokenizer, scheduler=scheduler,
|
| 377 |
+
pipe=pipe, prompt=prompt, height=height, width=width, steps=steps, guidance_scale=guidance_scale, negative_prompt="", num_images_per_prompt=1, generator=generator, cfg_normalization=False, cfg_truncation=1.0, max_sequence_length=4096, )
|
| 378 |
+
if latent_err is None: log("β
Latent generator succeeded.")
|
| 379 |
+
try: # Decode latents to image shift_factor = getattr(vae.config, "shift_factor", 0.0) or 0.0 dec = (latents.to(vae.dtype) / vae.config.scaling_factor) +
|
| 380 |
+
shift_factor image = vae.decode(dec, return_dict=False)[0] image = (image / 2 + 0.5).clamp(0, 1) image = image.cpu().permute(0, 2, 3, 1).numpy() image = (image * 255).round().astype("uint8") from PIL import Image image = Image.fromarray(image[0]) log("π’ Final image decoded from latent generator.") return image, latents, LOGS except Exception as decode_error: log(f"β οΈ Latent decode failed: {decode_error}") log("π Falling back to standard pipeline...") else: log(f"β οΈ Latent generator failed: {latent_err}") log("π Switching to standard pipeline...") # ========================================================== # π© STANDARD PIPELINE FALLBACK (Never fails) # ========================================================== try: output = pipe( prompt=prompt, height=height, width=width, num_inference_steps=steps, guidance_scale=guidance_scale, generator=generator, ) image = output.images[0] log("π’ Standard pipeline succeeded.") return image, None, LOGS except Exception as e: log(f"β Standard pipeline failed: {e}") return None, None, LOGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
|
| 382 |
|
| 383 |
|