JS6969 commited on
Commit
2ee5af8
Β·
verified Β·
1 Parent(s): 901db54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -32
app.py CHANGED
@@ -100,55 +100,48 @@ def render_progress(pct: float, label: str = "") -> str:
100
  <div style="font-size:12px;opacity:.8;margin-top:4px;">{label} {pct:.1f}%</div>'''
101
 
102
  def build_rrdb(scale: int, num_block: int):
103
- return _RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
104
- num_block=num_block, num_grow_ch=32, scale=scale)
105
 
106
- def _weights_dir() -> str:
107
- wdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "weights")
108
- os.makedirs(wdir, exist_ok=True)
109
- return wdir
110
-
111
- # ─────────────────────────────────────────────
112
- # Real-ESRGAN (CPU only)
113
- # ─────────────────────────────────────────────
114
- def get_realesrganer(model_id: str, scale: int, tile: int, half: bool, device: str = "cpu") -> RealESRGANer:
115
  wdir = _weights_dir()
 
116
  if model_id == "x4plus":
117
- model = build_rrdb(scale=4, num_block=23); netscale = 4
118
- urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"]
 
119
  model_path = os.path.join(wdir, "RealESRGAN_x4plus.pth")
120
- dni_weight = None
121
  elif model_id == "x4plus-anime":
122
- model = build_rrdb(scale=4, num_block=6); netscale = 4
123
- urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"]
 
124
  model_path = os.path.join(wdir, "RealESRGAN_x4plus_anime_6B.pth")
125
- dni_weight = None
126
  elif model_id == "x2plus":
127
- model = build_rrdb(scale=2, num_block=23); netscale = 2
128
- urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth"]
 
129
  model_path = os.path.join(wdir, "RealESRGAN_x2plus.pth")
130
- dni_weight = None
131
  else:
132
  raise ValueError(f"Unknown model_id: {model_id}")
133
 
134
- for url in urls:
135
- fname = os.path.basename(url)
136
- if not os.path.isfile(os.path.join(wdir, fname)):
137
- load_file_from_url(url=url, model_dir=wdir, progress=True)
 
 
138
 
139
- # πŸ”’ Force CPU only
140
- upsampler = RealESRGANer(
141
  scale=netscale,
142
  model_path=model_path,
143
- dni_weight=dni_weight,
144
  model=model,
145
- tile=tile or 256,
146
  tile_pad=10,
147
- pre_pad=10,
148
- half=False, # CPU cannot use half precision
149
- gpu_id=None # disable GPU completely
150
  )
151
- return upsampler
152
 
153
  # ─────────────────────────────────────────────
154
  # Step 2: Sources + Processing (batch click)
 
100
  <div style="font-size:12px;opacity:.8;margin-top:4px;">{label} {pct:.1f}%</div>'''
101
 
102
  def build_rrdb(scale: int, num_block: int):
103
+ return RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64,
104
+ num_block=num_block, num_grow_ch=32, scale=scale)
105
 
106
+ def get_realesrganer(model_id: str, scale: int, tile: int,
107
+ half: bool, device: str = "cpu") -> RealESRGANer:
 
 
 
 
 
 
 
108
  wdir = _weights_dir()
109
+
110
  if model_id == "x4plus":
111
+ model = build_rrdb(scale=4, num_block=23)
112
+ netscale = 4
113
+ url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"
114
  model_path = os.path.join(wdir, "RealESRGAN_x4plus.pth")
 
115
  elif model_id == "x4plus-anime":
116
+ model = build_rrdb(scale=4, num_block=6)
117
+ netscale = 4
118
+ url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"
119
  model_path = os.path.join(wdir, "RealESRGAN_x4plus_anime_6B.pth")
 
120
  elif model_id == "x2plus":
121
+ model = build_rrdb(scale=2, num_block=23)
122
+ netscale = 2
123
+ url = "https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth"
124
  model_path = os.path.join(wdir, "RealESRGAN_x2plus.pth")
 
125
  else:
126
  raise ValueError(f"Unknown model_id: {model_id}")
127
 
128
+ if not os.path.isfile(model_path):
129
+ load_file_from_url(url=url, model_dir=wdir, progress=True)
130
+
131
+ # auto device
132
+ device = "cuda" if torch.cuda.is_available() else "cpu"
133
+ half = half and device == "cuda"
134
 
135
+ return RealESRGANer(
 
136
  scale=netscale,
137
  model_path=model_path,
 
138
  model=model,
139
+ tile=tile,
140
  tile_pad=10,
141
+ pre_pad=0,
142
+ half=half,
143
+ device=device,
144
  )
 
145
 
146
  # ─────────────────────────────────────────────
147
  # Step 2: Sources + Processing (batch click)