"""Download and fingerprint the official RealESRGAN_x4plus checkpoint.""" from __future__ import annotations import argparse import hashlib import json import os import sys import tempfile from pathlib import Path from urllib.request import Request, urlopen BACKEND_ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(BACKEND_ROOT)) from app.models.weights import ( # noqa: E402 MODEL_BYTES, MODEL_FILENAME, MODEL_SHA256, sha256_file, ) WEIGHTS_DIR = BACKEND_ROOT / "weights" WEIGHTS_PATH = WEIGHTS_DIR / MODEL_FILENAME MANIFEST_PATH = WEIGHTS_DIR / "manifest.json" MODEL_URL = ( "https://github.com/xinntao/Real-ESRGAN/releases/download/" "v0.1.0/RealESRGAN_x4plus.pth" ) CHUNK_SIZE = 1024 * 1024 def download(force: bool = False) -> Path: WEIGHTS_DIR.mkdir(parents=True, exist_ok=True) if WEIGHTS_PATH.exists() and not force: manifest = ( json.loads(MANIFEST_PATH.read_text(encoding="utf-8")) if MANIFEST_PATH.exists() else None ) if ( manifest and manifest.get("bytes") == MODEL_BYTES and manifest.get("sha256") == MODEL_SHA256 and manifest.get("sha256") == sha256_file(WEIGHTS_PATH) ): print(f"Verified existing model: {WEIGHTS_PATH}") return WEIGHTS_PATH raise RuntimeError( "A weights file already exists without a matching manifest. " "Inspect it, or rerun with --force to replace it." ) request = Request(MODEL_URL, headers={"User-Agent": "SharpifyAI/0.1"}) digest = hashlib.sha256() total = 0 temp_path: Path | None = None try: with tempfile.NamedTemporaryFile( prefix="realesrgan-", suffix=".part", dir=WEIGHTS_DIR, delete=False ) as destination: temp_path = Path(destination.name) with urlopen(request, timeout=60) as response: declared_size = response.headers.get("Content-Length") if declared_size and int(declared_size) != MODEL_BYTES: raise RuntimeError( f"Release asset length changed: expected {MODEL_BYTES:,} bytes, " f"received declaration {declared_size} bytes." ) while chunk := response.read(CHUNK_SIZE): total += len(chunk) if total > MODEL_BYTES: raise RuntimeError( "The downloaded model exceeds its expected size." ) digest.update(chunk) destination.write(chunk) print(f"Downloaded {total:,}/{MODEL_BYTES:,} bytes", end="\r") destination.flush() os.fsync(destination.fileno()) if total != MODEL_BYTES: raise RuntimeError( f"Incomplete checkpoint: expected {MODEL_BYTES:,} bytes, got {total:,}." ) file_hash = digest.hexdigest() if file_hash != MODEL_SHA256: raise RuntimeError( f"Official release checksum changed: expected {MODEL_SHA256}, got {file_hash}." ) os.replace(temp_path, WEIGHTS_PATH) temp_path = None manifest = { "model": "RealESRGAN_x4plus", "source_url": MODEL_URL, "release_version": "v0.1.0", "bytes": total, "sha256": file_hash, "checksum_origin": "computed after download; release publishes no digest", } MANIFEST_PATH.write_text( json.dumps(manifest, indent=2) + "\n", encoding="utf-8" ) print(f"\nSaved verified checkpoint: {WEIGHTS_PATH}") print(f"SHA-256: {file_hash}") return WEIGHTS_PATH finally: if temp_path is not None: temp_path.unlink(missing_ok=True) if __name__ == "__main__": parser = argparse.ArgumentParser(description=__doc__) parser.add_argument( "--force", action="store_true", help="replace the checkpoint and regenerate its manifest", ) arguments = parser.parse_args() try: download(force=arguments.force) except Exception as error: print(f"Model download failed: {error}", file=sys.stderr) raise SystemExit(1) from error