rahul7star commited on
Commit
2314c25
Β·
verified Β·
1 Parent(s): 8c054ac

Update app_quant_latent.py

Browse files
Files changed (1) hide show
  1. app_quant_latent.py +66 -1
app_quant_latent.py CHANGED
@@ -247,9 +247,74 @@ log_system_stats("AFTER PIPELINE BUILD")
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
 
247
 
248
 
249
  from PIL import Image
250
+ @spaces.GPU
251
+ def generate_image(prompt, height, width, steps, seed, guidance_scale=0.0):
252
+
253
+ LOGS = []
254
+ latents = None
255
+ image = None
256
+ gallery = []
257
+
258
+ # safe placeholder image
259
+ placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
260
+
261
+ try:
262
+ generator = torch.Generator(device).manual_seed(int(seed))
263
+
264
+ # -------------------------------
265
+ # Try advanced latent extractor
266
+ # -------------------------------
267
+ try:
268
+ latents = safe_get_latents(pipe, height, width, generator, device, LOGS)
269
+
270
+ output = pipe(
271
+ prompt=prompt,
272
+ height=height,
273
+ width=width,
274
+ num_inference_steps=steps,
275
+ guidance_scale=guidance_scale,
276
+ generator=generator,
277
+ latents=latents
278
+ )
279
+
280
+ image = output.images[0]
281
+ gallery = [image]
282
+ LOGS.append("βœ… Advanced latent pipeline succeeded.")
283
+
284
+ except Exception as e:
285
+ LOGS.append(f"⚠️ Latent mode failed: {e}")
286
+ LOGS.append("πŸ” Switching to standard pipeline...")
287
+
288
+ # -------------------------------
289
+ # Standard generation fallback
290
+ # -------------------------------
291
+ try:
292
+ output = pipe(
293
+ prompt=prompt,
294
+ height=height,
295
+ width=width,
296
+ num_inference_steps=steps,
297
+ guidance_scale=guidance_scale,
298
+ generator=generator,
299
+ )
300
+ image = output.images[0]
301
+ gallery = [image]
302
+ LOGS.append("βœ… Standard pipeline succeeded.")
303
+
304
+ except Exception as e2:
305
+ LOGS.append(f"❌ Standard pipeline failed: {e2}")
306
+ image = placeholder
307
+ gallery = [image]
308
+
309
+ # final safe return
310
+ return image, gallery, LOGS
311
+
312
+ except Exception as e:
313
+ LOGS.append(f"❌ Total failure: {e}")
314
+ return placeholder, [placeholder], LOGS
315
 
316
  @spaces.GPU
317
+ def generate_image_backup(prompt, height, width, steps, seed, guidance_scale=0.0, return_latents=False):
318
  """
319
  Robust dual pipeline:
320
  - Advanced latent generation first