"""Identity and integrity checks for the official x4 model checkpoint.""" from __future__ import annotations import hashlib from pathlib import Path MODEL_FILENAME = "RealESRGAN_x4plus.pth" MODEL_BYTES = 67_040_989 MODEL_SHA256 = "4fa0d38905f75ac06eb49a7951b426670021be3018265fd191d2125df9d682f1" CHUNK_SIZE = 1024 * 1024 def sha256_file(path: Path) -> str: digest = hashlib.sha256() with path.open("rb") as source: for chunk in iter(lambda: source.read(CHUNK_SIZE), b""): digest.update(chunk) return digest.hexdigest() def verify_checkpoint(path: Path) -> str: """Require the known official checkpoint bytes before loading model state.""" size = path.stat().st_size if size != MODEL_BYTES: raise RuntimeError( f"Expected a {MODEL_BYTES:,}-byte {MODEL_FILENAME} checkpoint, got {size:,}." ) digest = sha256_file(path) if digest != MODEL_SHA256: raise RuntimeError( f"The checkpoint SHA-256 does not match the official {MODEL_FILENAME} file." ) return digest