from basicsr.archs.rrdbnet_arch import RRDBNet
from realesrgan import RealESRGANer

_upscaler = None


def get_upscaler():
    global _upscaler

    if _upscaler is not None:
        return _upscaler

    model = RRDBNet(
        num_in_ch=3,
        num_out_ch=3,
        num_feat=64,
        num_block=23,
        num_grow_ch=32,
        scale=4
    )

    _upscaler = RealESRGANer(
        scale=4,
        model_path="app/models/RealESRGAN_x4plus.pth",
        model=model,
        tile=0,
        tile_pad=10,
        pre_pad=0,
        half=False
    )

    return _upscaler