Skip to content

Commit

Permalink
Add description support for extra networks
Browse files Browse the repository at this point in the history
  • Loading branch information
heyhippari committed Feb 20, 2023
1 parent 0cc0ee1 commit 540da42
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
10 changes: 10 additions & 0 deletions extensions-builtin/Lora/ui_extra_networks_lora.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,27 @@ def list_items(self):
for name, lora_on_disk in lora.available_loras.items():
path, ext = os.path.splitext(lora_on_disk.filename)
previews = [path + ".png", path + ".preview.png"]
descriptions = [path + ".txt", path + ".description.txt"]

preview = None
for file in previews:
if os.path.isfile(file):
preview = self.link_preview(file)
break

description = None
for file in descriptions:
if os.path.isfile(file):
with open(file, "r") as description_file:
description = description_file.read()

break

yield {
"name": name,
"filename": path,
"preview": preview,
"description": description,
"search_term": self.search_terms_from_path(lora_on_disk.filename),
"prompt": json.dumps(f"<lora:{name}:") + " + opts.extra_networks_default_multiplier + " + json.dumps(">"),
"local_preview": path + ".png",
Expand Down
2 changes: 1 addition & 1 deletion html/extra-networks-card.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class='card' {preview_html} onclick={card_clicked}>
<div class='card' {preview_html} onclick={card_clicked} title={description}>
<div class='actions'>
<div class='additional'>
<ul>
Expand Down
2 changes: 2 additions & 0 deletions modules/ui_extra_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ def allowed_directories_for_previews(self):

def create_html_for_item(self, item, tabname):
preview = item.get("preview", None)
description = item.get("description", None)

onclick = item.get("onclick", None)
if onclick is None:
onclick = '"' + html.escape(f"""return cardClicked({json.dumps(tabname)}, {item["prompt"]}, {"true" if self.allow_negative_prompt else "false"})""") + '"'

args = {
"preview_html": "style='background-image: url(\"" + html.escape(preview) + "\")'" if preview else '',
"description": '"' + html.escape(description) + '"' if description else '',
"prompt": item.get("prompt", None),
"tabname": json.dumps(tabname),
"local_preview": json.dumps(item["local_preview"]),
Expand Down
10 changes: 10 additions & 0 deletions modules/ui_extra_networks_checkpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,27 @@ def list_items(self):
for name, checkpoint in sd_models.checkpoints_list.items():
path, ext = os.path.splitext(checkpoint.filename)
previews = [path + ".png", path + ".preview.png"]
descriptions = [path + ".txt", path + ".description.txt"]

preview = None
for file in previews:
if os.path.isfile(file):
preview = self.link_preview(file)
break

description = None
for file in descriptions:
if os.path.isfile(file):
with open(file, "r") as description_file:
description = description_file.read()

break

yield {
"name": checkpoint.name_for_extra,
"filename": path,
"preview": preview,
"description": description,
"search_term": self.search_terms_from_path(checkpoint.filename) + " " + (checkpoint.sha256 or ""),
"onclick": '"' + html.escape(f"""return selectCheckpoint({json.dumps(name)})""") + '"',
"local_preview": path + ".png",
Expand Down
10 changes: 10 additions & 0 deletions modules/ui_extra_networks_hypernets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ def list_items(self):
for name, path in shared.hypernetworks.items():
path, ext = os.path.splitext(path)
previews = [path + ".png", path + ".preview.png"]
descriptions = [path + ".txt", path + ".description.txt"]

preview = None
for file in previews:
if os.path.isfile(file):
preview = self.link_preview(file)
break

description = None
for file in descriptions:
if os.path.isfile(file):
with open(file, "r") as description_file:
description = description_file.read()

break

yield {
"name": name,
"filename": path,
"preview": preview,
"description": description,
"search_term": self.search_terms_from_path(path),
"prompt": json.dumps(f"<hypernet:{name}:") + " + opts.extra_networks_default_multiplier + " + json.dumps(">"),
"local_preview": path + ".png",
Expand Down
18 changes: 15 additions & 3 deletions modules/ui_extra_networks_textual_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,28 @@ def refresh(self):
def list_items(self):
for embedding in sd_hijack.model_hijack.embedding_db.word_embeddings.values():
path, ext = os.path.splitext(embedding.filename)
preview_file = path + ".preview.png"
previews = [path + ".png", path + ".preview.png"]
descriptions = [path + ".txt", path + ".description.txt"]

preview = None
if os.path.isfile(preview_file):
preview = self.link_preview(preview_file)
for file in previews:
if os.path.isfile(file):
preview = self.link_preview(file)
break

description = None
for file in descriptions:
if os.path.isfile(file):
with open(file, "r") as description_file:
description = description_file.read()

break

yield {
"name": embedding.name,
"filename": embedding.filename,
"preview": preview,
"description": description,
"search_term": self.search_terms_from_path(embedding.filename),
"prompt": json.dumps(embedding.name),
"local_preview": path + ".preview.png",
Expand Down

0 comments on commit 540da42

Please # to comment.