Package: dynamicprompts
Dynamic Prompts--Dynamic prompts is a Python library that provides developers with aflexible and intuitive templating languageandtoolsfor generating prompts for text-to-image generators like Stable Diffusion, MidJourney or Dall-e 2. It lets you create and managesophisticated prompt generation workflowsthat seamlessly integrate with your existing text-to-image generation pipelines.It includes:An easy-to-learn templating language that lets you create a number of unique prompts from a single template.Support for wildcard files as placeholders in templates.A mechanism for creating a wildcard library. Text, JSON, and YAML files are supported.Exhaustive generation of all prompts from a template.Variable assignment enabling re-usable prompt snippets.Supports Magic Prompt which automatically spices up your prompt with modifiersProvides an I'm Feeling Lucky feature which uses the semantic search on Lexica.art to find similar prompts.For systems that support attention syntax, Attention Grabber will emphasis random phrases in your prompt.Jinja-powered templating for advanced prompt creation.The dynamic prompts library powers theDynamic Promptsextension for Automatic1111.Table of contentsDynamic PromptsQuick overview of the templating language:VariantsChoosing multiple variantsWildcardsLet's try a real-world promptUse whitespace for readabilityInstallationQuick StartCombinatorial GenerationMagic PromptI'm feeling luckyAttention GeneratorJinja2 templatesTemplate syntaxSyntax customisationWildcard CollectionsDynamic Prompts in the wild.Quick overview of the templating language:Variants{summer|autumn|winter|spring} is comingRandomly generate one of:summer is coming
autumn is coming
winter is coming
spring is comingChoosing multiple variantsThis syntax{2$$ and $$A|B|C}will choose two values from the list:A and B
A and C
B and A
B and C
C and A
C and BWildcards__season__ is comingRandomly selects a value from season.txt in your wildcard directory.Let's try a real-world promptOne prompt template can generate a family of prompts:Funko pop {yoda|darth vader|jabba the hutt|princess leia|chewbacca|luke skywalker} figurine, made of {wood|plastic|metal|stone}, product studio shot, on a white background, diffused lighting, centeredNow, how about two characters at the same time:Funko pop {2$$ and $$yoda|darth vader|jabba the hutt|princess leia|chewbacca|luke skywalker} figurine, made of {wood|plastic|metal|stone}, product studio shot, on a white background, diffused lighting, centeredUse whitespace for readability# Add comments like this
Funko pop
    {2$$ and $$
  	    yoda
		|darth vader
		|jabba the hutt
		|princess leia
		|chewbacca
		|luke skywalker
	}
	figurine, made of
	{
		wood
		|plastic
		|metal
		|stone
	}, product studio shot, on a white background, diffused lighting, centeredUse wildcards for re-usable lists:# starwars.txt
yoda
darth vader
jabba the hutt
princess leia
chewbacca
luke skywalker# material.txt
wood
plastic
metal
stone# studio-shot.txt
product studio shot, on a white background, diffused lighting, centeredNow compose your prompt like this:Funko pop __starwars__ figurine, made of __material__, __studio-shot__and easily change it to:Funko pop __celebrities__ figurine, made of __material__, __studio-shot__Hat tip topublicpromptsfor the funko pop prompt.The complete syntax can be foundhere.Installationpip install dynamicpromptsAdditional functionality (see below) can be installed with this command:pip install "dynamicprompts[magicprompt, attentiongrabber]"Quick StartUse the RandomPromptGenerator to create 5 random prompts using a given template:fromdynamicprompts.generatorsimportRandomPromptGeneratorgenerator=RandomPromptGenerator()generator.generate("I love {red|green|blue} roses",num_images=5)>>['I love blue roses','I love red roses','I love green roses','I love red roses','I love red roses']If you want to use wildcards, instantiate a WildcardManager:frompathlibimportPathfromdynamicprompts.generatorsimportRandomPromptGeneratorfromdynamicprompts.wildcards.wildcard_managerimportWildcardManagerwm=WildcardManager(Path("/path/to/wildcard/directory"))generator=RandomPromptGenerator(wildcard_manager=wm)Assuming you have a file called colours.txt in /path/to/wildcards/directory with one colour per line, e.g.red
green
blue
purple
yellowthengenerator.generate("I love __colours__ roses",num_prompts)>>['I love pink roses','I love violet roses','I love white roses','I love violet roses','I love blue roses']Random seedsYou can pass a random seed in the constructor for predictable outputsgenerator = RandomPromptGenerator(wildcard_manager=wm, seed=999)A list of seeds can also be provided in thegeneratemethod.generator.generate("I love __colours__ roses", num_prompts, seeds=[1,2,3])In this example, a seed is provided for each prompt generated. The number of seeds must equal the number of prompts, i.e.len(seeds) == num_prompts. Iflen(seeds) == 1then the same seed is used for every image.As a convenience,seedsmay also be an int value.generator.generate("I love __colours__ roses", num_prompts=3, seeds=5)is equivalent to:generator.generate("I love __colours__ roses", num_prompts=3, seeds=[5, 5, 5])Combinatorial GenerationInstead of generating random prompts from a template, combinatorial generation produces every possible prompt from the given string. For example:I {love|hate} {New York|Chicago} in {June|July|August}will produce:I love New York in JuneI love New York in JulyI love New York in AugustI love Chicago in JuneI love Chicago in JulyI love Chicago in AugustI hate New York in JuneI hate New York in JulyI hate New York in AugustI hate Chicago in JuneI hate Chicago in JulyI hate Chicago in AugustIf a__wildcard__is provided, then a new prompt will be produced for every value in the wildcard file. For example:My favourite season is __seasons__will produce:My favourite season is SummerMy favourite season is AugustMy favourite season is WinterMy favourite season is SprintUsagefromdynamicprompts.generatorsimportCombinatorialPromptGeneratorgenerator=CombinatorialPromptGenerator()generator.generate("I love {red|green|blue} roses",max_prompts=5)>>['I love red roses','I love green roses','I love blue roses']Notice that only 3 prompts were generated, even though we requested 5. Since there are only three options, i.e. red, green, and blue, only 3 unique prompts can be created.num_promptsin this case acts as an upper bound. Combinatorial generation can very quickly produce many more prompts than you intended.num_promptsis a safeguard to ensure that you don't accidentally produced thousands or tens of thousands of prompts.Consider this template:My favourite colours are __colours__, __colours__, and __colours__If colours.txt contains 10 different colours, a combinatorial enumeration of that template will create10 * 10 * 10 = 1000different prompts. e.g.My favourite colours are red, green, and blueMy favourite colours are red, green, and yellowMy favourite colours are red, green, and purpleMy favourite colours are red, blue, and yellowMy favourite colours are red, blue, and purple...Magic PromptUsingGustavosta's MagicPrompt model, automatically generate new prompts from the input. Trained on 80,000 prompts fromLexica.art, it can help give you interesting new prompts on a given subject. Here are some automatically generated variations for "dogs playing football":dogs playing football, in the streets of a japanese town at night, with people watching in wonder, in the style of studio ghibli and makoto shinkai, highly detailed digital art, trending on artstationdogs playing football, in the background is a nuclear explosion. photorealism. hq. hyper. realistic. 4 k. award winning.dogs playing football, in the background is a nuclear explosion. photorealistic. realism. 4 k wideshot. cinematic. unreal engine. artgerm. marc simonetti. jc leyendeckerThis is compatible with the wildcard syntax described above.Usagefromdynamicprompts.generatorsimportRandomPromptGeneratorfromdynamicprompts.generators.magicpromptimportMagicPromptGeneratorgenerator=RandomPromptGenerator()magic_generator=MagicPromptGenerator(generator,device=...,# Torch device specifier (int, string, torch.device))num_prompts=5generator.generate("I love {red|green|blue} roses",num_prompts)>>['I love red roses trending on artstation #vividart #pixiv','I love red roses trending on artstation artwork','I love blue roses breakfast club, cute, intricate, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, unreal engine 5, 8 k, art by artgerm and greg rutkowski and alphonse mucha','I love green roses I love green flowers, smile, Tristan Eaton, victo ngai, artgerm, RHADS, ross draws','I love red roses smile, Tristan Eaton, victo ngai, artgerm, RHADS, ross draws']The first time you use it, the model is downloaded. It is approximately 500mb and so will take some time depending on how fast your connection is. It will also take a few seconds on first activation as the model is loaded into memory. Note, if you're low in VRAM, you might get a Cuda error. My GPU uses less than 8GB by YMMV.Magic Prompt is not available by default, you need to install it as follows:pip install "dynamicprompts[magicprompt]"Other modelsThere a few alternatives to Gustavosta's model available. You can try:magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-lexart")
magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-majinai-safe")
magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-majinai-unsafe")You can find a longer listhereNote that each model requires a download of large model files.I'm feeling luckyUse thelexica.artAPI to create random prompts. Useful if you're looking for inspiration, or are simply too lazy to think of your own prompts. When this option is selected, the template is used as a search string. For example, prompt "Mech warrior" might return:A large robot stone statue in the middle of a forest by Greg Rutkowski, Sung Choi, Mitchell Mohrhauser, Maciej Kuciara, Johnson Ting, Maxim Verehin, Peter Konig, final fantasy , 8k photorealistic, cinematic lighting, HD, high details, atmospheric,a beautiful portrait painting of a ( ( ( cyberpunk ) ) ) armor by simon stalenhag and pascal blanche and alphonse mucha and nekro. in style of digital art. colorful comic, film noirs, symmetry, brush stroke, vibrating colors, hyper detailed. octane render. trending on artstationsymmetry!! portrait of a robot astronaut, floral! horizon zero dawn machine, intricate, elegant, highly detailed, digital painting, artstation, concept art, smooth, sharp focus, illustration, art by artgerm and greg rutkowski and alphonse mucha, 8 kUsagefromdynamicprompts.generatorsimportRandomPromptGeneratorfromdynamicprompts.generators.feelingluckyimportFeelingLuckyGeneratorgenerator=RandomPromptGenerator()lucky_generator=FeelingLuckyGenerator(generator)num_prompts=5lucky_generator.generate("I love {red|green|blue} roses",num_prompts)>>['“ guns and roses ” ','🌹🥀🏜. 🌌🌠⭐. 💯. ','tattoo design, stencil, beautiful japanese girls face, roses and ivy surrounding by artgerm, artgerm, cat girl, anime ','rose made of glass dramatic lighting','a wireframe render of a red rose']Attention GeneratorIf you are usingAutomatic1111or a similar frontend to Stable Diffusion that uses attention syntax, e.g.(some text:1.4), AttentionGenerator will randomly add attention to various phrases in your prompt. This injects a small amount of randomness into your prompt.Usagefromdynamicprompts.generatorsimportRandomPromptGeneratorfromdynamicprompts.generators.attentiongeneratorimportAttentionGeneratorgenerator=RandomPromptGenerator()attention_generator=AttentionGenerator(generator)prompt="a portrait an anthropomorphic panda mage casting a spell, wearing mage robes, landscape in background, cute, dnd character art portrait, by jason felix and peter mohrbacher, cinematic lighting"attention_generator.generate(prompt,num_prompts=1)>>['a portrait an anthropomorphic panda mage casting a spell, wearing (mage robes:0.77), landscape in background, cute, dnd character art portrait, by jason felix and peter mohrbacher, cinematic lighting']You may get better results with AttentionGenerator by installing thespacyNLP library. Use this command to install it:pip install spacyWhen Spacy is available, an NLP model will automatically be downloaded on first use.Jinja2 templatesIf the standard template language is not sufficient for your needs, you can try the Jinja2 generator. Jinja2 templates have familiar programming constructs such as looping, conditionals, variables, etc. Youcan find a guide on using Jinja2 templates with Dynamic Prompts,here. Here is the minimal code you need to instantiate Jinja2 generator.fromdynamicprompts.generatorsimportJinjaGeneratorgenerator=JinjaGenerator()generator.generate("I love {red|green|blue} roses",num_images=5)template="""{% for colour in ['red', 'blue', 'green'] %}{% prompt %}I love {{ colour }} roses{% endprompt %}{% endfor %}"""generator.generate(template)>>['I love red roses','I love blue roses','I love green roses']Template syntaxYou can find the complete syntax guidehereSyntax customisationTo address potential syntax clashes with other tools it is possible to change various tokens. Instead of{red|green|blue}you can configure the library to use the<>pair instead, e.g.<red|green|blue>. You can also change the__used in wildcards. So instead of__colours__, you can configure wildcards to use**, e.g.**colours**fromdynamicprompts.generatorsimportRandomPromptGeneratorfromdynamicprompts.parser.configimportParserConfigparser_config=ParserConfig(variant_start="<",variant_end=">",wildcard_wrap="**")generator=RandomPromptGenerator(parser_config=parser_config)Wildcard CollectionsYou can bootstrap your wildcard library by using ourpre-existing collections. You'll find just under 80,000 wildcards divided into 1900 files. Feel free to pick and choose or take them in their entirety.Dynamic Prompts in the wild.Dynamic Prompts has been used in:SD Dynamic PromptsAuto1111 extensionDeforum 0.7colabComfyUI Nodes