为Stable Diffusion提供的动态提示模板库
项目描述
Dynamic Prompts
动态提示是一个Python库,为开发者提供了强大的模板语言和工具,用于生成文本到图像生成器(如Stable Diffusion、MidJourney或Dall-e 2)的提示。它允许您创建和管理复杂的提示生成工作流,这些工作流可以无缝集成到现有的文本到图像生成管道中。
它包括
- 一个易于学习的模板语言,让您从一个模板中创建多个独特的提示。
- 支持在模板中使用通配符文件作为占位符。
- 创建通配符库的机制。支持文本、JSON和YAML文件。
- 从模板中生成所有可能的提示。
- 变量分配,允许重用提示片段。
- 支持魔法提示,可自动用修饰符丰富您的提示。
- 提供“碰碰运气”功能,使用Lexica.art的语义搜索来查找相似的提示。
- 对于支持注意力语法的系统,注意力捕获器会在您的提示中强调随机短语。
- 使用Jinja的高级提示创建。
动态提示库为Automatic1111的动态提示扩展提供支持。
目录
模板语言快速概述
变体
{summer|autumn|winter|spring} is coming
随机生成一个
summer is coming
autumn is coming
winter is coming
spring is coming
选择多个变体
此语法{2$$ 和 $$A|B|C}
将选择列表中的两个值
A and B
A and C
B and A
B and C
C and A
C and B
通配符
__season__ is coming
从您的通配符目录中的season.txt随机选择一个值。
让我们尝试一个真实的提示
一个提示模板可以生成一系列提示
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, centered
现在,同时生成两个角色怎么样
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, centered
使用空白以提高可读性
# 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, centered
使用通配符为可重用列表
# 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, centered
现在像这样编写您的提示
Funko pop __starwars__ figurine, made of __material__, __studio-shot__
然后轻松地将其更改为
Funko pop __celebrities__ figurine, made of __material__, __studio-shot__
感谢publicprompts提供的funktôp pop提示。
完整的语法可以在这里找到。
安装
pip install dynamicprompts
可以使用以下命令安装附加功能(见下文)
pip install "dynamicprompts[magicprompt, attentiongrabber]"
快速入门
使用RandomPromptGenerator使用给定的模板创建5个随机提示
from dynamicprompts.generators import RandomPromptGenerator
generator = 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']
如果您想使用通配符,请实例化WildcardManager
from pathlib import Path
from dynamicprompts.generators import RandomPromptGenerator
from dynamicprompts.wildcards.wildcard_manager import WildcardManager
wm = WildcardManager(Path("/path/to/wildcard/directory"))
generator = RandomPromptGenerator(wildcard_manager=wm)
假设您有一个名为colours.txt的文件在/path/to/wildcards/directory中,每行一个颜色,例如。
red
green
blue
purple
yellow
然后
generator.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']
随机种子
您可以在构造函数中传递一个随机种子以获得可预测的输出
generator = RandomPromptGenerator(wildcard_manager=wm, seed=999)
在generate
方法中也可以提供种子列表。
generator.generate("I love __colours__ roses", num_prompts, seeds=[1,2,3])
在此示例中,为每个生成的提示提供了一个种子。种子数必须等于提示数,即len(seeds) == num_prompts
。如果len(seeds) == 1
,则每个图像都使用相同的种子。
作为便利,seeds
也可以是一个int值。
generator.generate("I love __colours__ roses", num_prompts=3, seeds=5)
是等效的
generator.generate("I love __colours__ roses", num_prompts=3, seeds=[5, 5, 5])
组合生成
组合生成不是从模板生成随机提示,而是从给定的字符串生成所有可能的提示。例如
I {love|hate} {New York|Chicago} in {June|July|August}
将产生
I love New York in June
I love New York in July
I love New York in August
I love Chicago in June
I love Chicago in July
I love Chicago in August
I hate New York in June
I hate New York in July
I hate New York in August
I hate Chicago in June
I hate Chicago in July
I hate Chicago in August
如果提供了__wildcard__
,则将为通配符文件中的每个值生成一个新的提示。例如
我最喜欢的季节是 __seasons__
将产生
我最喜欢的季节是夏季
我最喜欢的季节是八月
我最喜欢的季节是冬季
我最喜欢的季节是春季
用法
from dynamicprompts.generators import CombinatorialPromptGenerator
generator = CombinatorialPromptGenerator()
generator.generate("I love {red|green|blue} roses", max_prompts=5)
>> ['I love red roses', 'I love green roses', 'I love blue roses']
请注意,尽管我们请求了5个提示,但实际上只生成了3个。由于只有红色、绿色和蓝色三种选项,因此只能创建3个独特的提示。num_prompts
在这种情况下充当上限。组合生成可以非常快速地产生比预期更多的提示。num_prompts
是一个保护措施,以确保您不会意外地产生成千上万的提示。
考虑这个模板
我最喜欢的颜色是 __colours__、__colours__ 和 __colours__
如果colours.txt包含10种不同的颜色,该模板的组合枚举将创建10 * 10 * 10 = 1000
个不同的提示。例如:
我最喜欢的颜色是红色、绿色和蓝色
我最喜欢的颜色是红色、绿色和黄色
我最喜欢的颜色是红色、绿色和紫色
我最喜欢的颜色是红色、蓝色和黄色
我最喜欢的颜色是红色、蓝色和紫色
...
魔法提示
使用Gustavosta的MagicPrompt模型,从输入中自动生成新的提示。在Lexica.art上的80,000个提示上进行训练,可以帮助您在特定主题上获得有趣的提示。以下是针对“狗踢足球”的自动生成的变体:
狗在日本的街道上夜晚踢足球,有人在旁边惊奇地观看,风格模仿吉卜力工作室和新海诚,高度详细数字艺术,在Artstation上流行
狗在背景中是核爆炸。照片级。高清。超高清。4k。获奖作品。
狗在背景中是核爆炸。照片级。现实主义。4k宽画幅。电影化。虚幻引擎。artgerm。marc simonetti。jc leyendecker
这与上面描述的通配符语法兼容。
用法
from dynamicprompts.generators import RandomPromptGenerator
from dynamicprompts.generators.magicprompt import MagicPromptGenerator
generator = RandomPromptGenerator()
magic_generator = MagicPromptGenerator(
generator,
device=..., # Torch device specifier (int, string, torch.device)
)
num_prompts = 5
generator.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']
第一次使用时,模型会被下载。它大约有500MB,因此根据您的网络速度,可能需要一些时间。第一次激活时,模型被加载到内存中,也需要几秒钟。注意,如果您VRAM较低,可能会收到CUDA错误。我的GPU使用的少于8GB,具体情况可能因人而异。
默认情况下,Magic Prompt不可用,您需要按照以下步骤安装:
pip install "dynamicprompts[magicprompt]"
其他模型
有几种Gustavosta模型的替代方案可用。您可以尝试:
magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-lexart")
magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-majinai-safe")
magic_generator = MagicPromptGenerator(generator, "AUTOMATIC/promptgen-majinai-unsafe")
您可以在这里找到更长的列表。请注意,每个模型都需要下载大型模型文件。
碰碰运气
使用lexica.art API创建随机提示。如果您在寻找灵感,或者只是懒得自己想提示,这很有用。当选择此选项时,模板用作搜索字符串。例如,提示“机械战士”可能会返回:
森林中央一座大型的机器人石雕像,由Greg Rutkowski、Sung Choi、Mitchell Mohrhauser、Maciej Kuciara、Johnson Ting、Maxim Verehin、Peter Konig、final fantasy、8k照片级、电影化照明、高清、高细节、大气创作。
一幅美丽的(((赛博朋克)))装甲肖像画,由simon stalenhag和pascal blanche和alphonse mucha和nekro创作,风格为数字艺术。彩色漫画、电影黑帮、对称、笔触、颤动色彩、超细节。octane渲染。在Artstation上流行。
对称!!机器人宇航员的肖像,花卉!地平线零曙光机器,复杂、优雅、高度详细、数字绘画、Artstation、概念艺术、平滑、锐利聚焦、插画,艺术由artgerm和greg rutkowski和alphonse mucha创作,8k
用法
from dynamicprompts.generators import RandomPromptGenerator
from dynamicprompts.generators.feelinglucky import FeelingLuckyGenerator
generator = RandomPromptGenerator()
lucky_generator = FeelingLuckyGenerator(generator)
num_prompts = 5
lucky_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']
注意力生成器
如果您正在使用 Automatic1111 或类似的 Stable Diffusion 前端,并且使用注意力语法,例如 (some text:1.4)
,AttentionGenerator 将会随机为您的提示中的各种短语添加注意力。这会在您的提示中注入少量随机性。
用法
from dynamicprompts.generators import RandomPromptGenerator
from dynamicprompts.generators.attentiongenerator import AttentionGenerator
generator = 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']
通过安装 spacy
NLP 库,您可以使用 AttentionGenerator 获得更好的结果。使用以下命令安装它:
pip install spacy
当 Spacy 可用时,在首次使用时将自动下载 NLP 模型。
Jinja2模板
如果标准模板语言不能满足您的需求,您可以尝试使用 Jinja2 生成器。Jinja2 模板具有循环、条件、变量等熟悉的编程结构。您可以在 这里 找到有关使用 Jinja2 模板与 Dynamic Prompts 的指南。以下是您需要实例化 Jinja2 生成器的最小代码。
from dynamicprompts.generators import JinjaGenerator
generator = 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']
模板语法
您可以在 这里 找到完整的语法指南。
语法自定义
为了解决与其他工具可能存在的潜在语法冲突,您可以选择更改各种标记。例如,您可以将 {red|green|blue}
配置为使用 <
>
对,例如 <red|green|blue>
。您还可以更改用于通配符的 __
。因此,您可以将 __colours__
配置为使用 **
,例如 **colours**
。
from dynamicprompts.generators import RandomPromptGenerator
from dynamicprompts.parser.config import ParserConfig
parser_config = ParserConfig(variant_start="<", variant_end=">", wildcard_wrap="**")
generator = RandomPromptGenerator(parser_config=parser_config)
通配符集合
您可以通过使用我们提供的 现有集合 来引导您的通配符库。您会发现大约有 80,000 个通配符,分为 1900 个文件。您可以自由选择或全部使用。
野外的动态提示
Dynamic Prompts 已被用于
- SD Dynamic Prompts Auto1111 扩展
- Deforum 0.7 colab
- ComfyUI 节点
项目详情
下载文件
下载适用于您平台的应用程序。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码分发
构建分发
dynamicprompts-0.31.0-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a07f38c295ec2b77905cecba8b0f439bb1a84942bfb6874ff6b55448e2cc950e |
|
MD5 | 7375c012a2cf1032330980baa4bf8a83 |
|
BLAKE2b-256 | 64f0dbe05efee6a38fb075ba0995e497223d02c6d056303d5e8881e9bb20652a |