mirror of
https://github.com/openvinotoolkit/stable-diffusion-webui.git
synced 2024-12-15 07:03:06 +03:00
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
import os
|
|
from collections import namedtuple
|
|
|
|
import torch
|
|
|
|
from modules import devices, sd_models, cache, errors, hashes, shared
|
|
|
|
NetworkWeights = namedtuple('NetworkWeights', ['network_key', 'sd_key', 'w', 'sd_module'])
|
|
|
|
metadata_tags_order = {"ss_sd_model_name": 1, "ss_resolution": 2, "ss_clip_skip": 3, "ss_num_train_images": 10, "ss_tag_frequency": 20}
|
|
|
|
|
|
class NetworkOnDisk:
|
|
def __init__(self, name, filename):
|
|
self.name = name
|
|
self.filename = filename
|
|
self.metadata = {}
|
|
self.is_safetensors = os.path.splitext(filename)[1].lower() == ".safetensors"
|
|
|
|
def read_metadata():
|
|
metadata = sd_models.read_metadata_from_safetensors(filename)
|
|
metadata.pop('ssmd_cover_images', None) # those are cover images, and they are too big to display in UI as text
|
|
|
|
return metadata
|
|
|
|
if self.is_safetensors:
|
|
try:
|
|
self.metadata = cache.cached_data_for_file('safetensors-metadata', "lora/" + self.name, filename, read_metadata)
|
|
except Exception as e:
|
|
errors.display(e, f"reading lora {filename}")
|
|
|
|
if self.metadata:
|
|
m = {}
|
|
for k, v in sorted(self.metadata.items(), key=lambda x: metadata_tags_order.get(x[0], 999)):
|
|
m[k] = v
|
|
|
|
self.metadata = m
|
|
|
|
self.alias = self.metadata.get('ss_output_name', self.name)
|
|
|
|
self.hash = None
|
|
self.shorthash = None
|
|
self.set_hash(
|
|
self.metadata.get('sshs_model_hash') or
|
|
hashes.sha256_from_cache(self.filename, "lora/" + self.name, use_addnet_hash=self.is_safetensors) or
|
|
''
|
|
)
|
|
|
|
def set_hash(self, v):
|
|
self.hash = v
|
|
self.shorthash = self.hash[0:12]
|
|
|
|
if self.shorthash:
|
|
import networks
|
|
networks.available_network_hash_lookup[self.shorthash] = self
|
|
|
|
def read_hash(self):
|
|
if not self.hash:
|
|
self.set_hash(hashes.sha256(self.filename, "lora/" + self.name, use_addnet_hash=self.is_safetensors) or '')
|
|
|
|
def get_alias(self):
|
|
import networks
|
|
if shared.opts.lora_preferred_name == "Filename" or self.alias.lower() in networks.forbidden_network_aliases:
|
|
return self.name
|
|
else:
|
|
return self.alias
|
|
|
|
|
|
class Network: # LoraModule
|
|
def __init__(self, name, network_on_disk: NetworkOnDisk):
|
|
self.name = name
|
|
self.network_on_disk = network_on_disk
|
|
self.multiplier = 1.0
|
|
self.modules = {}
|
|
self.mtime = None
|
|
|
|
self.mentioned_name = None
|
|
"""the text that was used to add the network to prompt - can be either name or an alias"""
|
|
|
|
|
|
class ModuleType:
|
|
def create_module(self, net: Network, weights: NetworkWeights) -> Network | None:
|
|
return None
|
|
|
|
|
|
class NetworkModule:
|
|
def __init__(self, net: Network, weights: NetworkWeights):
|
|
self.network = net
|
|
self.network_key = weights.network_key
|
|
self.sd_key = weights.sd_key
|
|
self.sd_module = weights.sd_module
|
|
|
|
def calc_updown(self, target):
|
|
raise NotImplementedError()
|
|
|
|
def forward(self, x, y):
|
|
raise NotImplementedError()
|
|
|