Skip to content

Commit

Permalink
fix(api): scope patched imports and run GC after patching
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed May 15, 2023
1 parent f53e8c1 commit 579c5f9
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api/onnx_web/server/hacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
from os import path
from urllib.parse import urlparse

import basicsr.utils.download_util
import codeformer.facelib.utils.misc
import facexlib.utils

from .context import ServerContext
from ..utils import run_gc

logger = getLogger(__name__)

Expand Down Expand Up @@ -45,6 +42,8 @@ def unload(exclude):
for mod in to_unload:
del sys.modules[mod]

run_gc()


# these should be the same sources and names as `convert.base_models.sources`, but inverted so the source is the key
cache_path_map = {
Expand Down Expand Up @@ -135,18 +134,21 @@ def patch_cache_path(server: ServerContext, url: str, **kwargs) -> str:

def apply_patch_basicsr(server: ServerContext):
logger.debug("patching BasicSR module")
import basicsr.utils.download_util
basicsr.utils.download_util.download_file_from_google_drive = patch_not_impl
basicsr.utils.download_util.load_file_from_url = partial(patch_cache_path, server)


def apply_patch_codeformer(server: ServerContext):
logger.debug("patching CodeFormer module")
import codeformer.facelib.utils.misc
codeformer.facelib.utils.misc.download_pretrained_models = patch_not_impl
codeformer.facelib.utils.misc.load_file_from_url = partial(patch_cache_path, server)


def apply_patch_facexlib(server: ServerContext):
logger.debug("patching Facexlib module")
import facexlib.utils
facexlib.utils.load_file_from_url = partial(patch_cache_path, server)


Expand Down

0 comments on commit 579c5f9

Please sign in to comment.