Skip to main content

A Python library for parsing and processing prompts with support for embedding and tokenization.

Project description

Diffusion Prompt Embedder

PyPI version Python Version License: MIT Code Coverage

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

MIT

Author

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

diffusion_prompt_embedder-0.4.0.tar.gz (67.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

diffusion_prompt_embedder-0.4.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file diffusion_prompt_embedder-0.4.0.tar.gz.

File metadata

  • Download URL: diffusion_prompt_embedder-0.4.0.tar.gz
  • Upload date:
  • Size: 67.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for diffusion_prompt_embedder-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c4eca0f6c4482f82d6cb4a54b7da1ac5e4702e18513f3ad3680f92dc58d7d542
MD5 1710f03297d7f97fda5d58acba5fd2c1
BLAKE2b-256 108aa1f99b959e93eaaf6464d3999a21724213f04aa84d84042c534bf0303a1b

See more details on using hashes here.

File details

Details for the file diffusion_prompt_embedder-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: diffusion_prompt_embedder-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for diffusion_prompt_embedder-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db442f89a22f60e4ea2975d16e5f5d071051d0f02e805044de00007aaac59931
MD5 310bca0539749168d104f4f454b6368f
BLAKE2b-256 7466bfe6d938b515e7c5dd16ccf63990b1912f1eb6ea19264017dc1fecc040f8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page