From ead24c9361337a1ab52720c85b3daab431b00f24 Mon Sep 17 00:00:00 2001 From: xhoxye <129571231+xhoxye@users.noreply.github.com> Date: Mon, 11 Mar 2024 06:18:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20read=20wildcards=20in=20order=20?= =?UTF-8?q?=E9=80=9A=E9=85=8D=E7=AC=A6=E5=A2=9E=E5=BC=BA=EF=BC=8C=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E9=A1=BA=E5=BA=8F=E8=AF=BB=E5=8F=96=E3=80=82(#1761)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 通配符增强,切换顺序读取 通配符增强,通过勾选切换通配符读取方法,默认不勾选为随机读取一行,勾选后为按顺序读取,并使用相同的种子。 * 代码来自刁璐璐 * update * Update async_worker.py * refactor: rename read_wildcard_in_order_checkbox to read_wildcard_in_order * fix: use correct method call for interrupt_current_processing actually achieves the same result, stopping the task * refactor: move checkbox to developer debug mode, rename to plural below disable seed increment * refactor: code cleanup, separate code for disable_seed_increment * i18n: add translation for checkbox text --------- Co-authored-by: Manuel Schmid --- language/en.json | 1 + modules/async_worker.py | 12 +++++++----- modules/sdxl_styles.py | 7 +++++-- webui.py | 4 +++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/language/en.json b/language/en.json index f61255c9..241c5d54 100644 --- a/language/en.json +++ b/language/en.json @@ -50,6 +50,7 @@ "Seed": "Seed", "Disable seed increment": "Disable seed increment", "Disable automatic seed increment when image number is > 1.": "Disable automatic seed increment when image number is > 1.", + "Read wildcards in order": "Read wildcards in order", "\ud83d\udcda History Log": "\uD83D\uDCDA History Log", "Image Style": "Image Style", "Fooocus V2": "Fooocus V2", diff --git a/modules/async_worker.py b/modules/async_worker.py index c7df14f5..c5953a58 100644 --- a/modules/async_worker.py +++ b/modules/async_worker.py @@ -1,4 +1,5 @@ import threading +import re from modules.patch import PatchSettings, patch_settings, patch_all patch_all() @@ -148,6 +149,7 @@ def worker(): image_number = args.pop() output_format = args.pop() image_seed = args.pop() + read_wildcards_in_order = args.pop() sharpness = args.pop() guidance_scale = args.pop() base_model_name = args.pop() @@ -441,16 +443,16 @@ def worker(): for i in range(image_number): if disable_seed_increment: - task_seed = seed + task_seed = seed % (constants.MAX_SEED + 1) else: task_seed = (seed + i) % (constants.MAX_SEED + 1) # randint is inclusive, % is not task_rng = random.Random(task_seed) # may bind to inpaint noise in the future - task_prompt = apply_wildcards(prompt, task_rng) + task_prompt = apply_wildcards(prompt, task_rng, i, read_wildcards_in_order) task_prompt = apply_arrays(task_prompt, i) - task_negative_prompt = apply_wildcards(negative_prompt, task_rng) - task_extra_positive_prompts = [apply_wildcards(pmt, task_rng) for pmt in extra_positive_prompts] - task_extra_negative_prompts = [apply_wildcards(pmt, task_rng) for pmt in extra_negative_prompts] + task_negative_prompt = apply_wildcards(negative_prompt, task_rng, i, read_wildcards_in_order) + task_extra_positive_prompts = [apply_wildcards(pmt, task_rng, i, read_wildcards_in_order) for pmt in extra_positive_prompts] + task_extra_negative_prompts = [apply_wildcards(pmt, task_rng, i, read_wildcards_in_order) for pmt in extra_negative_prompts] positive_basic_workloads = [] negative_basic_workloads = [] diff --git a/modules/sdxl_styles.py b/modules/sdxl_styles.py index 0b07339c..77ad6b57 100644 --- a/modules/sdxl_styles.py +++ b/modules/sdxl_styles.py @@ -59,7 +59,7 @@ def apply_style(style, positive): return p.replace('{prompt}', positive).splitlines(), n.splitlines() -def apply_wildcards(wildcard_text, rng): +def apply_wildcards(wildcard_text, rng, i, read_wildcards_in_order): for _ in range(wildcards_max_bfs_depth): placeholders = re.findall(r'__([\w-]+)__', wildcard_text) if len(placeholders) == 0: @@ -72,7 +72,10 @@ def apply_wildcards(wildcard_text, rng): words = open(os.path.join(modules.config.path_wildcards, matches[0]), encoding='utf-8').read().splitlines() words = [x for x in words if x != ''] assert len(words) > 0 - wildcard_text = wildcard_text.replace(f'__{placeholder}__', rng.choice(words), 1) + if read_wildcards_in_order: + wildcard_text = wildcard_text.replace(f'__{placeholder}__', words[i % len(words)], 1) + else: + wildcard_text = wildcard_text.replace(f'__{placeholder}__', rng.choice(words), 1) except: print(f'[Wildcards] Warning: {placeholder}.txt missing or empty. ' f'Using "{placeholder}" as a normal word.') diff --git a/webui.py b/webui.py index 808db72d..ee7edc2d 100644 --- a/webui.py +++ b/webui.py @@ -434,6 +434,7 @@ with shared.gradio_root: disable_seed_increment = gr.Checkbox(label='Disable seed increment', info='Disable automatic seed increment when image number is > 1.', value=False) + read_wildcards_in_order = gr.Checkbox(label="Read wildcards in order", value=False) if not args_manager.args.disable_metadata: save_metadata_to_images = gr.Checkbox(label='Save Metadata to Images', value=modules.config.default_save_metadata_to_images, @@ -578,7 +579,8 @@ with shared.gradio_root: ctrls = [currentTask, generate_image_grid] ctrls += [ prompt, negative_prompt, style_selections, - performance_selection, aspect_ratios_selection, image_number, output_format, image_seed, sharpness, guidance_scale + performance_selection, aspect_ratios_selection, image_number, output_format, image_seed, + read_wildcards_in_order, sharpness, guidance_scale ] ctrls += [base_model, refiner_model, refiner_switch] + lora_ctrls