A quick script for creating prompt permutations for StableDiffusion

Author:

So, I enjoy making stuff in StableDiffusion and in the WebUI interface is an option for a prompt list. I like using the Prompt S/R in the scripts but it tends to top out after about 1500 permutations. Here is a script for generating those in as many dimensions as you want.

import itertools

def load_file(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
    return [line.strip() for line in lines]

def generate_permutations(prompt, modifiers):
    # Create all combinations of modifiers
    all_combinations = list(itertools.product(*modifiers))
    
    permutations = []
    for combination in all_combinations:
        new_prompt = prompt
        for original, replacement in zip(modifiers, combination):
            new_prompt = replace_first(new_prompt, original[0], replacement)
        permutations.append(new_prompt)
    
    return permutations

def replace_first(text, search, replacement):
    # Helper function to replace only the first occurrence of a term
    if search not in text:
        raise ValueError(f"Term '{search}' not found in the prompt.")
    return text.replace(search, replacement, 1)

def save_to_file(output_path, permutations):
    with open(output_path, 'w') as file:
        for permutation in permutations:
            file.write(permutation + '\n')

def main(input_file_path, output_file_path):
    lines = load_file(input_file_path)
    if not lines:
        print("The input file is empty.")
        return

    prompt = lines[0]
    modifiers = [line.split(', ') for line in lines[1:]]

    try:
        all_permutations = generate_permutations(prompt, modifiers)
        save_to_file(output_file_path, all_permutations)
        print(f"Generated prompts have been saved to {output_file_path}")
        print(f"Total number of permutations: {len(all_permutations)}")
    except ValueError as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    input_file_path = 'testprompt.txt'  # Change this to the path of your input file
    output_file_path = 'output.txt'  # Change this to the desired path for the output file
    main(input_file_path, output_file_path)

This Python script will allow you to generate a permutation of a prompt based on the original words that are in the prompt. So, for example, here is the prompt file with the first line being the raw prompt and the following lines being the search and replace terms:

A beautiful flower sitting on a wooden table. in a kitchen
beautiful flower, tall woman, eager beaver, soup can
sitting, jumping, glowering

This will generate the following in output.txt

A beautiful flower sitting on a wooden table. in a kitchen
A beautiful flower jumping on a wooden table. in a kitchen
A beautiful flower glowering on a wooden table. in a kitchen
A tall woman sitting on a wooden table. in a kitchen
A tall woman jumping on a wooden table. in a kitchen
A tall woman glowering on a wooden table. in a kitchen
A eager beaver sitting on a wooden table. in a kitchen
A eager beaver jumping on a wooden table. in a kitchen
A eager beaver glowering on a wooden table. in a kitchen
A soup can sitting on a wooden table. in a kitchen
A soup can jumping on a wooden table. in a kitchen
A soup can glowering on a wooden table. in a kitchen

So, the strings “Beautiful flower” and “sitting” and the following terms are replaced in the original prompt. So, in this case you have 4 terms in the first term line (line 2) and 3 terms in the second term line so, 4 time 3 is 12. All 12 permutations of that prompt are outputted to a file. It will tell you the total number of permutations and throw an error when a term isn’t found.

I make no guarantees about this script, enjoy anyways.