Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,8 +30,9 @@ import cv2
|
|
| 30 |
import numpy
|
| 31 |
import os
|
| 32 |
import random
|
|
|
|
| 33 |
|
| 34 |
-
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 35 |
from basicsr.utils.download_util import load_file_from_url
|
| 36 |
|
| 37 |
from realesrgan import RealESRGANer
|
|
@@ -112,30 +113,78 @@ def model_tip_text(model_name: str) -> str:
|
|
| 112 |
return tips.get(model_name, "")
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
# Core upscaling
|
| 117 |
# Decorated for Hugging Face Spaces ZeroGPU
|
| 118 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
-
@GPU() #
|
| 120 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 121 |
if img is None:
|
| 122 |
return
|
| 123 |
|
| 124 |
# ----- Select backbone + weights -----
|
| 125 |
if model_name == 'RealESRGAN_x4plus':
|
| 126 |
-
model =
|
| 127 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
| 128 |
|
| 129 |
elif model_name == 'RealESRNet_x4plus':
|
| 130 |
-
model =
|
| 131 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
|
| 132 |
|
| 133 |
elif model_name == 'RealESRGAN_x4plus_anime_6B':
|
| 134 |
-
model =
|
| 135 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
|
| 136 |
|
| 137 |
elif model_name == 'RealESRGAN_x2plus':
|
| 138 |
-
model =
|
| 139 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
|
| 140 |
|
| 141 |
elif model_name == 'realesr-general-x4v3':
|
|
@@ -283,7 +332,6 @@ def main():
|
|
| 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,8 +341,8 @@ def main():
|
|
| 293 |
|
| 294 |
gr.Markdown("") # spacer
|
| 295 |
|
| 296 |
-
# Disable SSR
|
| 297 |
-
demo.launch(ssr_mode=False) # share=True
|
| 298 |
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|
|
|
|
| 30 |
import numpy
|
| 31 |
import os
|
| 32 |
import random
|
| 33 |
+
import inspect
|
| 34 |
|
| 35 |
+
from basicsr.archs.rrdbnet_arch import RRDBNet as _RRDBNet
|
| 36 |
from basicsr.utils.download_util import load_file_from_url
|
| 37 |
|
| 38 |
from realesrgan import RealESRGANer
|
|
|
|
| 113 |
return tips.get(model_name, "")
|
| 114 |
|
| 115 |
|
| 116 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 117 |
+
# RRDBNet builder that tolerates different Basicsr signatures
|
| 118 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
+
def build_rrdb(scale: int, num_block: int):
|
| 120 |
+
"""
|
| 121 |
+
Creates an RRDBNet across several possible constructor signatures used by basicsr/realesrgan.
|
| 122 |
+
Tries, in order:
|
| 123 |
+
1) keyword style (num_in_ch/num_out_ch/num_feat/num_block/num_grow_ch/scale)
|
| 124 |
+
2) alt keyword style (in_nc/out_nc/nf/nb/gc/sf)
|
| 125 |
+
3) positional with gc before scale
|
| 126 |
+
4) positional with scale before gc
|
| 127 |
+
"""
|
| 128 |
+
# Try keyword: "num_*" + "scale"
|
| 129 |
+
try:
|
| 130 |
+
return _RRDBNet(
|
| 131 |
+
num_in_ch=3, num_out_ch=3,
|
| 132 |
+
num_feat=64, num_block=num_block,
|
| 133 |
+
num_grow_ch=32, scale=scale
|
| 134 |
+
)
|
| 135 |
+
except TypeError:
|
| 136 |
+
pass
|
| 137 |
+
|
| 138 |
+
# Try keyword: "in_nc/out_nc" + "sf"
|
| 139 |
+
try:
|
| 140 |
+
return _RRDBNet(
|
| 141 |
+
in_nc=3, out_nc=3,
|
| 142 |
+
nf=64, nb=num_block,
|
| 143 |
+
gc=32, sf=scale
|
| 144 |
+
)
|
| 145 |
+
except TypeError:
|
| 146 |
+
pass
|
| 147 |
+
|
| 148 |
+
# Inspect parameters to guess positional order
|
| 149 |
+
params = list(inspect.signature(_RRDBNet).parameters.keys())
|
| 150 |
+
|
| 151 |
+
# Common positional (gc, scale) order
|
| 152 |
+
try:
|
| 153 |
+
return _RRDBNet(3, 3, 64, num_block, 32, scale)
|
| 154 |
+
except TypeError:
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
# Alternate positional (scale, gc) order
|
| 158 |
+
try:
|
| 159 |
+
return _RRDBNet(3, 3, 64, num_block, scale, 32)
|
| 160 |
+
except TypeError as e:
|
| 161 |
+
raise TypeError(f"RRDBNet signature not recognized: {e}")
|
| 162 |
+
|
| 163 |
+
|
| 164 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
# Core upscaling
|
| 166 |
# Decorated for Hugging Face Spaces ZeroGPU
|
| 167 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 168 |
+
@GPU() # lets Spaces know this function uses GPU; safe no-op locally
|
| 169 |
def realesrgan(img, model_name, denoise_strength, face_enhance, outscale):
|
| 170 |
if img is None:
|
| 171 |
return
|
| 172 |
|
| 173 |
# ----- Select backbone + weights -----
|
| 174 |
if model_name == 'RealESRGAN_x4plus':
|
| 175 |
+
model = build_rrdb(scale=4, num_block=23); netscale = 4
|
| 176 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth']
|
| 177 |
|
| 178 |
elif model_name == 'RealESRNet_x4plus':
|
| 179 |
+
model = build_rrdb(scale=4, num_block=23); netscale = 4
|
| 180 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.1/RealESRNet_x4plus.pth']
|
| 181 |
|
| 182 |
elif model_name == 'RealESRGAN_x4plus_anime_6B':
|
| 183 |
+
model = build_rrdb(scale=4, num_block=6); netscale = 4
|
| 184 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth']
|
| 185 |
|
| 186 |
elif model_name == 'RealESRGAN_x2plus':
|
| 187 |
+
model = build_rrdb(scale=2, num_block=23); netscale = 2
|
| 188 |
file_url = ['https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth']
|
| 189 |
|
| 190 |
elif model_name == 'realesr-general-x4v3':
|
|
|
|
| 332 |
input_image.change(fn=image_properties, inputs=input_image, outputs=input_image_properties)
|
| 333 |
model_name.change(fn=model_tip_text, inputs=model_name, outputs=model_tips)
|
| 334 |
|
|
|
|
| 335 |
restore_btn.click(
|
| 336 |
fn=realesrgan,
|
| 337 |
inputs=[input_image, model_name, denoise_strength, face_enhance, outscale],
|
|
|
|
| 341 |
|
| 342 |
gr.Markdown("") # spacer
|
| 343 |
|
| 344 |
+
# Disable SSR (ZeroGPU + Gradio logs suggested turning this off)
|
| 345 |
+
demo.launch(ssr_mode=False) # set share=True for a public link
|
| 346 |
|
| 347 |
|
| 348 |
if __name__ == "__main__":
|