2023-01-21 13:15:53 +00:00
|
|
|
import os
|
2023-07-16 20:13:55 +00:00
|
|
|
import networks
|
2023-01-21 13:15:53 +00:00
|
|
|
|
|
|
|
from modules import shared, ui_extra_networks
|
2023-07-16 06:25:32 +00:00
|
|
|
from modules.ui_extra_networks import quote_js
|
2023-07-15 21:56:53 +00:00
|
|
|
from ui_edit_user_metadata import LoraUserMetadataEditor
|
2023-01-21 13:15:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__('Lora')
|
|
|
|
|
|
|
|
def refresh(self):
|
2023-07-16 20:13:55 +00:00
|
|
|
networks.list_available_networks()
|
2023-01-21 13:15:53 +00:00
|
|
|
|
2023-07-16 05:38:23 +00:00
|
|
|
def create_item(self, name, index=None):
|
2023-07-16 20:13:55 +00:00
|
|
|
lora_on_disk = networks.available_networks.get(name)
|
2023-07-16 05:38:23 +00:00
|
|
|
|
|
|
|
path, ext = os.path.splitext(lora_on_disk.filename)
|
|
|
|
|
|
|
|
alias = lora_on_disk.get_alias()
|
|
|
|
|
2023-07-16 06:25:32 +00:00
|
|
|
# in 1.5 filename changes to be full filename instead of path without extension, and metadata is dict instead of json string
|
2023-07-16 05:38:23 +00:00
|
|
|
item = {
|
|
|
|
"name": name,
|
|
|
|
"filename": lora_on_disk.filename,
|
|
|
|
"preview": self.find_preview(path),
|
|
|
|
"description": self.find_description(path),
|
|
|
|
"search_term": self.search_terms_from_path(lora_on_disk.filename),
|
|
|
|
"local_preview": f"{path}.{shared.opts.samples_format}",
|
2023-07-16 06:25:32 +00:00
|
|
|
"metadata": lora_on_disk.metadata,
|
2023-07-16 05:38:23 +00:00
|
|
|
"sort_keys": {'default': index, **self.get_sort_keys(lora_on_disk.filename)},
|
|
|
|
}
|
2023-07-15 21:56:53 +00:00
|
|
|
|
2023-07-16 05:38:23 +00:00
|
|
|
self.read_user_metadata(item)
|
|
|
|
activation_text = item["user_metadata"].get("activation text")
|
|
|
|
preferred_weight = item["user_metadata"].get("preferred weight", 0.0)
|
2023-07-16 06:25:32 +00:00
|
|
|
item["prompt"] = quote_js(f"<lora:{alias}:") + " + " + (str(preferred_weight) if preferred_weight else "opts.extra_networks_default_multiplier") + " + " + quote_js(">")
|
2023-07-16 05:38:23 +00:00
|
|
|
|
|
|
|
if activation_text:
|
2023-07-16 06:25:32 +00:00
|
|
|
item["prompt"] += " + " + quote_js(" " + activation_text)
|
2023-07-16 05:38:23 +00:00
|
|
|
|
|
|
|
return item
|
|
|
|
|
|
|
|
def list_items(self):
|
2023-07-16 20:13:55 +00:00
|
|
|
for index, name in enumerate(networks.available_networks):
|
2023-07-16 05:38:23 +00:00
|
|
|
item = self.create_item(name, index)
|
2023-07-15 21:56:53 +00:00
|
|
|
yield item
|
|
|
|
|
2023-01-21 13:15:53 +00:00
|
|
|
def allowed_directories_for_previews(self):
|
2023-01-21 20:11:37 +00:00
|
|
|
return [shared.cmd_opts.lora_dir]
|
2023-01-21 13:15:53 +00:00
|
|
|
|
2023-07-15 21:56:53 +00:00
|
|
|
def create_user_metadata_editor(self, ui, tabname):
|
|
|
|
return LoraUserMetadataEditor(ui, tabname, self)
|