tinysd 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tinysd-0.0.1/LICENSE +21 -0
- tinysd-0.0.1/PKG-INFO +205 -0
- tinysd-0.0.1/README.md +185 -0
- tinysd-0.0.1/pyproject.toml +31 -0
- tinysd-0.0.1/setup.cfg +4 -0
- tinysd-0.0.1/src/tinysd/__init__.py +31 -0
- tinysd-0.0.1/src/tinysd/generation.py +126 -0
- tinysd-0.0.1/src/tinysd/generator.py +128 -0
- tinysd-0.0.1/src/tinysd/pipeline.py +145 -0
- tinysd-0.0.1/src/tinysd/prompt.py +93 -0
- tinysd-0.0.1/src/tinysd/schedulers.py +56 -0
- tinysd-0.0.1/src/tinysd/schemas.py +37 -0
- tinysd-0.0.1/src/tinysd/sd.py +23 -0
- tinysd-0.0.1/src/tinysd/utils.py +35 -0
- tinysd-0.0.1/src/tinysd.egg-info/PKG-INFO +205 -0
- tinysd-0.0.1/src/tinysd.egg-info/SOURCES.txt +23 -0
- tinysd-0.0.1/src/tinysd.egg-info/dependency_links.txt +1 -0
- tinysd-0.0.1/src/tinysd.egg-info/requires.txt +11 -0
- tinysd-0.0.1/src/tinysd.egg-info/top_level.txt +1 -0
- tinysd-0.0.1/tests/test_generation.py +104 -0
- tinysd-0.0.1/tests/test_generator.py +87 -0
- tinysd-0.0.1/tests/test_pipeline.py +154 -0
- tinysd-0.0.1/tests/test_prompt.py +85 -0
- tinysd-0.0.1/tests/test_schedulers.py +59 -0
- tinysd-0.0.1/tests/test_utils.py +48 -0
tinysd-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kwan Yuet Stephen Ho
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
tinysd-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tinysd
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: A small wrapper around diffusers for Stable Diffusion 1.x and SDXL with A1111-style prompts.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Requires-Python: >=3.9
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: torch
|
|
10
|
+
Requires-Dist: diffusers
|
|
11
|
+
Requires-Dist: transformers
|
|
12
|
+
Requires-Dist: compel
|
|
13
|
+
Requires-Dist: safetensors
|
|
14
|
+
Requires-Dist: accelerate
|
|
15
|
+
Requires-Dist: peft
|
|
16
|
+
Requires-Dist: Pillow
|
|
17
|
+
Provides-Extra: test
|
|
18
|
+
Requires-Dist: pytest; extra == "test"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# TinySD
|
|
22
|
+
|
|
23
|
+
TinySD is a small Python wrapper around [🤗 diffusers](https://github.com/huggingface/diffusers) for
|
|
24
|
+
**Stable Diffusion 1.x** and **SDXL**. It lets you generate images with a few lines of code while keeping
|
|
25
|
+
the conveniences of the AUTOMATIC1111 (A1111) WebUI:
|
|
26
|
+
|
|
27
|
+
- **A1111 prompt syntax**: `(word:1.3)`, `[word]`, `((word))`, `BREAK`, and `<lora:...>` tags are converted
|
|
28
|
+
automatically to [Compel](https://github.com/damian0815/compel) syntax. Long prompts are not truncated.
|
|
29
|
+
- **A1111 sampler names**: `"Euler a"`, `"DPM++ 2M Karras"`, `"DPM++ 2M SDE Karras"`, `"UniPC"`, and others.
|
|
30
|
+
- **Flexible model loading**: Hugging Face Hub IDs, local diffusers folders, or single `.safetensors` / `.ckpt`
|
|
31
|
+
checkpoints (e.g. from Civitai).
|
|
32
|
+
- **Add-ons**: custom VAE, LoRA weights, textual inversion embeddings, custom text encoder/tokenizer.
|
|
33
|
+
- **CLIP skip** for SD 1.x models, and **reproducible seeds**.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
Requires Python ≥ 3.9. A CUDA GPU is strongly recommended; TinySD falls back to CPU (float32) otherwise.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
git clone <this-repo> TinySD
|
|
41
|
+
cd TinySD
|
|
42
|
+
pip install -e .
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
If you need a specific CUDA build of PyTorch, install it first (see [pytorch.org](https://pytorch.org)).
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from tinysd import SDImageGenerator
|
|
51
|
+
|
|
52
|
+
generator = SDImageGenerator(
|
|
53
|
+
base_model_id="stabilityai/stable-diffusion-xl-base-1.0",
|
|
54
|
+
lora_weight_path=None,
|
|
55
|
+
vae_weight_path=None,
|
|
56
|
+
scheduler_name="DPM++ 2M Karras",
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
result = generator.generate_images(
|
|
60
|
+
positive_prompt="a (fluffy cat:1.3) sitting on a windowsill, golden hour, ((highly detailed))",
|
|
61
|
+
negative_prompt="blurry, [low quality], watermark",
|
|
62
|
+
height=1024,
|
|
63
|
+
width=1024,
|
|
64
|
+
steps=30,
|
|
65
|
+
guidance_scale=7.0,
|
|
66
|
+
seed=42,
|
|
67
|
+
nbimages=2,
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
for i, image in enumerate(result.images):
|
|
71
|
+
image.save(f"cat_{i}.png")
|
|
72
|
+
|
|
73
|
+
print(result.seed, result.steps, result.base_model_id)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`generate_images` returns a `StableDiffusionOutput` dataclass that holds the generated PIL images and the
|
|
77
|
+
settings used to produce them (model, LoRA/VAE file names, prompts, size, steps, guidance scale, seed).
|
|
78
|
+
When `seed` is given, image `i` uses `seed + i`, so results are reproducible.
|
|
79
|
+
|
|
80
|
+
## Usage examples
|
|
81
|
+
|
|
82
|
+
### Stable Diffusion 1.5 with LoRA, VAE and CLIP skip
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from pathlib import Path
|
|
86
|
+
from tinysd import SDImageGenerator
|
|
87
|
+
|
|
88
|
+
generator = SDImageGenerator(
|
|
89
|
+
base_model_id="stable-diffusion-v1-5/stable-diffusion-v1-5", # "runwayml/..." is aliased automatically
|
|
90
|
+
lora_weight_path=Path("loras/my_style.safetensors"), # file, folder, or Hub repo ID
|
|
91
|
+
vae_weight_path="vae/vae-ft-mse-840000-ema-pruned.safetensors",
|
|
92
|
+
scheduler_name="Euler a",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
result = generator.generate_images(
|
|
96
|
+
positive_prompt="portrait of a knight, (intricate armor:1.2), BREAK dramatic lighting, castle background",
|
|
97
|
+
negative_prompt="(worst quality:1.4), lowres, bad anatomy",
|
|
98
|
+
seed=1234,
|
|
99
|
+
clip_skip=2,
|
|
100
|
+
)
|
|
101
|
+
result.images[0].save("knight.png")
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Local single-file checkpoint and textual inversion
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from tinysd import SDImageGenerator
|
|
108
|
+
|
|
109
|
+
generator = SDImageGenerator(
|
|
110
|
+
base_model_id="checkpoints/myModel_v10.safetensors", # "xl" in the name selects the SDXL pipeline
|
|
111
|
+
lora_weight_path=None,
|
|
112
|
+
vae_weight_path=None,
|
|
113
|
+
text_inversion_embedding_path="embeddings/ng_deepnegative_v1_75t.pt",
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
result = generator.generate_images(
|
|
117
|
+
positive_prompt="a cozy cabin in a snowy forest",
|
|
118
|
+
negative_prompt="ng_deepnegative_v1_75t, blurry", # embedding is registered under this token
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Lower-level API
|
|
123
|
+
|
|
124
|
+
If you want to manage the pipeline yourself, for example to change the scheduler between runs or generate
|
|
125
|
+
a batch in one call:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from tinysd import get_stable_diffusion_pipeline, generate_image, set_scheduler
|
|
129
|
+
|
|
130
|
+
pipe = get_stable_diffusion_pipeline(
|
|
131
|
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
|
132
|
+
lora_weights_path=None,
|
|
133
|
+
vae_input_path=None,
|
|
134
|
+
cuda=True,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
set_scheduler(pipe, "UniPC")
|
|
138
|
+
|
|
139
|
+
images = generate_image(
|
|
140
|
+
positive_prompt=["a red apple", "a green pear"], # one image per prompt
|
|
141
|
+
negative_prompt=["blurry", "blurry"],
|
|
142
|
+
stable_diffusion_pipeline=pipe,
|
|
143
|
+
height=1024,
|
|
144
|
+
width=1024,
|
|
145
|
+
steps=25,
|
|
146
|
+
seed=7, # prompt i uses seed + i
|
|
147
|
+
)
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### Prompt conversion only
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
from tinysd import convert_a1111_to_compel
|
|
154
|
+
|
|
155
|
+
convert_a1111_to_compel("a (cat:1.3) on a [mat]")
|
|
156
|
+
# 'a (cat)1.3 on a (mat)-'
|
|
157
|
+
|
|
158
|
+
convert_a1111_to_compel("a ((red)) car <lora:style:0.8>")
|
|
159
|
+
# 'a ((red)+)+ car'
|
|
160
|
+
|
|
161
|
+
convert_a1111_to_compel("a cat BREAK a dog")
|
|
162
|
+
# '("a cat", "a dog").and()'
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Pass `convert_a1111_syntax=False` to `generate_image` if your prompts are already written in Compel syntax.
|
|
166
|
+
|
|
167
|
+
### Available schedulers
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from tinysd import SCHEDULERS
|
|
171
|
+
print(list(SCHEDULERS))
|
|
172
|
+
# ['Euler a', 'Euler', 'DPM++ 2M', 'DPM++ 2M Karras', 'DPM++ 2M SDE', 'DPM++ 2M SDE Karras',
|
|
173
|
+
# 'DPM++ SDE', 'DPM++ SDE Karras', 'Heun', 'KDPM2', 'KDPM2 a', 'DDIM', 'UniPC']
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Package layout
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
src/tinysd/
|
|
180
|
+
├── __init__.py # public API
|
|
181
|
+
├── prompt.py # A1111 -> Compel prompt conversion
|
|
182
|
+
├── schedulers.py # SCHEDULERS registry and set_scheduler()
|
|
183
|
+
├── pipeline.py # get_stable_diffusion_pipeline(): model, VAE, LoRA, embeddings
|
|
184
|
+
├── generation.py # generate_image(): Compel encoding + inference
|
|
185
|
+
├── generator.py # SDImageGenerator high-level class
|
|
186
|
+
├── schemas.py # StableDiffusionOutput dataclass
|
|
187
|
+
├── utils.py # internal helpers
|
|
188
|
+
└── sd.py # backward-compatible re-exports
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Running tests
|
|
192
|
+
|
|
193
|
+
The unit tests mock all model loading, so they run quickly and need neither a GPU nor network access:
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
PYTHONPATH=src python -m unittest discover -s tests -t .
|
|
197
|
+
# or, with pytest installed:
|
|
198
|
+
pytest
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## Notes
|
|
202
|
+
|
|
203
|
+
- The safety checker is disabled for SD 1.x pipelines.
|
|
204
|
+
- The SD vs. SDXL choice is based on whether `"xl"` appears in the model ID or path.
|
|
205
|
+
- `clip_skip` applies only to SD 1.x. SDXL always uses the penultimate hidden states.
|
tinysd-0.0.1/README.md
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# TinySD
|
|
2
|
+
|
|
3
|
+
TinySD is a small Python wrapper around [🤗 diffusers](https://github.com/huggingface/diffusers) for
|
|
4
|
+
**Stable Diffusion 1.x** and **SDXL**. It lets you generate images with a few lines of code while keeping
|
|
5
|
+
the conveniences of the AUTOMATIC1111 (A1111) WebUI:
|
|
6
|
+
|
|
7
|
+
- **A1111 prompt syntax**: `(word:1.3)`, `[word]`, `((word))`, `BREAK`, and `<lora:...>` tags are converted
|
|
8
|
+
automatically to [Compel](https://github.com/damian0815/compel) syntax. Long prompts are not truncated.
|
|
9
|
+
- **A1111 sampler names**: `"Euler a"`, `"DPM++ 2M Karras"`, `"DPM++ 2M SDE Karras"`, `"UniPC"`, and others.
|
|
10
|
+
- **Flexible model loading**: Hugging Face Hub IDs, local diffusers folders, or single `.safetensors` / `.ckpt`
|
|
11
|
+
checkpoints (e.g. from Civitai).
|
|
12
|
+
- **Add-ons**: custom VAE, LoRA weights, textual inversion embeddings, custom text encoder/tokenizer.
|
|
13
|
+
- **CLIP skip** for SD 1.x models, and **reproducible seeds**.
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
Requires Python ≥ 3.9. A CUDA GPU is strongly recommended; TinySD falls back to CPU (float32) otherwise.
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
git clone <this-repo> TinySD
|
|
21
|
+
cd TinySD
|
|
22
|
+
pip install -e .
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
If you need a specific CUDA build of PyTorch, install it first (see [pytorch.org](https://pytorch.org)).
|
|
26
|
+
|
|
27
|
+
## Quick start
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from tinysd import SDImageGenerator
|
|
31
|
+
|
|
32
|
+
generator = SDImageGenerator(
|
|
33
|
+
base_model_id="stabilityai/stable-diffusion-xl-base-1.0",
|
|
34
|
+
lora_weight_path=None,
|
|
35
|
+
vae_weight_path=None,
|
|
36
|
+
scheduler_name="DPM++ 2M Karras",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
result = generator.generate_images(
|
|
40
|
+
positive_prompt="a (fluffy cat:1.3) sitting on a windowsill, golden hour, ((highly detailed))",
|
|
41
|
+
negative_prompt="blurry, [low quality], watermark",
|
|
42
|
+
height=1024,
|
|
43
|
+
width=1024,
|
|
44
|
+
steps=30,
|
|
45
|
+
guidance_scale=7.0,
|
|
46
|
+
seed=42,
|
|
47
|
+
nbimages=2,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
for i, image in enumerate(result.images):
|
|
51
|
+
image.save(f"cat_{i}.png")
|
|
52
|
+
|
|
53
|
+
print(result.seed, result.steps, result.base_model_id)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`generate_images` returns a `StableDiffusionOutput` dataclass that holds the generated PIL images and the
|
|
57
|
+
settings used to produce them (model, LoRA/VAE file names, prompts, size, steps, guidance scale, seed).
|
|
58
|
+
When `seed` is given, image `i` uses `seed + i`, so results are reproducible.
|
|
59
|
+
|
|
60
|
+
## Usage examples
|
|
61
|
+
|
|
62
|
+
### Stable Diffusion 1.5 with LoRA, VAE and CLIP skip
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from pathlib import Path
|
|
66
|
+
from tinysd import SDImageGenerator
|
|
67
|
+
|
|
68
|
+
generator = SDImageGenerator(
|
|
69
|
+
base_model_id="stable-diffusion-v1-5/stable-diffusion-v1-5", # "runwayml/..." is aliased automatically
|
|
70
|
+
lora_weight_path=Path("loras/my_style.safetensors"), # file, folder, or Hub repo ID
|
|
71
|
+
vae_weight_path="vae/vae-ft-mse-840000-ema-pruned.safetensors",
|
|
72
|
+
scheduler_name="Euler a",
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
result = generator.generate_images(
|
|
76
|
+
positive_prompt="portrait of a knight, (intricate armor:1.2), BREAK dramatic lighting, castle background",
|
|
77
|
+
negative_prompt="(worst quality:1.4), lowres, bad anatomy",
|
|
78
|
+
seed=1234,
|
|
79
|
+
clip_skip=2,
|
|
80
|
+
)
|
|
81
|
+
result.images[0].save("knight.png")
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Local single-file checkpoint and textual inversion
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from tinysd import SDImageGenerator
|
|
88
|
+
|
|
89
|
+
generator = SDImageGenerator(
|
|
90
|
+
base_model_id="checkpoints/myModel_v10.safetensors", # "xl" in the name selects the SDXL pipeline
|
|
91
|
+
lora_weight_path=None,
|
|
92
|
+
vae_weight_path=None,
|
|
93
|
+
text_inversion_embedding_path="embeddings/ng_deepnegative_v1_75t.pt",
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
result = generator.generate_images(
|
|
97
|
+
positive_prompt="a cozy cabin in a snowy forest",
|
|
98
|
+
negative_prompt="ng_deepnegative_v1_75t, blurry", # embedding is registered under this token
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Lower-level API
|
|
103
|
+
|
|
104
|
+
If you want to manage the pipeline yourself, for example to change the scheduler between runs or generate
|
|
105
|
+
a batch in one call:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
from tinysd import get_stable_diffusion_pipeline, generate_image, set_scheduler
|
|
109
|
+
|
|
110
|
+
pipe = get_stable_diffusion_pipeline(
|
|
111
|
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
|
112
|
+
lora_weights_path=None,
|
|
113
|
+
vae_input_path=None,
|
|
114
|
+
cuda=True,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
set_scheduler(pipe, "UniPC")
|
|
118
|
+
|
|
119
|
+
images = generate_image(
|
|
120
|
+
positive_prompt=["a red apple", "a green pear"], # one image per prompt
|
|
121
|
+
negative_prompt=["blurry", "blurry"],
|
|
122
|
+
stable_diffusion_pipeline=pipe,
|
|
123
|
+
height=1024,
|
|
124
|
+
width=1024,
|
|
125
|
+
steps=25,
|
|
126
|
+
seed=7, # prompt i uses seed + i
|
|
127
|
+
)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Prompt conversion only
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from tinysd import convert_a1111_to_compel
|
|
134
|
+
|
|
135
|
+
convert_a1111_to_compel("a (cat:1.3) on a [mat]")
|
|
136
|
+
# 'a (cat)1.3 on a (mat)-'
|
|
137
|
+
|
|
138
|
+
convert_a1111_to_compel("a ((red)) car <lora:style:0.8>")
|
|
139
|
+
# 'a ((red)+)+ car'
|
|
140
|
+
|
|
141
|
+
convert_a1111_to_compel("a cat BREAK a dog")
|
|
142
|
+
# '("a cat", "a dog").and()'
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Pass `convert_a1111_syntax=False` to `generate_image` if your prompts are already written in Compel syntax.
|
|
146
|
+
|
|
147
|
+
### Available schedulers
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
from tinysd import SCHEDULERS
|
|
151
|
+
print(list(SCHEDULERS))
|
|
152
|
+
# ['Euler a', 'Euler', 'DPM++ 2M', 'DPM++ 2M Karras', 'DPM++ 2M SDE', 'DPM++ 2M SDE Karras',
|
|
153
|
+
# 'DPM++ SDE', 'DPM++ SDE Karras', 'Heun', 'KDPM2', 'KDPM2 a', 'DDIM', 'UniPC']
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Package layout
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
src/tinysd/
|
|
160
|
+
├── __init__.py # public API
|
|
161
|
+
├── prompt.py # A1111 -> Compel prompt conversion
|
|
162
|
+
├── schedulers.py # SCHEDULERS registry and set_scheduler()
|
|
163
|
+
├── pipeline.py # get_stable_diffusion_pipeline(): model, VAE, LoRA, embeddings
|
|
164
|
+
├── generation.py # generate_image(): Compel encoding + inference
|
|
165
|
+
├── generator.py # SDImageGenerator high-level class
|
|
166
|
+
├── schemas.py # StableDiffusionOutput dataclass
|
|
167
|
+
├── utils.py # internal helpers
|
|
168
|
+
└── sd.py # backward-compatible re-exports
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Running tests
|
|
172
|
+
|
|
173
|
+
The unit tests mock all model loading, so they run quickly and need neither a GPU nor network access:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
PYTHONPATH=src python -m unittest discover -s tests -t .
|
|
177
|
+
# or, with pytest installed:
|
|
178
|
+
pytest
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Notes
|
|
182
|
+
|
|
183
|
+
- The safety checker is disabled for SD 1.x pipelines.
|
|
184
|
+
- The SD vs. SDXL choice is based on whether `"xl"` appears in the model ID or path.
|
|
185
|
+
- `clip_skip` applies only to SD 1.x. SDXL always uses the penultimate hidden states.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "tinysd"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "A small wrapper around diffusers for Stable Diffusion 1.x and SDXL with A1111-style prompts."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
dependencies = [
|
|
14
|
+
"torch",
|
|
15
|
+
"diffusers",
|
|
16
|
+
"transformers",
|
|
17
|
+
"compel",
|
|
18
|
+
"safetensors",
|
|
19
|
+
"accelerate",
|
|
20
|
+
"peft",
|
|
21
|
+
"Pillow",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.optional-dependencies]
|
|
25
|
+
test = ["pytest"]
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
29
|
+
|
|
30
|
+
[tool.pytest.ini_options]
|
|
31
|
+
testpaths = ["tests"]
|
tinysd-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""TinySD: a small wrapper around diffusers for Stable Diffusion 1.x and SDXL.
|
|
2
|
+
|
|
3
|
+
Modules:
|
|
4
|
+
- :mod:`tinysd.prompt`: A1111 -> Compel prompt syntax conversion.
|
|
5
|
+
- :mod:`tinysd.schedulers`: A1111-named scheduler registry.
|
|
6
|
+
- :mod:`tinysd.pipeline`: Pipeline loading (VAE, LoRA, textual inversion).
|
|
7
|
+
- :mod:`tinysd.generation`: Low-level image generation.
|
|
8
|
+
- :mod:`tinysd.generator`: High-level :class:`SDImageGenerator`.
|
|
9
|
+
- :mod:`tinysd.schemas`: Output data containers.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .generation import generate_image
|
|
13
|
+
from .generator import SDImageGenerator
|
|
14
|
+
from .pipeline import MODEL_ID_ALIASES, get_stable_diffusion_pipeline
|
|
15
|
+
from .prompt import convert_a1111_to_compel
|
|
16
|
+
from .schedulers import SCHEDULERS, set_scheduler
|
|
17
|
+
from .schemas import StableDiffusionOutput
|
|
18
|
+
|
|
19
|
+
__version__ = "0.1.0"
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"MODEL_ID_ALIASES",
|
|
23
|
+
"SCHEDULERS",
|
|
24
|
+
"SDImageGenerator",
|
|
25
|
+
"StableDiffusionOutput",
|
|
26
|
+
"convert_a1111_to_compel",
|
|
27
|
+
"generate_image",
|
|
28
|
+
"get_stable_diffusion_pipeline",
|
|
29
|
+
"set_scheduler",
|
|
30
|
+
"__version__",
|
|
31
|
+
]
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
"""Low-level image generation with Compel prompt weighting."""
|
|
2
|
+
|
|
3
|
+
from typing import Union, Optional
|
|
4
|
+
|
|
5
|
+
import PIL.Image
|
|
6
|
+
import torch
|
|
7
|
+
from compel import Compel, ReturnedEmbeddingsType
|
|
8
|
+
from diffusers import StableDiffusionPipeline, StableDiffusionXLPipeline
|
|
9
|
+
|
|
10
|
+
from .prompt import convert_a1111_to_compel
|
|
11
|
+
from .utils import _ensure_text_model_attr, _is_sdxl_pipeline
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def generate_image(
|
|
15
|
+
positive_prompt: Union[str, list[str]],
|
|
16
|
+
negative_prompt: Union[str, list[str]],
|
|
17
|
+
stable_diffusion_pipeline: Union[StableDiffusionPipeline, StableDiffusionXLPipeline],
|
|
18
|
+
height: int = 512,
|
|
19
|
+
width: int = 512,
|
|
20
|
+
steps: int = 30,
|
|
21
|
+
guidance_scale: float = 7.5,
|
|
22
|
+
seed: Optional[int] = None,
|
|
23
|
+
clip_skip: int = 1,
|
|
24
|
+
convert_a1111_syntax: bool = True,
|
|
25
|
+
) -> list[PIL.Image.Image]:
|
|
26
|
+
"""Generate images from weighted prompts using a loaded pipeline.
|
|
27
|
+
|
|
28
|
+
Prompts are encoded with Compel (supporting long prompts and weighting), padded
|
|
29
|
+
to the same length, and passed to the pipeline as embeddings.
|
|
30
|
+
|
|
31
|
+
Args:
|
|
32
|
+
positive_prompt: Prompt or list of prompts (one image per prompt).
|
|
33
|
+
negative_prompt: Negative prompt or list of negative prompts.
|
|
34
|
+
stable_diffusion_pipeline: A pipeline from
|
|
35
|
+
:func:`tinysd.pipeline.get_stable_diffusion_pipeline`.
|
|
36
|
+
height: Image height in pixels.
|
|
37
|
+
width: Image width in pixels.
|
|
38
|
+
steps: Number of denoising steps.
|
|
39
|
+
guidance_scale: Classifier-free guidance scale.
|
|
40
|
+
seed: Random seed. For a batch, prompt ``i`` uses ``seed + i``.
|
|
41
|
+
``None`` means non-deterministic.
|
|
42
|
+
clip_skip: For SD 1.x, values ``>= 2`` use the penultimate CLIP hidden
|
|
43
|
+
states. Ignored for SDXL, which always uses the penultimate layer.
|
|
44
|
+
convert_a1111_syntax: Convert A1111 prompt syntax to Compel syntax first.
|
|
45
|
+
|
|
46
|
+
Returns:
|
|
47
|
+
The list of generated PIL images.
|
|
48
|
+
"""
|
|
49
|
+
if convert_a1111_syntax:
|
|
50
|
+
if isinstance(positive_prompt, list):
|
|
51
|
+
positive_prompt = [convert_a1111_to_compel(p) for p in positive_prompt]
|
|
52
|
+
else:
|
|
53
|
+
positive_prompt = convert_a1111_to_compel(positive_prompt)
|
|
54
|
+
|
|
55
|
+
if isinstance(negative_prompt, list):
|
|
56
|
+
negative_prompt = [convert_a1111_to_compel(p) for p in negative_prompt]
|
|
57
|
+
else:
|
|
58
|
+
negative_prompt = convert_a1111_to_compel(negative_prompt)
|
|
59
|
+
|
|
60
|
+
batch_size = len(positive_prompt) if isinstance(positive_prompt, list) else 1
|
|
61
|
+
if seed is None:
|
|
62
|
+
generator = None
|
|
63
|
+
elif batch_size > 1:
|
|
64
|
+
generator = [torch.Generator("cpu").manual_seed(int(seed) + i) for i in range(batch_size)]
|
|
65
|
+
else:
|
|
66
|
+
generator = torch.Generator("cpu").manual_seed(int(seed))
|
|
67
|
+
|
|
68
|
+
_ensure_text_model_attr(getattr(stable_diffusion_pipeline, "text_encoder", None))
|
|
69
|
+
_ensure_text_model_attr(getattr(stable_diffusion_pipeline, "text_encoder_2", None))
|
|
70
|
+
is_xl = _is_sdxl_pipeline(stable_diffusion_pipeline)
|
|
71
|
+
|
|
72
|
+
if is_xl:
|
|
73
|
+
compel = Compel(
|
|
74
|
+
tokenizer=[stable_diffusion_pipeline.tokenizer, stable_diffusion_pipeline.tokenizer_2],
|
|
75
|
+
text_encoder=[stable_diffusion_pipeline.text_encoder, stable_diffusion_pipeline.text_encoder_2],
|
|
76
|
+
returned_embeddings_type=ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NON_NORMALIZED,
|
|
77
|
+
requires_pooled=[False, True],
|
|
78
|
+
truncate_long_prompts=False,
|
|
79
|
+
)
|
|
80
|
+
positive_prompt_embeds, positive_pooled = compel(positive_prompt)
|
|
81
|
+
negative_prompt_embeds, negative_pooled = compel(negative_prompt)
|
|
82
|
+
[positive_prompt_embeds, negative_prompt_embeds] = compel.pad_conditioning_tensors_to_same_length(
|
|
83
|
+
[positive_prompt_embeds, negative_prompt_embeds]
|
|
84
|
+
)
|
|
85
|
+
sd_output = stable_diffusion_pipeline(
|
|
86
|
+
prompt_embeds=positive_prompt_embeds,
|
|
87
|
+
negative_prompt_embeds=negative_prompt_embeds,
|
|
88
|
+
pooled_prompt_embeds=positive_pooled,
|
|
89
|
+
negative_pooled_prompt_embeds=negative_pooled,
|
|
90
|
+
height=height,
|
|
91
|
+
width=width,
|
|
92
|
+
num_inference_steps=steps,
|
|
93
|
+
guidance_scale=guidance_scale,
|
|
94
|
+
generator=generator,
|
|
95
|
+
output_type="pil",
|
|
96
|
+
)
|
|
97
|
+
else:
|
|
98
|
+
emb_type = (
|
|
99
|
+
ReturnedEmbeddingsType.PENULTIMATE_HIDDEN_STATES_NORMALIZED
|
|
100
|
+
if clip_skip and clip_skip >= 2
|
|
101
|
+
else ReturnedEmbeddingsType.LAST_HIDDEN_STATES_NORMALIZED
|
|
102
|
+
)
|
|
103
|
+
compel = Compel(
|
|
104
|
+
tokenizer=stable_diffusion_pipeline.tokenizer,
|
|
105
|
+
text_encoder=stable_diffusion_pipeline.text_encoder,
|
|
106
|
+
returned_embeddings_type=emb_type,
|
|
107
|
+
truncate_long_prompts=False,
|
|
108
|
+
)
|
|
109
|
+
positive_prompt_embeds = compel(positive_prompt)
|
|
110
|
+
negative_prompt_embeds = compel(negative_prompt)
|
|
111
|
+
[positive_prompt_embeds, negative_prompt_embeds] = compel.pad_conditioning_tensors_to_same_length(
|
|
112
|
+
[positive_prompt_embeds, negative_prompt_embeds]
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
sd_output = stable_diffusion_pipeline(
|
|
116
|
+
prompt_embeds=positive_prompt_embeds,
|
|
117
|
+
negative_prompt_embeds=negative_prompt_embeds,
|
|
118
|
+
height=height,
|
|
119
|
+
width=width,
|
|
120
|
+
num_inference_steps=steps,
|
|
121
|
+
guidance_scale=guidance_scale,
|
|
122
|
+
generator=generator,
|
|
123
|
+
output_type="pil",
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return sd_output.images
|