Merge branch 'main' into feature-sort-by-frequent-use

This commit is contained in:
DominikDoom
2024-01-26 16:21:15 +01:00
14 changed files with 184 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ var loras = [];
var lycos = [];
var modelKeywordDict = new Map();
var chants = [];
var styleNames = [];
// Selected model info for black/whitelisting
var currentModelHash = "";

View File

@@ -12,7 +12,8 @@ const ResultType = Object.freeze({
"hypernetwork": 8,
"lora": 9,
"lyco": 10,
"chant": 11
"chant": 11,
"styleName": 12
});
// Class to hold result data and annotations to make it clearer to use

View File

@@ -79,6 +79,13 @@ const thirdParty = {
"[id^=script_img2img_adetailer_ad_prompt] textarea",
"[id^=script_img2img_adetailer_ad_negative_prompt] textarea"
]
},
"deepdanbooru-object-recognition": {
"base": "#tab_deepdanboru_object_recg_tab",
"hasIds": false,
"selectors": [
"Found tags",
]
}
}
@@ -187,4 +194,4 @@ function getTextAreaIdentifier(textArea) {
break;
}
return modifier;
}
}

View File

@@ -123,6 +123,29 @@ async function getExtraNetworkPreviewURL(filename, type) {
}
}
lastStyleRefresh = 0;
// Refresh style file if needed
async function refreshStyleNamesIfChanged() {
// Only refresh once per second
currentTimestamp = new Date().getTime();
if (currentTimestamp - lastStyleRefresh < 1000) return;
lastStyleRefresh = currentTimestamp;
const response = await fetch(`tacapi/v1/refresh-styles-if-changed?${new Date().getTime()}`)
if (response.status === 304) {
// Not modified
} else if (response.status === 200) {
// Reload
QUEUE_FILE_LOAD.forEach(async fn => {
if (fn.toString().includes("styleNames"))
await fn.call(null, true);
})
} else {
// Error
console.error(`Error refreshing styles.txt: ` + response.status, response.statusText);
}
}
// Debounce function to prevent spamming the autocomplete function
var dbTimeOut;
const debounce = (func, wait = 300) => {

View File

@@ -41,7 +41,7 @@ async function load() {
function sanitize(tagType, text) {
if (tagType === ResultType.chant) {
return text.replace(/^.*?: /g, "");
return text;
}
return null;
}

View File

@@ -53,7 +53,7 @@ async function load() {
function sanitize(tagType, text) {
if (tagType === ResultType.embedding) {
return text.replace(/^.*?: /g, "");
return text;
}
return null;
}

View File

@@ -5,8 +5,8 @@ class LycoParser extends BaseTagParser {
parse() {
// Show lyco
let tempResults = [];
if (tagword !== "<" && tagword !== "<l:" && tagword !== "<lyco:") {
let searchTerm = tagword.replace("<lyco:", "").replace("<l:", "").replace("<", "");
if (tagword !== "<" && tagword !== "<l:" && tagword !== "<lyco:" && tagword !== "<lora:") {
let searchTerm = tagword.replace("<lyco:", "").replace("<lora:", "").replace("<l:", "").replace("<", "");
let filterCondition = x => x.toLowerCase().includes(searchTerm) || x.toLowerCase().replaceAll(" ", "_").includes(searchTerm);
tempResults = lycos.filter(x => filterCondition(x[0])); // Filter by tagword
} else {
@@ -52,7 +52,8 @@ async function sanitize(tagType, text) {
multiplier = info["preferred weight"];
}
return `<lyco:${text}:${multiplier}>`;
let prefix = TAC_CFG.useLoraPrefixForLycos ? "lora" : "lyco";
return `<${prefix}:${text}:${multiplier}>`;
}
return null;
}

67
javascript/ext_styles.js Normal file
View File

@@ -0,0 +1,67 @@
const STYLE_REGEX = /(\$(\d*)\(?)[^$|\]\s]*\)?/;
const STYLE_TRIGGER = () => TAC_CFG.useStyleVars && tagword.match(STYLE_REGEX);
var lastStyleVarIndex = "";
class StyleParser extends BaseTagParser {
async parse() {
// Refresh if needed
await refreshStyleNamesIfChanged();
// Show styles
let tempResults = [];
let matchGroups = tagword.match(STYLE_REGEX);
// Save index to insert again later or clear last one
lastStyleVarIndex = matchGroups[2] ? matchGroups[2] : "";
if (tagword !== matchGroups[1]) {
let searchTerm = tagword.replace(matchGroups[1], "");
let filterCondition = x => x[0].toLowerCase().includes(searchTerm) || x[0].toLowerCase().replaceAll(" ", "_").includes(searchTerm);
tempResults = styleNames.filter(x => filterCondition(x)); // Filter by tagword
} else {
tempResults = styleNames;
}
// Add final results
let finalResults = [];
tempResults.forEach(t => {
let result = new AutocompleteResult(t[0].trim(), ResultType.styleName)
result.meta = "Style";
finalResults.push(result);
});
return finalResults;
}
}
async function load(force = false) {
if (styleNames.length === 0 || force) {
try {
styleNames = (await loadCSV(`${tagBasePath}/temp/styles.txt`))
.filter(x => x[0]?.trim().length > 0) // Remove empty lines
.filter(x => x[0] !== "None") // Remove "None" style
.map(x => [x[0].trim()]); // Trim name
} catch (e) {
console.error("Error loading styles.txt: " + e);
}
}
}
function sanitize(tagType, text) {
if (tagType === ResultType.styleName) {
if (text.includes(" ")) {
return `$${lastStyleVarIndex}(${text})`;
} else {
return`$${lastStyleVarIndex}${text}`
}
}
return null;
}
PARSERS.push(new StyleParser(STYLE_TRIGGER));
// Add our utility functions to their respective queues
QUEUE_FILE_LOAD.push(load);
QUEUE_SANITIZE.push(sanitize);

View File

@@ -153,7 +153,7 @@ function sanitize(tagType, text) {
if (tagType === ResultType.wildcardFile || tagType === ResultType.yamlWildcard) {
return `__${text}__`;
} else if (tagType === ResultType.wildcardTag) {
return text.replace(/^.*?: /g, "");
return text;
}
return null;
}

View File

@@ -221,6 +221,7 @@ async function syncOptions() {
useHypernetworks: opts["tac_useHypernetworks"],
useLoras: opts["tac_useLoras"],
useLycos: opts["tac_useLycos"],
useLoraPrefixForLycos: opts["tac_useLoraPrefixForLycos"],
showWikiLinks: opts["tac_showWikiLinks"],
showExtraNetworkPreviews: opts["tac_showExtraNetworkPreviews"],
modelSortOrder: opts["tac_modelSortOrder"],
@@ -229,6 +230,7 @@ async function syncOptions() {
frequencyMinCount: opts["tac_frequencyMinCount"],
frequencyMaxAge: opts["tac_frequencyMaxAge"],
frequencyIncludeAlias: opts["tac_frequencyIncludeAlias"],
useStyleVars: opts["tac_useStyleVars"],
// Insertion related settings
replaceUnderscores: opts["tac_replaceUnderscores"],
escapeParentheses: opts["tac_escapeParentheses"],
@@ -407,9 +409,10 @@ function isEnabled() {
const WEIGHT_REGEX = /[([]([^()[\]:|]+)(?::(?:\d+(?:\.\d+)?|\.\d+))?[)\]]/g;
const POINTY_REGEX = /<[^\s,<](?:[^\t\n\r,<>]*>|[^\t\n\r,> ]*)/g;
const COMPLETED_WILDCARD_REGEX = /__[^\s,_][^\t\n\r,_]*[^\s,_]__[^\s,_]*/g;
const STYLE_VAR_REGEX = /\$\(?[^$|\]\s]*\)?/g;
const NORMAL_TAG_REGEX = /[^\s,|<>\]:]+_\([^\s,|<>\]:]*\)?|[^\s,|<>():\]]+|</g;
const RUBY_TAG_REGEX = /[\w\d<][\w\d' \-?!/$%]{2,}>?/g;
const TAG_REGEX = new RegExp(`${POINTY_REGEX.source}|${COMPLETED_WILDCARD_REGEX.source}|${NORMAL_TAG_REGEX.source}`, "g");
const TAG_REGEX = new RegExp(`${POINTY_REGEX.source}|${COMPLETED_WILDCARD_REGEX.source}|${STYLE_VAR_REGEX.source}|${NORMAL_TAG_REGEX.source}`, "g");
// On click, insert the tag into the prompt textbox with respect to the cursor position
async function insertTextAtCursor(textArea, result, tagword, tabCompletedWithoutChoice = false) {
@@ -606,6 +609,9 @@ async function insertTextAtCursor(textArea, result, tagword, tabCompletedWithout
textArea.selectionStart = afterInsertCursorPos + optionalSeparator.length + keywordsLength;
textArea.selectionEnd = textArea.selectionStart
// Set self trigger flag to show wildcard contents after the filename was inserted
if ([ResultType.wildcardFile, ResultType.yamlWildcard, ResultType.umiWildcard].includes(result.type))
tacSelfTrigger = true;
// Since we've modified a Gradio Textbox component manually, we need to simulate an `input` DOM event to ensure it's propagated back to python.
// Uses a built-in method from the webui's ui.js which also already accounts for event target
if (tagType === ResultType.wildcardTag || tagType === ResultType.wildcardFile || tagType === ResultType.yamlWildcard)
@@ -1050,7 +1056,7 @@ async function autocomplete(textArea, prompt, fixedTag = null) {
.map(match => match[1]);
let tags = prompt.match(TAG_REGEX)
if (weightedTags !== null && tags !== null) {
tags = tags.filter(tag => !weightedTags.some(weighted => tag.includes(weighted) && !tag.startsWith("<[")))
tags = tags.filter(tag => !weightedTags.some(weighted => tag.includes(weighted) && !tag.startsWith("<[") && !tag.startsWith("$(")))
.concat(weightedTags);
}