2023-10-01 15:15:23 +08:00
|
|
|
let promptTokenCountUpdateFunctions = {};
|
2023-05-22 04:16:14 +08:00
|
|
|
|
|
|
|
function update_txt2img_tokens(...args) {
|
|
|
|
// Called from Gradio
|
|
|
|
update_token_counter("txt2img_token_button");
|
2023-10-01 15:15:23 +08:00
|
|
|
update_token_counter("txt2img_negative_token_button");
|
2023-05-22 04:16:14 +08:00
|
|
|
if (args.length == 2) {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_img2img_tokens(...args) {
|
|
|
|
// Called from Gradio
|
|
|
|
update_token_counter("img2img_token_button");
|
2023-10-01 15:15:23 +08:00
|
|
|
update_token_counter("img2img_negative_token_button");
|
2023-05-22 04:16:14 +08:00
|
|
|
if (args.length == 2) {
|
|
|
|
return args[0];
|
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
function update_token_counter(button_id) {
|
2023-10-01 15:15:23 +08:00
|
|
|
promptTokenCountUpdateFunctions[button_id]?.();
|
2023-05-22 04:16:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function recalculatePromptTokens(name) {
|
|
|
|
promptTokenCountUpdateFunctions[name]?.();
|
|
|
|
}
|
|
|
|
|
|
|
|
function recalculate_prompts_txt2img() {
|
|
|
|
// Called from Gradio
|
|
|
|
recalculatePromptTokens('txt2img_prompt');
|
|
|
|
recalculatePromptTokens('txt2img_neg_prompt');
|
|
|
|
return Array.from(arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
function recalculate_prompts_img2img() {
|
|
|
|
// Called from Gradio
|
|
|
|
recalculatePromptTokens('img2img_prompt');
|
|
|
|
recalculatePromptTokens('img2img_neg_prompt');
|
|
|
|
return Array.from(arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupTokenCounting(id, id_counter, id_button) {
|
|
|
|
var prompt = gradioApp().getElementById(id);
|
|
|
|
var counter = gradioApp().getElementById(id_counter);
|
|
|
|
var textarea = gradioApp().querySelector(`#${id} > label > textarea`);
|
|
|
|
|
|
|
|
if (counter.parentElement == prompt.parentElement) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt.parentElement.insertBefore(counter, prompt);
|
|
|
|
prompt.parentElement.style.position = "relative";
|
|
|
|
|
2023-10-01 16:48:41 +08:00
|
|
|
var func = onEdit(id, textarea, 800, function() {
|
2024-02-17 15:32:10 +08:00
|
|
|
if (counter.classList.contains("token-counter-visible")) {
|
2024-02-17 15:31:16 +08:00
|
|
|
gradioApp().getElementById(id_button)?.click();
|
|
|
|
}
|
2023-10-01 15:15:23 +08:00
|
|
|
});
|
|
|
|
promptTokenCountUpdateFunctions[id] = func;
|
|
|
|
promptTokenCountUpdateFunctions[id_button] = func;
|
2023-05-22 04:16:14 +08:00
|
|
|
}
|
|
|
|
|
2024-02-17 15:31:16 +08:00
|
|
|
function toggleTokenCountingVisibility(id, id_counter, id_button) {
|
|
|
|
var counter = gradioApp().getElementById(id_counter);
|
|
|
|
|
|
|
|
counter.style.display = opts.disable_token_counters ? "none" : "block";
|
2024-02-17 15:32:10 +08:00
|
|
|
counter.classList.toggle("token-counter-visible", !opts.disable_token_counters);
|
2023-05-22 04:16:14 +08:00
|
|
|
}
|
2024-02-17 15:31:16 +08:00
|
|
|
|
2024-02-17 15:32:10 +08:00
|
|
|
function runCodeForTokenCounters(fun) {
|
2024-02-17 15:31:16 +08:00
|
|
|
fun('txt2img_prompt', 'txt2img_token_counter', 'txt2img_token_button');
|
|
|
|
fun('txt2img_neg_prompt', 'txt2img_negative_token_counter', 'txt2img_negative_token_button');
|
|
|
|
fun('img2img_prompt', 'img2img_token_counter', 'img2img_token_button');
|
|
|
|
fun('img2img_neg_prompt', 'img2img_negative_token_counter', 'img2img_negative_token_button');
|
|
|
|
}
|
|
|
|
|
2024-02-17 15:32:10 +08:00
|
|
|
onUiLoaded(function() {
|
2024-02-17 15:31:16 +08:00
|
|
|
runCodeForTokenCounters(setupTokenCounting);
|
|
|
|
});
|
|
|
|
|
2024-02-17 15:32:10 +08:00
|
|
|
onOptionsChanged(function() {
|
2024-02-17 15:31:16 +08:00
|
|
|
runCodeForTokenCounters(toggleTokenCountingVisibility);
|
|
|
|
});
|