speedup extra networks listing
This commit is contained in:
parent
a1d6ada69a
commit
47d9dd0240
@ -3,7 +3,7 @@ import re
|
|||||||
import torch
|
import torch
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from modules import shared, devices, sd_models, errors, scripts, sd_hijack, hashes
|
from modules import shared, devices, sd_models, errors, scripts, sd_hijack, hashes, cache
|
||||||
|
|
||||||
metadata_tags_order = {"ss_sd_model_name": 1, "ss_resolution": 2, "ss_clip_skip": 3, "ss_num_train_images": 10, "ss_tag_frequency": 20}
|
metadata_tags_order = {"ss_sd_model_name": 1, "ss_resolution": 2, "ss_clip_skip": 3, "ss_num_train_images": 10, "ss_tag_frequency": 20}
|
||||||
|
|
||||||
@ -78,9 +78,16 @@ class LoraOnDisk:
|
|||||||
self.metadata = {}
|
self.metadata = {}
|
||||||
self.is_safetensors = os.path.splitext(filename)[1].lower() == ".safetensors"
|
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:
|
if self.is_safetensors:
|
||||||
try:
|
try:
|
||||||
self.metadata = sd_models.read_metadata_from_safetensors(filename)
|
#self.metadata = sd_models.read_metadata_from_safetensors(filename)
|
||||||
|
self.metadata = cache.cached_data_for_file('safetensors-metadata', "lora/" + self.name, filename, read_metadata)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.display(e, f"reading lora {filename}")
|
errors.display(e, f"reading lora {filename}")
|
||||||
|
|
||||||
@ -91,7 +98,6 @@ class LoraOnDisk:
|
|||||||
|
|
||||||
self.metadata = m
|
self.metadata = m
|
||||||
|
|
||||||
self.ssmd_cover_images = self.metadata.pop('ssmd_cover_images', None) # those are cover images and they are too big to display in UI as text
|
|
||||||
self.alias = self.metadata.get('ss_output_name', self.name)
|
self.alias = self.metadata.get('ss_output_name', self.name)
|
||||||
|
|
||||||
self.hash = None
|
self.hash = None
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import html
|
import html
|
||||||
import json
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
@ -64,7 +63,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
def get_metadata_table(self, name):
|
def get_metadata_table(self, name):
|
||||||
table = super().get_metadata_table(name)
|
table = super().get_metadata_table(name)
|
||||||
item = self.page.items.get(name, {})
|
item = self.page.items.get(name, {})
|
||||||
metadata = json.loads(item.get("metadata") or '{}')
|
metadata = item.get("metadata") or {}
|
||||||
|
|
||||||
keys = [
|
keys = [
|
||||||
('ss_sd_model_name', "Model:"),
|
('ss_sd_model_name', "Model:"),
|
||||||
@ -91,7 +90,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
values = super().put_values_into_components(name)
|
values = super().put_values_into_components(name)
|
||||||
|
|
||||||
item = self.page.items.get(name, {})
|
item = self.page.items.get(name, {})
|
||||||
metadata = json.loads(item.get("metadata") or '{}')
|
metadata = item.get("metadata") or {}
|
||||||
|
|
||||||
tags = build_tags(metadata)
|
tags = build_tags(metadata)
|
||||||
gradio_tags = [(tag, str(count)) for tag, count in tags[0:24]]
|
gradio_tags = [(tag, str(count)) for tag, count in tags[0:24]]
|
||||||
@ -108,7 +107,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
|
|
||||||
def generate_random_prompt(self, name):
|
def generate_random_prompt(self, name):
|
||||||
item = self.page.items.get(name, {})
|
item = self.page.items.get(name, {})
|
||||||
metadata = json.loads(item.get("metadata") or '{}')
|
metadata = item.get("metadata") or {}
|
||||||
tags = build_tags(metadata)
|
tags = build_tags(metadata)
|
||||||
|
|
||||||
return self.generate_random_prompt_from_tags(tags)
|
return self.generate_random_prompt_from_tags(tags)
|
||||||
@ -142,7 +141,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
|
|
||||||
self.edit_notes = gr.TextArea(label='Notes', lines=4)
|
self.edit_notes = gr.TextArea(label='Notes', lines=4)
|
||||||
|
|
||||||
generate_random_prompt.click(fn=self.generate_random_prompt, inputs=[self.edit_name_input], outputs=[random_prompt])
|
generate_random_prompt.click(fn=self.generate_random_prompt, inputs=[self.edit_name_input], outputs=[random_prompt], show_progress=False)
|
||||||
|
|
||||||
def select_tag(activation_text, evt: gr.SelectData):
|
def select_tag(activation_text, evt: gr.SelectData):
|
||||||
tag = evt.value[0]
|
tag = evt.value[0]
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import lora
|
import lora
|
||||||
|
|
||||||
from modules import shared, ui_extra_networks
|
from modules import shared, ui_extra_networks
|
||||||
|
from modules.ui_extra_networks import quote_js
|
||||||
from ui_edit_user_metadata import LoraUserMetadataEditor
|
from ui_edit_user_metadata import LoraUserMetadataEditor
|
||||||
|
|
||||||
|
|
||||||
@ -20,6 +20,7 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
|
|
||||||
alias = lora_on_disk.get_alias()
|
alias = lora_on_disk.get_alias()
|
||||||
|
|
||||||
|
# in 1.5 filename changes to be full filename instead of path without extension, and metadata is dict instead of json string
|
||||||
item = {
|
item = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"filename": lora_on_disk.filename,
|
"filename": lora_on_disk.filename,
|
||||||
@ -27,17 +28,17 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
"description": self.find_description(path),
|
"description": self.find_description(path),
|
||||||
"search_term": self.search_terms_from_path(lora_on_disk.filename),
|
"search_term": self.search_terms_from_path(lora_on_disk.filename),
|
||||||
"local_preview": f"{path}.{shared.opts.samples_format}",
|
"local_preview": f"{path}.{shared.opts.samples_format}",
|
||||||
"metadata": json.dumps(lora_on_disk.metadata, indent=4) if lora_on_disk.metadata else None,
|
"metadata": lora_on_disk.metadata,
|
||||||
"sort_keys": {'default': index, **self.get_sort_keys(lora_on_disk.filename)},
|
"sort_keys": {'default': index, **self.get_sort_keys(lora_on_disk.filename)},
|
||||||
}
|
}
|
||||||
|
|
||||||
self.read_user_metadata(item)
|
self.read_user_metadata(item)
|
||||||
activation_text = item["user_metadata"].get("activation text")
|
activation_text = item["user_metadata"].get("activation text")
|
||||||
preferred_weight = item["user_metadata"].get("preferred weight", 0.0)
|
preferred_weight = item["user_metadata"].get("preferred weight", 0.0)
|
||||||
item["prompt"] = json.dumps(f"<lora:{alias}:") + " + " + (str(preferred_weight) if preferred_weight else "opts.extra_networks_default_multiplier") + " + " + json.dumps(">")
|
item["prompt"] = quote_js(f"<lora:{alias}:") + " + " + (str(preferred_weight) if preferred_weight else "opts.extra_networks_default_multiplier") + " + " + quote_js(">")
|
||||||
|
|
||||||
if activation_text:
|
if activation_text:
|
||||||
item["prompt"] += " + " + json.dumps(" " + activation_text)
|
item["prompt"] += " + " + quote_js(" " + activation_text)
|
||||||
|
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
|
import threading
|
||||||
import filelock
|
|
||||||
|
|
||||||
from modules.paths import data_path, script_path
|
from modules.paths import data_path, script_path
|
||||||
|
|
||||||
cache_filename = os.path.join(data_path, "cache.json")
|
cache_filename = os.path.join(data_path, "cache.json")
|
||||||
cache_data = None
|
cache_data = None
|
||||||
|
cache_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
def dump_cache():
|
def dump_cache():
|
||||||
@ -14,7 +14,7 @@ def dump_cache():
|
|||||||
Saves all cache data to a file.
|
Saves all cache data to a file.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with filelock.FileLock(f"{cache_filename}.lock"):
|
with cache_lock:
|
||||||
with open(cache_filename, "w", encoding="utf8") as file:
|
with open(cache_filename, "w", encoding="utf8") as file:
|
||||||
json.dump(cache_data, file, indent=4)
|
json.dump(cache_data, file, indent=4)
|
||||||
|
|
||||||
@ -33,17 +33,18 @@ def cache(subsection):
|
|||||||
global cache_data
|
global cache_data
|
||||||
|
|
||||||
if cache_data is None:
|
if cache_data is None:
|
||||||
with filelock.FileLock(f"{cache_filename}.lock"):
|
with cache_lock:
|
||||||
if not os.path.isfile(cache_filename):
|
if cache_data is None:
|
||||||
cache_data = {}
|
if not os.path.isfile(cache_filename):
|
||||||
else:
|
|
||||||
try:
|
|
||||||
with open(cache_filename, "r", encoding="utf8") as file:
|
|
||||||
cache_data = json.load(file)
|
|
||||||
except Exception:
|
|
||||||
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
|
|
||||||
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
|
|
||||||
cache_data = {}
|
cache_data = {}
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
with open(cache_filename, "r", encoding="utf8") as file:
|
||||||
|
cache_data = json.load(file)
|
||||||
|
except Exception:
|
||||||
|
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
|
||||||
|
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
|
||||||
|
cache_data = {}
|
||||||
|
|
||||||
s = cache_data.get(subsection, {})
|
s = cache_data.get(subsection, {})
|
||||||
cache_data[subsection] = s
|
cache_data[subsection] = s
|
||||||
|
@ -73,6 +73,12 @@ def add_pages_to_demo(app):
|
|||||||
app.add_api_route("/sd_extra_networks/get-single-card", get_single_card, methods=["GET"])
|
app.add_api_route("/sd_extra_networks/get-single-card", get_single_card, methods=["GET"])
|
||||||
|
|
||||||
|
|
||||||
|
def quote_js(s):
|
||||||
|
s = s.replace('\\', '\\\\')
|
||||||
|
s = s.replace('"', '\\"')
|
||||||
|
return f'"{s}"'
|
||||||
|
|
||||||
|
|
||||||
class ExtraNetworksPage:
|
class ExtraNetworksPage:
|
||||||
def __init__(self, title):
|
def __init__(self, title):
|
||||||
self.title = title
|
self.title = title
|
||||||
@ -203,7 +209,7 @@ class ExtraNetworksPage:
|
|||||||
|
|
||||||
onclick = item.get("onclick", None)
|
onclick = item.get("onclick", None)
|
||||||
if onclick is None:
|
if onclick is None:
|
||||||
onclick = '"' + html.escape(f"""return cardClicked({json.dumps(tabname)}, {item["prompt"]}, {"true" if self.allow_negative_prompt else "false"})""") + '"'
|
onclick = '"' + html.escape(f"""return cardClicked({quote_js(tabname)}, {item["prompt"]}, {"true" if self.allow_negative_prompt else "false"})""") + '"'
|
||||||
|
|
||||||
height = f"height: {shared.opts.extra_networks_card_height}px;" if shared.opts.extra_networks_card_height else ''
|
height = f"height: {shared.opts.extra_networks_card_height}px;" if shared.opts.extra_networks_card_height else ''
|
||||||
width = f"width: {shared.opts.extra_networks_card_width}px;" if shared.opts.extra_networks_card_width else ''
|
width = f"width: {shared.opts.extra_networks_card_width}px;" if shared.opts.extra_networks_card_width else ''
|
||||||
@ -211,9 +217,9 @@ class ExtraNetworksPage:
|
|||||||
metadata_button = ""
|
metadata_button = ""
|
||||||
metadata = item.get("metadata")
|
metadata = item.get("metadata")
|
||||||
if metadata:
|
if metadata:
|
||||||
metadata_button = f"<div class='metadata-button card-button' title='Show internal metadata' onclick='extraNetworksRequestMetadata(event, {json.dumps(self.name)}, {json.dumps(item['name'])})'></div>"
|
metadata_button = f"<div class='metadata-button card-button' title='Show internal metadata' onclick='extraNetworksRequestMetadata(event, {quote_js(self.name)}, {quote_js(item['name'])})'></div>"
|
||||||
|
|
||||||
edit_button = f"<div class='edit-button card-button' title='Edit metadata' onclick='extraNetworksEditUserMetadata(event, {json.dumps(tabname)}, {json.dumps(self.id_page)}, {json.dumps(item['name'])})'></div>"
|
edit_button = f"<div class='edit-button card-button' title='Edit metadata' onclick='extraNetworksEditUserMetadata(event, {quote_js(tabname)}, {quote_js(self.id_page)}, {quote_js(item['name'])})'></div>"
|
||||||
|
|
||||||
local_path = ""
|
local_path = ""
|
||||||
filename = item.get("filename", "")
|
filename = item.get("filename", "")
|
||||||
@ -239,12 +245,12 @@ class ExtraNetworksPage:
|
|||||||
"background_image": background_image,
|
"background_image": background_image,
|
||||||
"style": f"'display: none; {height}{width}'",
|
"style": f"'display: none; {height}{width}'",
|
||||||
"prompt": item.get("prompt", None),
|
"prompt": item.get("prompt", None),
|
||||||
"tabname": json.dumps(tabname),
|
"tabname": quote_js(tabname),
|
||||||
"local_preview": json.dumps(item["local_preview"]),
|
"local_preview": quote_js(item["local_preview"]),
|
||||||
"name": item["name"],
|
"name": item["name"],
|
||||||
"description": (item.get("description") or ""),
|
"description": (item.get("description") or ""),
|
||||||
"card_clicked": onclick,
|
"card_clicked": onclick,
|
||||||
"save_card_preview": '"' + html.escape(f"""return saveCardPreview(event, {json.dumps(tabname)}, {json.dumps(item["local_preview"])})""") + '"',
|
"save_card_preview": '"' + html.escape(f"""return saveCardPreview(event, {quote_js(tabname)}, {quote_js(item["local_preview"])})""") + '"',
|
||||||
"search_term": item.get("search_term", ""),
|
"search_term": item.get("search_term", ""),
|
||||||
"metadata_button": metadata_button,
|
"metadata_button": metadata_button,
|
||||||
"edit_button": edit_button,
|
"edit_button": edit_button,
|
||||||
@ -359,7 +365,7 @@ def create_ui(container, button, tabname):
|
|||||||
page_elem = gr.HTML('Loading...', elem_id=elem_id)
|
page_elem = gr.HTML('Loading...', elem_id=elem_id)
|
||||||
ui.pages.append(page_elem)
|
ui.pages.append(page_elem)
|
||||||
|
|
||||||
page_elem.change(fn=lambda: None, _js='function(){applyExtraNetworkFilter(' + json.dumps(tabname) + '); return []}', inputs=[], outputs=[])
|
page_elem.change(fn=lambda: None, _js='function(){applyExtraNetworkFilter(' + quote_js(tabname) + '); return []}', inputs=[], outputs=[])
|
||||||
|
|
||||||
editor = page.create_user_metadata_editor(ui, tabname)
|
editor = page.create_user_metadata_editor(ui, tabname)
|
||||||
editor.create_ui()
|
editor.create_ui()
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import html
|
import html
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from modules import shared, ui_extra_networks, sd_models
|
from modules import shared, ui_extra_networks, sd_models
|
||||||
|
from modules.ui_extra_networks import quote_js
|
||||||
|
|
||||||
|
|
||||||
class ExtraNetworksPageCheckpoints(ui_extra_networks.ExtraNetworksPage):
|
class ExtraNetworksPageCheckpoints(ui_extra_networks.ExtraNetworksPage):
|
||||||
@ -21,7 +21,7 @@ class ExtraNetworksPageCheckpoints(ui_extra_networks.ExtraNetworksPage):
|
|||||||
"preview": self.find_preview(path),
|
"preview": self.find_preview(path),
|
||||||
"description": self.find_description(path),
|
"description": self.find_description(path),
|
||||||
"search_term": self.search_terms_from_path(checkpoint.filename) + " " + (checkpoint.sha256 or ""),
|
"search_term": self.search_terms_from_path(checkpoint.filename) + " " + (checkpoint.sha256 or ""),
|
||||||
"onclick": '"' + html.escape(f"""return selectCheckpoint({json.dumps(name)})""") + '"',
|
"onclick": '"' + html.escape(f"""return selectCheckpoint({quote_js(name)})""") + '"',
|
||||||
"local_preview": f"{path}.{shared.opts.samples_format}",
|
"local_preview": f"{path}.{shared.opts.samples_format}",
|
||||||
"sort_keys": {'default': index, **self.get_sort_keys(checkpoint.filename)},
|
"sort_keys": {'default': index, **self.get_sort_keys(checkpoint.filename)},
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from modules import shared, ui_extra_networks
|
from modules import shared, ui_extra_networks
|
||||||
|
from modules.ui_extra_networks import quote_js
|
||||||
|
|
||||||
|
|
||||||
class ExtraNetworksPageHypernetworks(ui_extra_networks.ExtraNetworksPage):
|
class ExtraNetworksPageHypernetworks(ui_extra_networks.ExtraNetworksPage):
|
||||||
@ -21,7 +21,7 @@ class ExtraNetworksPageHypernetworks(ui_extra_networks.ExtraNetworksPage):
|
|||||||
"preview": self.find_preview(path),
|
"preview": self.find_preview(path),
|
||||||
"description": self.find_description(path),
|
"description": self.find_description(path),
|
||||||
"search_term": self.search_terms_from_path(path),
|
"search_term": self.search_terms_from_path(path),
|
||||||
"prompt": json.dumps(f"<hypernet:{name}:") + " + opts.extra_networks_default_multiplier + " + json.dumps(">"),
|
"prompt": quote_js(f"<hypernet:{name}:") + " + opts.extra_networks_default_multiplier + " + quote_js(">"),
|
||||||
"local_preview": f"{path}.preview.{shared.opts.samples_format}",
|
"local_preview": f"{path}.preview.{shared.opts.samples_format}",
|
||||||
"sort_keys": {'default': index, **self.get_sort_keys(path + ext)},
|
"sort_keys": {'default': index, **self.get_sort_keys(path + ext)},
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import json
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from modules import ui_extra_networks, sd_hijack, shared
|
from modules import ui_extra_networks, sd_hijack, shared
|
||||||
|
from modules.ui_extra_networks import quote_js
|
||||||
|
|
||||||
|
|
||||||
class ExtraNetworksPageTextualInversion(ui_extra_networks.ExtraNetworksPage):
|
class ExtraNetworksPageTextualInversion(ui_extra_networks.ExtraNetworksPage):
|
||||||
@ -22,7 +22,7 @@ class ExtraNetworksPageTextualInversion(ui_extra_networks.ExtraNetworksPage):
|
|||||||
"preview": self.find_preview(path),
|
"preview": self.find_preview(path),
|
||||||
"description": self.find_description(path),
|
"description": self.find_description(path),
|
||||||
"search_term": self.search_terms_from_path(embedding.filename),
|
"search_term": self.search_terms_from_path(embedding.filename),
|
||||||
"prompt": json.dumps(embedding.name),
|
"prompt": quote_js(embedding.name),
|
||||||
"local_preview": f"{path}.preview.{shared.opts.samples_format}",
|
"local_preview": f"{path}.preview.{shared.opts.samples_format}",
|
||||||
"sort_keys": {'default': index, **self.get_sort_keys(embedding.filename)},
|
"sort_keys": {'default': index, **self.get_sort_keys(embedding.filename)},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user