Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ninjaneural
GitHub Repository: ninjaneural/webui
Path: blob/master/misc/direct/v1.3.2/modelloader.py
3275 views
1
import os
2
import shutil
3
import importlib
4
from urllib.parse import urlparse
5
6
from modules import shared
7
from modules.upscaler import Upscaler, UpscalerLanczos, UpscalerNearest, UpscalerNone
8
from modules.paths import script_path, models_path
9
10
11
def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None, ext_blacklist=None) -> list:
12
"""
13
A one-and done loader to try finding the desired models in specified directories.
14
15
@param download_name: Specify to download from model_url immediately.
16
@param model_url: If no other models are found, this will be downloaded on upscale.
17
@param model_path: The location to store/find models in.
18
@param command_path: A command-line argument to search for models in first.
19
@param ext_filter: An optional list of filename extensions to filter by
20
@return: A list of paths containing the desired model(s)
21
"""
22
output = []
23
24
try:
25
places = []
26
27
if command_path is not None and command_path != model_path:
28
pretrained_path = os.path.join(command_path, 'experiments/pretrained_models')
29
if os.path.exists(pretrained_path):
30
print(f"Appending path: {pretrained_path}")
31
places.append(pretrained_path)
32
elif os.path.exists(command_path):
33
places.append(command_path)
34
35
places.append(model_path)
36
37
for place in places:
38
for full_path in shared.walk_files(place, allowed_extensions=ext_filter):
39
if os.path.islink(full_path) and not os.path.exists(full_path):
40
print(f"Skipping broken symlink: {full_path}")
41
continue
42
if ext_blacklist is not None and any(full_path.endswith(x) for x in ext_blacklist):
43
continue
44
if full_path not in output:
45
output.append(full_path)
46
47
if model_url is not None and len(output) == 0:
48
if download_name is not None:
49
from basicsr.utils.download_util import load_file_from_url
50
dl = load_file_from_url(model_url, places[0], True, download_name)
51
output.append(dl)
52
else:
53
output.append(model_url)
54
55
except Exception:
56
pass
57
58
return output
59
60
61
def friendly_name(file: str):
62
if "http" in file:
63
file = urlparse(file).path
64
65
file = os.path.basename(file)
66
model_name, extension = os.path.splitext(file)
67
return model_name
68
69
70
def cleanup_models():
71
# This code could probably be more efficient if we used a tuple list or something to store the src/destinations
72
# and then enumerate that, but this works for now. In the future, it'd be nice to just have every "model" scaler
73
# somehow auto-register and just do these things...
74
root_path = script_path
75
src_path = models_path
76
dest_path = os.path.join(models_path, "Stable-diffusion")
77
move_files(src_path, dest_path, ".ckpt")
78
move_files(src_path, dest_path, ".safetensors")
79
src_path = os.path.join(root_path, "ESRGAN")
80
dest_path = os.path.join(models_path, "ESRGAN")
81
move_files(src_path, dest_path)
82
src_path = os.path.join(models_path, "BSRGAN")
83
dest_path = os.path.join(models_path, "ESRGAN")
84
move_files(src_path, dest_path, ".pth")
85
src_path = os.path.join(root_path, "gfpgan")
86
dest_path = os.path.join(models_path, "GFPGAN")
87
move_files(src_path, dest_path)
88
src_path = os.path.join(root_path, "SwinIR")
89
dest_path = os.path.join(models_path, "SwinIR")
90
move_files(src_path, dest_path)
91
src_path = os.path.join(root_path, "repositories/latent-diffusion/experiments/pretrained_models/")
92
dest_path = os.path.join(models_path, "LDSR")
93
move_files(src_path, dest_path)
94
95
96
def move_files(src_path: str, dest_path: str, ext_filter: str = None):
97
try:
98
if not os.path.exists(dest_path):
99
os.makedirs(dest_path)
100
if os.path.exists(src_path):
101
for file in os.listdir(src_path):
102
fullpath = os.path.join(src_path, file)
103
if os.path.isfile(fullpath):
104
if ext_filter is not None:
105
if ext_filter not in file:
106
continue
107
print(f"Moving {file} from {src_path} to {dest_path}.")
108
try:
109
shutil.move(fullpath, dest_path)
110
except Exception:
111
pass
112
if len(os.listdir(src_path)) == 0:
113
print(f"Removing empty folder: {src_path}")
114
shutil.rmtree(src_path, True)
115
except Exception:
116
pass
117
118
119
def load_upscalers():
120
# We can only do this 'magic' method to dynamically load upscalers if they are referenced,
121
# so we'll try to import any _model.py files before looking in __subclasses__
122
modules_dir = os.path.join(shared.script_path, "modules")
123
for file in os.listdir(modules_dir):
124
if "_model.py" in file:
125
model_name = file.replace("_model.py", "")
126
full_model = f"modules.{model_name}_model"
127
try:
128
importlib.import_module(full_model)
129
except Exception:
130
pass
131
132
datas = []
133
134
# some of upscaler classes will not go away after reloading their modules, and we'll end
135
# up with two copies of those classes. The newest copy will always be the last in the list,
136
# so we go from end to beginning and ignore duplicates
137
used_classes = {}
138
commandline_options = {} # or initialize with appropriate values
139
for cls in reversed(Upscaler.__subclasses__()):
140
classname = str(cls)
141
if classname not in used_classes:
142
used_classes[classname] = cls
143
144
for cls in reversed(used_classes.values()):
145
name = cls.__name__
146
cmd_name = f"{name.lower().replace('upscaler', '')}_models_path"
147
commandline_model_path = commandline_options.get(cmd_name, None)
148
scaler = cls(commandline_model_path)
149
scaler.user_path = commandline_model_path
150
scaler.model_download_path = commandline_model_path or scaler.model_path
151
datas += scaler.scalers
152
153
shared.sd_upscalers = sorted(
154
datas,
155
# Special case for UpscalerNone keeps it at the beginning of the list.
156
key=lambda x: x.name.lower() if not isinstance(x.scaler, (UpscalerNone, UpscalerLanczos, UpscalerNearest)) else ""
157
)
158
159