Extract file load to queue

This enables other parsers to keep their load function in the same file
This commit is contained in:
Dominik Reh
2023-02-10 11:55:56 +01:00
parent 890f1a48c2
commit cbeced9121
8 changed files with 119 additions and 94 deletions

View File

@@ -42,6 +42,7 @@ var umiPreviousTags = [];
// Queues
const QUEUE_AFTER_INSERT = [];
const QUEUE_AFTER_SETUP = [];
const QUEUE_FILE_LOAD = [];
const QUEUE_AFTER_CONFIG_CHANGE = [];
// List of parsers to try

View File

@@ -96,9 +96,9 @@ function escapeHTML(unsafeText) {
}
// Queue calling function to process global queues
function processQueue(queue, context, ...args) {
async function processQueue(queue, context, ...args) {
for (let i = 0; i < queue.length; i++) {
queue[i].call(context, ...args);
await queue[i].call(context, ...args);
}
}
// The same but with return values

View File

@@ -32,4 +32,19 @@ class EmbeddingParser extends BaseTagParser {
}
}
PARSERS.push(new EmbeddingParser(EMB_TRIGGER));
async function load() {
if (embeddings.length === 0) {
try {
embeddings = (await readFile(`${tagBasePath}/temp/emb.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim().split(",")); // Split into name, version type pairs
} catch (e) {
console.error("Error loading embeddings.txt: " + e);
}
}
}
PARSERS.push(new EmbeddingParser(EMB_TRIGGER));
// Add load function to the queue
QUEUE_FILE_LOAD.push(load);

View File

@@ -24,4 +24,19 @@ class HypernetParser extends BaseTagParser {
}
}
PARSERS.push(new HypernetParser(HYP_TRIGGER));
async function load() {
if (hypernetworks.length === 0) {
try {
hypernetworks = (await readFile(`${tagBasePath}/temp/hyp.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) //Remove empty lines
.map(x => x.trim()); // Remove carriage returns and padding if it exists
} catch (e) {
console.error("Error loading hypernetworks.txt: " + e);
}
}
}
PARSERS.push(new HypernetParser(HYP_TRIGGER));
// Add load function to the queue
QUEUE_FILE_LOAD.push(load);

View File

@@ -24,4 +24,19 @@ class LoraParser extends BaseTagParser {
}
}
PARSERS.push(new LoraParser(LORA_TRIGGER));
async function load() {
if (loras.length === 0) {
try {
loras = (await readFile(`${tagBasePath}/temp/lora.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim()); // Remove carriage returns and padding if it exists
} catch (e) {
console.error("Error loading lora.txt: " + e);
}
}
}
PARSERS.push(new LoraParser(LORA_TRIGGER));
// Add load function to the queue
QUEUE_FILE_LOAD.push(load);

View File

@@ -202,7 +202,29 @@ function updateUmiTags( tagType, sanitizedText, newPrompt, textArea) {
return false;
}
async function load() {
if (yamlWildcards.length === 0) {
try {
let yamlTags = (await readFile(`${tagBasePath}/temp/wcet.txt?${new Date().getTime()}`)).split("\n");
// Split into tag, count pairs
yamlWildcards = yamlTags.map(x => x
.trim()
.split(","))
.map(([i, ...rest]) => [
i,
rest.reduce((a, b) => {
a[b.toLowerCase()] = true;
return a;
}, {}),
]);
} catch (e) {
console.error("Error loading yaml wildcards: " + e);
}
}
}
// Add UMI parser
PARSERS.push(new UmiParser(UMI_TRIGGER));
// Add tag update after insert
QUEUE_AFTER_INSERT.push(updateUmiTags);
QUEUE_AFTER_INSERT.push(updateUmiTags);
QUEUE_FILE_LOAD.push(load);

View File

@@ -55,6 +55,44 @@ class WildcardFileParser extends BaseTagParser {
}
}
async function load() {
if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) {
try {
let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt?${new Date().getTime()}`)).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 => [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(`${tagBasePath}/temp/wce.txt?${new Date().getTime()}`)).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);
}
}
}
function keepOpenIfWildcard(tagType, sanitizedText, newPrompt, textArea) {
// If it's a wildcard, we want to keep the results open so the user can select another wildcard
if (tagType === ResultType.wildcardFile) {
@@ -71,4 +109,6 @@ PARSERS.push(new WildcardParser(WC_TRIGGER));
PARSERS.push(new WildcardFileParser(WC_FILE_TRIGGER));
// Add the keep open function to the queue
QUEUE_AFTER_INSERT.push(keepOpenIfWildcard);
QUEUE_AFTER_INSERT.push(keepOpenIfWildcard);
// Add the load function to the queue
QUEUE_FILE_LOAD.push(load);

View File

@@ -226,7 +226,7 @@ async function syncOptions() {
CFG = newCFG;
// Callback
processQueue(QUEUE_AFTER_CONFIG_CHANGE, null);
await processQueue(QUEUE_AFTER_CONFIG_CHANGE, null);
}
// Create the result list div and necessary styling
@@ -783,91 +783,8 @@ async function setup() {
// Load colors
CFG["colors"] = (await readFile(`${tagBasePath}/colors.json?${new Date().getTime()}`, true));
// Load wildcards
if (wildcardFiles.length === 0 && wildcardExtFiles.length === 0) {
try {
let wcFileArr = (await readFile(`${tagBasePath}/temp/wc.txt?${new Date().getTime()}`)).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 => [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(`${tagBasePath}/temp/wce.txt?${new Date().getTime()}`)).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);
}
}
// Load yaml wildcards
if (yamlWildcards.length === 0) {
try {
let yamlTags = (await readFile(`${tagBasePath}/temp/wcet.txt?${new Date().getTime()}`)).split("\n");
// Split into tag, count pairs
yamlWildcards = yamlTags.map(x => x
.trim()
.split(","))
.map(([i, ...rest]) => [
i,
rest.reduce((a, b) => {
a[b.toLowerCase()] = true;
return a;
}, {}),
]);
} catch (e) {
console.error("Error loading yaml wildcards: " + e);
}
}
// Load embeddings
if (embeddings.length === 0) {
try {
embeddings = (await readFile(`${tagBasePath}/temp/emb.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim().split(",")); // Split into name, version type pairs
} catch (e) {
console.error("Error loading embeddings.txt: " + e);
}
}
// Load hypernetworks
if (hypernetworks.length === 0) {
try {
hypernetworks = (await readFile(`${tagBasePath}/temp/hyp.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) //Remove empty lines
.map(x => x.trim()); // Remove carriage returns and padding if it exists
} catch (e) {
console.error("Error loading hypernetworks.txt: " + e);
}
}
// Load lora
if (loras.length === 0) {
try {
loras = (await readFile(`${tagBasePath}/temp/lora.txt?${new Date().getTime()}`)).split("\n")
.filter(x => x.trim().length > 0) // Remove empty lines
.map(x => x.trim()); // Remove carriage returns and padding if it exists
} catch (e) {
console.error("Error loading lora.txt: " + e);
}
}
// Load external files needed by completion extensions
await processQueue(QUEUE_FILE_LOAD, null);
// Find all textareas
let textAreas = getTextAreas();
@@ -984,7 +901,7 @@ async function setup() {
gradioApp().appendChild(acStyle);
// Callback
processQueue(QUEUE_AFTER_SETUP, null);
await processQueue(QUEUE_AFTER_SETUP, null);
}
onUiUpdate(async () => {