stable-diffusion-webui/scripts/prompt_matrix.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
4.7 KiB
Python
Raw Normal View History

2022-09-03 19:32:45 +03:00
import math
import modules.scripts as scripts
import gradio as gr
from modules import images
2023-05-10 08:43:42 +03:00
from modules.processing import process_images
from modules.shared import opts, state
2022-09-03 19:32:45 +03:00
import modules.sd_samplers
def draw_xy_grid(xs, ys, x_label, y_label, cell):
res = []
ver_texts = [[images.GridAnnotation(y_label(y))] for y in ys]
hor_texts = [[images.GridAnnotation(x_label(x))] for x in xs]
2022-12-15 05:01:32 +03:00
first_processed = None
2022-09-03 19:32:45 +03:00
state.job_count = len(xs) * len(ys)
2022-09-03 19:32:45 +03:00
for iy, y in enumerate(ys):
for ix, x in enumerate(xs):
state.job = f"{ix + iy * len(xs) + 1} out of {len(xs) * len(ys)}"
processed = cell(x, y)
2022-12-15 05:01:32 +03:00
if first_processed is None:
first_processed = processed
2022-09-03 19:32:45 +03:00
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)
2022-12-15 05:01:32 +03:00
first_processed.images = [grid]
2022-09-03 19:32:45 +03:00
2022-12-15 05:01:32 +03:00
return first_processed
2022-09-03 19:32:45 +03:00
class Script(scripts.Script):
def title(self):
return "Prompt matrix"
def ui(self, is_img2img):
2023-01-29 17:23:29 +03:00
gr.HTML('<br />')
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"))
different_seeds = gr.Checkbox(label='Use different seed for each picture', value=False, elem_id=self.elem_id("different_seeds"))
2023-01-29 17:23:29 +03:00
with gr.Column():
prompt_type = gr.Radio(["positive", "negative"], label="Select prompt", elem_id=self.elem_id("prompt_type"), value="positive")
variations_delimiter = gr.Radio(["comma", "space"], label="Select joining char", elem_id=self.elem_id("variations_delimiter"), value="comma")
2023-01-29 17:23:29 +03:00
with gr.Column():
margin_size = gr.Slider(label="Grid margins (px)", minimum=0, maximum=500, value=0, step=2, elem_id=self.elem_id("margin_size"))
return [put_at_start, different_seeds, prompt_type, variations_delimiter, margin_size]
def run(self, p, put_at_start, different_seeds, prompt_type, variations_delimiter, margin_size):
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"]:
2023-01-29 18:14:46 +03:00
raise ValueError(f"Unknown variations delimiter {variations_delimiter}")
2022-09-03 19:32:45 +03:00
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
2022-09-03 19:32:45 +03:00
delimiter = ", " if variations_delimiter == "comma" else " "
2022-09-03 19:32:45 +03:00
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)]
if put_at_start:
selected_prompts = selected_prompts + [prompt_matrix_parts[0]]
else:
selected_prompts = [prompt_matrix_parts[0]] + selected_prompts
all_prompts.append(delimiter.join(selected_prompts))
2022-09-03 19:32:45 +03:00
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.")
if prompt_type == "positive":
p.prompt = all_prompts
else:
p.negative_prompt = all_prompts
2022-12-10 11:20:43 +03:00
p.seed = [p.seed + (i if different_seeds else 0) for i in range(len(all_prompts))]
p.prompt_for_display = positive_prompt
2022-09-03 19:32:45 +03:00
processed = process_images(p)
grid = images.image_grid(processed.images, p.batch_size, rows=1 << ((len(prompt_matrix_parts) - 1) // 2))
2023-02-22 20:55:07 +03:00
grid = images.draw_prompt_matrix(grid, processed.images[0].width, processed.images[0].height, prompt_matrix_parts, margin_size)
2022-09-03 19:32:45 +03:00
processed.images.insert(0, grid)
processed.index_of_first_image = 1
processed.infotexts.insert(0, processed.infotexts[0])
2022-09-03 19:32:45 +03:00
2022-09-04 03:38:24 +03:00
if opts.grid_save:
2022-11-06 05:12:57 +03:00
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)
2022-09-04 03:38:24 +03:00
2022-09-03 19:32:45 +03:00
return processed