Dynamic Prompts

0.31.0 · active · verified Thu Apr 16

Dynamic Prompts is a Python library designed for creating flexible and expressive prompt templates for text-to-image generators like Stable Diffusion, MidJourney, or Dall-e 2. It enables users to generate a multitude of unique prompts from a single template using features such as variants, wildcards, and combinatorial generation. The library is actively maintained, with its current version being 0.31.0, and receives regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `RandomPromptGenerator` and `WildcardManager` to create dynamic prompts. It uses both inline variants (`{option1|option2}`) and a simple wildcard file (`__colours__`) to show how diverse prompts can be generated from a single template. The `WILDCARD_DIR` is created locally, and a sample wildcard file is generated for immediate execution.

from pathlib import Path
from dynamicprompts.wildcards import WildcardManager
from dynamicprompts.generators import RandomPromptGenerator

# Define a directory for wildcards (optional, create if needed)
WILDCARD_DIR = Path("./wildcards")
WILDCARD_DIR.mkdir(exist_ok=True)

# Create a dummy wildcard file for demonstration
(WILDCARD_DIR / "colours.txt").write_text("red\ngreen\nblue")

# Initialize WildcardManager and RandomPromptGenerator
wm = WildcardManager(WILDCARD_DIR)
generator = RandomPromptGenerator(wildcard_manager=wm)

# Generate 5 random prompts using a template with a variant and a wildcard
template = "A {flower|tree} in __colours__ hue."
num_prompts = 5

print(f"Generating {num_prompts} prompts from template: '{template}'")
for i, prompt in enumerate(generator.generate(template, num_prompts=num_prompts)):
    print(f"Prompt {i+1}: {prompt}")

view raw JSON →