remove stray app.py
Browse files
app.py
DELETED
|
@@ -1,104 +0,0 @@
|
|
| 1 |
-
"""NERVE — upscale & restore images with the released NERVE models.
|
| 2 |
-
|
| 3 |
-
Simple Space: pick a model, drop an image, get an uncompressed PNG back.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from __future__ import annotations
|
| 7 |
-
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import numpy as np
|
| 10 |
-
import spaces
|
| 11 |
-
import torch
|
| 12 |
-
from huggingface_hub import hf_hub_download
|
| 13 |
-
from PIL import Image
|
| 14 |
-
from safetensors.torch import load_file
|
| 15 |
-
|
| 16 |
-
from nerve_arch import nerve
|
| 17 |
-
|
| 18 |
-
REPO = "Phips/NERVE"
|
| 19 |
-
MODELS: dict[str, tuple[str, int]] = {
|
| 20 |
-
"NERVE 2x — release (clean input)": ("models/2x_NERVE_release.safetensors", 2),
|
| 21 |
-
"NERVE 4x — release (clean input)": ("models/4x_NERVE_release.safetensors", 4),
|
| 22 |
-
"NERVE 4x — OTF GAN (blurry / noisy / JPEG input)": (
|
| 23 |
-
"models/4x_NERVE_OTF_gan.safetensors",
|
| 24 |
-
4,
|
| 25 |
-
),
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
_cache: dict[str, torch.nn.Module] = {}
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def get_model(key: str) -> torch.nn.Module:
|
| 32 |
-
if key not in _cache:
|
| 33 |
-
path, scale = MODELS[key]
|
| 34 |
-
model = nerve(scale=scale)
|
| 35 |
-
model.load_state_dict(
|
| 36 |
-
load_file(hf_hub_download(REPO, path), device="cpu"), strict=True
|
| 37 |
-
)
|
| 38 |
-
_cache[key] = model.eval()
|
| 39 |
-
return _cache[key]
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
@torch.inference_mode()
|
| 43 |
-
def _run(model: torch.nn.Module, img: np.ndarray, scale: int) -> np.ndarray:
|
| 44 |
-
"""img: float32 HWC RGB in [0,1]; returns float32 HWC RGB in [0,1]."""
|
| 45 |
-
h, w = img.shape[:2]
|
| 46 |
-
tile = 512 # LR tile size; keeps VRAM low on ZeroGPU
|
| 47 |
-
overlap = 32
|
| 48 |
-
out = np.zeros((h * scale, w * scale, 3), dtype=np.float32)
|
| 49 |
-
for y in range(0, h, tile - overlap):
|
| 50 |
-
for x in range(0, w, tile - overlap):
|
| 51 |
-
y1, x1 = min(y + tile, h), min(x + tile, w)
|
| 52 |
-
y0, x0 = max(0, y1 - tile), max(0, x1 - tile)
|
| 53 |
-
crop = img[y0:y1, x0:x1]
|
| 54 |
-
t = torch.from_numpy(crop.transpose(2, 0, 1)).unsqueeze(0).cuda()
|
| 55 |
-
y_hat = model(t).clamp(0, 1).squeeze(0).permute(1, 2, 0).cpu().numpy()
|
| 56 |
-
out[y0 * scale : y1 * scale, x0 * scale : x1 * scale] = y_hat
|
| 57 |
-
return out
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
@spaces.GPU(duration=90)
|
| 61 |
-
def upscale(image: Image.Image, model_key: str) -> Image.Image:
|
| 62 |
-
if image is None:
|
| 63 |
-
raise gr.Error("Please upload an image first.")
|
| 64 |
-
model = get_model(model_key).cuda()
|
| 65 |
-
scale = MODELS[model_key][1]
|
| 66 |
-
|
| 67 |
-
rgb = np.asarray(image.convert("RGB"), dtype=np.float32) / 255.0
|
| 68 |
-
result = _run(model, rgb, scale)
|
| 69 |
-
return Image.fromarray((result * 255.0 + 0.5).astype(np.uint8), mode="RGB")
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
with gr.Blocks(title="NERVE", theme=gr.themes.Soft()) as demo:
|
| 73 |
-
gr.Markdown(
|
| 74 |
-
"""
|
| 75 |
-
# NERVE
|
| 76 |
-
Lightweight super-resolution / restoration. Drop an image, pick a model,
|
| 77 |
-
press **Upscale**, download the full-quality PNG.
|
| 78 |
-
|
| 79 |
-
- **release** models are for **clean** images.
|
| 80 |
-
- the **OTF GAN** model is for **degraded** images (blurry, noisy, JPEG-y).
|
| 81 |
-
"""
|
| 82 |
-
)
|
| 83 |
-
with gr.Row():
|
| 84 |
-
with gr.Column():
|
| 85 |
-
inp = gr.Image(type="pil", label="Input image", height=320)
|
| 86 |
-
model = gr.Dropdown(
|
| 87 |
-
choices=list(MODELS),
|
| 88 |
-
value=list(MODELS)[1],
|
| 89 |
-
label="Model",
|
| 90 |
-
)
|
| 91 |
-
btn = gr.Button("Upscale", variant="primary")
|
| 92 |
-
with gr.Column():
|
| 93 |
-
out = gr.Image(type="pil", label="Output (uncompressed PNG)", height=320)
|
| 94 |
-
dl = gr.File(label="Download PNG")
|
| 95 |
-
|
| 96 |
-
def _wrap(image, model_key):
|
| 97 |
-
result = upscale(image, model_key)
|
| 98 |
-
result.save("/tmp/nerve_output.png")
|
| 99 |
-
return result, "/tmp/nerve_output.png"
|
| 100 |
-
|
| 101 |
-
btn.click(_wrap, inputs=[inp, model], outputs=[out, dl])
|
| 102 |
-
|
| 103 |
-
if __name__ == "__main__":
|
| 104 |
-
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|