2023-08-04 13:05:40 -06:00
|
|
|
import json
|
|
|
|
import os
|
2023-01-20 22:36:07 -07:00
|
|
|
import re
|
2023-08-26 23:41:26 -06:00
|
|
|
import logging
|
2023-01-20 22:36:07 -07:00
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
from modules import errors
|
|
|
|
|
|
|
|
extra_network_registry = {}
|
2023-07-17 01:06:02 -06:00
|
|
|
extra_network_aliases = {}
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
def initialize():
|
|
|
|
extra_network_registry.clear()
|
2023-07-17 01:06:02 -06:00
|
|
|
extra_network_aliases.clear()
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
|
|
|
|
def register_extra_network(extra_network):
|
|
|
|
extra_network_registry[extra_network.name] = extra_network
|
|
|
|
|
|
|
|
|
2023-07-17 01:06:02 -06:00
|
|
|
def register_extra_network_alias(extra_network, alias):
|
|
|
|
extra_network_aliases[alias] = extra_network
|
|
|
|
|
|
|
|
|
2023-05-19 06:35:16 -06:00
|
|
|
def register_default_extra_networks():
|
|
|
|
from modules.extra_networks_hypernet import ExtraNetworkHypernet
|
|
|
|
register_extra_network(ExtraNetworkHypernet())
|
|
|
|
|
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
class ExtraNetworkParams:
|
|
|
|
def __init__(self, items=None):
|
|
|
|
self.items = items or []
|
2023-05-19 13:59:29 -06:00
|
|
|
self.positional = []
|
|
|
|
self.named = {}
|
|
|
|
|
|
|
|
for item in self.items:
|
2023-05-31 13:45:16 -06:00
|
|
|
parts = item.split('=', 2) if isinstance(item, str) else [item]
|
2023-05-19 13:59:29 -06:00
|
|
|
if len(parts) == 2:
|
|
|
|
self.named[parts[0]] = parts[1]
|
|
|
|
else:
|
|
|
|
self.positional.append(item)
|
2023-01-20 22:36:07 -07:00
|
|
|
|
2023-06-03 13:24:44 -06:00
|
|
|
def __eq__(self, other):
|
2023-06-03 14:19:47 -06:00
|
|
|
return self.items == other.items
|
2023-06-03 13:24:44 -06:00
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
class ExtraNetwork:
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def activate(self, p, params_list):
|
|
|
|
"""
|
|
|
|
Called by processing on every run. Whatever the extra network is meant to do should be activated here.
|
|
|
|
Passes arguments related to this extra network in params_list.
|
|
|
|
User passes arguments by specifying this in his prompt:
|
|
|
|
|
|
|
|
<name:arg1:arg2:arg3>
|
|
|
|
|
|
|
|
Where name matches the name of this ExtraNetwork object, and arg1:arg2:arg3 are any natural number of text arguments
|
|
|
|
separated by colon.
|
|
|
|
|
2024-03-03 23:37:23 -07:00
|
|
|
Even if the user does not mention this ExtraNetwork in his prompt, the call will still be made, with empty params_list -
|
2023-01-20 22:36:07 -07:00
|
|
|
in this case, all effects of this extra networks should be disabled.
|
|
|
|
|
|
|
|
Can be called multiple times before deactivate() - each new call should override the previous call completely.
|
|
|
|
|
|
|
|
For example, if this ExtraNetwork's name is 'hypernet' and user's prompt is:
|
|
|
|
|
|
|
|
> "1girl, <hypernet:agm:1.1> <extrasupernet:master:12:13:14> <hypernet:ray>"
|
|
|
|
|
|
|
|
params_list will be:
|
|
|
|
|
|
|
|
[
|
|
|
|
ExtraNetworkParams(items=["agm", "1.1"]),
|
|
|
|
ExtraNetworkParams(items=["ray"])
|
|
|
|
]
|
|
|
|
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def deactivate(self, p):
|
|
|
|
"""
|
|
|
|
Called at the end of processing for housekeeping. No need to do anything here.
|
|
|
|
"""
|
|
|
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
def lookup_extra_networks(extra_network_data):
|
|
|
|
"""returns a dict mapping ExtraNetwork objects to lists of arguments for those extra networks.
|
2023-01-20 22:36:07 -07:00
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
Example input:
|
|
|
|
{
|
|
|
|
'lora': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58310>],
|
|
|
|
'lyco': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58F70>],
|
|
|
|
'hypernet': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D5A800>]
|
|
|
|
}
|
|
|
|
|
|
|
|
Example output:
|
|
|
|
|
|
|
|
{
|
|
|
|
<extra_networks_lora.ExtraNetworkLora object at 0x0000020581BEECE0>: [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58310>, <modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58F70>],
|
|
|
|
<modules.extra_networks_hypernet.ExtraNetworkHypernet object at 0x0000020581BEEE60>: [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D5A800>]
|
|
|
|
}
|
|
|
|
"""
|
2023-07-17 01:06:02 -06:00
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
res = {}
|
|
|
|
|
|
|
|
for extra_network_name, extra_network_args in list(extra_network_data.items()):
|
2023-01-20 22:36:07 -07:00
|
|
|
extra_network = extra_network_registry.get(extra_network_name, None)
|
2023-08-26 23:41:26 -06:00
|
|
|
alias = extra_network_aliases.get(extra_network_name, None)
|
2023-07-17 01:06:02 -06:00
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
if alias is not None and extra_network is None:
|
|
|
|
extra_network = alias
|
2023-07-17 01:06:02 -06:00
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
if extra_network is None:
|
2023-08-26 23:41:26 -06:00
|
|
|
logging.info(f"Skipping unknown extra network: {extra_network_name}")
|
2023-01-20 22:36:07 -07:00
|
|
|
continue
|
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
res.setdefault(extra_network, []).extend(extra_network_args)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
|
|
def activate(p, extra_network_data):
|
|
|
|
"""call activate for extra networks in extra_network_data in specified order, then call
|
|
|
|
activate for all remaining registered networks with an empty argument list"""
|
|
|
|
|
|
|
|
activated = []
|
|
|
|
|
|
|
|
for extra_network, extra_network_args in lookup_extra_networks(extra_network_data).items():
|
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
try:
|
|
|
|
extra_network.activate(p, extra_network_args)
|
2023-07-17 01:06:02 -06:00
|
|
|
activated.append(extra_network)
|
2023-01-20 22:36:07 -07:00
|
|
|
except Exception as e:
|
2023-08-26 23:41:26 -06:00
|
|
|
errors.display(e, f"activating extra network {extra_network.name} with arguments {extra_network_args}")
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
for extra_network_name, extra_network in extra_network_registry.items():
|
2023-07-17 01:06:02 -06:00
|
|
|
if extra_network in activated:
|
2023-01-20 22:36:07 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
extra_network.activate(p, [])
|
|
|
|
except Exception as e:
|
|
|
|
errors.display(e, f"activating extra network {extra_network_name}")
|
|
|
|
|
2023-06-19 00:37:20 -06:00
|
|
|
if p.scripts is not None:
|
|
|
|
p.scripts.after_extra_networks_activate(p, batch_number=p.iteration, prompts=p.prompts, seeds=p.seeds, subseeds=p.subseeds, extra_network_data=extra_network_data)
|
|
|
|
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
def deactivate(p, extra_network_data):
|
|
|
|
"""call deactivate for extra networks in extra_network_data in specified order, then call
|
|
|
|
deactivate for all remaining registered networks"""
|
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
data = lookup_extra_networks(extra_network_data)
|
2023-01-20 22:36:07 -07:00
|
|
|
|
2023-08-26 23:41:26 -06:00
|
|
|
for extra_network in data:
|
2023-01-20 22:36:07 -07:00
|
|
|
try:
|
|
|
|
extra_network.deactivate(p)
|
|
|
|
except Exception as e:
|
2023-08-26 23:41:26 -06:00
|
|
|
errors.display(e, f"deactivating extra network {extra_network.name}")
|
2023-01-20 22:36:07 -07:00
|
|
|
|
|
|
|
for extra_network_name, extra_network in extra_network_registry.items():
|
2023-08-26 23:41:26 -06:00
|
|
|
if extra_network in data:
|
2023-01-20 22:36:07 -07:00
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
extra_network.deactivate(p)
|
|
|
|
except Exception as e:
|
|
|
|
errors.display(e, f"deactivating unmentioned extra network {extra_network_name}")
|
|
|
|
|
|
|
|
|
|
|
|
re_extra_net = re.compile(r"<(\w+):([^>]+)>")
|
|
|
|
|
|
|
|
|
|
|
|
def parse_prompt(prompt):
|
|
|
|
res = defaultdict(list)
|
|
|
|
|
|
|
|
def found(m):
|
|
|
|
name = m.group(1)
|
|
|
|
args = m.group(2)
|
|
|
|
|
|
|
|
res[name].append(ExtraNetworkParams(items=args.split(":")))
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
prompt = re.sub(re_extra_net, found, prompt)
|
|
|
|
|
|
|
|
return prompt, res
|
|
|
|
|
|
|
|
|
|
|
|
def parse_prompts(prompts):
|
|
|
|
res = []
|
|
|
|
extra_data = None
|
|
|
|
|
|
|
|
for prompt in prompts:
|
|
|
|
updated_prompt, parsed_extra_data = parse_prompt(prompt)
|
|
|
|
|
|
|
|
if extra_data is None:
|
|
|
|
extra_data = parsed_extra_data
|
|
|
|
|
|
|
|
res.append(updated_prompt)
|
|
|
|
|
|
|
|
return res, extra_data
|
|
|
|
|
2023-08-04 13:05:40 -06:00
|
|
|
|
2024-01-03 16:28:05 -07:00
|
|
|
def get_user_metadata(filename, lister=None):
|
2023-08-04 13:05:40 -06:00
|
|
|
if filename is None:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
basename, ext = os.path.splitext(filename)
|
|
|
|
metadata_filename = basename + '.json'
|
|
|
|
|
|
|
|
metadata = {}
|
|
|
|
try:
|
2024-01-03 16:28:05 -07:00
|
|
|
exists = lister.exists(metadata_filename) if lister else os.path.exists(metadata_filename)
|
|
|
|
if exists:
|
2023-08-04 13:05:40 -06:00
|
|
|
with open(metadata_filename, "r", encoding="utf8") as file:
|
|
|
|
metadata = json.load(file)
|
|
|
|
except Exception as e:
|
|
|
|
errors.display(e, f"reading extra network user metadata from {metadata_filename}")
|
|
|
|
|
|
|
|
return metadata
|