File size: 1,529 Bytes
0070fce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import warnings
from functools import wraps

import safetensors
import torch

from modules.errors import display


def build_loaded(module, loader_name):
    original_loader_name = f"{loader_name}_origin"

    if not hasattr(module, original_loader_name):
        setattr(module, original_loader_name, getattr(module, loader_name))

    original_loader = getattr(module, original_loader_name)

    @wraps(original_loader)
    def loader(*args, **kwargs):
        try:
            with warnings.catch_warnings():
                warnings.simplefilter(action="ignore", category=FutureWarning)
                return original_loader(*args, **kwargs)
        except Exception as e:
            display(e, f"{module.__name__}.{loader_name}")

            exc = "\n"
            for path in list(args) + list(kwargs.values()):
                if isinstance(path, str) and os.path.isfile(path):
                    exc += f'Failed to read file "{path}"\n'
                    backup_file = f"{path}.corrupted"
                    if os.path.exists(backup_file):
                        os.remove(backup_file)
                    os.replace(path, backup_file)
                    exc += f'Forge has moved the corrupted file to "{backup_file}"\n'
                    exc += "Please try downloading the model again\n"
            print(exc)
            raise ValueError from None

    setattr(module, loader_name, loader)


def patch_all_basics():
    build_loaded(safetensors.torch, "load_file")
    build_loaded(torch, "load")