rahul7star commited on
Commit
8c054ac
Β·
verified Β·
1 Parent(s): 6b68641

Update app_quant_latent.py

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +21 -15
app_quant_latent.py CHANGED
@@ -246,26 +246,31 @@ log_system_stats("AFTER PIPELINE BUILD")
246
 
247
 
248
 
 
 
249
  @spaces.GPU
250
  def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
251
  """
252
- Robust dual pipeline image generation:
253
- - Tries advanced latent method first
254
- - Falls back to standard pipeline if latent fails
255
- - Always returns an image
256
- - Optionally returns latents
257
- - Logs progress and errors
258
  """
259
 
260
  LOGS = []
261
  image = None
262
  latents = None
 
 
 
 
263
 
264
  try:
265
  generator = torch.Generator(device).manual_seed(int(seed))
266
 
267
  # -------------------------------
268
- # Attempt advanced latent generation
269
  # -------------------------------
270
  try:
271
  batch_size = 1
@@ -294,6 +299,8 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
294
  latents=latents
295
  )
296
  image = output.images[0]
 
 
297
  LOGS.append("βœ… Advanced latent generation succeeded.")
298
 
299
  # -------------------------------
@@ -302,6 +309,7 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
302
  except Exception as e_latent:
303
  LOGS.append(f"⚠️ Advanced latent generation failed: {e_latent}")
304
  LOGS.append("πŸ” Falling back to standard pipeline...")
 
305
  try:
306
  output = pipe(
307
  prompt=prompt,
@@ -312,23 +320,21 @@ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, retur
312
  generator=generator
313
  )
314
  image = output.images[0]
 
315
  LOGS.append("βœ… Standard pipeline generation succeeded.")
316
  except Exception as e_standard:
317
  LOGS.append(f"❌ Standard pipeline generation failed: {e_standard}")
318
- # If everything fails, image remains None
 
319
 
320
  # -------------------------------
321
- # Return results
322
  # -------------------------------
323
- if return_latents and latents is not None:
324
- return image, latents, LOGS
325
- else:
326
- return image, image, LOGS
327
 
328
  except Exception as e:
329
  LOGS.append(f"❌ Inference failed entirely: {e}")
330
- return None, None,LOGS
331
-
332
 
333
  # ============================================================
334
  # UI
 
246
 
247
 
248
 
249
+ from PIL import Image
250
+
251
  @spaces.GPU
252
  def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
253
  """
254
+ Robust dual pipeline:
255
+ - Advanced latent generation first
256
+ - Fallback to standard pipeline if latent fails
257
+ - Always returns final image
258
+ - Returns gallery (latents or final image) and logs
 
259
  """
260
 
261
  LOGS = []
262
  image = None
263
  latents = None
264
+ gallery = []
265
+
266
+ # Keep a placeholder original image (white) in case everything fails
267
+ original_image = Image.new("RGB", (width, height), color=(255, 255, 255))
268
 
269
  try:
270
  generator = torch.Generator(device).manual_seed(int(seed))
271
 
272
  # -------------------------------
273
+ # Try advanced latent generation
274
  # -------------------------------
275
  try:
276
  batch_size = 1
 
299
  latents=latents
300
  )
301
  image = output.images[0]
302
+ gallery = [image] if image else []
303
+
304
  LOGS.append("βœ… Advanced latent generation succeeded.")
305
 
306
  # -------------------------------
 
309
  except Exception as e_latent:
310
  LOGS.append(f"⚠️ Advanced latent generation failed: {e_latent}")
311
  LOGS.append("πŸ” Falling back to standard pipeline...")
312
+
313
  try:
314
  output = pipe(
315
  prompt=prompt,
 
320
  generator=generator
321
  )
322
  image = output.images[0]
323
+ gallery = [image] if image else []
324
  LOGS.append("βœ… Standard pipeline generation succeeded.")
325
  except Exception as e_standard:
326
  LOGS.append(f"❌ Standard pipeline generation failed: {e_standard}")
327
+ image = original_image # Always return some image
328
+ gallery = [image]
329
 
330
  # -------------------------------
331
+ # Return all 3 outputs
332
  # -------------------------------
333
+ return image, gallery, LOGS
 
 
 
334
 
335
  except Exception as e:
336
  LOGS.append(f"❌ Inference failed entirely: {e}")
337
+ return original_image, [original_image], LOGS
 
338
 
339
  # ============================================================
340
  # UI