Skip to content

Commit f219398

Browse files
committed
feat: improve pipeline
- add types - update documents
1 parent 0bb70c6 commit f219398

File tree

4 files changed

+188
-18
lines changed

4 files changed

+188
-18
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,27 @@ const { images, parameters } = await new Pipeline(client)
9494

9595
> For details about extensions, refer to the **Processor Usage** section.
9696
97+
### img2img (or inpainting) with pipeline
98+
```ts
99+
const input_image = fs.readFileSync("./input_image.jpg").toString("base64");
100+
const input_mask = fs.readFileSync("./input_mask.jpg").toString("base64");
101+
const { images, parameters } = await new Pipeline(client)
102+
.model("sdxl.safetensors")
103+
.prompt("A beautiful sunset over the mountains")
104+
.negative("Low quality, blurry")
105+
.sampler("DPM++ 2M")
106+
.scheduler("exponential")
107+
.size(1024, 768)
108+
.steps(35)
109+
.cfg(5)
110+
.images(input_image)
111+
// 👇 inpainting params 👇
112+
// .inpainting(InpaintFill.original)
113+
// .mask(input_mask)
114+
.use(/* extension */)
115+
.run();
116+
```
117+
97118
## API Usage
98119

99120
### Documentation

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stable-canvas/sd-webui-a1111-client",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "API client for AUTOMATIC1111/stable-diffusion-webui for Node.js and Browser.",
55
"source": "src/main.ts",
66
"main": "dist/main.umd.js",

src/pipeline/Pipeline.ts

Lines changed: 160 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import type {
2-
SDWebUIA1111Client,
3-
StableDiffusionProcessingImg2Img,
4-
StableDiffusionProcessingTxt2Img,
5-
} from "../client";
1+
import type { SDWebUIA1111Client } from "../client";
62
import { ExtensionScript } from "../extensions";
73
import { not } from "../misc";
4+
import {
5+
InpaintFill,
6+
Img2imgProcessParams,
7+
Txt2imgProcessParams,
8+
} from "../process";
89
import { SDProcessor } from "../process/SDProcessor";
910
import { SamplerName, SchedulerName } from "./types";
1011

11-
type RequestBody = StableDiffusionProcessingTxt2Img &
12-
StableDiffusionProcessingImg2Img;
12+
type RequestBody = Img2imgProcessParams & Txt2imgProcessParams;
1313
type BodyKey = keyof RequestBody;
1414
type BodyValue<K extends BodyKey> = NonNullable<RequestBody[K]>;
1515

@@ -91,62 +91,126 @@ export class Pipeline {
9191
}
9292

9393
// **** 拓展参数 ****
94+
95+
/**
96+
* Sets the text prompt for image generation.
97+
* @param {BodyValue<"prompt">} text - The prompt text.
98+
* @returns {this} The pipeline instance.
99+
*/
94100
prompt(text: BodyValue<"prompt">) {
95101
this._write("prompt", text);
96102
return this;
97103
}
98104

105+
/**
106+
* Sets the negative prompt to avoid undesired features in the image.
107+
* @param {BodyValue<"negative_prompt">} text - The negative prompt text.
108+
* @returns {this} The pipeline instance.
109+
*/
99110
negative(text: BodyValue<"negative_prompt">) {
100111
this._write("negative_prompt", text);
101112
return this;
102113
}
103114

115+
/**
116+
* Sets the seed value for deterministic image generation.
117+
* @param {BodyValue<"seed">} seed - The seed value.
118+
* @returns {this} The pipeline instance.
119+
*/
104120
seed(seed: BodyValue<"seed">) {
105121
this._write("seed", seed);
106122
return this;
107123
}
108124

125+
/**
126+
* Sets the subseed value for variations in deterministic generation.
127+
* @param {BodyValue<"subseed">} seed - The subseed value.
128+
* @returns {this} The pipeline instance.
129+
*/
109130
subseed(seed: BodyValue<"subseed">) {
110131
this._write("subseed", seed);
111132
return this;
112133
}
113134

135+
/**
136+
* Sets the sampler method used for generating images.
137+
* @param {SamplerName} name - The sampler name.
138+
* @returns {this} The pipeline instance.
139+
*/
114140
sampler(name: SamplerName) {
115141
this._write("sampler_name", name);
116142
return this;
117143
}
118144

145+
/**
146+
* Sets the scheduler method for image generation.
147+
* @param {SchedulerName} name - The scheduler name.
148+
* @returns {this} The pipeline instance.
149+
*/
119150
scheduler(name: SchedulerName) {
120151
this._write("scheduler", name);
121152
return this;
122153
}
123154

155+
/**
156+
* Sets the batch size for image generation.
157+
* @param {BodyValue<"batch_size">} size - The batch size.
158+
* @returns {this} The pipeline instance.
159+
*/
124160
batch(size: BodyValue<"batch_size">) {
125161
this._write("batch_size", size);
126162
return this;
127163
}
128164

165+
/**
166+
* Sets the number of inference steps.
167+
* @param {BodyValue<"steps">} steps - The number of steps.
168+
* @returns {this} The pipeline instance.
169+
*/
129170
steps(steps: BodyValue<"steps">) {
130171
this._write("steps", steps);
131172
return this;
132173
}
133174

175+
/**
176+
* Sets the classifier-free guidance scale.
177+
* @param {BodyValue<"cfg_scale">} scale - The CFG scale value.
178+
* @returns {this} The pipeline instance.
179+
*/
134180
cfg(scale: BodyValue<"cfg_scale">) {
135181
this._write("cfg_scale", scale);
136182
return this;
137183
}
138184

185+
/**
186+
* Sets the image size.
187+
* @param {BodyValue<"width">} width - The image width.
188+
* @param {BodyValue<"height">} height - The image height.
189+
* @returns {this} The pipeline instance.
190+
*/
139191
size(width: BodyValue<"width">, height: BodyValue<"height">) {
140192
this._write("width", width);
141193
this._write("height", height);
142194
return this;
143195
}
144196

197+
/**
198+
* Enables high-resolution image generation.
199+
* @param {BodyValue<"enable_hr">} [enable=true] - Whether to enable high-resolution mode.
200+
* @returns {this} The pipeline instance.
201+
*/
145202
enableHR(enable: BodyValue<"enable_hr"> = true) {
146203
this._write("enable_hr", enable);
147204
return this;
148205
}
149206

207+
/**
208+
* Configures high-resolution settings.
209+
* @param {BodyValue<"hr_scale">} scale - The upscaling factor.
210+
* @param {BodyValue<"hr_upscaler">} upscaler - The upscaler algorithm.
211+
* @param {BodyValue<"hr_second_pass_steps">} [secondPassSteps] - Additional processing steps.
212+
* @returns {this} The pipeline instance.
213+
*/
150214
hr(
151215
scale: BodyValue<"hr_scale">,
152216
upscaler: BodyValue<"hr_upscaler">,
@@ -161,6 +225,12 @@ export class Pipeline {
161225
return this;
162226
}
163227

228+
/**
229+
* Overrides some of the processing settings.
230+
* @param {BodyValue<"override_settings">} settings - The settings to override.
231+
* @param {boolean} [restore_afterwards=false] - Whether to restore the original settings after processing.
232+
* @returns {this} The pipeline instance.
233+
*/
164234
override(
165235
settings: BodyValue<"override_settings">,
166236
restore_afterwards = false
@@ -173,6 +243,13 @@ export class Pipeline {
173243
return this;
174244
}
175245

246+
/**
247+
* Sets the model checkpoint to be used for processing.
248+
* @param {string} sd_model_checkpoint - The checkpoint identifier for the model.
249+
* @param {boolean} [restore_afterwards=false] - Whether to restore the original model checkpoint after processing.
250+
* @returns {this} The pipeline instance.
251+
*/
252+
176253
model(sd_model_checkpoint: string, restore_afterwards = false) {
177254
this.override(
178255
{ sd_model_checkpoint: sd_model_checkpoint },
@@ -181,6 +258,13 @@ export class Pipeline {
181258
return this;
182259
}
183260

261+
/**
262+
* Sets the script to be used for processing.
263+
*
264+
* @param {BodyValue<"script_name">} name - The name of the script.
265+
* @param {BodyValue<"script_args">} [args] - Optional arguments for the script.
266+
* @returns {this} The pipeline instance.
267+
*/
184268
script(name: BodyValue<"script_name">, args?: BodyValue<"script_args">) {
185269
this._write("script_name", name);
186270
if (args) {
@@ -189,36 +273,78 @@ export class Pipeline {
189273
return this;
190274
}
191275

276+
/**
277+
* Controls whether images are saved to disk or not.
278+
* @param {BodyValue<"save_images">} [save=true] - Whether to save images.
279+
* @returns {this} The pipeline instance.
280+
*/
192281
saveImages(save: BodyValue<"save_images"> = true) {
193282
this._write("save_images", save);
194283
return this;
195284
}
196285

286+
/**
287+
* Controls whether generated images are sent back to the client or not.
288+
* @param {BodyValue<"send_images">} [send=true] - Whether to send images.
289+
* @returns {this} The pipeline instance.
290+
*/
197291
sendImages(send: BodyValue<"send_images"> = true) {
198292
this._write("send_images", send);
199293
return this;
200294
}
201295

296+
/**
297+
* Sets the denoising strength for image processing.
298+
* @param {BodyValue<"denoising_strength">} value - The denoising strength value.
299+
* @returns {this} The pipeline instance.
300+
*/
301+
202302
strength(value: BodyValue<"denoising_strength">) {
203303
this._write("denoising_strength", value);
204304
return this;
205305
}
206306

307+
/**
308+
* Sets the initial images for image processing.
309+
* @param {...BodyValue<"init_images">} images - The initial images.
310+
* @returns {this} The pipeline instance.
311+
*/
207312
images(...images: BodyValue<"init_images">) {
208313
this._write("init_images", images);
209314
return this;
210315
}
211316

317+
/**
318+
* Sets the resize mode for image processing.
319+
* @param {BodyValue<"resize_mode">} mode - The resize mode: 0 to upscale by upscaling_resize amount, 1 to upscale up to upscaling_resize_h x upscaling_resize_w.
320+
* @returns {this} The pipeline instance.
321+
*/
212322
resizeMode(mode: BodyValue<"resize_mode">) {
213323
this._write("resize_mode", mode);
214324
return this;
215325
}
216326

327+
/**
328+
* Sets the image configuration scale used in image processing.
329+
* @param {BodyValue<"image_cfg_scale">} scale - The scale value for image configuration.
330+
* @returns {this} The pipeline instance.
331+
*/
332+
217333
imageCfgScale(scale: BodyValue<"image_cfg_scale">) {
218334
this._write("image_cfg_scale", scale);
219335
return this;
220336
}
221337

338+
/**
339+
* Sets the mask for image processing.
340+
* @param {BodyValue<"mask">} mask - The mask image (base64).
341+
* @param {Object} [options] - Optional parameters.
342+
* @param {BodyValue<"mask_blur_x">} [options.blurX] - The blur value for the mask in the X direction.
343+
* @param {BodyValue<"mask_blur_y">} [options.blurY] - The blur value for the mask in the Y direction.
344+
* @param {BodyValue<"mask_blur">} [options.blur] - The blur value for the mask.
345+
* @param {BodyValue<"mask_round">} [options.round] - Whether to round the mask.
346+
* @returns {this} The pipeline instance.
347+
*/
222348
mask(
223349
mask: BodyValue<"mask">,
224350
{
@@ -231,7 +357,7 @@ export class Pipeline {
231357
blurY?: BodyValue<"mask_blur_y">;
232358
blur?: BodyValue<"mask_blur">;
233359
round?: BodyValue<"mask_round">;
234-
}
360+
} = {}
235361
) {
236362
this._write("mask", mask);
237363
if (blurX !== undefined) this._write("mask_blur_x", blurX);
@@ -241,17 +367,28 @@ export class Pipeline {
241367
return this;
242368
}
243369

370+
/**
371+
* Configures inpainting settings for image processing.
372+
*
373+
* @param {BodyValue<"inpainting_fill">} fill - The fill type for inpainting, defaults to original.
374+
* @param {Object} [options] - Optional parameters for inpainting.
375+
* @param {BodyValue<"inpaint_full_res">} [options.fullRes] - Whether to use full resolution for inpainting.
376+
* @param {BodyValue<"inpaint_full_res_padding">} [options.fullResPadding] - Padding to apply when using full resolution.
377+
* @param {BodyValue<"inpainting_mask_invert">} [options.maskInvert] - Invert the mask for inpainting.
378+
* @returns {this} The pipeline instance.
379+
*/
380+
244381
inpainting(
245-
fill: BodyValue<"inpainting_fill">,
382+
fill: BodyValue<"inpainting_fill"> = InpaintFill.original,
246383
{
247384
fullRes,
248385
fullResPadding,
249386
maskInvert,
250387
}: {
251-
fullRes: BodyValue<"inpaint_full_res">;
388+
fullRes?: BodyValue<"inpaint_full_res">;
252389
fullResPadding?: BodyValue<"inpaint_full_res_padding">;
253390
maskInvert?: BodyValue<"inpainting_mask_invert">;
254-
}
391+
} = {}
255392
) {
256393
this._write("inpainting_fill", fill);
257394
this._write("inpaint_full_res", fullRes);
@@ -262,13 +399,25 @@ export class Pipeline {
262399
return this;
263400
}
264401

402+
/**
403+
* Sets the initial noise multiplier.
404+
* @param {BodyValue<"initial_noise_multiplier">} value - The initial noise multiplier.
405+
* @returns {this} The pipeline instance.
406+
*/
265407
initialNoiseMultiplier(value: BodyValue<"initial_noise_multiplier">) {
266408
this._write("initial_noise_multiplier", value);
267409
return this;
268410
}
269411

412+
/**
413+
* Sets the always-on scripts.
414+
* @param {BodyValue<"alwayson_scripts">} scripts - The always-on scripts configuration.
415+
* @returns {this} The pipeline instance.
416+
*/
417+
270418
alwaysOnScripts(scripts: BodyValue<"alwayson_scripts">) {
271419
this._write("alwayson_scripts", scripts);
420+
272421
return this;
273422
}
274423
}

0 commit comments

Comments
 (0)