0
# WebUIApi
1
2
A comprehensive Python API client for AUTOMATIC1111/stable-diffusion-webui that enables programmatic image generation using Stable Diffusion models. WebUIApi provides complete access to txt2img, img2img, upscaling, model management, and extensive extension support including ControlNet, ADetailer, AnimateDiff, and face swapping capabilities.
3
4
## Package Information
5
6
- **Package Name**: webuiapi
7
- **Package Type**: Python package
8
- **Language**: Python
9
- **Version**: 0.9.17
10
- **Installation**: `pip install webuiapi`
11
- **Dependencies**: requests, Pillow
12
13
## Core Imports
14
15
```python
16
import webuiapi
17
18
# Create API client
19
api = webuiapi.WebUIApi()
20
```
21
22
Specific imports:
23
24
```python
25
from webuiapi import (
26
WebUIApi,
27
WebUIApiResult,
28
ControlNetUnit,
29
ADetailer,
30
Upscaler,
31
b64_img
32
)
33
```
34
35
## Basic Usage
36
37
```python
38
import webuiapi
39
40
# Create API client with default settings
41
api = webuiapi.WebUIApi()
42
43
# Or with custom configuration
44
api = webuiapi.WebUIApi(
45
host='127.0.0.1',
46
port=7860,
47
sampler='Euler a',
48
steps=20
49
)
50
51
# Simple text-to-image generation
52
result = api.txt2img(
53
prompt="cute cat, digital art",
54
negative_prompt="blurry, low quality",
55
width=512,
56
height=512
57
)
58
59
# Save the generated image
60
result.image.save("generated_cat.png")
61
62
# Image-to-image generation
63
from PIL import Image
64
init_image = Image.open("input.jpg")
65
66
result = api.img2img(
67
prompt="oil painting style",
68
images=[init_image],
69
denoising_strength=0.7
70
)
71
72
result.image.save("styled_image.png")
73
```
74
75
## Architecture
76
77
WebUIApi follows a modular design centered around the main API client:
78
79
- **WebUIApi**: Primary client handling all API communication and configuration
80
- **Result Objects**: Structured responses (WebUIApiResult) containing images, parameters, and metadata
81
- **Configuration Classes**: Specialized parameter objects for extensions (ControlNetUnit, ADetailer, etc.)
82
- **Extension Interfaces**: Dedicated classes for extension-specific functionality
83
- **Utility Functions**: Helper functions for image encoding and common operations
84
85
The API maintains connection state, handles authentication, and provides both synchronous operations and extension integration through a unified interface.
86
87
## Capabilities
88
89
### Image Generation
90
91
Core text-to-image and image-to-image generation functionality with extensive parameter control including sampling methods, steps, guidance scale, and batch processing.
92
93
```python { .api }
94
def txt2img(
95
prompt: str,
96
negative_prompt: str = "",
97
width: int = 512,
98
height: int = 512,
99
steps: int = 20,
100
sampler_name: str = None,
101
cfg_scale: float = 7.0,
102
seed: int = -1,
103
batch_size: int = 1,
104
**kwargs
105
) -> WebUIApiResult: ...
106
107
def img2img(
108
prompt: str,
109
images: List[Image.Image],
110
negative_prompt: str = "",
111
denoising_strength: float = 0.75,
112
width: int = 512,
113
height: int = 512,
114
**kwargs
115
) -> WebUIApiResult: ...
116
```
117
118
[Image Generation](./image-generation.md)
119
120
### Model Management
121
122
Complete model lifecycle management including loading, switching, and querying available models, samplers, schedulers, and other resources.
123
124
```python { .api }
125
def get_sd_models() -> List[Dict]: ...
126
def get_samplers() -> List[Dict]: ...
127
def get_schedulers() -> List[Dict]: ...
128
def util_set_model(name: str) -> None: ...
129
def util_get_current_model() -> str: ...
130
```
131
132
[Model Management](./model-management.md)
133
134
### Image Processing
135
136
Advanced image processing operations including upscaling, interrogation, metadata extraction, and batch processing capabilities.
137
138
```python { .api }
139
def extra_single_image(
140
image: Image.Image,
141
upscaler_1: str = "None",
142
upscaling_resize: float = 2.0,
143
**kwargs
144
) -> WebUIApiResult: ...
145
146
def interrogate(
147
image: Image.Image,
148
model: str = "clip"
149
) -> str: ...
150
151
def png_info(image: Image.Image) -> Dict: ...
152
```
153
154
[Image Processing](./image-processing.md)
155
156
### ControlNet Integration
157
158
Precise image generation control using ControlNet with support for depth, canny, pose, and other conditioning methods.
159
160
```python { .api }
161
class ControlNetUnit:
162
def __init__(
163
self,
164
image: Image.Image = None,
165
module: str = "none",
166
model: str = "None",
167
weight: float = 1.0,
168
**kwargs
169
): ...
170
171
def controlnet_detect(
172
controlnet_module: str,
173
controlnet_input_images: List[str],
174
**kwargs
175
) -> Dict: ...
176
```
177
178
[ControlNet](./controlnet.md)
179
180
### Extensions
181
182
Integration with popular WebUI extensions including ADetailer for face enhancement, AnimateDiff for video generation, and face swapping capabilities.
183
184
```python { .api }
185
class ADetailer:
186
def __init__(
187
self,
188
ad_model: str = "face_yolov8n.pt",
189
ad_confidence: float = 0.3,
190
ad_denoising_strength: float = 0.4,
191
**kwargs
192
): ...
193
194
class AnimateDiff:
195
def __init__(
196
self,
197
model: str = "mm_sd_v14.ckpt",
198
fps: int = 8,
199
video_length: int = 16,
200
**kwargs
201
): ...
202
```
203
204
[Extensions](./extensions.md)
205
206
### Configuration and Settings
207
208
WebUI configuration management, option retrieval and modification, and system status monitoring.
209
210
```python { .api }
211
def get_options() -> Dict: ...
212
def set_options(options: Dict) -> None: ...
213
def get_progress() -> Dict: ...
214
def interrupt() -> None: ...
215
```
216
217
[Configuration](./configuration.md)
218
219
### Utility Functions
220
221
Helper functions for image encoding and common operations.
222
223
```python { .api }
224
def b64_img(image: Image.Image) -> str:
225
"""Convert PIL Image to base64 with data URL header."""
226
227
def raw_b64_img(image: Image.Image) -> str:
228
"""Convert PIL Image to raw base64 (no header)."""
229
```
230
231
### Extension Interfaces
232
233
Specialized interfaces for extension functionality and image analysis.
234
235
```python { .api }
236
class ModelKeywordInterface:
237
def get_keywords(self, model_name: str) -> ModelKeywordResult: ...
238
239
class SegmentAnythingInterface:
240
def sam_predict(self, **kwargs) -> SegmentAnythingSamResult: ...
241
def dino_predict(self, **kwargs) -> SegmentAnythingGinoResult: ...
242
243
class TaggerInterface:
244
def tagger_interrogate(self, **kwargs) -> Dict: ...
245
```
246
247
[Extension Interfaces](./interfaces.md)
248
249
## Types
250
251
```python { .api }
252
__version__: str = "0.9.17" # Package version constant
253
254
class WebUIApiResult:
255
"""Container for API response data."""
256
images: List[Image.Image] # Generated images
257
parameters: Dict # Generation parameters used
258
info: Dict # Additional generation info
259
json: Dict # Raw JSON response
260
261
@property
262
def image(self) -> Image.Image:
263
"""First generated image (convenience property)."""
264
265
class Upscaler(str, Enum):
266
"""Available upscaling algorithms."""
267
none = "None"
268
Lanczos = "Lanczos"
269
ESRGAN_4x = "R-ESRGAN 4x+"
270
# ... additional upscaler options
271
272
class HiResUpscaler(str, Enum):
273
"""High-resolution upscaling methods."""
274
none = "None"
275
Latent = "Latent"
276
LatentAntialiased = "Latent (antialiased)"
277
# ... additional hi-res options
278
279
class ModelKeywordResult:
280
"""Result from model keyword lookup."""
281
keywords: List[str] # Associated keywords
282
model: str # Model name
283
oldhash: str # Model hash
284
match_source: str # Source of keyword match
285
286
# SegmentAnything Result Classes
287
class SegmentAnythingSamResult:
288
"""Result from SAM prediction operations."""
289
290
class SegmentAnythingGinoResult:
291
"""Result from DINO prediction operations."""
292
293
class SegmentAnythingDilationResult:
294
"""Result from mask dilation operations."""
295
296
class SegmentAnythingControlNetSegRandomResult:
297
"""Result from ControlNet segmentation (random mode)."""
298
299
class SegmentAnythingControlNetSegNotRandomResult:
300
"""Result from ControlNet segmentation (non-random mode)."""
301
302
class SegmentAnythingSemanticSegWithCatIdResult:
303
"""Result from semantic segmentation with category ID."""
304
```