techfreakworm commited on
Commit
6d862f4
·
unverified ·
1 Parent(s): 6bd8e31

fix: depth preprocessor name + basicsr/torchvision import shim

Browse files

Two bugs caught after the HF Space redesign push:

1. ControlNet Depth preview always showed the raw image. controlnet_aux's
Processor registers depth under 'depth_midas', not 'midas'. The plain
'midas' name raised KeyError, which _preview_cn silently swallowed
(returning the raw input). Fix: use 'depth_midas' in preprocessors._run_depth.

2. Upscale crashed with ModuleNotFoundError on torchvision.transforms.
functional_tensor — basicsr (a realesrgan dep) imports that module, but
torchvision >=0.17 removed it. Fix: alias torchvision.transforms.functional
into sys.modules['torchvision.transforms.functional_tensor'] inside
upscale._realesrgan_4x before basicsr loads.

3. Hardened _preview_cn to log the underlying exception to stderr instead of
swallowing silently. The next preprocessor bug surfaces in HF logs.

Files changed (3) hide show
  1. app.py +9 -1
  2. preprocessors.py +3 -1
  3. upscale.py +9 -0
app.py CHANGED
@@ -105,12 +105,20 @@ def _preview_cn(image, mode):
105
 
106
  Wrapped in ``try/except`` so that a missing optional dep (controlnet_aux for
107
  Depth / Pose) never breaks the form — it just falls back to the raw input.
 
 
 
108
  """
109
  if image is None:
110
  return None
111
  try:
112
  return preprocessors.run(mode, image)
113
- except Exception:
 
 
 
 
 
114
  return image
115
 
116
 
 
105
 
106
  Wrapped in ``try/except`` so that a missing optional dep (controlnet_aux for
107
  Depth / Pose) never breaks the form — it just falls back to the raw input.
108
+ We DO log the exception to stderr so the next failure surfaces in the logs
109
+ rather than silently showing the user the original image (the previous
110
+ behavior hid a typo'd processor name for weeks).
111
  """
112
  if image is None:
113
  return None
114
  try:
115
  return preprocessors.run(mode, image)
116
+ except Exception as e:
117
+ import sys
118
+ import traceback
119
+
120
+ print(f"[preview_cn] {mode!r} failed: {e}", file=sys.stderr, flush=True)
121
+ traceback.print_exc(file=sys.stderr)
122
  return image
123
 
124
 
preprocessors.py CHANGED
@@ -35,7 +35,9 @@ def _run_canny(image: Image.Image) -> Image.Image:
35
 
36
 
37
  def _run_depth(image: Image.Image) -> Image.Image:
38
- proc = _get_processor("midas")
 
 
39
  out: Any = proc(image)
40
  if isinstance(out, Image.Image):
41
  return out.convert("RGB")
 
35
 
36
 
37
  def _run_depth(image: Image.Image) -> Image.Image:
38
+ # controlnet_aux's Processor takes "depth_midas", NOT "midas".
39
+ # Plain "midas" is not in its MODELS dict and raises KeyError.
40
+ proc = _get_processor("depth_midas")
41
  out: Any = proc(image)
42
  if isinstance(out, Image.Image):
43
  return out.convert("RGB")
upscale.py CHANGED
@@ -26,7 +26,16 @@ _MODEL_CACHE: dict[str, Any] = {}
26
 
27
  def _realesrgan_4x(model_path: Path | str, image: Image.Image) -> Image.Image:
28
  """Run RealESRGAN x4plus on ``image``. Caches the model in-process."""
 
 
29
  import numpy as np
 
 
 
 
 
 
 
30
  from basicsr.archs.rrdbnet_arch import RRDBNet
31
  from realesrgan import RealESRGANer
32
 
 
26
 
27
  def _realesrgan_4x(model_path: Path | str, image: Image.Image) -> Image.Image:
28
  """Run RealESRGAN x4plus on ``image``. Caches the model in-process."""
29
+ import sys
30
+
31
  import numpy as np
32
+ import torchvision.transforms.functional as _tvf
33
+
34
+ # basicsr (a realesrgan dep) imports torchvision.transforms.functional_tensor,
35
+ # which was removed in torchvision >=0.17. Alias the old path to the current
36
+ # module so basicsr's degradations import keeps working.
37
+ sys.modules.setdefault("torchvision.transforms.functional_tensor", _tvf)
38
+
39
  from basicsr.archs.rrdbnet_arch import RRDBNet
40
  from realesrgan import RealESRGANer
41