Merge pull request #9609 from akx/bracket-fix

prompt-bracket-checker: Simplify code + improve error reporting
This commit is contained in:
AUTOMATIC1111 2023-04-29 12:58:20 +03:00 committed by GitHub
commit e847df7ee9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,103 +1,42 @@
// Stable Diffusion WebUI - Bracket checker // Stable Diffusion WebUI - Bracket checker
// Version 1.0 // By Hingashi no Florin/Bwin4L & @akx
// By Hingashi no Florin/Bwin4L
// Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs. // Counts open and closed brackets (round, square, curly) in the prompt and negative prompt text boxes in the txt2img and img2img tabs.
// If there's a mismatch, the keyword counter turns red and if you hover on it, a tooltip tells you what's wrong. // If there's a mismatch, the keyword counter turns red and if you hover on it, a tooltip tells you what's wrong.
function checkBrackets(evt, textArea, counterElt) { function checkBrackets(textArea, counterElt) {
errorStringParen = '(...) - Different number of opening and closing parentheses detected.\n'; var counts = {};
errorStringSquare = '[...] - Different number of opening and closing square brackets detected.\n'; (textArea.value.match(/[(){}\[\]]/g) || []).forEach(bracket => {
errorStringCurly = '{...} - Different number of opening and closing curly brackets detected.\n'; counts[bracket] = (counts[bracket] || 0) + 1;
});
var errors = [];
openBracketRegExp = /\(/g; function checkPair(open, close, kind) {
closeBracketRegExp = /\)/g; if (counts[open] !== counts[close]) {
errors.push(
openSquareBracketRegExp = /\[/g; `${open}...${close} - Detected ${counts[open] || 0} opening and ${counts[close] || 0} closing ${kind}.`
closeSquareBracketRegExp = /\]/g; );
openCurlyBracketRegExp = /\{/g;
closeCurlyBracketRegExp = /\}/g;
totalOpenBracketMatches = 0;
totalCloseBracketMatches = 0;
totalOpenSquareBracketMatches = 0;
totalCloseSquareBracketMatches = 0;
totalOpenCurlyBracketMatches = 0;
totalCloseCurlyBracketMatches = 0;
openBracketMatches = textArea.value.match(openBracketRegExp);
if(openBracketMatches) {
totalOpenBracketMatches = openBracketMatches.length;
}
closeBracketMatches = textArea.value.match(closeBracketRegExp);
if(closeBracketMatches) {
totalCloseBracketMatches = closeBracketMatches.length;
}
openSquareBracketMatches = textArea.value.match(openSquareBracketRegExp);
if(openSquareBracketMatches) {
totalOpenSquareBracketMatches = openSquareBracketMatches.length;
}
closeSquareBracketMatches = textArea.value.match(closeSquareBracketRegExp);
if(closeSquareBracketMatches) {
totalCloseSquareBracketMatches = closeSquareBracketMatches.length;
}
openCurlyBracketMatches = textArea.value.match(openCurlyBracketRegExp);
if(openCurlyBracketMatches) {
totalOpenCurlyBracketMatches = openCurlyBracketMatches.length;
}
closeCurlyBracketMatches = textArea.value.match(closeCurlyBracketRegExp);
if(closeCurlyBracketMatches) {
totalCloseCurlyBracketMatches = closeCurlyBracketMatches.length;
}
if(totalOpenBracketMatches != totalCloseBracketMatches) {
if(!counterElt.title.includes(errorStringParen)) {
counterElt.title += errorStringParen;
} }
} else {
counterElt.title = counterElt.title.replace(errorStringParen, '');
} }
if(totalOpenSquareBracketMatches != totalCloseSquareBracketMatches) { checkPair('(', ')', 'round brackets');
if(!counterElt.title.includes(errorStringSquare)) { checkPair('[', ']', 'square brackets');
counterElt.title += errorStringSquare; checkPair('{', '}', 'curly brackets');
} counterElt.title = errors.join('\n');
} else { counterElt.classList.toggle('error', errors.length !== 0);
counterElt.title = counterElt.title.replace(errorStringSquare, ''); }
}
if(totalOpenCurlyBracketMatches != totalCloseCurlyBracketMatches) { function setupBracketChecking(id_prompt, id_counter) {
if(!counterElt.title.includes(errorStringCurly)) { var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea");
counterElt.title += errorStringCurly; var counter = gradioApp().getElementById(id_counter)
}
} else {
counterElt.title = counterElt.title.replace(errorStringCurly, '');
}
if(counterElt.title != '') { if (textarea && counter) {
counterElt.classList.add('error'); textarea.addEventListener("input", () => checkBrackets(textarea, counter));
} else {
counterElt.classList.remove('error');
} }
} }
function setupBracketChecking(id_prompt, id_counter){ onUiLoaded(function () {
var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea"); setupBracketChecking('txt2img_prompt', 'txt2img_token_counter');
var counter = gradioApp().getElementById(id_counter) setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter');
setupBracketChecking('img2img_prompt', 'img2img_token_counter');
textarea.addEventListener("input", function(evt){ setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter');
checkBrackets(evt, textarea, counter) });
});
}
onUiLoaded(function(){
setupBracketChecking('txt2img_prompt', 'txt2img_token_counter')
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter')
setupBracketChecking('img2img_prompt', 'img2img_token_counter')
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter')
})