stability-ai
Version:
Node SDK for Stability AI REST API
144 lines • 6.53 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.fetchCreativeResult = exports.startCreative = exports.conservative = 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/upscale';
var Endpoint;
(function (Endpoint) {
Endpoint["CONSERVATIVE"] = "conservative";
Endpoint["CREATIVE"] = "creative";
Endpoint["CREATIVE_RESULT"] = "creative/result";
})(Endpoint || (Endpoint = {}));
/**
* Stability AI Stable Image Conservative Upscale (v2beta)
*
* @param image - Local filepath or public URL of the image to upscale
* @param prompt - Prompt to use for upscaling
* @param options - Extra options for the upscale
*/
async function conservative(...args) {
const [image, prompt, options] = args;
const imagePath = new Util.ImagePath(image);
const formData = {
image: fs_extra_1.default.createReadStream(await imagePath.filepath()),
prompt,
};
if (options === null || options === void 0 ? void 0 : options.negativePrompt)
formData.negative_prompt = options.negativePrompt;
if (options === null || options === void 0 ? void 0 : options.outputFormat)
formData.output_format = options.outputFormat;
if (options === null || options === void 0 ? void 0 : options.seed)
formData.seed = options.seed;
if (options === null || options === void 0 ? void 0 : options.creativity)
formData.creativity = options.creativity;
const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.CONSERVATIVE), axios_1.default.toFormData(formData, new form_data_1.default()), {
validateStatus: undefined,
headers: {
...this.authHeaders,
Accept: 'application/json',
},
});
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_upscale_conservative');
}
throw new error_1.StabilityAIError(response.status, 'Failed to perform conservative upscale', response.data);
}
exports.conservative = conservative;
/**
* Stability AI Stable Image Start Creative Upscale (v2beta)
*
* @param image - Local filepath or public URL of the image to upscale
* @param prompt - Prompt to use for upscaling
* @param options - Extra options for the upscale
*/
async function startCreative(...args) {
const [image, prompt, options] = args;
const imagePath = new Util.ImagePath(image);
const formData = {
image: fs_extra_1.default.createReadStream(await imagePath.filepath()),
prompt,
};
if (options === null || options === void 0 ? void 0 : options.negativePrompt)
formData.negative_prompt = options.negativePrompt;
if (options === null || options === void 0 ? void 0 : options.outputFormat)
formData.output_format = options.outputFormat;
if (options === null || options === void 0 ? void 0 : options.seed)
formData.seed = options.seed;
if (options === null || options === void 0 ? void 0 : options.creativity)
formData.creativity = options.creativity;
const response = await axios_1.default.postForm(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.CREATIVE), axios_1.default.toFormData(formData, new form_data_1.default()), {
validateStatus: undefined,
headers: this.authHeaders,
});
imagePath.cleanup();
if (response.status === 200 && typeof response.data.id === 'string') {
return {
id: response.data.id,
outputFormat: (options === null || options === void 0 ? void 0 : options.outputFormat) || Util.DEFAULT_OUTPUT_FORMAT,
};
}
throw new error_1.StabilityAIError(response.status, 'Failed to start creative upscale', response.data);
}
exports.startCreative = startCreative;
/**
* Stability AI Stable Image Fetch Creative Upscale Result (v2beta)
*
* @param id - ID of the upscale job
* @param output_format - Output format requested in original upscale request
* @returns
*/
async function fetchCreativeResult(...args) {
const [id, outputFormat] = args;
const response = await axios_1.default.get(Util.makeUrl(util_1.APIVersion.V2_BETA, RESOURCE, Endpoint.CREATIVE_RESULT) +
`/${id}`, {
validateStatus: undefined,
headers: {
...this.authHeaders,
Accept: 'application/json',
},
});
if (response.status === 200) {
return Util.processContentResponse(response.data, outputFormat, 'v2beta_stable_image_upscale_creative');
}
else if (response.status === 202 &&
typeof response.data.id === 'string' &&
response.data.status === 'in-progress') {
const { id, status } = response.data;
return { id, status };
}
throw new error_1.StabilityAIError(response.status, 'Failed to fetch createive upscale result', response.data);
}
exports.fetchCreativeResult = fetchCreativeResult;
//# sourceMappingURL=upscale.js.map