A Python library for parsing and processing prompts with support for embedding and tokenization.
Project description
Diffusion Prompt Embedder
A Python library specialized for parsing and processing weighted prompt text, supporting embedding generation and tokenization to enhance text processing for AI models like Stable Diffusion. It's compatible with SD Web UI's weighted prompts but doesn't include scheduling.
Features
- 💬 Prompt Parsing: Parse text prompts with weight markers (e.g.,
a (cat:1.5) in the garden) - 🔢 Weight Management: Support for positive weight
(text)and negative weight[text]syntax - 📚 CLIP Integration: Seamless integration with CLIP text models for embedding generation
- 🔄 Batch Processing: Efficiently process batches of multiple prompts
- 🪄 Long Text Support: Handle prompts that exceed standard CLIP context length
Installation
Install the base library using pip:
pip install diffusion-prompt-embedder
Usage Examples
Parse Weighted Prompts
from diffusion_prompt_embedder import parse_prompt_attention
# Basic parsing
result = parse_prompt_attention("a (cat:1.5) in the garden")
print(result) # [['a ', 1.0], ['cat', 1.5], [' in the garden', 1.0]]
# Using brackets to lower weight
result = parse_prompt_attention("a [cat] in the garden")
print(result) # [['a ', 1.0], ['cat', 0.9090909090909091], [' in the garden', 1.0]]
# Complex prompt example
result = parse_prompt_attention("a (((house:1.3)) [on] a (hill:0.5), sun, (((sky))).")
print(result)
Generate CLIP Embeddings
import torch
from transformers import CLIPTokenizer, CLIPTextModel
from prompt_parser import get_embeddings_sd15
# Initialize CLIP model
tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
text_encoder = CLIPTextModel.from_pretrained(
"openai/clip-vit-large-patch14",
torch_dtype=torch.float16
).to("cuda")
# Generate embeddings
prompt_embeds, neg_prompt_embeds = get_embeddings_sd15(
tokenizer=tokenizer,
text_encoder=text_encoder,
prompt="a (white:1.2) cat",
neg_prompt="blur, bad quality",
clip_skip=1 # Optional: skip layers in CLIP model
)
# Batch processing multiple prompts
from prompt_parser import get_embeddings_sd_15_batch
batch_embeds = get_embeddings_sd_15_batch(
tokenizer=tokenizer,
text_encoder=text_encoder,
prompts=["a (white:1.2) cat", "a (blue:1.4) dog", "a red bird"]
)
Prompt Syntax
Basic Weight Syntax
(text)- Increases the prompt weight by 1.1x(text:1.5)- Sets the prompt weight to 1.5[text]- Decreases the prompt weight to 1/1.1 of original\( \[ \) \]- Use backslash to escape bracket characters
BREAK Syntax
Use the BREAK keyword to create breakpoints in prompts:
result = parse_prompt_attention("text1 BREAK text2")
# Result: [["text1", 1.0], ["BREAK", -1], ["text2", 1.0]]
Development
Clone the repository and install development dependencies:
git clone https://github.com/jannchie/diffusion-prompt-parser.git
cd diffusion-prompt-parser
pip install -e ".[dev]"
Run tests:
pytest
License
Author
- Jianqi Pan (@jannchie)
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file diffusion_prompt_embedder-0.2.0.tar.gz.
File metadata
- Download URL: diffusion_prompt_embedder-0.2.0.tar.gz
- Upload date:
- Size: 60.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f55ec3905b71bfd74762970f587067ca9b7ad6eddf2032e8a8d34d47f2c4bf8
|
|
| MD5 |
9af8d19eb4b77e13fe3fc2b62aedb8da
|
|
| BLAKE2b-256 |
74b5ddcf561c46b6e7f172c4cf9677d098e1e26466f6c56c66658c4335d2e974
|
File details
Details for the file diffusion_prompt_embedder-0.2.0-py3-none-any.whl.
File metadata
- Download URL: diffusion_prompt_embedder-0.2.0-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e81ba071432aa41149d36bd8aeff50960e9e99b3f567fc5c3d5569a5a4b7c866
|
|
| MD5 |
75ef179e44da5c3c4a5a0ea3c79fc51b
|
|
| BLAKE2b-256 |
7d2fa3b142999bcb0b883b866dc9f61721c69b051ea93affa9650b6a4c9a5c45
|