mirror of
https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git
synced 2026-01-27 03:29:55 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
106fa13f65 | ||
|
|
a038664616 | ||
|
|
789f44d52a | ||
|
|
59ec54b171 | ||
|
|
983da36329 | ||
|
|
48bd3d7b51 | ||
|
|
c6c9e01410 | ||
|
|
bf5bb34605 | ||
|
|
860fd34fb4 | ||
|
|
886de4df29 | ||
|
|
3e71890489 | ||
|
|
dc77b3f17f |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
tags/temp/
|
||||
25
README.md
25
README.md
@@ -19,12 +19,10 @@ You can either clone / download the files manually as described [below](#install
|
||||
For example, editing `atago (azur lane)`, it would be replaced with e.g. `taihou (azur lane), lane)`, since the script currently doesn't see the second part of the bracket as the same tag. So in those cases you should delete the old tag beforehand.
|
||||
|
||||
### Wildcard & Embedding support
|
||||
Autocompletion also works with wildcard files used by [this script](https://github.com/jtkelm2/stable-diffusion-webui-1/blob/master/scripts/wildcards.py) of the same name (demo video further down). This enables you to either insert categories to be replaced by the script, or even replace them with the actual wildcard file content in the same step.
|
||||
Autocompletion also works with wildcard files used by [this script](https://github.com/jtkelm2/stable-diffusion-webui-1/blob/master/scripts/wildcards.py) of the same name (demo video further down). This enables you to either insert categories to be replaced by the script, or even replace them with the actual wildcard file content in the same step. Wildcards are searched for in every extension folder as well as the `scripts/wildcards` folder to support legacy versions. This means that you can combine wildcards from multiple extensions. Nested folders are also supported if you have grouped your wildcards in that way.
|
||||
|
||||
It also scans the embeddings folder and displays completion hints for the names of all .pt and .bin files inside if you start typing `<`. Note that some normal tags also use < in Kaomoji (like ">_<" for example), so the results will contain both.
|
||||
|
||||
Both are now enabled by default and scan the `/embeddings` and `/scripts/wildcards` folders automatically.
|
||||
|
||||
## Screenshots
|
||||
Demo video (with keyboard navigation):
|
||||
|
||||
@@ -40,14 +38,27 @@ Dark and Light mode supported, including tag colors:
|
||||

|
||||
|
||||
## Installation
|
||||
Simply copy the `javascript`, `scripts` and `tags` folder into your web UI installation root. It will run automatically the next time the web UI is started.
|
||||
### As an extension (recommended)
|
||||
Either clone the repo into your extensions folder:
|
||||
```bash
|
||||
git clone "https://github.com/DominikDoom/a1111-sd-webui-tagcomplete.git" extensions/tag-autocomplete
|
||||
```
|
||||
(The second argument specifies the name of the folder, you can choose whatever you like).
|
||||
|
||||
The tags folder contains `config.json` and the tag data the script uses for autocompletion. By default, Danbooru and e621 tags are included.
|
||||
Or create a folder there manually and place the `javascript`, `scripts` and `tags` folders in it.
|
||||
|
||||
### In the root folder (old)
|
||||
Copy the `javascript`, `scripts` and `tags` folder into your web UI installation root. It will run automatically the next time the web UI is started.
|
||||
|
||||
---
|
||||
|
||||
In both configurations, the tags folder contains `config.json` and the tag data the script uses for autocompletion. By default, Danbooru and e621 tags are included.
|
||||
After scanning for embeddings and wildcards, the script will also create a `temp` directory here which lists the found files so they can be accessed in the browser side of the script. You can delete the temp folder without consequences as it will be recreated on the next startup.
|
||||
|
||||
### Important:
|
||||
The script needs **all three folders** to work properly.
|
||||
|
||||
### Config
|
||||
## Config
|
||||
The config contains the following settings and defaults:
|
||||
```json
|
||||
{
|
||||
@@ -137,7 +148,7 @@ Methods 1 & 2 can also be mixed, in which case translations in the extra file wi
|
||||
The extra files can also be used to just add new / custom tags not included in the main set, provided `onlyTranslationExtraFile` is false.
|
||||
If an extra tag doesn't match any existing tag, it will be added to the list as a new tag instead.
|
||||
|
||||
### CSV tag data
|
||||
## CSV tag data
|
||||
The script expects a CSV file with tags saved in the following way:
|
||||
```csv
|
||||
1girl,0
|
||||
|
||||
@@ -92,15 +92,24 @@ function parseCSV(str) {
|
||||
|
||||
// Load file
|
||||
function readFile(filePath) {
|
||||
let request = new XMLHttpRequest();
|
||||
request.open("GET", filePath, false);
|
||||
request.send(null);
|
||||
return request.responseText;
|
||||
return new Promise(function (resolve, reject) {
|
||||
let request = new XMLHttpRequest();
|
||||
request.open("GET", filePath, true);
|
||||
request.onload = function () {
|
||||
var status = request.status;
|
||||
if (status == 200) {
|
||||
resolve(request.responseText);
|
||||
} else {
|
||||
reject(status);
|
||||
}
|
||||
};
|
||||
request.send(null);
|
||||
});
|
||||
}
|
||||
|
||||
// Load CSV
|
||||
function loadCSV(path) {
|
||||
let text = readFile(path);
|
||||
async function loadCSV(path) {
|
||||
let text = await readFile(path);
|
||||
return parseCSV(text);
|
||||
}
|
||||
|
||||
@@ -220,6 +229,7 @@ function escapeRegExp(string) {
|
||||
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
|
||||
}
|
||||
|
||||
const TAG_REGEX = /[([]([^,()[\]:| ]+)(?::(?:\d+(?:\.\d+)?|\.\d+))?[)\]]|((?:\b|<)[^,|\n\r ]*(?:>|\b)?)/g
|
||||
let hideBlocked = false;
|
||||
|
||||
// On click, insert the tag into the prompt textbox with respect to the cursor position
|
||||
@@ -255,16 +265,16 @@ function insertTextAtCursor(textArea, result, tagword) {
|
||||
let editStart = Math.max(cursorPos - tagword.length, 0);
|
||||
let editEnd = Math.min(cursorPos + tagword.length, prompt.length);
|
||||
let surrounding = prompt.substring(editStart, editEnd);
|
||||
let match = surrounding.match(new RegExp(escapeRegExp(`${tagword}`)));
|
||||
let match = surrounding.match(new RegExp(escapeRegExp(`${tagword}`), "i"));
|
||||
let afterInsertCursorPos = editStart + match.index + sanitizedText.length;
|
||||
|
||||
var optionalComma = "";
|
||||
if (tagType !== "wildcardFile") {
|
||||
optionalComma = surrounding.match(new RegExp(escapeRegExp(`${tagword},`))) !== null ? "" : ", ";
|
||||
optionalComma = surrounding.match(new RegExp(`${escapeRegExp(tagword)}[,:]`, "i")) !== null ? "" : ", ";
|
||||
}
|
||||
|
||||
// Replace partial tag word with new text, add comma if needed
|
||||
let insert = surrounding.replace(tagword, sanitizedText + optionalComma);
|
||||
let insert = surrounding.replace(match, sanitizedText + optionalComma);
|
||||
|
||||
// Add back start
|
||||
var newPrompt = prompt.substring(0, editStart) + insert + prompt.substring(editEnd);
|
||||
@@ -277,7 +287,9 @@ function insertTextAtCursor(textArea, result, tagword) {
|
||||
textArea.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
|
||||
// Update previous tags with the edited prompt to prevent re-searching the same term
|
||||
let tags = newPrompt.match(/[^, ]+/g);
|
||||
let tags = [...newPrompt.matchAll(TAG_REGEX)]
|
||||
.filter(x => x[2] ? x[2].length > 0 : x)
|
||||
.flatMap(x => x[1] || x[2])
|
||||
previousTags = tags;
|
||||
|
||||
// Hide results after inserting
|
||||
@@ -372,7 +384,7 @@ var allTags = [];
|
||||
var results = [];
|
||||
var tagword = "";
|
||||
var resultCount = 0;
|
||||
function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
async function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
// Return if the function is deactivated in the UI
|
||||
if (!acActive) return;
|
||||
|
||||
@@ -384,7 +396,10 @@ function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
|
||||
if (fixedTag === null) {
|
||||
// Match tags with RegEx to get the last edited one
|
||||
let tags = prompt.match(/[^, ]+/g);
|
||||
// We also match for the weighting format (e.g. "tag:1.0") here, and flatMap it to just the tag part if it exists
|
||||
let tags = [...prompt.matchAll(TAG_REGEX)]
|
||||
.filter(x => x[2] ? x[2].length > 0 : x)
|
||||
.flatMap(x => x[1] || x[2])
|
||||
let diff = difference(tags, previousTags)
|
||||
previousTags = tags;
|
||||
|
||||
@@ -405,7 +420,7 @@ function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
tagword = fixedTag;
|
||||
}
|
||||
|
||||
tagword = tagword.toLowerCase();
|
||||
tagword = tagword.toLowerCase().replace(/[\n\r]/g, "");
|
||||
|
||||
if (acConfig.useWildcards && [...tagword.matchAll(/\b__([^, ]+)__([^, ]*)\b/g)].length > 0) {
|
||||
// Show wildcards from a file with that name
|
||||
@@ -413,16 +428,16 @@ function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
let wcFile = wcMatch[0][1];
|
||||
let wcWord = wcMatch[0][2];
|
||||
|
||||
let wcBasePath = "";
|
||||
if (wildcardExtFiles.includes(wcFile))
|
||||
wcBasePath = "extensions/wildcards/wildcards";
|
||||
else if (wildcardFiles.includes(wcFile))
|
||||
wcBasePath = "scripts/wildcards";
|
||||
else
|
||||
throw "No valid wildcard file found";
|
||||
var wcPair;
|
||||
|
||||
let wildcards = readFile(`file/${wcBasePath}/${wcFile}.txt`).split("\n")
|
||||
.filter(x => x.trim().length > 0); // Remove empty lines
|
||||
// Look in normal wildcard files
|
||||
if (wcFound = wildcardFiles.find(x => x[1].toLowerCase() === wcFile))
|
||||
wcPair = wcFound;
|
||||
else // Look in extensions wildcard files
|
||||
wcPair = wildcardExtFiles.find(x => x[1].toLowerCase() === wcFile);
|
||||
|
||||
let wildcards = (await readFile(`file/${wcPair[0]}/${wcPair[1]}.txt`)).split("\n")
|
||||
.filter(x => x.trim().length > 0); // Remove empty lines
|
||||
|
||||
results = wildcards.filter(x => (wcWord !== null && wcWord.length > 0) ? x.toLowerCase().includes(wcWord) : x) // Filter by tagword
|
||||
.map(x => [wcFile + ": " + x.trim(), "wildcardTag"]); // Mark as wildcard
|
||||
@@ -430,13 +445,12 @@ function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
// Show available wildcard files
|
||||
let tempResults = [];
|
||||
if (tagword !== "__") {
|
||||
let lmb = (x) => x.toLowerCase().includes(tagword.replace("__", ""))
|
||||
let lmb = (x) => x[1].toLowerCase().includes(tagword.replace("__", ""))
|
||||
tempResults = wildcardFiles.filter(lmb).concat(wildcardExtFiles.filter(lmb)) // Filter by tagword
|
||||
|
||||
} else {
|
||||
tempResults = wildcardFiles.concat(wildcardExtFiles);
|
||||
}
|
||||
results = tempResults.map(x => ["Wildcards: " + x.trim(), "wildcardFile"]); // Mark as wildcard
|
||||
results = tempResults.map(x => ["Wildcards: " + x[1].trim(), "wildcardFile"]); // Mark as wildcard
|
||||
} else if (acConfig.useEmbeddings && tagword.match(/<[^,> ]*>?/g)) {
|
||||
// Show embeddings
|
||||
let tempResults = [];
|
||||
@@ -475,6 +489,7 @@ function autocomplete(textArea, prompt, fixedTag = null) {
|
||||
addResultsToList(textArea, results, tagword, true);
|
||||
}
|
||||
|
||||
var oldSelectedTag = null;
|
||||
function navigateInList(textArea, event) {
|
||||
// Return if the function is deactivated in the UI
|
||||
if (!acActive) return;
|
||||
@@ -560,11 +575,14 @@ function navigateInList(textArea, event) {
|
||||
}
|
||||
|
||||
var styleAdded = false;
|
||||
onUiUpdate(function () {
|
||||
onUiUpdate(async function () {
|
||||
// Get our tag base path from the temp file
|
||||
let tagBasePath = await readFile("file/tmp/tagAutocompletePath.txt");
|
||||
|
||||
// Load config
|
||||
if (acConfig === null) {
|
||||
try {
|
||||
acConfig = JSON.parse(readFile("file/tags/config.json"));
|
||||
acConfig = JSON.parse(await readFile(`file/${tagBasePath}/config.json`));
|
||||
if (acConfig.translation.onlyShowTranslation) {
|
||||
acConfig.translation.searchByTranslation = true; // if only show translation, enable search by translation is necessary
|
||||
}
|
||||
@@ -576,14 +594,14 @@ onUiUpdate(function () {
|
||||
// Load main tags and translations
|
||||
if (allTags.length === 0) {
|
||||
try {
|
||||
allTags = loadCSV(`file/tags/${acConfig.tagFile}`);
|
||||
allTags = await loadCSV(`file/${tagBasePath}/${acConfig.tagFile}`);
|
||||
} catch (e) {
|
||||
console.error("Error loading tags file: " + e);
|
||||
return;
|
||||
}
|
||||
if (acConfig.extra.extraFile) {
|
||||
try {
|
||||
extras = loadCSV(`file/tags/${acConfig.extra.extraFile}`);
|
||||
extras = await loadCSV(`file/${tagBasePath}/${acConfig.extra.extraFile}`);
|
||||
if (acConfig.extra.onlyTranslationExtraFile) {
|
||||
// This works purely on index, so it's not very robust. But a lot faster.
|
||||
for (let i = 0, n = extras.length; i < n; i++) {
|
||||
@@ -612,12 +630,35 @@ onUiUpdate(function () {
|
||||
// Load wildcards
|
||||
if (wildcardFiles.length === 0 && acConfig.useWildcards) {
|
||||
try {
|
||||
wildcardFiles = readFile("file/tags/temp/wc.txt").split("\n")
|
||||
let wcFileArr = (await readFile(`file/${tagBasePath}/temp/wc.txt`)).split("\n");
|
||||
let wcBasePath = wcFileArr[0].trim(); // First line should be the base path
|
||||
wildcardFiles = wcFileArr.slice(1)
|
||||
.filter(x => x.trim().length > 0) // Remove empty lines
|
||||
.map(x => x.trim().replace(".txt", "")); // Remove file extension & newlines
|
||||
wildcardExtFiles = readFile("file/tags/temp/wce.txt").split("\n")
|
||||
.filter(x => x.trim().length > 0) // Remove empty lines
|
||||
.map(x => x.trim().replace(".txt", "")); // Remove file extension & newlines
|
||||
.map(x => [wcBasePath, x.trim().replace(".txt", "")]); // Remove file extension & newlines
|
||||
|
||||
// To support multiple sources, we need to separate them using the provided "-----" strings
|
||||
let wcExtFileArr = (await readFile(`file/${tagBasePath}/temp/wce.txt`)).split("\n");
|
||||
let splitIndices = [];
|
||||
for (let index = 0; index < wcExtFileArr.length; index++) {
|
||||
if (wcExtFileArr[index].trim() === "-----") {
|
||||
splitIndices.push(index);
|
||||
}
|
||||
}
|
||||
// For each group, add them to the wildcardFiles array with the base path as the first element
|
||||
for (let i = 0; i < splitIndices.length; i++) {
|
||||
let start = splitIndices[i - 1] || 0;
|
||||
if (i > 0) start++; // Skip the "-----" line
|
||||
let end = splitIndices[i];
|
||||
|
||||
let wcExtFile = wcExtFileArr.slice(start, end);
|
||||
let base = wcExtFile[0].trim() + "/";
|
||||
wcExtFile = wcExtFile.slice(1)
|
||||
.filter(x => x.trim().length > 0) // Remove empty lines
|
||||
.map(x => x.trim().replace(base, "").replace(".txt", "")); // Remove file extension & newlines;
|
||||
|
||||
wcExtFile = wcExtFile.map(x => [base, x]);
|
||||
wildcardExtFiles.push(...wcExtFile);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error loading wildcards: " + e);
|
||||
}
|
||||
@@ -625,7 +666,7 @@ onUiUpdate(function () {
|
||||
// Load embeddings
|
||||
if (embeddings.length === 0 && acConfig.useEmbeddings) {
|
||||
try {
|
||||
embeddings = readFile("file/tags/temp/emb.txt").split("\n")
|
||||
embeddings = (await readFile(`file/${tagBasePath}/temp/emb.txt`)).split("\n")
|
||||
.filter(x => x.trim().length > 0) // Remove empty lines
|
||||
.map(x => x.replace(".bin", "").replace(".pt", "").replace(".png", "")); // Remove file extensions
|
||||
} catch (e) {
|
||||
@@ -672,7 +713,7 @@ onUiUpdate(function () {
|
||||
hideResults(area);
|
||||
|
||||
// Add autocomplete event listener
|
||||
area.addEventListener('input', debounce(() => autocomplete(area, area.value), 100));
|
||||
area.addEventListener('input', debounce(() => autocomplete(area, area.value), acConfig.delayTime));
|
||||
// Add focusout event listener
|
||||
area.addEventListener('focusout', debounce(() => hideResults(area), 400));
|
||||
// Add up and down arrow event listener
|
||||
|
||||
@@ -2,27 +2,64 @@
|
||||
# to a temporary file to expose it to the javascript side
|
||||
|
||||
from pathlib import Path
|
||||
from modules import scripts
|
||||
|
||||
# Webui root path
|
||||
FILE_DIR = Path().absolute()
|
||||
|
||||
# The extension base path
|
||||
EXT_PATH = FILE_DIR.joinpath('extensions')
|
||||
|
||||
# Tags base path
|
||||
|
||||
|
||||
def get_tags_base_path():
|
||||
script_path = Path(scripts.basedir())
|
||||
if (script_path.is_relative_to(EXT_PATH)):
|
||||
return script_path.joinpath('tags')
|
||||
else:
|
||||
return FILE_DIR.joinpath('tags')
|
||||
|
||||
|
||||
TAGS_PATH = get_tags_base_path()
|
||||
|
||||
# The path to the folder containing the wildcards and embeddings
|
||||
FILE_DIR = Path().absolute()
|
||||
WILDCARD_PATH = FILE_DIR.joinpath('scripts/wildcards')
|
||||
WILDCARD_EXT_PATH = FILE_DIR.joinpath('extensions/wildcards/wildcards')
|
||||
EMB_PATH = FILE_DIR.joinpath('embeddings')
|
||||
# The path to the temporary file
|
||||
TEMP_PATH = FILE_DIR.joinpath('tags/temp')
|
||||
|
||||
|
||||
def find_ext_wildcard_paths():
|
||||
"""Returns the path to the extension wildcards folder"""
|
||||
found = list(EXT_PATH.glob('*/wildcards/'))
|
||||
return found
|
||||
|
||||
|
||||
# The path to the extension wildcards folder
|
||||
WILDCARD_EXT_PATHS = find_ext_wildcard_paths()
|
||||
|
||||
# The path to the temporary files
|
||||
STATIC_TEMP_PATH = FILE_DIR.joinpath('tmp') # In the webui root, on windows it exists by default, on linux it doesn't
|
||||
TEMP_PATH = TAGS_PATH.joinpath('temp') # Extension specific temp files
|
||||
|
||||
|
||||
def get_wildcards():
|
||||
"""Returns a list of all wildcards. Works on nested folders."""
|
||||
wildcard_files = list(WILDCARD_PATH.rglob("*.txt"))
|
||||
resolved = [w.relative_to(WILDCARD_PATH).as_posix() for w in wildcard_files if w.name != "put wildcards here.txt"]
|
||||
resolved = [w.relative_to(WILDCARD_PATH).as_posix(
|
||||
) for w in wildcard_files if w.name != "put wildcards here.txt"]
|
||||
return resolved
|
||||
|
||||
|
||||
def get_ext_wildcards():
|
||||
"""Returns a list of all extension wildcards. Works on nested folders."""
|
||||
wildcard_files = list(WILDCARD_EXT_PATH.rglob("*.txt"))
|
||||
resolved = [w.relative_to(WILDCARD_EXT_PATH).as_posix() for w in wildcard_files if w.name != "put wildcards here.txt"]
|
||||
return resolved
|
||||
wildcard_files = []
|
||||
|
||||
for path in WILDCARD_EXT_PATHS:
|
||||
wildcard_files.append(path.relative_to(FILE_DIR).as_posix())
|
||||
wildcard_files.extend(p.relative_to(path).as_posix() for p in path.rglob("*.txt") if p.name != "put wildcards here.txt")
|
||||
wildcard_files.append("-----")
|
||||
|
||||
return wildcard_files
|
||||
|
||||
|
||||
def get_embeddings():
|
||||
@@ -30,12 +67,25 @@ def get_embeddings():
|
||||
return [str(e.relative_to(EMB_PATH)) for e in EMB_PATH.glob("**/*") if e.suffix in {".bin", ".pt", ".png"}]
|
||||
|
||||
|
||||
def write_tag_base_path():
|
||||
"""Writes the tag base path to a fixed location temporary file"""
|
||||
with open(STATIC_TEMP_PATH.joinpath('tagAutocompletePath.txt'), 'w', encoding="utf-8") as f:
|
||||
f.write(TAGS_PATH.relative_to(FILE_DIR).as_posix())
|
||||
|
||||
|
||||
def write_to_temp_file(name, data):
|
||||
"""Writes the given data to a temporary file"""
|
||||
with open(TEMP_PATH.joinpath(name), 'w', encoding="utf-8") as f:
|
||||
f.write(('\n'.join(data)))
|
||||
|
||||
|
||||
# Write the tag base path to a fixed location temporary file
|
||||
# to enable the javascript side to find our files regardless of extension folder name
|
||||
if not STATIC_TEMP_PATH.exists():
|
||||
STATIC_TEMP_PATH.mkdir(exist_ok=True)
|
||||
|
||||
write_tag_base_path()
|
||||
|
||||
# Check if the temp path exists and create it if not
|
||||
if not TEMP_PATH.exists():
|
||||
TEMP_PATH.mkdir(parents=True, exist_ok=True)
|
||||
@@ -48,12 +98,12 @@ write_to_temp_file('emb.txt', [])
|
||||
|
||||
# Write wildcards to wc.txt if found
|
||||
if WILDCARD_PATH.exists():
|
||||
wildcards = get_wildcards()
|
||||
wildcards = [WILDCARD_PATH.relative_to(FILE_DIR).as_posix()] + get_wildcards()
|
||||
if wildcards:
|
||||
write_to_temp_file('wc.txt', wildcards)
|
||||
|
||||
# Write extension wildcards to wce.txt if found
|
||||
if WILDCARD_EXT_PATH.exists():
|
||||
if WILDCARD_EXT_PATHS is not None:
|
||||
wildcards_ext = get_ext_wildcards()
|
||||
if wildcards_ext:
|
||||
write_to_temp_file('wce.txt', wildcards_ext)
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
},
|
||||
"maxResults": 5,
|
||||
"resultStepLength": 500,
|
||||
"delayTime": 100,
|
||||
"showAllResults": false,
|
||||
"useLeftRightArrowKeys": false,
|
||||
"replaceUnderscores": true,
|
||||
|
||||
Reference in New Issue
Block a user