my-sd/modules_forge/patch_basic.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

44 lines
1.5 KiB
Python
Raw Normal View History

2024-01-28 07:11:56 +00:00
import torch
2024-01-30 03:28:45 +00:00
import os
import safetensors
2024-01-29 17:28:38 +00:00
2024-01-30 03:28:45 +00:00
def build_loaded(module, loader_name):
original_loader_name = 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)
def loader(*args, **kwargs):
result = None
try:
result = original_loader(*args, **kwargs)
except Exception as e:
result = None
exp = str(e) + '\n'
for path in list(args) + list(kwargs.values()):
if isinstance(path, str):
if os.path.exists(path):
exp += f'File corrupted: {path} \n'
corrupted_backup_file = path + '.corrupted'
if os.path.exists(corrupted_backup_file):
os.remove(corrupted_backup_file)
os.replace(path, corrupted_backup_file)
if os.path.exists(path):
os.remove(path)
exp += f'Forge has tried to move the corrupted file to {corrupted_backup_file} \n'
exp += f'You may try again now and Forge will download models again. \n'
raise ValueError(exp)
2024-01-30 03:28:45 +00:00
return result
setattr(module, loader_name, loader)
return
2024-01-28 04:42:38 +00:00
def patch_all_basics():
2024-01-30 03:28:45 +00:00
build_loaded(safetensors.torch, 'load_file')
build_loaded(torch, 'load')
2024-01-28 04:42:38 +00:00
return