gheinrich commited on
Commit
991484b
·
1 Parent(s): 9c74eea

Show a real denoising progress bar

Browse files

Re-enable track_tqdm and shim Gradio's Progress.tqdm to backfill the missing
length (from n_steps) and description onto the model's denoising bar, gated to
the generation phase so download/load bars are untouched.

Files changed (1) hide show
  1. app.py +55 -12
app.py CHANGED
@@ -57,6 +57,44 @@ except ImportError:
57
 
58
  spaces = _SpacesStub()
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  os.environ.setdefault("DEBUG_FIX_PADDING", "1")
61
  os.environ.setdefault("NOT_ALWASY_DO_2DPOOL", "1")
62
 
@@ -373,18 +411,23 @@ class T2IEngine:
373
  if progress is not None:
374
  progress(0.0, desc="Generating…")
375
 
 
 
376
  t0 = time.time()
377
- with torch.no_grad():
378
- with torch.inference_mode():
379
- result = self._model.text_to_image(
380
- prompt,
381
- tokenizer=self._tokenizer,
382
- **gen_cfg,
383
- image_resolution=int(image_resolution),
384
- n_tokens=n_tokens,
385
- disable_tqdm=progress is None,
386
- return_intermediate_steps=return_animation,
387
- )
 
 
 
388
  latency = time.time() - t0
389
 
390
  meta = (
@@ -434,7 +477,7 @@ def generate(
434
  micro_cond: str,
435
  return_animation: bool,
436
  enable_image_guard: bool,
437
- progress: gr.Progress = gr.Progress(),
438
  ):
439
  return engine.generate(
440
  prompt,
 
57
 
58
  spaces = _SpacesStub()
59
 
60
+
61
+ # The released model drives its denoising loop with a tqdm bar over a
62
+ # length-less iterable (`enumerate(...)` plus a separate `total=`). Gradio's
63
+ # track_tqdm ignores that explicit total and the bar has no description, so the
64
+ # UI shows a bogus "Downloading (incomplete total...)" indicator. This shim
65
+ # backfills the missing length/description onto the tracked bar, but only while
66
+ # a generation is in flight (so download bars during model load are untouched).
67
+ _DENOISE_PROGRESS = {"active": False, "total": None, "desc": "Generating image"}
68
+
69
+
70
+ def _install_progress_shim() -> None:
71
+ try:
72
+ from gradio import helpers as _gr_helpers
73
+ except Exception:
74
+ return
75
+ if getattr(_gr_helpers.Progress.tqdm, "_denoise_shim", False):
76
+ return
77
+ _orig_tqdm = _gr_helpers.Progress.tqdm
78
+
79
+ def _patched_tqdm(self, *args, **kwargs):
80
+ out = _orig_tqdm(self, *args, **kwargs)
81
+ try:
82
+ if _DENOISE_PROGRESS["active"] and getattr(self, "iterables", None):
83
+ ti = self.iterables[-1]
84
+ if getattr(ti, "length", None) in (None, 0) and _DENOISE_PROGRESS["total"]:
85
+ ti.length = _DENOISE_PROGRESS["total"]
86
+ if not getattr(ti, "desc", None):
87
+ ti.desc = _DENOISE_PROGRESS["desc"]
88
+ except Exception:
89
+ pass
90
+ return out
91
+
92
+ _patched_tqdm._denoise_shim = True
93
+ _gr_helpers.Progress.tqdm = _patched_tqdm
94
+
95
+
96
+ _install_progress_shim()
97
+
98
  os.environ.setdefault("DEBUG_FIX_PADDING", "1")
99
  os.environ.setdefault("NOT_ALWASY_DO_2DPOOL", "1")
100
 
 
411
  if progress is not None:
412
  progress(0.0, desc="Generating…")
413
 
414
+ _DENOISE_PROGRESS["total"] = int(n_steps)
415
+ _DENOISE_PROGRESS["active"] = progress is not None
416
  t0 = time.time()
417
+ try:
418
+ with torch.no_grad():
419
+ with torch.inference_mode():
420
+ result = self._model.text_to_image(
421
+ prompt,
422
+ tokenizer=self._tokenizer,
423
+ **gen_cfg,
424
+ image_resolution=int(image_resolution),
425
+ n_tokens=n_tokens,
426
+ disable_tqdm=progress is None,
427
+ return_intermediate_steps=return_animation,
428
+ )
429
+ finally:
430
+ _DENOISE_PROGRESS["active"] = False
431
  latency = time.time() - t0
432
 
433
  meta = (
 
477
  micro_cond: str,
478
  return_animation: bool,
479
  enable_image_guard: bool,
480
+ progress: gr.Progress = gr.Progress(track_tqdm=True),
481
  ):
482
  return engine.generate(
483
  prompt,