Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,15 +4,24 @@
|
|
| 4 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
import sys, types
|
| 6 |
try:
|
| 7 |
-
# If old path exists, do nothing
|
| 8 |
import torchvision.transforms.functional_tensor as _ft # noqa: F401
|
| 9 |
except Exception:
|
| 10 |
-
# Map to the new API location
|
| 11 |
from torchvision.transforms import functional as _F
|
| 12 |
_mod = types.ModuleType("torchvision.transforms.functional_tensor")
|
| 13 |
_mod.rgb_to_grayscale = _F.rgb_to_grayscale
|
| 14 |
sys.modules["torchvision.transforms.functional_tensor"] = _mod
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
# Standard imports
|
| 18 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -105,7 +114,9 @@ def model_tip_text(model_name: str) -> str:
|
|
| 105 |
|
| 106 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 107 |
# Core upscaling
|
|
|
|
| 108 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 109 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 110 |
if img is None:
|
| 111 |
return
|
|
@@ -141,13 +152,11 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 141 |
weights_dir = os.path.join(ROOT_DIR, 'weights')
|
| 142 |
os.makedirs(weights_dir, exist_ok=True)
|
| 143 |
|
| 144 |
-
local_paths = []
|
| 145 |
for url in file_url:
|
| 146 |
fname = os.path.basename(url)
|
| 147 |
local_path = os.path.join(weights_dir, fname)
|
| 148 |
if not os.path.isfile(local_path):
|
| 149 |
-
|
| 150 |
-
local_paths.append(local_path)
|
| 151 |
|
| 152 |
if model_name == 'realesr-general-x4v3':
|
| 153 |
base_path = os.path.join(weights_dir, 'realesr-general-x4v3.pth')
|
|
@@ -172,7 +181,7 @@ def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
|
| 172 |
model_path=model_path,
|
| 173 |
dni_weight=dni_weight,
|
| 174 |
model=model,
|
| 175 |
-
tile=256,
|
| 176 |
tile_pad=10,
|
| 177 |
pre_pad=10,
|
| 178 |
half=bool(use_cuda),
|
|
@@ -274,6 +283,7 @@ def main():
|
|
| 274 |
input_image.change(fn=image_properties, inputs=input_image, outputs=input_image_properties)
|
| 275 |
model_name.change(fn=model_tip_text, inputs=model_name, outputs=model_tips)
|
| 276 |
|
|
|
|
| 277 |
restore_btn.click(
|
| 278 |
fn=realesrgan,
|
| 279 |
inputs=[input_image, model_name, denoise_strength, face_enhance, outscale],
|
|
@@ -283,7 +293,8 @@ def main():
|
|
| 283 |
|
| 284 |
gr.Markdown("") # spacer
|
| 285 |
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
|
| 289 |
if __name__ == "__main__":
|
|
|
|
| 4 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
import sys, types
|
| 6 |
try:
|
|
|
|
| 7 |
import torchvision.transforms.functional_tensor as _ft # noqa: F401
|
| 8 |
except Exception:
|
|
|
|
| 9 |
from torchvision.transforms import functional as _F
|
| 10 |
_mod = types.ModuleType("torchvision.transforms.functional_tensor")
|
| 11 |
_mod.rgb_to_grayscale = _F.rgb_to_grayscale
|
| 12 |
sys.modules["torchvision.transforms.functional_tensor"] = _mod
|
| 13 |
|
| 14 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
# Spaces ZeroGPU decorator (safe no-op locally)
|
| 16 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
+
try:
|
| 18 |
+
import spaces
|
| 19 |
+
GPU = spaces.GPU
|
| 20 |
+
except Exception:
|
| 21 |
+
def GPU(*args, **kwargs):
|
| 22 |
+
def _wrap(f): return f
|
| 23 |
+
return _wrap
|
| 24 |
+
|
| 25 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 26 |
# Standard imports
|
| 27 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 114 |
|
| 115 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
# Core upscaling
|
| 117 |
+
# Decorated for Hugging Face Spaces ZeroGPU
|
| 118 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
+
@GPU() # <-- lets Spaces know this function uses GPU; safe no-op locally
|
| 120 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 121 |
if img is None:
|
| 122 |
return
|
|
|
|
| 152 |
weights_dir = os.path.join(ROOT_DIR, 'weights')
|
| 153 |
os.makedirs(weights_dir, exist_ok=True)
|
| 154 |
|
|
|
|
| 155 |
for url in file_url:
|
| 156 |
fname = os.path.basename(url)
|
| 157 |
local_path = os.path.join(weights_dir, fname)
|
| 158 |
if not os.path.isfile(local_path):
|
| 159 |
+
load_file_from_url(url=url, model_dir=weights_dir, progress=True)
|
|
|
|
| 160 |
|
| 161 |
if model_name == 'realesr-general-x4v3':
|
| 162 |
base_path = os.path.join(weights_dir, 'realesr-general-x4v3.pth')
|
|
|
|
| 181 |
model_path=model_path,
|
| 182 |
dni_weight=dni_weight,
|
| 183 |
model=model,
|
| 184 |
+
tile=256, # VRAM-safe default; lower to 128 if OOM
|
| 185 |
tile_pad=10,
|
| 186 |
pre_pad=10,
|
| 187 |
half=bool(use_cuda),
|
|
|
|
| 283 |
input_image.change(fn=image_properties, inputs=input_image, outputs=input_image_properties)
|
| 284 |
model_name.change(fn=model_tip_text, inputs=model_name, outputs=model_tips)
|
| 285 |
|
| 286 |
+
# IMPORTANT: call the @GPU-decorated function here
|
| 287 |
restore_btn.click(
|
| 288 |
fn=realesrgan,
|
| 289 |
inputs=[input_image, model_name, denoise_strength, face_enhance, outscale],
|
|
|
|
| 293 |
|
| 294 |
gr.Markdown("") # spacer
|
| 295 |
|
| 296 |
+
# Disable SSR as suggested by the log
|
| 297 |
+
demo.launch(ssr_mode=False) # share=True if you want a public link
|
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|