From b89d1525408e53ad159d077ea802126cc5be432e Mon Sep 17 00:00:00 2001 From: JeLuF Date: Tue, 29 Aug 2023 07:18:57 +0200 Subject: [PATCH] Support lora models in subfolders when scanning the tag (#1521) * Recursive lora search * Support lora models in subfolders when scanning the tag --- ui/media/js/main.js | 23 ++++++++++++++++++++++ ui/plugins/ui/lora-prompt-parser.plugin.js | 19 ++++++------------ 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ui/media/js/main.js b/ui/media/js/main.js index 909ee668..12ad5a0c 100644 --- a/ui/media/js/main.js +++ b/ui/media/js/main.js @@ -678,6 +678,29 @@ function getAllModelNames(type) { return f(modelsOptions[type]) } +// gets a flattened list of all models of a certain type. e.g. "path/subpath/modelname" +// use the filter to search for all models having a certain name. +function getAllModelPathes(type,filter="") { + function f(tree, prefix) { + if (tree == undefined) { + return [] + } + let result = [] + tree.forEach((e) => { + if (typeof e == "object") { + result = result.concat(f(e[1], prefix + e[0] + "/")) + } else { + if (filter=="" || e==filter) { + result.push(prefix + e) + } + } + }) + return result + } + return f(modelsOptions[type], "") +} + + function onUseAsThumbnailClick(req, img) { let scale = 1 let targetWidth = img.naturalWidth diff --git a/ui/plugins/ui/lora-prompt-parser.plugin.js b/ui/plugins/ui/lora-prompt-parser.plugin.js index c1cbcac7..0510b47f 100644 --- a/ui/plugins/ui/lora-prompt-parser.plugin.js +++ b/ui/plugins/ui/lora-prompt-parser.plugin.js @@ -29,22 +29,13 @@ let modelWeights = LoRA.map(e => e.lora_alpha_0) loraModelField.value = {modelNames: modelNames, modelWeights: modelWeights} - showToast("Prompt successfully processed", LoRA[0].lora_model_0); + showToast("Prompt successfully processed") } //promptField.dispatchEvent(new Event('change')); }); - function isModelAvailable(array, searchString) { - const foundItem = array.find(function(item) { - item = item.toString().toLowerCase(); - return item === searchString.toLowerCase() - }); - - return foundItem || ""; - } - // extract LoRA tags from strings function extractLoraTags(prompt) { // Define the regular expression for the tags @@ -55,11 +46,13 @@ // Iterate over the string, finding matches for (const match of prompt.matchAll(regex)) { - const modelFileName = isModelAvailable(modelsCache.options.lora, match[1].trim()) - if (modelFileName !== "") { + const modelFileName = match[1].trim() + const loraPathes = getAllModelPathes("lora", modelFileName) + if (loraPathes.length > 0) { + const loraPath = loraPathes[0] // Initialize an object to hold a match let loraTag = { - lora_model_0: modelFileName, + lora_model_0: loraPath, } //console.log("Model:" + modelFileName);