2023-08-13 07:38:10 -06:00
|
|
|
from modules import extra_networks, shared
|
2023-07-16 14:13:55 -06:00
|
|
|
import networks
|
2023-01-21 06:15:53 -07:00
|
|
|
|
2023-05-06 23:25:34 -06:00
|
|
|
|
2023-01-21 06:15:53 -07:00
|
|
|
class ExtraNetworkLora(extra_networks.ExtraNetwork):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__('lora')
|
|
|
|
|
2023-08-13 06:07:37 -06:00
|
|
|
self.errors = {}
|
|
|
|
"""mapping of network names to the number of errors the network had during operation"""
|
|
|
|
|
2023-01-21 06:15:53 -07:00
|
|
|
def activate(self, p, params_list):
|
2023-01-28 07:18:47 -07:00
|
|
|
additional = shared.opts.sd_lora
|
|
|
|
|
2023-08-13 06:07:37 -06:00
|
|
|
self.errors.clear()
|
|
|
|
|
2023-07-16 14:13:55 -06:00
|
|
|
if additional != "None" and additional in networks.available_networks and not any(x for x in params_list if x.items[0] == additional):
|
2023-01-28 07:18:47 -07:00
|
|
|
p.all_prompts = [x + f"<lora:{additional}:{shared.opts.extra_networks_default_multiplier}>" for x in p.all_prompts]
|
|
|
|
params_list.append(extra_networks.ExtraNetworkParams(items=[additional, shared.opts.extra_networks_default_multiplier]))
|
|
|
|
|
2023-01-21 06:15:53 -07:00
|
|
|
names = []
|
2023-07-17 00:00:47 -06:00
|
|
|
te_multipliers = []
|
|
|
|
unet_multipliers = []
|
|
|
|
dyn_dims = []
|
2023-01-21 06:15:53 -07:00
|
|
|
for params in params_list:
|
2023-06-02 05:58:10 -06:00
|
|
|
assert params.items
|
2023-01-21 06:15:53 -07:00
|
|
|
|
2023-07-17 00:00:47 -06:00
|
|
|
names.append(params.positional[0])
|
2023-01-21 06:15:53 -07:00
|
|
|
|
2023-07-17 00:00:47 -06:00
|
|
|
te_multiplier = float(params.positional[1]) if len(params.positional) > 1 else 1.0
|
|
|
|
te_multiplier = float(params.named.get("te", te_multiplier))
|
|
|
|
|
2023-07-18 15:41:00 -06:00
|
|
|
unet_multiplier = float(params.positional[2]) if len(params.positional) > 2 else te_multiplier
|
2023-07-17 00:00:47 -06:00
|
|
|
unet_multiplier = float(params.named.get("unet", unet_multiplier))
|
|
|
|
|
|
|
|
dyn_dim = int(params.positional[3]) if len(params.positional) > 3 else None
|
|
|
|
dyn_dim = int(params.named["dyn"]) if "dyn" in params.named else dyn_dim
|
|
|
|
|
|
|
|
te_multipliers.append(te_multiplier)
|
|
|
|
unet_multipliers.append(unet_multiplier)
|
|
|
|
dyn_dims.append(dyn_dim)
|
|
|
|
|
|
|
|
networks.load_networks(names, te_multipliers, unet_multipliers, dyn_dims)
|
2023-01-21 06:15:53 -07:00
|
|
|
|
2023-05-19 13:59:29 -06:00
|
|
|
if shared.opts.lora_add_hashes_to_infotext:
|
2023-07-16 14:13:55 -06:00
|
|
|
network_hashes = []
|
|
|
|
for item in networks.loaded_networks:
|
|
|
|
shorthash = item.network_on_disk.shorthash
|
2023-05-19 13:59:29 -06:00
|
|
|
if not shorthash:
|
|
|
|
continue
|
|
|
|
|
|
|
|
alias = item.mentioned_name
|
|
|
|
if not alias:
|
|
|
|
continue
|
|
|
|
|
|
|
|
alias = alias.replace(":", "").replace(",", "")
|
|
|
|
|
2023-07-16 14:13:55 -06:00
|
|
|
network_hashes.append(f"{alias}: {shorthash}")
|
2023-05-19 13:59:29 -06:00
|
|
|
|
2023-07-16 14:13:55 -06:00
|
|
|
if network_hashes:
|
|
|
|
p.extra_generation_params["Lora hashes"] = ", ".join(network_hashes)
|
2023-05-19 13:59:29 -06:00
|
|
|
|
2023-01-21 06:15:53 -07:00
|
|
|
def deactivate(self, p):
|
2023-08-13 06:07:37 -06:00
|
|
|
if self.errors:
|
|
|
|
p.comment("Networks with errors: " + ", ".join(f"{k} ({v})" for k, v in self.errors.items()))
|
|
|
|
|
|
|
|
self.errors.clear()
|