Skip to content

Commit

Permalink
修改prompt-bracket-checker,兼容错误
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxie-crypto committed Apr 14, 2023
1 parent 3c43afc commit 364f5e3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
2 changes: 1 addition & 1 deletion publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Type: Application
Name: fc-stable-diffusion
Provider:
- 阿里云
Version: 0.0.16
Version: 0.0.17
Description: 使用serverless devs将stable-diffusion部署到阿里云函数计算上
HomePage: 项目首页地址
Tags: #标签详情
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// 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) {
errorStringParen = '(...) - Different number of opening and closing parentheses detected.\n';
errorStringParen = '(...) - Different number of opening and closing parentheses detected.\n';
errorStringSquare = '[...] - Different number of opening and closing square brackets detected.\n';
errorStringCurly = '{...} - Different number of opening and closing curly brackets detected.\n';
errorStringCurly = '{...} - Different number of opening and closing curly brackets detected.\n';

openBracketRegExp = /\(/g;
closeBracketRegExp = /\)/g;
Expand All @@ -26,85 +26,89 @@ function checkBrackets(evt, textArea, counterElt) {
totalCloseCurlyBracketMatches = 0;

openBracketMatches = textArea.value.match(openBracketRegExp);
if(openBracketMatches) {
if (openBracketMatches) {
totalOpenBracketMatches = openBracketMatches.length;
}

closeBracketMatches = textArea.value.match(closeBracketRegExp);
if(closeBracketMatches) {
if (closeBracketMatches) {
totalCloseBracketMatches = closeBracketMatches.length;
}

openSquareBracketMatches = textArea.value.match(openSquareBracketRegExp);
if(openSquareBracketMatches) {
if (openSquareBracketMatches) {
totalOpenSquareBracketMatches = openSquareBracketMatches.length;
}

closeSquareBracketMatches = textArea.value.match(closeSquareBracketRegExp);
if(closeSquareBracketMatches) {
if (closeSquareBracketMatches) {
totalCloseSquareBracketMatches = closeSquareBracketMatches.length;
}

openCurlyBracketMatches = textArea.value.match(openCurlyBracketRegExp);
if(openCurlyBracketMatches) {
if (openCurlyBracketMatches) {
totalOpenCurlyBracketMatches = openCurlyBracketMatches.length;
}

closeCurlyBracketMatches = textArea.value.match(closeCurlyBracketRegExp);
if(closeCurlyBracketMatches) {
if (closeCurlyBracketMatches) {
totalCloseCurlyBracketMatches = closeCurlyBracketMatches.length;
}

if(totalOpenBracketMatches != totalCloseBracketMatches) {
if(!counterElt.title.includes(errorStringParen)) {
if (totalOpenBracketMatches != totalCloseBracketMatches) {
if (!counterElt.title.includes(errorStringParen)) {
counterElt.title += errorStringParen;
}
} else {
counterElt.title = counterElt.title.replace(errorStringParen, '');
}

if(totalOpenSquareBracketMatches != totalCloseSquareBracketMatches) {
if(!counterElt.title.includes(errorStringSquare)) {
if (totalOpenSquareBracketMatches != totalCloseSquareBracketMatches) {
if (!counterElt.title.includes(errorStringSquare)) {
counterElt.title += errorStringSquare;
}
} else {
counterElt.title = counterElt.title.replace(errorStringSquare, '');
}

if(totalOpenCurlyBracketMatches != totalCloseCurlyBracketMatches) {
if(!counterElt.title.includes(errorStringCurly)) {
if (totalOpenCurlyBracketMatches != totalCloseCurlyBracketMatches) {
if (!counterElt.title.includes(errorStringCurly)) {
counterElt.title += errorStringCurly;
}
} else {
counterElt.title = counterElt.title.replace(errorStringCurly, '');
}

if(counterElt.title != '') {
if (counterElt.title != '') {
counterElt.classList.add('error');
} else {
counterElt.classList.remove('error');
}
}

function setupBracketChecking(id_prompt, id_counter){
var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea");
var counter = gradioApp().getElementById(id_counter)
textarea.addEventListener("input", function(evt){
checkBrackets(evt, textarea, counter)
});
function setupBracketChecking(id_prompt, id_counter) {
var textarea = gradioApp().querySelector("#" + id_prompt + " > label > textarea");
var counter = gradioApp().getElementById(id_counter)
textarea.addEventListener("input", function (evt) {
checkBrackets(evt, textarea, counter)
});
}

var shadowRootLoaded = setInterval(function() {
var shadowRoot = document.querySelector('gradio-app').shadowRoot;
if(! shadowRoot) return false;
var shadowRootLoaded = setInterval(function () {
var gradioApp = document.querySelector('gradio-app');
if (!gradioApp) {
return;
}
var shadowRoot = gradioApp.shadowRoot;
if (!shadowRoot) return false;

var shadowTextArea = shadowRoot.querySelectorAll('#txt2img_prompt > label > textarea');
if(shadowTextArea.length < 1) return false;
var shadowTextArea = shadowRoot.querySelectorAll('#txt2img_prompt > label > textarea');
if (shadowTextArea.length < 1) return false;

clearInterval(shadowRootLoaded);
clearInterval(shadowRootLoaded);

setupBracketChecking('txt2img_prompt', 'txt2img_token_counter')
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter')
setupBracketChecking('img2img_prompt', 'imgimg_token_counter')
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter')
setupBracketChecking('txt2img_prompt', 'txt2img_token_counter')
setupBracketChecking('txt2img_neg_prompt', 'txt2img_negative_token_counter')
setupBracketChecking('img2img_prompt', 'imgimg_token_counter')
setupBracketChecking('img2img_neg_prompt', 'img2img_negative_token_counter')
}, 1000);

0 comments on commit 364f5e3

Please sign in to comment.