Spaces:
Paused
Paused
Update app_quant_latent.py
Browse files552 unmodified lines5535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935943 unmodified lines598599600601602603604605606607608609610611612613614615616617618552 unmodified lines@spaces.GPUdef generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):LOGS = []latents = Noneimage = Nonegallery = []# placeholder image if all failsplaceholder = Image.new("RGB", (width, height), color=(255, 255, 255))print(prompt)try:generator = torch.Generator(device).manual_seed(int(seed))# -------------------------------# Try advanced latent extraction# -------------------------------try:latents = safe_get_latents(pipe, height, width, generator, device, LOGS)output = pipe(prompt=prompt,height=height,width=width,num_inference_steps=steps,guidance_scale=guidance_scale,generator=generator,latents=latents)image = output.images[0]gallery = [image]LOGS.extend(getattr(pipe, "_latents_log", []))LOGS.append("β Advanced latent pipeline succeeded.")except Exception as e:LOGS.append(f"β οΈ Latent mode failed: {e}")LOGS.append("π Switching to standard pipeline...")try:output = pipe(prompt=prompt,3 unmodified linesguidance_scale=guidance_scale,generator=generator,)image = output.images[0]gallery = [image]LOGS.append("β Standard pipeline succeeded.")except Exception as e2:LOGS.append(f"β Standard pipeline failed: {e2}")image = placeholdergallery = [image]return image, gallery, LOGSexcept Exception as e:LOGS.append(f"β Total failure: {e}")return placeholder, [placeholder], LOGS@spaces.GPUdef generate_image_backup(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):"""552 unmodified lines5535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975983 unmodified lines602603604605606607608609610611612613614615616617618619620621622623624552 unmodified lines@spaces.GPUdef generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):LOGS = []device = "cuda"generator = torch.Generator(device).manual_seed(int(seed))# placeholdersplaceholder = Image.new("RGB", (width, height), color=(255, 255, 255))latent_gallery = []final_gallery = []try:# --- Try advanced latent mode ---try:latents = safe_get_latents(pipe, height, width, generator, device, LOGS)for i, t in enumerate(pipe.scheduler.timesteps):# Step-wise denoisingwith torch.no_grad():noise_pred = pipe.unet(latents, t, encoder_hidden_states=pipe.get_text_embeddings(prompt))["sample"]latents = pipe.scheduler.step(noise_pred, t, latents)["prev_sample"]# Convert latent to preview imagetry:latent_img = latent_to_image(latents, pipe.vae)[0]except Exception:latent_img = placeholderlatent_gallery.append(latent_img)# Yield intermediate update: latents updated, final gallery emptyyield None, latent_gallery, final_gallery, LOGS# decode final imagefinal_img = pipe.decode_latents(latents)[0]final_gallery.append(final_img)LOGS.append("β Advanced latent pipeline succeeded.")yield final_img, latent_gallery, final_gallery, LOGSexcept Exception as e:LOGS.append(f"β οΈ Advanced latent mode failed: {e}")LOGS.append("π Switching to standard pipeline...")# Standard pipeline fallbacktry:output = pipe(prompt=prompt,3 unmodified linesguidance_scale=guidance_scale,generator=generator,)final_img = output.images[0]final_gallery.append(final_img)latent_gallery.append(final_img) # optionally show in latent gallery as last stepLOGS.append("β Standard pipeline succeeded.")yield final_img, latent_gallery, final_gallery, LOGSexcept Exception as e2:LOGS.append(f"β Standard pipeline failed: {e2}")final_gallery.append(placeholder)latent_gallery.append(placeholder)yield placeholder, latent_gallery, final_gallery, LOGSexcept Exception as e:LOGS.append(f"β Total failure: {e}")final_gallery.append(placeholder)latent_gallery.append(placeholder)yield placeholder, latent_gallery, final_gallery, LOGS@spaces.GPUdef generate_image_backup(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):"""
app_quant_latent.py+37-31