stability-ai
Version:
Node SDK for Stability AI REST API
160 lines • 7.14 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sd3 = exports.core = exports.ultra = void 0;
const axios_1 = __importDefault(require("axios"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const form_data_1 = __importDefault(require("form-data"));
const util_1 = require("../../util");
const error_1 = require("../../error");
const Util = __importStar(require("../../util"));
const RESOURCE = 'stable-image/generate';
var Endpoint;
(function (Endpoint) {
Endpoint["ULTRA"] = "ultra";
Endpoint["CORE"] = "core";
Endpoint["SD3"] = "sd3";
})(Endpoint || (Endpoint = {}));
/**
* Stability AI Stable Image Generation Ultra (v2beta)
*
* @param options - Ultra Options
*/
async function ultra(...args) {
const [prompt, options] = args;
const formData = {
prompt,
};
if (options === null || options === void 0 ? void 0 : options.aspectRatio)
formData.aspect_ratio = options.aspectRatio;
if (options === null || options === void 0 ? void 0 : options.negativePrompt)
formData.negative_prompt = options.negativePrompt;
if (options === null || options === void 0 ? void 0 : options.seed)
formData.seed = options.seed;
if (options === null || options === void 0 ? void 0 : options.outputFormat)
formData.output_format = options.outputFormat;
const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.ULTRA), axios_1.default.toFormData(formData, new form_data_1.default()), {
validateStatus: undefined,
headers: {
...this.authHeaders,
Accept: 'application/json',
},
});
if (response.status === 200) {
return Util.processContentResponse(response.data, (options === null || options === void 0 ? void 0 : options.outputFormat) || Util.DEFAULT_OUTPUT_FORMAT, 'v2beta_stable_image_generate_ultra');
}
throw new error_1.StabilityAIError(response.status, 'Failed to stable image generation ultra', response.data);
}
exports.ultra = ultra;
/**
* Stability AI Stable Image Generation Core (v2beta)
*
* @param options - Core Options
*/
async function core(...args) {
const [prompt, options] = args;
const formData = {
prompt,
};
if (options === null || options === void 0 ? void 0 : options.aspectRatio)
formData.aspect_ratio = options.aspectRatio;
if (options === null || options === void 0 ? void 0 : options.negativePrompt)
formData.negative_prompt = options.negativePrompt;
if (options === null || options === void 0 ? void 0 : options.seed)
formData.seed = options.seed;
if (options === null || options === void 0 ? void 0 : options.outputFormat)
formData.output_format = options.outputFormat;
const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.CORE), axios_1.default.toFormData(formData, new form_data_1.default()), {
validateStatus: undefined,
headers: {
...this.authHeaders,
Accept: 'application/json',
},
});
if (response.status === 200) {
return Util.processContentResponse(response.data, (options === null || options === void 0 ? void 0 : options.outputFormat) || Util.DEFAULT_OUTPUT_FORMAT, 'v2beta_stable_image_generate_core');
}
throw new error_1.StabilityAIError(response.status, 'Failed to stable image generation core', response.data);
}
exports.core = core;
/**
* Stability AI Stable Image Generation SD3 (v2beta)
*
* @param options - SD3 (StableDiffusion 3) Options
*/
async function sd3(...args) {
const [prompt, options] = args;
let imagePath = undefined;
const formData = {
prompt,
};
// general options
if (options === null || options === void 0 ? void 0 : options.mode)
formData.mode = options.mode;
if (options === null || options === void 0 ? void 0 : options.model)
formData.model = options.model;
if (options === null || options === void 0 ? void 0 : options.seed)
formData.seed = options.seed;
if (options === null || options === void 0 ? void 0 : options.outputFormat)
formData.output_format = options.outputFormat;
switch (options === null || options === void 0 ? void 0 : options.mode) {
case 'image-to-image':
imagePath = new Util.ImagePath(options.image);
formData.strength = options.strength;
formData.image = fs_extra_1.default.createReadStream(await imagePath.filepath());
break;
case 'text-to-image':
default:
if (options === null || options === void 0 ? void 0 : options.aspectRatio)
formData.aspect_ratio = options.aspectRatio;
break;
}
switch (options === null || options === void 0 ? void 0 : options.model) {
case 'sd3-turbo':
break;
case 'sd3':
default:
if (options === null || options === void 0 ? void 0 : options.negativePrompt)
formData.negative_prompt = options.negativePrompt;
break;
}
const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.SD3), axios_1.default.toFormData(formData, new form_data_1.default()), {
validateStatus: undefined,
headers: {
...this.authHeaders,
Accept: 'application/json',
},
});
imagePath === null || imagePath === void 0 ? void 0 : imagePath.cleanup();
if (response.status === 200) {
return Util.processContentResponse(response.data, (options === null || options === void 0 ? void 0 : options.outputFormat) || Util.DEFAULT_OUTPUT_FORMAT, 'v2beta_stable_image_generate_sd3');
}
throw new error_1.StabilityAIError(response.status, 'Failed to stable image generation sd3', response.data);
}
exports.sd3 = sd3;
//# sourceMappingURL=generate.js.map
;