add node:Load Lora With Shared

This commit is contained in:
wTechArtist
2024-08-21 10:59:43 +08:00
parent 1648e5f361
commit e05ce931a2
3 changed files with 470 additions and 2 deletions
+132 -1
View File
@@ -25,6 +25,8 @@ import time
import random
import logging
import uuid
import folder_paths
import nodes
# from ..ComfyUI_IPAdapter_plus import IPAdapterPlus
from PIL import Image, ImageOps, ImageSequence, ImageFile,UnidentifiedImageError
from PIL.PngImagePlugin import PngInfo
@@ -51,6 +53,36 @@ import importlib
import folder_paths
import latent_preview
import node_helpers
from .libs.utils import TaggedCache, any_typ
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
settings_file = os.path.join(root_dir, 'cache_settings.json')
try:
with open(settings_file) as f:
cache_settings = json.load(f)
except Exception as e:
print(e)
cache_settings = {}
cache = TaggedCache(cache_settings)
cache_count = {}
def update_cache(k, tag, v):
cache[k] = (tag, v)
cnt = cache_count.get(k)
if cnt is None:
cnt = 0
cache_count[k] = cnt
else:
cache_count[k] += 1
def cache_weak_hash(k):
cnt = cache_count.get(k)
if cnt is None:
cnt = 0
return k, cnt
# 获取当前文件的目录
current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -443,4 +475,103 @@ class IPAdapter_FaceID_Bool():
return (work_model, face_image, True)
except Exception as e:
print(f"Error: {e}")
return (model, image, False)
return (model, image, False)
class LoraLoaderShared(nodes.LoraLoader):
def __init__(self):
self.loaded_lora = None
@classmethod
def INPUT_TYPES(s):
file_list = folder_paths.get_filename_list("loras")
file_list.insert(0, "None")
return {
"required": {
"model": ("MODEL", {"tooltip": "The diffusion model the LoRA will be applied to."}),
"clip": ("CLIP", {"tooltip": "The CLIP model the LoRA will be applied to."}),
"lora_name": (file_list, {"tooltip": "The name of the LoRA."}),
"strength_model": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01, "tooltip": "How strongly to modify the diffusion model. This value can be negative."}),
"strength_clip": ("FLOAT", {"default": 1.0, "min": -100.0, "max": 100.0, "step": 0.01, "tooltip": "How strongly to modify the CLIP model. This value can be negative."}),
"key_opt": ("STRING", {"multiline": False, "placeholder": "If empty, use 'lora_name' as the key."}),
},
"optional": {
"mode": (['Auto', 'Override Cache', 'Read Only'],),
}
}
RETURN_TYPES = ("MODEL", "CLIP", "STRING")
RETURN_NAMES = ("model", "clip", "cache key")
FUNCTION = "doit"
CATEGORY = "InspirePack/Backend"
def doit(self, model, clip, lora_name, strength_model, strength_clip, key_opt, mode='Auto'):
if mode == 'Read Only':
if key_opt.strip() == '':
raise Exception("[LoraLoaderShared] key_opt cannot be omit if mode is 'Read Only'")
key = key_opt.strip()
elif key_opt.strip() == '':
key = lora_name
else:
key = key_opt.strip()
if strength_model == 0 and strength_clip == 0:
return (model, clip, key)
if lora_name == "None":
return (model, clip, key)
if key not in cache or mode == 'Override Cache':
res = self.load_lora(model, clip, lora_name, strength_model, strength_clip)
update_cache(key, "lora", (False, res))
cache_kind = 'lora'
print(f"[Inspire Pack] LoraLoaderShared: Lora '{lora_name}' is cached to '{key}'.")
else:
cache_kind, (_, res) = cache[key]
print(f"[Inspire Pack] LoraLoaderShared: Cached lora '{key}' is loaded. (Loading skip)")
if cache_kind == 'lora':
model, clip = res
else:
raise Exception(f"[LoraLoaderShared] Unexpected cache_kind '{cache_kind}'")
return model, clip, key
@staticmethod
def IS_CHANGED(model, clip, lora_name, strength_model, strength_clip, key_opt, mode='Auto'):
if mode == 'Read Only':
if key_opt.strip() == '':
raise Exception("[LoraLoaderShared] key_opt cannot be omit if mode is 'Read Only'")
key = key_opt.strip()
elif key_opt.strip() == '':
key = lora_name
else:
key = key_opt.strip()
if mode == 'Read Only':
return (None, cache_weak_hash(key))
elif mode == 'Override Cache':
return (lora_name, key)
return (None, cache_weak_hash(key))
def load_lora(self, model, clip, lora_name, strength_model, strength_clip):
if strength_model == 0 and strength_clip == 0:
return (model, clip)
lora_path = folder_paths.get_full_path("loras", lora_name)
lora = None
if self.loaded_lora is not None:
if self.loaded_lora[0] == lora_path:
lora = self.loaded_lora[1]
else:
temp = self.loaded_lora
self.loaded_lora = None
del temp
if lora is None:
lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
self.loaded_lora = (lora_path, lora)
model_lora, clip_lora = comfy.sd.load_lora_for_models(model, clip, lora, strength_model, strength_clip)
return (model_lora, clip_lora)