JS6969 commited on
Commit
7477470
·
verified ·
1 Parent(s): 104f55b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +285 -0
app.py ADDED
@@ -0,0 +1,285 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py Upscale Images (Real-ESRGAN)
2
+ # ---- TorchVision shim (keeps basicsr happy if torchvision isn't installed) ----
3
+ import sys, types
4
+ try:
5
+ import torchvision.transforms.functional_tensor as _ft # noqa: F401
6
+ except Exception:
7
+ import torch
8
+ _mod = types.ModuleType("torchvision.transforms.functional_tensor")
9
+ def rgb_to_grayscale(img: "torch.Tensor", num_output_channels: int = 1) -> "torch.Tensor":
10
+ if not torch.is_tensor(img):
11
+ raise TypeError("rgb_to_grayscale expects a torch.Tensor")
12
+ if img.ndim < 3 or img.shape[-3] != 3:
13
+ raise ValueError(f"expected tensor with C=3 as the third-from-last dim, got shape {tuple(img.shape)}")
14
+ r, g, b = img[..., -3, :, :], img[..., -2, :, :], img[..., -1, :, :]
15
+ gray = 0.2989*r + 0.5870*g + 0.1140*b
16
+ return torch.stack([gray, gray, gray], dim=-3) if num_output_channels == 3 else gray.unsqueeze(-3)
17
+ _mod.rgb_to_grayscale = rgb_to_grayscale
18
+ sys.modules["torchvision.transforms.functional_tensor"] = _mod
19
+ # ------------------------------------------------------------------------------
20
+
21
+ import os, time, zipfile, tempfile, shutil, base64
22
+ from pathlib import Path
23
+ from typing import List, Optional, Tuple
24
+ import gradio as gr
25
+ import numpy as np
26
+ import cv2
27
+ from PIL import Image
28
+
29
+ from basicsr.archs.rrdbnet_arch import RRDBNet as _RRDBNet
30
+ from basicsr.utils.download_util import load_file_from_url
31
+ from realesrgan import RealESRGANer
32
+ from realesrgan.archs.srvgg_arch import SRVGGNetCompact
33
+
34
+ def try_load_logo_b64() -> str:
35
+ try:
36
+ with open("bifrost_logo.png", "rb") as f:
37
+ import base64
38
+ return base64.b64encode(f.read()).decode("utf-8")
39
+ except Exception:
40
+ return ""
41
+ LOGO_B64 = try_load_logo_b64()
42
+
43
+ def render_logo_html(px: int = 96) -> str:
44
+ img = f'<img src="data:image/png;base64,{LOGO_B64}" style="height:{px}px;width:auto;" />' if LOGO_B64 else ""
45
+ return f"""
46
+ <div style="display:flex;align-items:center;gap:16px;">
47
+ {img}
48
+ <div>
49
+ <div style="font-size:1.6rem;font-weight:800;">Bifröst · Upscale Images</div>
50
+ <div style="opacity:0.8;">Real-ESRGAN (batch click with progress)</div>
51
+ </div>
52
+ </div>
53
+ <hr>
54
+ """
55
+
56
+ _num = __import__("re").compile(r'(\d+)')
57
+ def _natural_key(p: Path | str):
58
+ s = str(p)
59
+ return [int(t) if t.isdigit() else t.lower() for t in _num.split(s)]
60
+ def sample_paths(paths: List[Path] | List[str], n: int = 30) -> List[str]:
61
+ if not paths: return []
62
+ paths = sorted(paths, key=_natural_key)
63
+ total = len(paths); n = max(1, min(n, total))
64
+ if n == total: return [str(p) for p in paths]
65
+ step = (total - 1) / (n - 1); idxs = [round(i * step) for i in range(n)]
66
+ out, seen = [], set()
67
+ for i in idxs:
68
+ if i not in seen:
69
+ out.append(str(paths[int(i)])); seen.add(int(i))
70
+ return out
71
+
72
+ def render_progress(pct: float, label: str = "") -> str:
73
+ pct = max(0.0, min(100.0, pct))
74
+ return f'''<div style="width:100%;border:1px solid #ddd;border-radius:8px;overflow:hidden;height:18px;">
75
+ <div style="height:100%;width:{pct:.1f}%;"></div></div>
76
+ <div style="font-size:12px;opacity:.8;margin-top:4px;">{label} {pct:.1f}%</div>'''
77
+
78
+ def build_rrdb(scale: int, num_block: int):
79
+ return _RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=num_block, num_grow_ch=32, scale=scale)
80
+
81
+ def _weights_dir() -> str:
82
+ wdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "weights")
83
+ os.makedirs(wdir, exist_ok=True)
84
+ return wdir
85
+
86
+ def get_realesrganer(model_id: str, scale: int, tile: int, half: bool, device: str = "cpu") -> RealESRGANer:
87
+ wdir = _weights_dir()
88
+ if model_id == "x4plus":
89
+ model = build_rrdb(scale=4, num_block=23); netscale = 4
90
+ urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth"]
91
+ model_path = os.path.join(wdir, "RealESRGAN_x4plus.pth")
92
+ dni_weight = None
93
+ elif model_id == "x4plus-anime":
94
+ model = build_rrdb(scale=4, num_block=6); netscale = 4
95
+ urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth"]
96
+ model_path = os.path.join(wdir, "RealESRGAN_x4plus_anime_6B.pth")
97
+ dni_weight = None
98
+ elif model_id == "x2plus":
99
+ model = build_rrdb(scale=2, num_block=23); netscale = 2
100
+ urls = ["https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth"]
101
+ model_path = os.path.join(wdir, "RealESRGAN_x2plus.pth")
102
+ dni_weight = None
103
+ else:
104
+ raise ValueError(f"Unknown model_id: {model_id}")
105
+
106
+ for url in urls:
107
+ fname = os.path.basename(url)
108
+ if not os.path.isfile(os.path.join(wdir, fname)):
109
+ load_file_from_url(url=url, model_dir=wdir, progress=True)
110
+
111
+ gpu_id = 0 if (device == "cuda") else None
112
+ return RealESRGANer(
113
+ scale=netscale, model_path=model_path, dni_weight=dni_weight, model=model,
114
+ tile=tile or 256, tile_pad=10, pre_pad=10, half=bool(half and device == "cuda"), gpu_id=gpu_id
115
+ )
116
+
117
+ def _ensure_dir(p: Path) -> Path:
118
+ p.mkdir(parents=True, exist_ok=True); return p
119
+
120
+ def _save_zip_of_dir(dir_path: Path, zip_path: Path) -> str:
121
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
122
+ for p in sorted(dir_path.glob("*.*"), key=_natural_key):
123
+ if p.suffix.lower() in [".jpg", ".jpeg", ".png"]:
124
+ zf.write(p, p.name)
125
+ return str(zip_path)
126
+
127
+ def _list_image_paths_from_upload(files: List[gr.File] | None) -> List[str]:
128
+ if not files: return []
129
+ return [str(Path(f.name)) for f in files if Path(f.name).suffix.lower() in [".jpg",".jpeg",".png"]]
130
+
131
+ def _build_gallery_from_dir(dir_path: Path, n: int = 30) -> List[str]:
132
+ paths = sorted(list(dir_path.glob("*.jpg")) + list(dir_path.glob("*.png")), key=_natural_key)
133
+ return sample_paths(paths, n)
134
+
135
+ def map_ui_model_to_internal(ui_name: str) -> str:
136
+ return {
137
+ "RealESRGAN_x4plus": "x4plus",
138
+ "RealESRGAN_x4plus_anime_6B": "x4plus-anime",
139
+ "RealESRGAN_x2plus": "x2plus",
140
+ "RealESRNet_x4plus": "x4plus",
141
+ "realesr-general-x4v3": "x4plus",
142
+ }.get(ui_name, "x4plus")
143
+
144
+ def clamp_scale_for_model(outscale: int, model_id: str) -> int:
145
+ return 2 if model_id == "x2plus" else 4
146
+
147
+ def step2_prepare_sources(frames_list, uploaded_imgs, max_images):
148
+ src = _list_image_paths_from_upload(uploaded_imgs) or (frames_list or [])
149
+ if not src:
150
+ return [], "", 0, 0, "No images found. Upload files first.", render_progress(0.0, "Idle")
151
+ try:
152
+ max_images = int(max_images or 0)
153
+ except Exception:
154
+ max_images = 0
155
+ if max_images > 0:
156
+ src = src[:max_images]
157
+ work = Path(tempfile.mkdtemp(prefix="up_manual_"))
158
+ out_dir = _ensure_dir(work / "upscaled")
159
+ total = len(src); done_idx = 0
160
+ return src, str(out_dir), done_idx, total, f"Sources loaded: {total} image(s). Click 'Process Next Batch'.", render_progress(0.0, "Ready"))
161
+
162
+ def step2_process_next_batch(
163
+ up_src_paths, up_out_dir, up_done_idx, up_total,
164
+ ui_model_name, outscale, tile, precision, denoise_strength, face_enhance, batch_size,
165
+ ):
166
+ if not up_src_paths or not up_out_dir:
167
+ yield None, None, "Load sources first.", render_progress(0.0, "Idle"), up_done_idx, up_out_dir
168
+ return
169
+
170
+ model_id = map_ui_model_to_internal(ui_model_name)
171
+ scale = clamp_scale_for_model(int(outscale or 4), model_id)
172
+ device = "cuda" if os.environ.get("CUDA_VISIBLE_DEVICES") else "cpu"
173
+ half = (precision == "half") and (device == "cuda")
174
+ tile = int(tile or 256)
175
+ batch_size = max(1, int(batch_size or 8))
176
+ upsampler = get_realesrganer(model_id, scale, tile, half, device=device)
177
+
178
+ face_enhancer = None
179
+ if face_enhance:
180
+ try:
181
+ from gfpgan import GFPGANer
182
+ face_enhancer = GFPGANer(
183
+ model_path="https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth",
184
+ upscale=scale, arch="clean", channel_multiplier=2, bg_upsampler=upsampler
185
+ )
186
+ except Exception as e:
187
+ print("GFPGAN load failed:", e)
188
+
189
+ start = int(up_done_idx or 0)
190
+ end = min(start + batch_size, int(up_total or 0))
191
+ out_dir = Path(up_out_dir)
192
+
193
+ if start >= up_total:
194
+ gallery = _build_gallery_from_dir(out_dir, 30)
195
+ zip_file = _save_zip_of_dir(out_dir, Path(out_dir.parent) / "upscaled.zip")
196
+ yield gallery, zip_file, "All images processed.", render_progress(100.0, "Done"), start, up_out_dir
197
+ return
198
+
199
+ batch_paths = up_src_paths[start:end]
200
+ total_in_batch = len(batch_paths)
201
+ t0 = time.time()
202
+
203
+ for idx, fp in enumerate(batch_paths, start=1):
204
+ try:
205
+ with Image.open(fp) as im:
206
+ img = im.convert("RGB")
207
+ cv_img = np.array(img)
208
+ if face_enhancer:
209
+ _, _, output = face_enhancer.enhance(cv_img, has_aligned=False, only_center_face=False, paste_back=True)
210
+ else:
211
+ output, _ = upsampler.enhance(cv_img, outscale=scale, denoise_strength=float(denoise_strength or 0.5))
212
+ Image.fromarray(output).save(out_dir / (Path(fp).stem + ".jpg"), quality=95)
213
+ except Exception as e:
214
+ print("Upscale error:", e)
215
+
216
+ elapsed = time.time() - t0
217
+ pct_batch = (idx / total_in_batch) * 100.0
218
+ eta = (total_in_batch - idx) * (elapsed / max(1, idx))
219
+ label = (f"Batch: {idx}/{total_in_batch} · ~{eta:.1f}s ETA · "
220
+ f"global {start+idx}/{up_total} (x{scale}, model={ui_model_name})")
221
+ gallery = _build_gallery_from_dir(out_dir, 30)
222
+ zip_file = _save_zip_of_dir(out_dir, Path(out_dir.parent) / "upscaled.zip")
223
+ yield gallery, zip_file, label, render_progress(pct_batch, f"Upscaling {pct_batch:.0f}% (batch)"), start+idx, up_out_dir
224
+
225
+ next_idx = end
226
+ pct_global = (next_idx / up_total) * 100.0 if up_total else 100.0
227
+ gallery = _build_gallery_from_dir(out_dir, 30)
228
+ zip_file = _save_zip_of_dir(out_dir, Path(out_dir.parent) / "upscaled.zip")
229
+ yield gallery, zip_file, f"Processed batch of {total_in_batch}. {next_idx}/{up_total} done.", render_progress(pct_global, "Upscaling… (global)"), next_idx, up_out_dir
230
+
231
+ def build_ui():
232
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
233
+ gr.HTML(render_logo_html(88))
234
+ gr.Markdown("Upload images and upscale with Real-ESRGAN. Process in batches with live progress.")
235
+
236
+ frames_state = gr.State([]) # Not used here but kept for simple wiring
237
+ up_src_paths_state = gr.State([])
238
+ up_out_dir_state = gr.State("")
239
+ up_done_idx_state = gr.State(0)
240
+ up_total_state = gr.State(0)
241
+
242
+ imgs_override = gr.Files(label="Upload images (JPG/PNG)", file_types=[".jpg",".jpeg",".png"], type="filepath")
243
+
244
+ with gr.Accordion("Upscaling options", open=True):
245
+ with gr.Row():
246
+ ui_model_name = gr.Dropdown(
247
+ label="Upscaler model",
248
+ choices=["RealESRGAN_x4plus", "RealESRNet_x4plus", "RealESRGAN_x4plus_anime_6B", "RealESRGAN_x2plus", "realesr-general-x4v3"],
249
+ value="RealESRGAN_x4plus"
250
+ )
251
+ denoise_strength = gr.Slider(0, 1, value=0.5, step=0.1, label="Denoise (only general-x4v3)")
252
+ outscale = gr.Slider(1, 6, value=4, step=1, label="Resolution upscale")
253
+ face_enhance = gr.Checkbox(value=False, label="Face Enhancement (GFPGAN)")
254
+ with gr.Row():
255
+ tile = gr.Number(value=256, label="Tile size (try 128 if OOM; 0=auto)")
256
+ precision = gr.Dropdown(["auto", "half", "full"], value="auto", label="Precision (GPU=half, CPU=full)")
257
+ with gr.Row():
258
+ batch_size = gr.Number(value=12, precision=0, label="Batch size per click")
259
+ max_images = gr.Number(value=0, precision=0, label="Max images to process (0 = all)")
260
+
261
+ with gr.Row():
262
+ btn_prepare = gr.Button("Load / Reset Sources", variant="secondary")
263
+ btn_next = gr.Button("Process Next Batch", variant="primary")
264
+
265
+ prog = gr.HTML(render_progress(0.0, "Idle"))
266
+ gallery_up = gr.Gallery(label="Upscaled preview (30 sampled)", columns=6, height=480)
267
+ zip_up = gr.File(label="Download upscaled ZIP")
268
+ details = gr.Markdown("")
269
+
270
+ btn_prepare.click(
271
+ step2_prepare_sources,
272
+ inputs=[frames_state, imgs_override, max_images],
273
+ outputs=[up_src_paths_state, up_out_dir_state, up_done_idx_state, up_total_state, details, prog]
274
+ )
275
+
276
+ btn_next.click(
277
+ step2_process_next_batch,
278
+ inputs=[up_src_paths_state, up_out_dir_state, up_done_idx_state, up_total_state, ui_model_name, outscale, tile, precision, denoise_strength, face_enhance, batch_size],
279
+ outputs=[gallery_up, zip_up, details, prog, up_done_idx_state, up_out_dir_state]
280
+ )
281
+
282
+ return demo
283
+
284
+ if __name__ == "__main__":
285
+ build_ui().queue().launch()