diff --git a/README.md b/README.md index f401b6a..3637f34 100644 --- a/README.md +++ b/README.md @@ -5,11 +5,9 @@ This is an Extension for the [Automatic1111 Webui](https://github.com/AUTOMATIC1

-> The above demo was achieved in just 1 process - Sometimes, when you type too fast or copy prompts from all over the places, you end up with duplicated **spaces** or **commas**. This simple Extension helps removing them whenever you click **Generate**. -## Feature List +## Features - [x] Works in both `txt2img` and `img2img` - [x] Remove duplicated **spaces** and **commas** - [x] Fix misplaced **brackets** and **commas** @@ -20,17 +18,17 @@ Sometimes, when you type too fast or copy prompts from all over the places, you - [x] Toggle `Remove Underscores` to replace `_` with **space** - *Some newer anime checkpoints claim to eliminate the need of using underscores* - [x] Respect line breaks - - **Note:** `Remove Duplicates` only checks within the same line + - `Remove Duplicates` only checks within the same line - [x] Pressing `Ctrl + \` to quickly escape the **parentheses** of the hovered tag *(the words where the caret is)* - - Normally, **parentheses** are used to increase the weight of a prompt. Therefore, for tags like `mejiro mcqueen (umamusume)`, you will need to escape it like `mejiro mcqueen \(umamusume\)`. + - Normally, **parentheses** are used to increase the weight of a prompt. Therefore, tags like `mejiro mcqueen (umamusume)` will need to be escaped to `mejiro mcqueen \(umamusume\)`. - [x] Toggle between auto formatting and manual formatting - In `Auto`: The process is ran whenever you press **Generate** - In `Manual`: The process is only ran when you press the **Format** button -- [x] Toggle between auto updating prompts or not[^1] - - Some Extensions *(**eg.** [tagcomplete](https://github.com/DominikDoom/a1111-sd-webui-tagcomplete))* listen to the text editing event, which means the formatting causes them to trigger - - You only really need to disable this if you have the above Extension - - You can open `prompt_format.js` and edit this line at the top `static updateInput = true;` to `static updateInput = false;` to save the setting - - When `disabled`, the formatting is purely visual. It will only update once you manually edit the prompt again -- [x] **[New]** Use `Shift + ScrollWheel` to quickly shift the hovered tag in the prompt +- [x] **[New]** Use `Shift + ScrollWheel` to quickly move the hovered tag *(determined by **commas**)* around the prompt -[^1]: Due to the implementation being in `JavaScript` instead of `Python`, the image's metadata will still only be updated in the next generation. This toggle mainly affects when you click `Send to img2img`, will the prompt be already formatted or not, etc. \ No newline at end of file +## Note +1. Since the auto formatting is triggered at the same time as the generation, +the immediate image will not have its metadata updated unless you already did manual formatting beforehand. + +2. Some Extensions *(**eg.** [tagcomplete](https://github.com/DominikDoom/a1111-sd-webui-tagcomplete))* listen to the text editing event, +meaning the formatting will cause them to trigger. You can disable updating the actual prompts in the **System** section of the **Settings** tab. \ No newline at end of file diff --git a/README_ZH.md b/README_ZH.md index 8aa9943..c4a5f1c 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1,37 +1,33 @@ # SD Webui Prompt Format [[English](README.md)|中文] -這是一個[Automatic1111 Webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)的插件,可以幫忙校正咒語。 +這是一個[Automatic1111 Webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui)的插件,用來幫忙校正咒語。

-> 示範圖 +有時候如果打字太快或是從各處東拼西湊咒語,常會造成多個重複的空格或逗點。這個擴充可以幫忙移除它們。 -有時候打字太快,或是從各地東拼西湊咒語,常造成多個重複的空格或逗點。這項插件可以幫忙移除它們。 - -## 功能實作 -- [x] 你可以打開`prompt_format.js`並把上面的`static UseCN = false;`改成`static UseCN = true;`來套用中文翻譯 +## 功能 - [x] 在`txt2img`和`img2img`都有用 -- [x] 移除多餘**空格**和**逗點** +- [x] 移除多餘的**空格**和**逗點** - [x] 修改錯誤的**括弧** - [x] 開啟`Remove Duplicates`會把咒語中重複的單詞消除 - **注意:** 只對單詞類咒語有效 - **例.** `1girl, solo, smile, 1girl` 會變成 `1girl, solo, smile` - **例.** `a girl smiling, a girl standing` 則不變 -- [x] 開啟`Remove Underscores`會將 `_` 換成**空格** +- [x] 開啟`Remove Underscores`會將 `_` 換成 **空格** - *一些較新的動漫模型聲稱不用再加底線* - [x] 保留咒語的換行 - - **注意:** 上述的`Remove Duplicates`只在同一行中有效 + - 上述的`Remove Duplicates`只在同一行中有效 - [x] 按下`Ctrl + \`來跳脫目前游標所在的單字 - 平時,**括弧**是用來強調單字。所以若使用像是`mejiro mcqueen (umamusume)`的咒語,便必須跳脫成`mejiro mcqueen \(umamusume\)` - [x] 按下`Auto Format`以在手動與自動間切換 - `自動`: 每次按下 **生成 (Generate)** 時處裡 - - `手動`: 手動按下`Format`時才處裡 -- [x] 按下`Update Input`以開/關格式套用[^1] - - 有些擴充 *(**如.** [tagcomplete](https://github.com/DominikDoom/a1111-sd-webui-tagcomplete))* 訂閱文字的編輯事件,意即我的格式化會導致它們啟動 - - 基本上,只有在有安裝上述擴充時才需要關閉這個功能 - - 你可以打開`prompt_format.js`並把上面的`static updateInput = true;`改成`static updateInput = false;`來永久關閉 - - 在`關閉`時,前述的美化只是視覺效果。唯有再次手動編輯後,咒語才會更新 -- [x] **[New]** 使用`Shift + 滾輪`來橫移目前游標所在的單字 + - `手動`: 手動按下 **Format** 時才處裡 +- [x] **[New]** 使用`Shift + 滾輪`來橫移目前游標所在的單字 *(由**逗點**決定)* -[^1]: 由於透過`JavaScript`而非`Python`撰寫,生成圖片的內部資料只會在下一次生成才更新。這個選項主要影響,當你按下`Send to img2img`時所傳送的咒語是否已美化過等等。 \ No newline at end of file +## 注意 +1. 由於自動校正和生成是同時觸發,除非你有用手動校正,不然當下所產的圖片之內部資料並不會被更新。 + +2. 有些擴充 *(如. [tagcomplete](https://github.com/DominikDoom/a1111-sd-webui-tagcomplete))* 追蹤文字的編輯事件,意即文字校正會導致它們啟動。 +你可以到 **Settings** 的 **System** 頁面關閉咒語的自動編輯。 \ No newline at end of file diff --git a/javascript/prompt_format.js b/javascript/prompt_format.js index 661182c..c193073 100644 --- a/javascript/prompt_format.js +++ b/javascript/prompt_format.js @@ -1,12 +1,5 @@ class LeFormatter { - // === Configs === - - static UseCN = false; - static updateInput = true; - - // === Configs === - static manualButton(text, id, { onClick }) { const button = gradioApp().getElementById(id).cloneNode() @@ -232,9 +225,17 @@ onUiLoaded(async () => { let autoRun = true let dedupe = false let removeUnderscore = false - let refreshInput = LeFormatter.updateInput + let refreshInput = null - const manualBtn = LeFormatter.manualButton((LeFormatter.UseCN ? '格式化' : 'Format'), 'txt2img_generate', { + try { + const temp = gradioApp().getElementById('pf-config-true') + temp.remove() + refreshInput = false + } catch { + refreshInput = true + } + + const manualBtn = LeFormatter.manualButton('Format', 'txt2img_generate', { onClick: () => { const ids = ['txt2img_prompt', 'txt2img_neg_prompt', 'img2img_prompt', 'img2img_neg_prompt'] @@ -254,25 +255,21 @@ onUiLoaded(async () => { manualBtn.style.display = 'none' - const autoCB = LeFormatter.checkbox((LeFormatter.UseCN ? '自動格式化' : 'Auto Format'), autoRun, { + const autoCB = LeFormatter.checkbox('Auto Format', autoRun, { onChange: (checked) => { autoRun = checked manualBtn.style.display = autoRun ? 'none' : 'block' } }) - const dedupeCB = LeFormatter.checkbox((LeFormatter.UseCN ? '去除重複' : 'Remove Duplicates'), dedupe, { + const dedupeCB = LeFormatter.checkbox('Remove Duplicates', dedupe, { onChange: (checked) => { dedupe = checked } }) - const underlineCB = LeFormatter.checkbox((LeFormatter.UseCN ? '去除底線' : 'Remove Underscores'), removeUnderscore, { + const underlineCB = LeFormatter.checkbox('Remove Underscores', removeUnderscore, { onChange: (checked) => { removeUnderscore = checked } }) - const refreshCB = LeFormatter.checkbox((LeFormatter.UseCN ? '更新文字' : 'Update Input'), autoRun, { - onChange: (checked) => { refreshInput = checked } - }) - const formatter = document.createElement('div') formatter.id = 'le-formatter' formatter.style.display = 'flex' @@ -282,7 +279,6 @@ onUiLoaded(async () => { formatter.appendChild(manualBtn) formatter.appendChild(dedupeCB) formatter.appendChild(underlineCB) - formatter.appendChild(refreshCB) const tools = document.getElementById('quicksettings') tools.after(formatter) diff --git a/scripts/pf_settings.py b/scripts/pf_settings.py new file mode 100644 index 0000000..d5d2a34 --- /dev/null +++ b/scripts/pf_settings.py @@ -0,0 +1,27 @@ +from modules import script_callbacks, shared +import modules.scripts as scripts +import gradio as gr + +class Script(scripts.Script): + + def title(self): + return "Prompt Format" + + def show(self, is_img2img): + if is_img2img is True: + return None + return scripts.AlwaysVisible + + def ui(self, is_img2img): + if is_img2img is True: + return None + + if shared.opts.pf_disableupdateinput and shared.opts.pf_disableupdateinput is True: + gr.Box(elem_id = 'pf-config-true') + + return None + +def on_ui_settings(): + shared.opts.add_option("pf_disableupdateinput", shared.OptionInfo(False, "Prompt Format - Disable Update Input", section=("system", "System"))) + +script_callbacks.on_ui_settings(on_ui_settings)