Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExtraNetworks: Performance Updates and Improvements #15530

Open
wants to merge 141 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
141 commits
Select commit Hold shift + click to select a range
83e85ad
add clusterize lists. need to debug.
Sj-Si Mar 13, 2024
f490498
Fix bugs. clean up. Need to finish code cleanup.
Sj-Si Mar 14, 2024
08da356
fix more bugs. clean up more.
Sj-Si Mar 15, 2024
fb07a60
clean up presentation of dirs view. need to resolve issue with initia…
Sj-Si Mar 15, 2024
9194c0b
fix more bugs. clean up more.
Sj-Si Mar 19, 2024
8c8536f
fix remaining known bugs. clean up code.
Sj-Si Mar 20, 2024
ef29f65
fix more bugs. clean up more.
Sj-Si Mar 21, 2024
26b0416
fix more bugs and improve stability of code.
Sj-Si Mar 22, 2024
dc3a41b
switch to use local version of Clusterize.js.
Sj-Si Mar 22, 2024
4885d59
clean up code and add documentation
Sj-Si Mar 23, 2024
62a5b2b
clean up and fix bugs
Sj-Si Mar 25, 2024
d069901
fix more bugs. clean up more.
Sj-Si Mar 25, 2024
b5f3a85
fix more bugs. not happy with how refreshing completely regenerates h…
Sj-Si Mar 25, 2024
e9968e7
Switch to zlib since gzip was not deterministic.
Sj-Si Mar 26, 2024
4a1ca42
fix bugs.
Sj-Si Mar 26, 2024
4866585
disable eslint for file
Sj-Si Mar 26, 2024
a3228a5
run eslint and ruff
Sj-Si Mar 26, 2024
5c0f315
silence eslint undefined function errors
Sj-Si Mar 26, 2024
d88e91c
merge dev into branch
Sj-Si Mar 26, 2024
0e0b613
cleanup and fix bugs.
Sj-Si Mar 27, 2024
8bde522
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si Mar 27, 2024
5877993
Switch to using api calls to retrieve data.
Sj-Si Mar 29, 2024
9c77d3f
fix linting
Sj-Si Mar 29, 2024
4e31bca
Move utility functions to separate script file. Begin redesign of clu…
Sj-Si Apr 5, 2024
806bbff
update html templates
Sj-Si Apr 9, 2024
ec80d57
update to use new clusterize class
Sj-Si Apr 9, 2024
fc7a8a4
need to fix bug with row width.
Sj-Si Apr 9, 2024
5e4dfee
Finish initial conversion to clusterize using data loader. Need to fi…
Sj-Si Apr 12, 2024
0853c2b
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si Apr 12, 2024
6b8374c
fix some bugs. need to continue bug testing.
Sj-Si Apr 14, 2024
b7a6df9
run eslint and ruff
Sj-Si Apr 14, 2024
451190c
clean up
Sj-Si Apr 15, 2024
9357ee9
fix bugs.
Sj-Si Apr 15, 2024
ed04f79
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si Apr 15, 2024
4d3661c
fix bug
Sj-Si Apr 15, 2024
f97e76b
fix bug
Sj-Si Apr 15, 2024
8ca069e
fix comment
Sj-Si Apr 15, 2024
634f7bc
fix bug caused by refresh not being called properly
Sj-Si Apr 15, 2024
fc20d1d
run formatter
Sj-Si Apr 15, 2024
18c6795
add documentation
Sj-Si Apr 15, 2024
1167468
remove log line
Sj-Si Apr 15, 2024
49d1463
fix missing newline
Sj-Si Apr 15, 2024
2a8403e
fix sorting of tree list
Sj-Si Apr 15, 2024
226267e
fix button selection bug
Sj-Si Apr 15, 2024
5d64690
add timeouts to wait functions. fix control duplication bug.
Sj-Si Apr 16, 2024
43f769b
fix bug
Sj-Si Apr 16, 2024
5747321
fix refresh bug
Sj-Si Apr 17, 2024
429b9fa
add missing stopPropagation calls to event handlers.
Sj-Si Apr 17, 2024
b5a2c2a
wrap specific data attributes in quotes to allow for spaces in names
Sj-Si Apr 17, 2024
b4d184a
fix style to push controls to right of page
Sj-Si Apr 17, 2024
baa8746
fix conflict with function name. fix page ready request loop.
Sj-Si Apr 17, 2024
2d51558
fix syntax error
Sj-Si Apr 17, 2024
157073b
fix firefox resize-handle issues
Sj-Si Apr 17, 2024
57d0554
test fix for request error handling
Sj-Si Apr 18, 2024
580711c
fix error handling for fetching div ids
Sj-Si Apr 19, 2024
7d953cc
add expand/collapse buttons to directories in tree view
Sj-Si Apr 21, 2024
ba365e4
Fix broken CopyPath button text.
Sj-Si Apr 22, 2024
f051937
Fix hidden directory and model settings.
Sj-Si Apr 22, 2024
42e61d5
remove unused setting
Sj-Si Apr 22, 2024
7c2446c
Fix various filtering/sorting bugs
Sj-Si Apr 22, 2024
04535c5
Fix hypernetworks raw text prompt bug
Sj-Si Apr 22, 2024
58eef03
fix formatting of prompt string
Sj-Si Apr 22, 2024
2c5c126
fix activation text and updating of cards
Sj-Si Apr 22, 2024
7e006b8
fix filtering based on directory buttons.
Sj-Si Apr 22, 2024
05a0bf0
reduce verbosity of dirs view buttons
Sj-Si Apr 22, 2024
d96d4ce
Fix bugs caused by escape chars in paths.
Sj-Si Apr 23, 2024
b5271fb
Fix bug
Sj-Si Apr 23, 2024
c550bcc
Fix more bugs
Sj-Si Apr 23, 2024
85197f8
Fix more bugs
Sj-Si Apr 23, 2024
7182748
fix overlapping tree actions
Sj-Si Apr 23, 2024
4fa6f7c
Add scrollbar to dirs view. Add new class for custom scrollbar compon…
Sj-Si Apr 23, 2024
4279f7c
Fix sorting bugs.
Sj-Si Apr 24, 2024
3cf26b5
TEMP: Add logging to debug issues.
Sj-Si Apr 24, 2024
8b27a00
Redesign requests to server to prevent errors.
Sj-Si Apr 26, 2024
7eb0325
Fix linting errors.
Sj-Si Apr 26, 2024
15c36b9
adjust timeouts
Sj-Si Apr 26, 2024
6f2daaf
migrate to use event delegation for items instead of inline onclick e…
Sj-Si Apr 27, 2024
d912e72
change behavior of click actions
Sj-Si Apr 29, 2024
250e767
fix bugs with multi-function buttons and recursive filters.
Sj-Si Apr 30, 2024
bb28126
run linter
Sj-Si Apr 30, 2024
62efce4
fix doubleclick bug
Sj-Si Apr 30, 2024
f439705
fix bug with longpress deselect
Sj-Si Apr 30, 2024
fcb2e99
fix linting error
Sj-Si May 1, 2024
f07fbc0
fix bug when files disabled in tree view.
Sj-Si May 1, 2024
06a4e59
Fix windows filtering bug
Sj-Si May 2, 2024
4ccfc9c
fix bug with rapid enable/disable of views causing tree view to break.
Sj-Si May 2, 2024
edab04e
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si May 2, 2024
ec300d6
add old message for empty results to cards list.
Sj-Si May 2, 2024
2f8ee19
fix scrolling bug caused by invalid filtering and sorting
Sj-Si May 3, 2024
c8e21f6
fix loading/nodata messages
Sj-Si May 3, 2024
c92c400
fix bug caused by resize on selecting other page tabs.
Sj-Si May 3, 2024
da204f8
fix splash bugs caused by rapid changing of tabs during fetch requests.
Sj-Si May 3, 2024
b497467
fix hidden directory options and their functionality.
Sj-Si May 3, 2024
fbefe7f
Add option for default tree expanded depth.
Sj-Si May 3, 2024
1288298
Fix functionality of buttons in tabs. add more functions.
Sj-Si May 6, 2024
26623f9
fix name conflict with options
Sj-Si May 7, 2024
792ef08
fix missing filter update bug
Sj-Si May 7, 2024
14442b8
add button to disable cards view
Sj-Si May 7, 2024
5a2fca8
refactor CardsView to CardView to match Dirs and Tree view lengths. G…
Sj-Si May 7, 2024
6d07652
Add option to change left click behavior for directory filter.
Sj-Si May 8, 2024
1a2d4ae
fix default option
Sj-Si May 8, 2024
c674dcf
fix divs showing when disabled on load
Sj-Si May 8, 2024
6bb5613
Fix button event handling bugs on mobile. Add button to clear directo…
Sj-Si May 10, 2024
1e907d8
fix bug. run linter
Sj-Si May 10, 2024
a44e104
Fix views not loading with proper defaults.
Sj-Si May 10, 2024
cae1d87
patch bug by adding delay to tab setup
Sj-Si May 10, 2024
a10934b
add backward compatibility class to cards view
Sj-Si May 12, 2024
f179df3
Fix click event handling for mobile. Fix recursive filter visual bug.
Sj-Si May 12, 2024
998e574
move clear filters button
Sj-Si May 12, 2024
a2f8f99
add new util functions
Sj-Si May 12, 2024
4e9760c
clean up svg code
Sj-Si May 13, 2024
50a5ad8
Add a resizable grid component and apply to extra networks tabs.
Sj-Si May 20, 2024
17e17ce
fix bug
Sj-Si May 20, 2024
ee81dad
fix bugs. add initial card detail view code
Sj-Si May 22, 2024
4a2c806
Fix bugs with resize grid. Add documentation. Add example code.
Sj-Si May 23, 2024
ecfa65f
Change color of clearfilters button when filters are applied to give …
Sj-Si May 24, 2024
16495c2
Fix dblclick of resize grid handle. Fix resizing bugs.
Sj-Si May 24, 2024
f9ce4ec
Fix mobile resize grid dragging.
Sj-Si May 25, 2024
d05e6b9
fix bug
Sj-Si May 25, 2024
93fd33d
Prevent user from enabling view if no room on screen.
Sj-Si May 25, 2024
ab05f9c
Fix slow data fetch times. Fix scrollbar bug in dirs view.
Sj-Si May 26, 2024
dc5b155
Fix loading bugs. Clean up unused code.
Sj-Si May 26, 2024
25e516a
Fix bug with resizegrid resetting on tab change. Add content to detai…
Sj-Si May 29, 2024
e4be1d5
Added missing long press event for tree file items to enable the deta…
Sj-Si May 29, 2024
e78c9d6
Fix bug
Sj-Si May 29, 2024
478c695
Escape description HTML
Sj-Si May 29, 2024
2a7b2d9
Update setting for tree expansion depth with more intuitive options.
Sj-Si May 31, 2024
a3e9b0c
Remove toggling resize grid on refresh since it is not needed and was…
Sj-Si May 31, 2024
19f2c61
Fix parsing of HTML in model descriptions.
Sj-Si May 31, 2024
12f1670
Add visual feedback when long pressing card.
Sj-Si May 31, 2024
da17f5f
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si Jun 1, 2024
5f32eb1
Add option to swap left click behavior for model details. Fix missing…
Sj-Si Jun 3, 2024
4b0f321
fix refresh bug
Sj-Si Jun 3, 2024
5b8ed54
fix typo
Sj-Si Jun 3, 2024
26f77b9
Fix css animation times to reflect user settings.
Sj-Si Jun 4, 2024
11ed5f6
Fix details view errors
Sj-Si Jun 21, 2024
e147b57
Merge branch 'dev' into extra-networks-performance-updates
Sj-Si Jun 21, 2024
f8b72d7
Clean up clusterizer code.
Sj-Si Jun 25, 2024
3292091
Fix scrolling bugs.
Sj-Si Jun 25, 2024
9adb618
fix bug
Sj-Si Jun 25, 2024
fa7aa62
fix incorrect variable reference.
Sj-Si Jul 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 155 additions & 4 deletions extensions-builtin/Lora/ui_extra_networks_lora.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import os
import html
import datetime
import math
import matplotlib as mpl
import colorsys

import network
import 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, build_tags


class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
Expand All @@ -14,6 +19,7 @@ def __init__(self):

def refresh(self):
networks.list_available_networks()
super().refresh()

def create_item(self, name, index=None, enable_filter=True):
lora_on_disk = networks.available_networks.get(name)
Expand Down Expand Up @@ -43,10 +49,10 @@ def create_item(self, name, index=None, enable_filter=True):
self.read_user_metadata(item)
activation_text = item["user_metadata"].get("activation text")
preferred_weight = item["user_metadata"].get("preferred weight", 0.0)
item["prompt"] = quote_js(f"<lora:{alias}:") + " + " + (str(preferred_weight) if preferred_weight else "opts.extra_networks_default_multiplier") + " + " + quote_js(">")

prompt = f"<lora:{alias}:{str(preferred_weight) if preferred_weight else shared.opts.extra_networks_default_multiplier}>"
if activation_text:
item["prompt"] += " + " + quote_js(" " + activation_text)
prompt += f" {activation_text}"
item["prompt"] = quote_js(prompt)

negative_prompt = item["user_metadata"].get("negative text")
item["negative_prompt"] = quote_js("")
Expand Down Expand Up @@ -88,3 +94,148 @@ def allowed_directories_for_previews(self):

def create_user_metadata_editor(self, ui, tabname):
return LoraUserMetadataEditor(ui, tabname, self)

def get_model_detail_metadata_table(self, model_name: str) -> str:
res = super().get_model_detail_metadata_table(model_name)

metadata = self.metadata.get(model_name)
if metadata is None:
metadata = {}

keys = {
'ss_output_name': "Output name:",
'ss_sd_model_name': "Model:",
'ss_clip_skip': "Clip skip:",
'ss_network_module': "Kohya module:",
}

params = []

for k, lbl in keys.items():
v = metadata.get(k, None)
if v is not None and str(v) != "None":
params.append((lbl, html.escape(v)))

ss_training_started_at = metadata.get('ss_training_started_at')
if ss_training_started_at:
date_trained = datetime.datetime.utcfromtimestamp(
float(ss_training_started_at)
).strftime('%Y-%m-%d %H:%M')
params.append(("Date trained:", date_trained))

ss_bucket_info = metadata.get("ss_bucket_info")
if ss_bucket_info and "buckets" in ss_bucket_info:
resolutions = {}
for _, bucket in ss_bucket_info["buckets"].items():
resolution = bucket["resolution"]
resolution = f'{resolution[1]}x{resolution[0]}'
resolutions[resolution] = resolutions.get(resolution, 0) + int(bucket["count"])

resolutions_list = sorted(resolutions.keys(), key=resolutions.get, reverse=True)
resolutions_text = html.escape(", ".join(resolutions_list))
resolutions_text = (
"<div class='styled-scrollbar' style='overflow-x: auto'>"
f"{resolutions_text}"
"</div>"
)
params.append(("Resolutions:", resolutions_text))

image_count = 0
for v in metadata.get("ss_dataset_dirs", {}).values():
image_count += int(v.get("img_count", 0))

if image_count:
params.append(("Dataset size:", image_count))

tbl_metadata = "".join([f"<tr><th>{tr[0]}</th><td>{tr[1]}</td>" for tr in params])

return res + tbl_metadata

def get_model_detail_extra_html(self, model_name: str) -> str:
"""Generates HTML to show in the details view."""
res = ""

item = self.items.get(model_name, {})
metadata = item.get("metadata", {}) or {}
user_metadata = item.get("user_metadata", {}) or {}

sd_version = item.get("sd_version", None)
preferred_weight = user_metadata.get("preferred weight", None)
activation_text = user_metadata.get("activation text", None)
negative_text = user_metadata.get("negative text", None)

rows = []

if sd_version is not None:
rows.append(("SD Version:", sd_version))

if preferred_weight is not None:
rows.append(("Preferred weight:", preferred_weight))

if activation_text is not None:
rows.append(("Activation text:", activation_text))

if negative_text is not None:
rows.append(("Negative propmt:", negative_text))

rows_html = "".join([f"<tr><th>{tr[0]}</th><td>{tr[1]}</td>" for tr in rows])

if rows_html:
res += "<h3>User Metadata</h3>"
res += f"<table><tbody>{rows_html}</tbody></table>"

tags = build_tags(metadata)
if tags is None or len(tags) == 0:
res += "<h3>Model Tags</h3>"
res += "<div class='model-info--tags'>Metadata contains no tags</div>"
return res

min_tag = min(int(x[1]) for x in tags)
max_tag = max(int(x[1]) for x in tags)

cmap = mpl.colormaps["coolwarm"]

def _clamp(x: float, min_val: float, max_val: float) -> float:
return max(min_val, min(x, max_val))

def _get_fg_color(r, g, b) -> str:
return "#000000" if (r * 0.299 + g * 0.587 + b * 0.114) > 0.5 else "#FFFFFF"

tag_elems = []
for (tag_name, tag_count) in tags:
# Normalize tag count
tag_count = int(tag_count)
if min_tag == max_tag: # Prevent DivideByZero error.
cmap_idx = cmap.N // 2
else:
cmap_idx = math.floor(
(tag_count - min_tag) / (max_tag - min_tag) * (cmap.N - 1)
)

# Get the bg color based on tag count and a contrasting fg color.
base_color = cmap(cmap_idx)
base_color = [_clamp(x, 0, 1) for x in base_color]
base_fg_color = _get_fg_color(*base_color[:3])
# Now get a slightly darker background for the tag count bg color.
h, lum, s = colorsys.rgb_to_hls(*base_color[:3])
lum = max(min(lum * 0.7, 1.0), 0.0)
dark_color = colorsys.hls_to_rgb(h, lum, s)
dark_color = [_clamp(x, 0, 1) for x in dark_color]
dark_fg_color = _get_fg_color(*dark_color[:3])
# Convert the colors to a hex string.
base_color = mpl.colors.rgb2hex(base_color)
dark_color = mpl.colors.rgb2hex(dark_color)
# Finally, generate the HTML for this tag.
tag_style = f"background: {base_color};"
name_style = f"color: {base_fg_color};"
count_style = f"background: {dark_color}; color: {dark_fg_color};"

tag_elems.append((
f"<span class='model-info--tag' style='{tag_style}'>"
f"<span class='model-info--tag-name' style='{name_style}'>{tag_name}</span>"
f"<span class='model-info--tag-count' style='{count_style}'>{tag_count}</span>"
"</span>"
))
res += "<h3>Model Tags</h3>"
res += f"<div class='model-info--tags'>{''.join(tag_elems)}</div>"
return res
11 changes: 11 additions & 0 deletions html/extra-networks-btn-chevron.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div class="tree-list-item-action--chevron {extra_classes}">
<svg class="chevron-icon-single" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M 4 4 H 12 V 12" />
</svg>
<svg class="chevron-icon-double" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M 5 3 H 13 V 11" />
<path d="M 1 7 H 9 V 15" />
</svg>
</div>
2 changes: 2 additions & 0 deletions html/extra-networks-btn-copy-path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div class="copy-path-button card-button" title="Copy path to clipboard" data-clipboard-text="{clipboard_text}">
</div>
2 changes: 2 additions & 0 deletions html/extra-networks-btn-dirs-view-item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<button class='lg secondary gradio-button custom-button extra-network-dirs-view-button {extra_class}' title="{title}"
{data_attributes}>{label}</button>
1 change: 1 addition & 0 deletions html/extra-networks-btn-edit-metadata.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="edit-button card-button" title="Edit metadata"></div>
1 change: 1 addition & 0 deletions html/extra-networks-btn-show-metadata.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="metadata-button card-button" title="Show internal metadata"></div>
15 changes: 7 additions & 8 deletions html/extra-networks-card.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<div class="card" style="{style}" onclick="{card_clicked}" data-name="{name}" {sort_keys}>
{background_image}
<div class="button-row">{copy_path_button}{metadata_button}{edit_button}</div>
<div class="actions">
<div class="additional">{search_terms}</div>
<span class="name">{name}</span>
<span class="description">{description}</span>
</div>
<div class="card" style="{style}" {data_attributes}>
{background_image}
{button_row}
<div class="actions">
<span class="name">{name}</span>
<span class="description" {description_data_attributes}>{description}</span>
</div>
</div>
5 changes: 0 additions & 5 deletions html/extra-networks-copy-path-button.html

This file was deleted.

4 changes: 0 additions & 4 deletions html/extra-networks-edit-item-button.html

This file was deleted.

4 changes: 0 additions & 4 deletions html/extra-networks-metadata-button.html

This file was deleted.

17 changes: 17 additions & 0 deletions html/extra-networks-model-details.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="extra-network-content--dets-view-model-info">
<div class="model-info--header">
<h1 class="model-info--name">{name}</h1>
<button class="extra-network-control model-info--close" title="Close model details">
<svg class="extra-network-control--icon" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" fill="none"
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M 2 2 L 14 14 M 2 14 L 14 2" />
</svg>
</button>
</div>
<p class="model-info--description" {description_data_attributes}>{description}</p>
<h3>Model Metadata</h3>
<table class="model-info--metadata">
<tbody>{metadata_table}</tbody>
</table>
{model_specific}
</div>
8 changes: 0 additions & 8 deletions html/extra-networks-no-cards.html

This file was deleted.

8 changes: 0 additions & 8 deletions html/extra-networks-pane-dirs.html

This file was deleted.

8 changes: 0 additions & 8 deletions html/extra-networks-pane-tree.html

This file was deleted.

Loading