stable-diffusion-client
Version:
smart stable-diffusion-webui client
91 lines (90 loc) • 2.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const deps_js_1 = require("../deps.js");
/**
* @class StableDiffusionResult
* @classdesc Result of a Stable Diffusion image processing API call
* @param {StableDiffusionApiRawResponse} Raw axios response
* @property {sharp.Sharp} image - First sharp image from the result list
* @property {sharp.Sharp[]} images - List of sharp images
* @property {any} info - Info object
* @property {any} parameters - Parameters object
* @property {AxiosApiRawResponse} response - Raw response from the API
* @example
* const api = new StableDiffusionApi()
* const result = await api.txt2img({
* prompt: "The brain of a computer",
* })
*
* // Save the first image
* result.image.toFile("result.png")
*
* // Save all images
* result.images.map((image, i) => {
* image.toFile(`result_${i}.png`)
* })
*/
class StableDiffusionResult {
constructor(data) {
Object.defineProperty(this, "data", {
enumerable: true,
configurable: true,
writable: true,
value: data
});
Object.defineProperty(this, "images", {
enumerable: true,
configurable: true,
writable: true,
value: []
});
// images: sharp.Sharp[] = [];
Object.defineProperty(this, "info", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "parameters", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
if (data.image && typeof data.image === "string") {
this.images.push(data.image);
}
if (data.images && Array.isArray(data.images)) {
for (const image of data.images) {
this.images.push(image);
}
}
const { info, html_info } = data;
if (info && info.startsWith("{")) {
this.info = JSON.parse(info);
}
else if (html_info) {
if (html_info.startsWith("{")) {
this.info = JSON.parse(html_info);
}
else {
this.info = { html_info };
}
}
else {
this.info = { html_info: "" };
}
this.parameters = data.parameters || {};
}
static base64toSharp(base64) {
const imageBuffer = (0, deps_js_1.decodeBase64)(base64);
return (0, deps_js_1.sharp)(imageBuffer);
}
getSharpImage(id) {
if (!this.images[id])
throw Error(`OutOfBound Error no image id: ${id}, Total images: ${this.images.length}`);
return StableDiffusionResult.base64toSharp(this.images[id]);
}
;
}
exports.default = StableDiffusionResult;