main
Haoming 2026-04-07 11:18:31 +08:00
parent c4e856c5eb
commit f321d7d13d
4 changed files with 36 additions and 80 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2025 Haoming
Copyright (c) 2026 Haoming
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -52,9 +52,11 @@ class pfConfigs {
/** @returns {string[]} */
static cacheCards() {
const cards = document.getElementById("pf_embeddings").querySelector("textarea").value.split("\n");
const config = document.getElementById("setting_pf_exclusion").querySelector("textarea").value;
for (const tag of config.split(",").map((t) => t.trim())) if (tag) cards.push(tag);
const cards = new Set();
const embeddings = document.getElementById("pf_embeddings").querySelector("textarea").value;
for (const tag of embeddings.split("\n").map((t) => t.trim())) if (tag) cards.add(tag);
const settings = document.getElementById("setting_pf_exclusion").querySelector("textarea").value;
for (const tag of settings.split(",").map((t) => t.trim())) if (tag) cards.add(tag);
return cards;
}
@ -67,21 +69,10 @@ class pfConfigs {
for (const line of config.split("\n")) {
const [tag, words] = line.split(":");
const mainTag = tag.trim();
for (const word of words.split(",").map((part) => part.trim())) {
if (word === mainTag) continue;
const pattern = this.#parseRegExp(word);
alias.set(pattern, mainTag);
}
const pattern = new RegExp(words);
alias.set(pattern, tag);
}
return alias;
}
/** @param {string} input @returns {RegExp} */
static #parseRegExp(input) {
return new RegExp(`^${input.trimStart("^").trimEnd("$")}$`);
}
}

View File

@ -1,10 +1,10 @@
class pfUI {
/** @param {string} text @param {string} tip @returns {HTMLButtonElement} */
static #button(text, tip) {
/** @param {string} text @param {string} hint @returns {HTMLButtonElement} */
static #button(text, hint) {
const button = document.createElement("button");
button.classList.add(["lg", "secondary", "gradio-button"]);
button.textContent = text;
if (tip) button.title = tip;
if (hint) button.title = hint;
return button;
}
@ -14,10 +14,8 @@ class pfUI {
label.classList.add("pf-checkbox");
label.removeAttribute("id");
const checkbox = label.children[0];
checkbox.checked = value;
const span = label.children[1];
span.textContent = text;
label.children[0].checked = value;
label.children[1].textContent = text;
return label;
}
@ -27,26 +25,26 @@ class pfUI {
const formatter = document.createElement("div");
formatter.id = "le-formatter";
const autoCB = this.#checkbox(autoRun, "Auto Format");
const dedupeCB = this.#checkbox(dedupe, "Remove Duplicates");
const underscoreCB = this.#checkbox(rmUnderscore, "Remove Underscores");
const manualBtn = this.#button("Format", "Manually Format the Prompts");
manualBtn.style.display = autoRun ? "none" : "flex";
const refreshBtn = this.#button("Reload", "Reload Exclusion & Alias");
refreshBtn.style.display = rmUnderscore ? "flex" : "none";
const autoCB = this.#checkbox(autoRun, "Auto Format");
const dedupeCB = this.#checkbox(dedupe, "Remove Duplicates");
const underscoreCB = this.#checkbox(rmUnderscore, "Remove Underscores");
formatter.appendChild(autoCB);
formatter.appendChild(manualBtn);
formatter.appendChild(dedupeCB);
formatter.appendChild(underscoreCB);
formatter.appendChild(refreshBtn);
formatter.manual = manualBtn;
formatter.refresh = refreshBtn;
formatter.auto = autoCB.children[0];
formatter.manual = manualBtn;
formatter.dedupe = dedupeCB.children[0];
formatter.underscore = underscoreCB.children[0];
formatter.refresh = refreshBtn;
return formatter;
}

View File

@ -62,58 +62,28 @@ def on_settings():
.needs_reload_ui(),
)
opts.add_option(
"pf_startinauto",
OptionInfo(
True, "Launch the WebUI with Auto Format enabled", **args
).needs_reload_ui(),
)
for label, desc in [
("pf_startinauto", 'Launch the WebUI with "Auto Format" enabled'),
("pf_startwithdedupe", 'Launch the WebUI with "Remove Duplicates" enabled'),
("pf_startwithrmudscr", 'Launch the WebUI with "Remove Underscores" enabled'),
("pf_appendcomma", "Append a comma at the end of each line"),
("pf_onpaste", "Format the texts pasted from clipboard"),
("pf_booru", 'Process the "Booru Structure" on Paste'),
]:
opts.add_option(label, OptionInfo(True, desc, **args).needs_reload_ui())
opts.add_option(
"pf_startwithdedupe",
OptionInfo(
True, "Launch the WebUI with Remove Duplicates enabled", **args
).needs_reload_ui(),
)
opts.add_option(
"pf_startwithrmudscr",
OptionInfo(
True, "Launch the WebUI with Remove Underscores enabled", **args
).needs_reload_ui(),
)
opts.add_option(
"pf_appendcomma",
OptionInfo(True, "Append a comma at the end of each line", **args)
.info("only take effect when there are more than 1 line")
.needs_reload_ui(),
)
opts.add_option(
"pf_onpaste",
OptionInfo(
False, "Format the texts pasted from clipboard", **args
).needs_reload_ui(),
)
opts.add_option(
"pf_booru",
OptionInfo(False, 'Process the "Booru Structure"', **args)
.info("only take effect when the above option is enabled")
.needs_reload_ui(),
)
_SCORE = "score_1, score_2, score_3, score_7, score_8, score_9"
opts.add_option(
"pf_exclusion",
OptionInfo(
default="",
label="Tags excluded from Remove Underscores",
default=_SCORE,
label='Tags to exclude from "Remove Underscores"',
component=Textbox,
component_args={
"placeholder": "score_9, score_8_up, score_7_up",
"max_lines": 4,
"placeholder": _SCORE,
"lines": 1,
"max_lines": 2,
},
**args,
).info("requires <b>Reload</b> button"),
@ -123,20 +93,17 @@ def on_settings():
"pf_alias",
OptionInfo(
default="",
label="Tag Alias for Remove Duplicates",
label='Tag Alias for "Remove Duplicates"',
component=Textbox,
component_args={
"placeholder": "1girl: girl, woman, lady\nadult: \\d+\\s*(y\\.?o\\.?|[Yy]ear[s]? [Oo]ld)",
"placeholder": "1girl:girl|woman|lady",
"max_lines": 8,
"lines": 2,
},
**args,
)
.info("requires <b>Reload</b> button")
.link(
"Regexper",
"https://regexper.com/#%5Cd%2B%5Cs*%28y%5C.%3Fo%5C.%3F%7C%5BYy%5Dear%5Bs%5D%3F%20%5BOo%5Dld%29",
),
.link("RegExr", "https://regexr.com/"),
)