# 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




import threading

from gfpgan import GFPGANer

_face_enhancer = None

_lock = threading.Lock()


def get_face_enhancer():

    global _face_enhancer

    if _face_enhancer is not None:
        return _face_enhancer

    with _lock:

        if _face_enhancer is not None:
            return _face_enhancer

        print("=================================")
        print("Loading GFPGAN...")
        print("=================================")

        _face_enhancer = GFPGANer(

            model_path="app/models/GFPGANv1.4.pth",

            upscale=2,

            arch="clean",

            channel_multiplier=2,

            bg_upsampler=None

        )

        print("=================================")
        print("GFPGAN Loaded Successfully")
        print("=================================")

    return _face_enhancer