File size: 6,152 Bytes
2de3774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import datetime
import random
import time
from pathlib import Path

from contextlib import contextmanager
from typing import Optional
from urllib.parse import urlparse
from shared import path_manager, shared_cache
import json


def get_wildcard_files():
    directories = ["wildcards", "wildcards_official"]
    files = []

    for directory in directories:
        for file in Path(directory).rglob("*.txt"):
            name = file.stem
            if name not in files:
                files.append(name)

    onebutton = [
        "onebuttonprompt",
        "onebuttonsubject",
        "onebuttonhumanoid",
        "onebuttonmale",
        "onebuttonfemale",
        "onebuttonanimal",
        "onebuttonobject",
        "onebuttonlandscape",
        "onebuttonconcept",
        "onebuttonartist",
        "onebutton1girl",
        "onebutton1boy",
        "onebuttonfurry",
    ]
    both = files + onebutton
    return both


def generate_temp_filename(folder="./outputs/", extension="png"):
    current_time = datetime.datetime.now()
    date_string = current_time.strftime("%Y-%m-%d")
    time_string = current_time.strftime("%Y-%m-%d_%H-%M-%S")
    random_number = random.randint(1000, 9999)
    filename = f"{time_string}_{random_number}.{extension}"
    result = Path(folder) / date_string / filename
    return result.absolute()


def load_keywords(lora):
    filename = Path(
        path_manager.model_paths["cache_path"] / "loras" / Path(lora).name
    ).with_suffix(".txt")
    try:
        with open(filename, "r") as file:
            data = file.read()
        return data
    except FileNotFoundError:
        return " "

def _get_model_hashes(cache_path, not_found=None):
    hashes = {
        "AutoV1": "",
        "AutoV2": "",
        "SHA256": "",
        "CRC32": "",
        "BLAKE3": "",
        "AutoV3": ""
    }
    filename = cache_path.with_suffix(".json")
    if Path(filename).is_file():
        try:
            with open(filename) as f:
                data = json.load(f)
        except:
            print(f"ERROR: model {cache_path} is missing json-file")
            data = {}
        if "files" not in data:
            data = {"files": [{"hashes": {}}]}
        hashes.update(data['files'][0]['hashes'])
        return hashes
    else:
        if not_found:
            return not_found
        else:
            return hashes

def get_checkpoint_hashes(model):
    return _get_model_hashes(
        Path(path_manager.model_paths["cache_path"] / "checkpoints" / Path(model).name)
    )

def get_lora_hashes(model):
    return _get_model_hashes(
        Path(path_manager.model_paths["cache_path"] / "loras" / Path(model).name)
    )

def _get_model_thumbnail(cache_path, not_found="html/warning.png"):
    if cache_path in shared_cache:
        return shared_cache[cache_path]
    suffixes = [".jpeg", ".jpg", ".png", ".gif"]
    for suffix in suffixes:
        filename = cache_path.with_suffix(suffix)
        if Path(filename).is_file():
            shared_cache[cache_path] = str(filename)
            return str(filename)
    else:
        return not_found

def get_model_thumbnail(model):
    res = _get_model_thumbnail(
        Path(path_manager.model_paths["cache_path"] / "checkpoints" / Path(model).name),
        not_found=None
    )
    if res is not None:
        return res
    res = _get_model_thumbnail(
        Path(path_manager.model_paths["cache_path"] / "loras" / Path(model).name),
        not_found=None
    )
    if res is not None:
        return str(res)
    else:
        return "html/warning.png"

def get_checkpoint_thumbnail(model):
    if Path(model).suffix == ".merge":
        not_found="html/merge.jpeg"
    else:
        not_found="html/warning.jpeg"

    return _get_model_thumbnail(
        Path(path_manager.model_paths["cache_path"] / "checkpoints" / Path(model).name),
        not_found=not_found
    )

def get_lora_thumbnail(model):
    return _get_model_thumbnail(
        Path(path_manager.model_paths["cache_path"] / "loras" / Path(model).name)
    )

def get_model_path(model, folders):
    for folder in folders:
        filename = Path(folder) / model
        if filename.exists():
            return filename
    return None

def get_checkpoint_path(model, folders=None):
    if folders is None:
        folders = [path_manager_model_paths["modelfile_path"]]
    return get_model_path(model, folders)

def get_lora_path(model, folders=None):
    if folders is None:
        folders = [path_manager_model_paths["lorafile_path"]]
    return get_model_path(model, folders)

def url_to_filename(url):
    import string
    keep = "-_.() %s%s" % (string.ascii_letters, string.digits)
    url = url.replace(' ','_')
    url = ''.join(c for c in url if c in keep)
    return url

def load_file_from_url(
    url: str,
    *,
    model_dir: str,
    progress: bool = True,
    file_name: Optional[str] = None,
) -> str:
    """Download a file from `url` into `model_dir`, using the file present if possible.

    Returns the path to the downloaded file.
    """
    Path(model_dir).mkdir(parents=True, exist_ok=True)
    if not file_name:
        parts = urlparse(url)
        file_name = Path(parts.path).stem

    for file in Path(model_dir).glob("**/*"):
        if file.name == file_name:
            cached_file = file
            return str(cached_file)

    cached_file = Path(model_dir) / file_name
    if not cached_file.exists():
        print(f'Downloading: "{url}" to {cached_file}\n')
        from torch.hub import download_url_to_file

        download_url_to_file(url, cached_file, progress=progress)
    return str(cached_file)


class TimeIt:
    def __init__(self, text=""):
        self.text = text

    def __enter__(self):
        self.start = time.time()
        return self

    def __exit__(self, *args):
        self.end = time.time()
        self.interval = self.end - self.start
        print(f"\033[91mTime taken: {self.interval:0.2f} seconds {self.text}\033[0m")


def remove_empty_str(items, default=None):
    items = [x for x in items if x != ""]
    if len(items) == 0 and default is not None:
        return [default]
    return items