Expand a prompt matrix strings into a list of prompts.
Project description
Prompt Matrix
A Python package to expand prompt matrix strings, e.g. the string "The <dog|cat> in the hat"
expands to the list ["The dog in the hat", "The cat in the hat"]
.
The motivating use case for this package is the comparison of different prompts in text and image generation systems such as Stable Diffusion and GPT-3.
A prompt string may contain multiple alternations. For example, "The <dog|cat> in the <cardigan|hat>"
produces a list of the four strings "The dog in the cardigan"
, "The dog in the hat"
, "The cat in the cardigan"
, and "The cat in the hat"
.
A prompt string may also contain nested alternations. For example, "The <<small|large> dog|cat>"
produces the strings "The small dog"
, "The large do"
, and "The cat"
.
Note: The disjunction is bounded by the enclosing brackets, if any.
"The dog|cat in the cardigan|hat"
generates the three strings"The dog"
,"cat in the gardigan"
, and"hat"
. This is in constrat to some other systems, that scope a disjunction to the text delimited by surrounding whitespace.
Install
$ pip install prompt-matrix
Usage
import prompt_matrix
prompt_matrix.expand("The <dog|cat> in the hat")
# ->
# ["The dog in the hat",
# "The cat in the hat"]
prompt_matrix.expand("The <dog|cat> in the <cardigan|hat>")
# ->
# ["The dog in the cardigan",
# "The dog in the hat",
# "The cat in the cardigan",
# "The cat in the hat"]
prompt_matrix.expand("The <<small|large> <brown|black> dog|<red|blue> fish>")
# ->
# ["The small brown dog",
# "The small black dog",
# "The large brown dog",
# "The large black dog",
# "The red fish",
# "The blue fish"]
prompt_matrix.expand("The {dog,cat} in the {cardigan,hat}",
brackets=['{', '}'], alt=',')
# ->
# ["The dog in the cardigan",
# "The dog in the hat",
# "The cat in the cardigan",
# "The cat in the hat"]
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for prompt_matrix-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c2cfbe0e2e98e4dd64c49fe7856d9d85a0a91e6f80257ade5c7309a24cf04b3 |
|
MD5 | 00c118fe18933904d8dec1c655a8ba02 |
|
BLAKE2b-256 | 2c4ed42fcbaf0ac5b5b606d1e47ae2b77dc67cdbfa36a27802671b575054ca23 |