diff --git a/README.md b/README.md index ca72cd7..170fdaf 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,6 @@ This is an Extension for the [Automatic1111 Webui](https://github.com/AUTOMATIC1 ## What is This ? - Sometimes when you are typing too fast or are copying tags from all over the places, you end up with duplicated spaces or commas. - This simple script helps removing them whenever you click **Generate**. -- Works for both `txt2img` and `img2img`. +- Works in both `txt2img` and `img2img`. +- You can now toggle `Remove Duplicates` to remove duplicated tags from the prompts + - **Note:** Only works for tag-based prompt *(**eg.** 1girl, solo, smile, 1girl)*, but not for sentence-based prompt *(**eg.** a girl smiling, a girl standing)* diff --git a/javascript/prompt_format.js b/javascript/prompt_format.js index 9ce1404..530602a 100644 --- a/javascript/prompt_format.js +++ b/javascript/prompt_format.js @@ -14,23 +14,50 @@ class LeFormatter { i2i_button.addEventListener('click', onClick) } + static checkbox(text, { onChange }) { + const label = document.createElement('label') + label.style.display = 'flex' + label.style.alignItems = 'center' + + const checkbox = gradioApp().querySelector('input[type=checkbox]').cloneNode() + checkbox.addEventListener('change', (event) => { + onChange(event.target.checked) + }) + + const span = document.createElement('span') + span.style.marginLeft = 'var(--size-2, 4px)' + span.textContent = text + + label.appendChild(checkbox) + label.appendChild(span) + + return label + } } onUiLoaded(async () => { + this.dedupe = false + + const tools = document.getElementById('txt2img_tools') + const checkbox = LeFormatter.checkbox('Remove Duplicates', { + onChange: (checked) => { this.dedupe = checked } + }) + tools.appendChild(checkbox) LeFormatter.injectTxt2ImgButton({ onClick: () => { const idP = 'txt2img_prompt' const textareaP = gradioApp().getElementById(idP).querySelector('textarea') const tagsP = textareaP.value.split(',').map(word => word.trim()).filter(word => word !== ''); - const sentenceP = tagsP.join(', '); + const sentenceP = this.dedupe ? [...new Set(tagsP)].join(', ') : tagsP.join(', '); textareaP.value = sentenceP.replace(/\s+/g, ' ').trim(); updateInput(textareaP) + const idN = 'txt2img_neg_prompt' const textareaN = gradioApp().getElementById(idN).querySelector('textarea') const tagsN = textareaN.value.split(',').map(word => word.trim()).filter(word => word !== ''); - const sentenceN = tagsN.join(', '); + const sentenceN = this.dedupe ? [...new Set(tagsN)].join(', ') : tagsN.join(', '); textareaN.value = sentenceN.replace(/\s+/g, ' ').trim(); updateInput(textareaN) } @@ -41,13 +68,14 @@ onUiLoaded(async () => { const idP = 'img2img_prompt' const textareaP = gradioApp().getElementById(idP).querySelector('textarea') const tagsP = textareaP.value.split(',').map(word => word.trim()).filter(word => word !== ''); - const sentenceP = tagsP.join(', '); + const sentenceP = this.dedupe ? [...new Set(tagsP)].join(', ') : tagsP.join(', '); textareaP.value = sentenceP.replace(/\s+/g, ' ').trim(); updateInput(textareaP) + const idN = 'img2img_neg_prompt' const textareaN = gradioApp().getElementById(idN).querySelector('textarea') const tagsN = textareaN.value.split(',').map(word => word.trim()).filter(word => word !== ''); - const sentenceN = tagsN.join(', '); + const sentenceN = this.dedupe ? [...new Set(tagsN)].join(', ') : tagsN.join(', '); textareaN.value = sentenceN.replace(/\s+/g, ' ').trim(); updateInput(textareaN) }