|
| 1 | +FROM pytorch/pytorch:2.4.0-cuda12.4-cudnn9-runtime |
| 2 | + |
| 3 | +# System deps for opencv |
| 4 | +RUN apt-get update && apt-get install -y libxcb1 libgl1-mesa-glx libglib2.0-0 && rm -rf /var/lib/apt/lists/* |
| 5 | + |
| 6 | +# Install python dependencies including GFPGAN for face enhancement |
| 7 | +RUN pip install --no-cache-dir opencv-python-headless basicsr realesrgan gfpgan runpod |
| 8 | + |
| 9 | +# Patch basicsr without importing it (compatibility patch for newer PyTorch/torchvision) |
| 10 | +RUN python -c "\ |
| 11 | +import importlib.util, os;\ |
| 12 | +spec = importlib.util.find_spec('basicsr');\ |
| 13 | +path = os.path.join(os.path.dirname(spec.origin), 'data', 'degradations.py');\ |
| 14 | +content = open(path).read();\ |
| 15 | +content = content.replace(\ |
| 16 | + 'from torchvision.transforms.functional_tensor import rgb_to_grayscale',\ |
| 17 | + 'from torchvision.transforms.functional import rgb_to_grayscale');\ |
| 18 | +open(path, 'w').write(content);\ |
| 19 | +print('Patched:', path)\ |
| 20 | +" |
| 21 | + |
| 22 | +# Pre-download core upscale and face enhancement weights so cold start is fast |
| 23 | +RUN mkdir -p /weights && python -c "\ |
| 24 | +import urllib.request;\ |
| 25 | +print('Downloading Real-ESRGAN weights...');\ |
| 26 | +urllib.request.urlretrieve('https://huggingface.co/nateraw/real-esrgan/resolve/main/RealESRGAN_x2plus.pth', '/weights/RealESRGAN_x2plus.pth');\ |
| 27 | +urllib.request.urlretrieve('https://github.com/xinntao/Real-ESRGAN/releases/download/v0.1.0/RealESRGAN_x4plus.pth', '/weights/RealESRGAN_x4plus.pth');\ |
| 28 | +urllib.request.urlretrieve('https://huggingface.co/Kim2091/UltraSharp/resolve/main/4x-UltraSharp.pth', '/weights/4x-UltraSharp.pth');\ |
| 29 | +urllib.request.urlretrieve('https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.2.4/RealESRGAN_x4plus_anime_6B.pth', '/weights/RealESRGAN_x4plus_anime_6B.pth');\ |
| 30 | +print('Downloading GFPGAN weights...');\ |
| 31 | +urllib.request.urlretrieve('https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth', '/weights/GFPGANv1.3.pth');\ |
| 32 | +print('Pre-download core weights completed!')\ |
| 33 | +" |
| 34 | + |
| 35 | +# Trigger gfpgan/facexlib auto-download for face helper models during build to prevent cold start latency |
| 36 | +RUN python -c "\ |
| 37 | +import sys, os, types;\ |
| 38 | +import torchvision.transforms.functional as F_tv;\ |
| 39 | +shim = types.ModuleType('torchvision.transforms.functional_tensor');\ |
| 40 | +shim.rgb_to_grayscale = F_tv.rgb_to_grayscale;\ |
| 41 | +sys.modules['torchvision.transforms.functional_tensor'] = shim;\ |
| 42 | +from gfpgan import GFPGANer;\ |
| 43 | +from realesrgan import RealESRGANer;\ |
| 44 | +from basicsr.archs.rrdbnet_arch import RRDBNet;\ |
| 45 | +model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2);\ |
| 46 | +upsampler = RealESRGANer(scale=2, model_path='/weights/RealESRGAN_x2plus.pth', model=model);\ |
| 47 | +GFPGANer(model_path='/weights/GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler);\ |
| 48 | +print('Pre-download face helper detection/parsing models completed!')\ |
| 49 | +" |
| 50 | + |
| 51 | +COPY rp_handler.py /rp_handler.py |
| 52 | + |
| 53 | +CMD ["python", "-u", "/rp_handler.py"] |
0 commit comments