From 7c53f81caf817a7e7dc9c2fafebfcce269ecb1d7 Mon Sep 17 00:00:00 2001 From: Yevhenii Hurin Date: Sun, 29 Jan 2023 15:29:03 +0200 Subject: [PATCH 1/4] Prompt selector for Prompt Matrix script --- scripts/prompt_matrix.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py index dd95e588..702870ce 100644 --- a/scripts/prompt_matrix.py +++ b/scripts/prompt_matrix.py @@ -10,7 +10,7 @@ from modules import images from modules.processing import process_images, Processed from modules.shared import opts, cmd_opts, state import modules.sd_samplers - +from pprint import pprint def draw_xy_grid(xs, ys, x_label, y_label, cell): res = [] @@ -44,16 +44,23 @@ class Script(scripts.Script): def title(self): return "Prompt matrix" - def ui(self, is_img2img): + def ui(self, is_img2img): put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', value=False, elem_id=self.elem_id("put_at_start")) different_seeds = gr.Checkbox(label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) + # Radio buttons for selecting the prompt between positive and negative + prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", elem_id=self.elem_id("prompt_type"), value="positive") - return [put_at_start, different_seeds] + return [put_at_start, different_seeds, prompt_type] - def run(self, p, put_at_start, different_seeds): + def run(self, p, put_at_start, different_seeds, prompt_type): modules.processing.fix_seed(p) + # Raise error if promp type is not positive or negative + if prompt_type not in ["positive", "negative"]: + raise ValueError(f"Unknown prompt type {prompt_type}") - original_prompt = p.prompt[0] if type(p.prompt) == list else p.prompt + prompt = p.prompt if prompt_type == "positive" else p.negative_prompt + original_prompt = prompt[0] if type(prompt) == list else prompt + positive_prompt = p.prompt[0] if type(p.prompt) == list else p.prompt all_prompts = [] prompt_matrix_parts = original_prompt.split("|") @@ -73,9 +80,12 @@ class Script(scripts.Script): print(f"Prompt matrix will create {len(all_prompts)} images using a total of {p.n_iter} batches.") - p.prompt = all_prompts + if prompt_type == "positive": + p.prompt = all_prompts + else: + p.negative_prompt = all_prompts p.seed = [p.seed + (i if different_seeds else 0) for i in range(len(all_prompts))] - p.prompt_for_display = original_prompt + p.prompt_for_display = positive_prompt processed = process_images(p) grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2)) From edabd927296d389856ffc87c7789111a21a051a2 Mon Sep 17 00:00:00 2001 From: Yevhenii Hurin Date: Sun, 29 Jan 2023 16:05:59 +0200 Subject: [PATCH 2/4] Add delimiter selector to the Prompt Matrix script --- scripts/prompt_matrix.py | 47 ++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py index 702870ce..89db9e63 100644 --- a/scripts/prompt_matrix.py +++ b/scripts/prompt_matrix.py @@ -12,6 +12,7 @@ from modules.shared import opts, cmd_opts, state import modules.sd_samplers from pprint import pprint + def draw_xy_grid(xs, ys, x_label, y_label, cell): res = [] @@ -33,7 +34,8 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell): res.append(processed.images[0]) grid = images.image_grid(res, rows=len(ys)) - grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts) + grid = images.draw_grid_annotations( + grid, res[0].width, res[0].height, hor_texts, ver_texts) first_processed.images = [grid] @@ -45,56 +47,73 @@ class Script(scripts.Script): return "Prompt matrix" def ui(self, is_img2img): - put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', value=False, elem_id=self.elem_id("put_at_start")) - different_seeds = gr.Checkbox(label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) + put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', + value=False, elem_id=self.elem_id("put_at_start")) + different_seeds = gr.Checkbox( + label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) # Radio buttons for selecting the prompt between positive and negative - prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", elem_id=self.elem_id("prompt_type"), value="positive") + prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", + elem_id=self.elem_id("prompt_type"), value="positive") + # Radio buttons for selecting the delimiter to use in the resulting prompt + variations_delimiter = gr.Radio(["comma", "space"], label="Select delimiter", elem_id=self.elem_id( + "variations_delimiter"), value="comma") + return [put_at_start, different_seeds, prompt_type, variations_delimiter] - return [put_at_start, different_seeds, prompt_type] - - def run(self, p, put_at_start, different_seeds, prompt_type): + def run(self, p, put_at_start, different_seeds, prompt_type, variations_delimiter): modules.processing.fix_seed(p) # Raise error if promp type is not positive or negative if prompt_type not in ["positive", "negative"]: raise ValueError(f"Unknown prompt type {prompt_type}") + # Raise error if variations delimiter is not comma or space + if variations_delimiter not in ["comma", "space"]: + raise ValueError( + f"Unknown variations delimiter {variations_delimiter}") prompt = p.prompt if prompt_type == "positive" else p.negative_prompt original_prompt = prompt[0] if type(prompt) == list else prompt positive_prompt = p.prompt[0] if type(p.prompt) == list else p.prompt + delimiter = ", " if variations_delimiter == "comma" else " " + all_prompts = [] prompt_matrix_parts = original_prompt.split("|") combination_count = 2 ** (len(prompt_matrix_parts) - 1) for combination_num in range(combination_count): - selected_prompts = [text.strip().strip(',') for n, text in enumerate(prompt_matrix_parts[1:]) if combination_num & (1 << n)] + selected_prompts = [text.strip().strip(',') for n, text in enumerate( + prompt_matrix_parts[1:]) if combination_num & (1 << n)] if put_at_start: selected_prompts = selected_prompts + [prompt_matrix_parts[0]] else: selected_prompts = [prompt_matrix_parts[0]] + selected_prompts - all_prompts.append(", ".join(selected_prompts)) + all_prompts.append(delimiter.join(selected_prompts)) p.n_iter = math.ceil(len(all_prompts) / p.batch_size) p.do_not_save_grid = True - print(f"Prompt matrix will create {len(all_prompts)} images using a total of {p.n_iter} batches.") + print( + f"Prompt matrix will create {len(all_prompts)} images using a total of {p.n_iter} batches.") if prompt_type == "positive": p.prompt = all_prompts else: p.negative_prompt = all_prompts - p.seed = [p.seed + (i if different_seeds else 0) for i in range(len(all_prompts))] + p.seed = [p.seed + (i if different_seeds else 0) + for i in range(len(all_prompts))] p.prompt_for_display = positive_prompt processed = process_images(p) - grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2)) - grid = images.draw_prompt_matrix(grid, p.width, p.height, prompt_matrix_parts) + grid = images.image_grid(processed.images, p.batch_size, rows=1 << ( + (len(prompt_matrix_parts) - 1) // 2)) + grid = images.draw_prompt_matrix( + grid, p.width, p.height, prompt_matrix_parts) processed.images.insert(0, grid) processed.index_of_first_image = 1 processed.infotexts.insert(0, processed.infotexts[0]) if opts.grid_save: - images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", extension=opts.grid_format, prompt=original_prompt, seed=processed.seed, grid=True, p=p) + images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", + extension=opts.grid_format, prompt=original_prompt, seed=processed.seed, grid=True, p=p) return processed From 5997457fd48c9c0fc31c9d96bf0b9c217585c526 Mon Sep 17 00:00:00 2001 From: Yevhenii Hurin Date: Sun, 29 Jan 2023 16:23:29 +0200 Subject: [PATCH 3/4] Compact options UI for Prompt Matrix --- scripts/prompt_matrix.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py index 89db9e63..03212b31 100644 --- a/scripts/prompt_matrix.py +++ b/scripts/prompt_matrix.py @@ -47,16 +47,23 @@ class Script(scripts.Script): return "Prompt matrix" def ui(self, is_img2img): - put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', - value=False, elem_id=self.elem_id("put_at_start")) - different_seeds = gr.Checkbox( - label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) - # Radio buttons for selecting the prompt between positive and negative - prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", - elem_id=self.elem_id("prompt_type"), value="positive") - # Radio buttons for selecting the delimiter to use in the resulting prompt - variations_delimiter = gr.Radio(["comma", "space"], label="Select delimiter", elem_id=self.elem_id( - "variations_delimiter"), value="comma") + gr.HTML('
') + with gr.Row(): + with gr.Column(): + put_at_start = gr.Checkbox(label='Put variable parts at start of prompt', + value=False, elem_id=self.elem_id("put_at_start")) + with gr.Column(): + # Radio buttons for selecting the prompt between positive and negative + prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", + elem_id=self.elem_id("prompt_type"), value="positive") + with gr.Row(): + with gr.Column(): + different_seeds = gr.Checkbox( + label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds")) + with gr.Column(): + # Radio buttons for selecting the delimiter to use in the resulting prompt + variations_delimiter = gr.Radio(["comma", "space"], label="Select delimiter", elem_id=self.elem_id( + "variations_delimiter"), value="comma") return [put_at_start, different_seeds, prompt_type, variations_delimiter] def run(self, p, put_at_start, different_seeds, prompt_type, variations_delimiter): From 1e2b10d2dcdf41a6cce0c525c85ebd42a521e0f1 Mon Sep 17 00:00:00 2001 From: Yevhenii Hurin Date: Sun, 29 Jan 2023 17:14:46 +0200 Subject: [PATCH 4/4] Cleanup changes made by formatter --- scripts/prompt_matrix.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/scripts/prompt_matrix.py b/scripts/prompt_matrix.py index 03212b31..de921ea8 100644 --- a/scripts/prompt_matrix.py +++ b/scripts/prompt_matrix.py @@ -10,7 +10,6 @@ from modules import images from modules.processing import process_images, Processed from modules.shared import opts, cmd_opts, state import modules.sd_samplers -from pprint import pprint def draw_xy_grid(xs, ys, x_label, y_label, cell): @@ -34,8 +33,7 @@ def draw_xy_grid(xs, ys, x_label, y_label, cell): res.append(processed.images[0]) grid = images.image_grid(res, rows=len(ys)) - grid = images.draw_grid_annotations( - grid, res[0].width, res[0].height, hor_texts, ver_texts) + grid = images.draw_grid_annotations(grid, res[0].width, res[0].height, hor_texts, ver_texts) first_processed.images = [grid] @@ -73,8 +71,7 @@ class Script(scripts.Script): raise ValueError(f"Unknown prompt type {prompt_type}") # Raise error if variations delimiter is not comma or space if variations_delimiter not in ["comma", "space"]: - raise ValueError( - f"Unknown variations delimiter {variations_delimiter}") + raise ValueError(f"Unknown variations delimiter {variations_delimiter}") prompt = p.prompt if prompt_type == "positive" else p.negative_prompt original_prompt = prompt[0] if type(prompt) == list else prompt @@ -86,8 +83,7 @@ class Script(scripts.Script): prompt_matrix_parts = original_prompt.split("|") combination_count = 2 ** (len(prompt_matrix_parts) - 1) for combination_num in range(combination_count): - selected_prompts = [text.strip().strip(',') for n, text in enumerate( - prompt_matrix_parts[1:]) if combination_num & (1 << n)] + selected_prompts = [text.strip().strip(',') for n, text in enumerate(prompt_matrix_parts[1:]) if combination_num & (1 << n)] if put_at_start: selected_prompts = selected_prompts + [prompt_matrix_parts[0]] @@ -99,28 +95,23 @@ class Script(scripts.Script): p.n_iter = math.ceil(len(all_prompts) / p.batch_size) p.do_not_save_grid = True - print( - f"Prompt matrix will create {len(all_prompts)} images using a total of {p.n_iter} batches.") + print(f"Prompt matrix will create {len(all_prompts)} images using a total of {p.n_iter} batches.") if prompt_type == "positive": p.prompt = all_prompts else: p.negative_prompt = all_prompts - p.seed = [p.seed + (i if different_seeds else 0) - for i in range(len(all_prompts))] + p.seed = [p.seed + (i if different_seeds else 0) for i in range(len(all_prompts))] p.prompt_for_display = positive_prompt processed = process_images(p) - grid = images.image_grid(processed.images, p.batch_size, rows=1 << ( - (len(prompt_matrix_parts) - 1) // 2)) - grid = images.draw_prompt_matrix( - grid, p.width, p.height, prompt_matrix_parts) + grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2)) + grid = images.draw_prompt_matrix(grid, p.width, p.height, prompt_matrix_parts) processed.images.insert(0, grid) processed.index_of_first_image = 1 processed.infotexts.insert(0, processed.infotexts[0]) if opts.grid_save: - images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", - extension=opts.grid_format, prompt=original_prompt, seed=processed.seed, grid=True, p=p) + images.save_image(processed.images[0], p.outpath_grids, "prompt_matrix", extension=opts.grid_format, prompt=original_prompt, seed=processed.seed, grid=True, p=p) return processed