stable-diffusion-webui

Форк
0
169 строк · 6.0 Кб
1
from __future__ import annotations
2

3
import importlib
4
import logging
5
import os
6
from typing import TYPE_CHECKING
7
from urllib.parse import urlparse
8

9
import torch
10

11
from modules import shared
12
from modules.upscaler import Upscaler, UpscalerLanczos, UpscalerNearest, UpscalerNone
13

14
if TYPE_CHECKING:
15
    import spandrel
16

17
logger = logging.getLogger(__name__)
18

19

20
def load_file_from_url(
21
    url: str,
22
    *,
23
    model_dir: str,
24
    progress: bool = True,
25
    file_name: str | None = None,
26
) -> str:
27
    """Download a file from `url` into `model_dir`, using the file present if possible.
28

29
    Returns the path to the downloaded file.
30
    """
31
    os.makedirs(model_dir, exist_ok=True)
32
    if not file_name:
33
        parts = urlparse(url)
34
        file_name = os.path.basename(parts.path)
35
    cached_file = os.path.abspath(os.path.join(model_dir, file_name))
36
    if not os.path.exists(cached_file):
37
        print(f'Downloading: "{url}" to {cached_file}\n')
38
        from torch.hub import download_url_to_file
39
        download_url_to_file(url, cached_file, progress=progress)
40
    return cached_file
41

42

43
def load_models(model_path: str, model_url: str = None, command_path: str = None, ext_filter=None, download_name=None, ext_blacklist=None) -> list:
44
    """
45
    A one-and done loader to try finding the desired models in specified directories.
46

47
    @param download_name: Specify to download from model_url immediately.
48
    @param model_url: If no other models are found, this will be downloaded on upscale.
49
    @param model_path: The location to store/find models in.
50
    @param command_path: A command-line argument to search for models in first.
51
    @param ext_filter: An optional list of filename extensions to filter by
52
    @return: A list of paths containing the desired model(s)
53
    """
54
    output = []
55

56
    try:
57
        places = []
58

59
        if command_path is not None and command_path != model_path:
60
            pretrained_path = os.path.join(command_path, 'experiments/pretrained_models')
61
            if os.path.exists(pretrained_path):
62
                print(f"Appending path: {pretrained_path}")
63
                places.append(pretrained_path)
64
            elif os.path.exists(command_path):
65
                places.append(command_path)
66

67
        places.append(model_path)
68

69
        for place in places:
70
            for full_path in shared.walk_files(place, allowed_extensions=ext_filter):
71
                if os.path.islink(full_path) and not os.path.exists(full_path):
72
                    print(f"Skipping broken symlink: {full_path}")
73
                    continue
74
                if ext_blacklist is not None and any(full_path.endswith(x) for x in ext_blacklist):
75
                    continue
76
                if full_path not in output:
77
                    output.append(full_path)
78

79
        if model_url is not None and len(output) == 0:
80
            if download_name is not None:
81
                output.append(load_file_from_url(model_url, model_dir=places[0], file_name=download_name))
82
            else:
83
                output.append(model_url)
84

85
    except Exception:
86
        pass
87

88
    return output
89

90

91
def friendly_name(file: str):
92
    if file.startswith("http"):
93
        file = urlparse(file).path
94

95
    file = os.path.basename(file)
96
    model_name, extension = os.path.splitext(file)
97
    return model_name
98

99

100
def load_upscalers():
101
    # We can only do this 'magic' method to dynamically load upscalers if they are referenced,
102
    # so we'll try to import any _model.py files before looking in __subclasses__
103
    modules_dir = os.path.join(shared.script_path, "modules")
104
    for file in os.listdir(modules_dir):
105
        if "_model.py" in file:
106
            model_name = file.replace("_model.py", "")
107
            full_model = f"modules.{model_name}_model"
108
            try:
109
                importlib.import_module(full_model)
110
            except Exception:
111
                pass
112

113
    datas = []
114
    commandline_options = vars(shared.cmd_opts)
115

116
    # some of upscaler classes will not go away after reloading their modules, and we'll end
117
    # up with two copies of those classes. The newest copy will always be the last in the list,
118
    # so we go from end to beginning and ignore duplicates
119
    used_classes = {}
120
    for cls in reversed(Upscaler.__subclasses__()):
121
        classname = str(cls)
122
        if classname not in used_classes:
123
            used_classes[classname] = cls
124

125
    for cls in reversed(used_classes.values()):
126
        name = cls.__name__
127
        cmd_name = f"{name.lower().replace('upscaler', '')}_models_path"
128
        commandline_model_path = commandline_options.get(cmd_name, None)
129
        scaler = cls(commandline_model_path)
130
        scaler.user_path = commandline_model_path
131
        scaler.model_download_path = commandline_model_path or scaler.model_path
132
        datas += scaler.scalers
133

134
    shared.sd_upscalers = sorted(
135
        datas,
136
        # Special case for UpscalerNone keeps it at the beginning of the list.
137
        key=lambda x: x.name.lower() if not isinstance(x.scaler, (UpscalerNone, UpscalerLanczos, UpscalerNearest)) else ""
138
    )
139

140

141
def load_spandrel_model(
142
    path: str | os.PathLike,
143
    *,
144
    device: str | torch.device | None,
145
    prefer_half: bool = False,
146
    dtype: str | torch.dtype | None = None,
147
    expected_architecture: str | None = None,
148
) -> spandrel.ModelDescriptor:
149
    import spandrel
150
    model_descriptor = spandrel.ModelLoader(device=device).load_from_file(str(path))
151
    if expected_architecture and model_descriptor.architecture != expected_architecture:
152
        logger.warning(
153
            f"Model {path!r} is not a {expected_architecture!r} model (got {model_descriptor.architecture!r})",
154
        )
155
    half = False
156
    if prefer_half:
157
        if model_descriptor.supports_half:
158
            model_descriptor.model.half()
159
            half = True
160
        else:
161
            logger.info("Model %s does not support half precision, ignoring --half", path)
162
    if dtype:
163
        model_descriptor.model.to(dtype=dtype)
164
    model_descriptor.model.eval()
165
    logger.debug(
166
        "Loaded %s from %s (device=%s, half=%s, dtype=%s)",
167
        model_descriptor, path, device, half, dtype,
168
    )
169
    return model_descriptor
170

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.