CoolFace
Apppublic

cymic/Waifu_Diffusion_Webui

sourceHugging Faceupdated 4y agoView on Hugging Face
1likes
modelloader.py154 linesDownload Raw Back to modules
1import glob2import os3import shutil4import importlib5from urllib.parse import urlparse6 7from basicsr.utils.download_util import load_file_from_url8from modules import shared9from modules.upscaler import Upscaler10from modules.paths import script_path, models_path11 12 13def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None) -> list:14    """15    A one-and done loader to try finding the desired models in specified directories.16 17    @param download_name: Specify to download from model_url immediately.18    @param model_url: If no other models are found, this will be downloaded on upscale.19    @param model_path: The location to store/find models in.20    @param command_path: A command-line argument to search for models in first.21    @param ext_filter: An optional list of filename extensions to filter by22    @return: A list of paths containing the desired model(s)23    """24    output = []25 26    if ext_filter is None:27        ext_filter = []28 29    try:30        places = []31 32        if command_path is not None and command_path != model_path:33            pretrained_path = os.path.join(command_path, 'experiments/pretrained_models')34            if os.path.exists(pretrained_path):35                print(f"Appending path: {pretrained_path}")36                places.append(pretrained_path)37            elif os.path.exists(command_path):38                places.append(command_path)39 40        places.append(model_path)41 42        for place in places:43            if os.path.exists(place):44                for file in glob.iglob(place + '**/**', recursive=True):45                    full_path = file46                    if os.path.isdir(full_path):47                        continue48                    if len(ext_filter) != 0:49                        model_name, extension = os.path.splitext(file)50                        if extension not in ext_filter:51                            continue52                    if file not in output:53                        output.append(full_path)54 55        if model_url is not None and len(output) == 0:56            if download_name is not None:57                dl = load_file_from_url(model_url, model_path, True, download_name)58                output.append(dl)59            else:60                output.append(model_url)61 62    except Exception:63        pass64 65    return output66 67 68def friendly_name(file: str):69    if "http" in file:70        file = urlparse(file).path71 72    file = os.path.basename(file)73    model_name, extension = os.path.splitext(file)74    return model_name75 76 77def cleanup_models():78    # This code could probably be more efficient if we used a tuple list or something to store the src/destinations79    # and then enumerate that, but this works for now. In the future, it'd be nice to just have every "model" scaler80    # somehow auto-register and just do these things...81    root_path = script_path82    src_path = models_path83    dest_path = os.path.join(models_path, "Stable-diffusion")84    move_files(src_path, dest_path, ".ckpt")85    src_path = os.path.join(root_path, "ESRGAN")86    dest_path = os.path.join(models_path, "ESRGAN")87    move_files(src_path, dest_path)88    src_path = os.path.join(root_path, "gfpgan")89    dest_path = os.path.join(models_path, "GFPGAN")90    move_files(src_path, dest_path)91    src_path = os.path.join(root_path, "SwinIR")92    dest_path = os.path.join(models_path, "SwinIR")93    move_files(src_path, dest_path)94    src_path = os.path.join(root_path, "repositories/latent-diffusion/experiments/pretrained_models/")95    dest_path = os.path.join(models_path, "LDSR")96    move_files(src_path, dest_path)97 98 99def move_files(src_path: str, dest_path: str, ext_filter: str = None):100    try:101        if not os.path.exists(dest_path):102            os.makedirs(dest_path)103        if os.path.exists(src_path):104            for file in os.listdir(src_path):105                fullpath = os.path.join(src_path, file)106                if os.path.isfile(fullpath):107                    if ext_filter is not None:108                        if ext_filter not in file:109                            continue110                    print(f"Moving {file} from {src_path} to {dest_path}.")111                    try:112                        shutil.move(fullpath, dest_path)113                    except:114                        pass115            if len(os.listdir(src_path)) == 0:116                print(f"Removing empty folder: {src_path}")117                shutil.rmtree(src_path, True)118    except:119        pass120 121 122def load_upscalers():123    sd = shared.script_path124    # We can only do this 'magic' method to dynamically load upscalers if they are referenced,125    # so we'll try to import any _model.py files before looking in __subclasses__126    modules_dir = os.path.join(sd, "modules")127    for file in os.listdir(modules_dir):128        if "_model.py" in file:129            model_name = file.replace("_model.py", "")130            full_model = f"modules.{model_name}_model"131            try:132                importlib.import_module(full_model)133            except:134                pass135    datas = []136    c_o = vars(shared.cmd_opts)137    for cls in Upscaler.__subclasses__():138        name = cls.__name__139        module_name = cls.__module__140        module = importlib.import_module(module_name)141        class_ = getattr(module, name)142        cmd_name = f"{name.lower().replace('upscaler', '')}_models_path"143        opt_string = None144        try:145            if cmd_name in c_o:146                opt_string = c_o[cmd_name]147        except:148            pass149        scaler = class_(opt_string)150        for child in scaler.scalers:151            datas.append(child)152 153    shared.sd_upscalers = datas154