-
Notifications
You must be signed in to change notification settings - Fork 3
/
stabilityAiHelper.js
231 lines (194 loc) · 6.79 KB
/
stabilityAiHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import fs from "node:fs";
import axios from "axios";
import FormData from "form-data";
import dotenv from 'dotenv';
import sharp from "sharp";
import { removeBackground } from '@imgly/background-removal-node';
import * as pathTools from "path";
dotenv.config();
export let apiKey = process.env.STABILITY_API_KEY;
const statusComplete = 200;
const statusStillRunning = 202;
export async function saveImage({prompt = '', path = '', doRemoveBackground = false, doRemoveBackgroundInAddition = false, aspectRatio = '1:1', contextForErrorLogging = "Stability TextToImage", imageInfo = {error: null}} = {}) {
// API Reference: https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1sd3/post
let success = false;
const format = getExtensionWithoutDot(path);
const formData = {
prompt: prompt,
output_format: format,
aspect_ratio: aspectRatio
};
let response;
try {
response = await axios.postForm(
`https://api.stability.ai/v2beta/stable-image/generate/sd3`,
axios.toFormData(formData, new FormData()),
{
validateStatus: undefined,
responseType: "arraybuffer",
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: "image/*"
},
},
);
}
catch (error) {
await logError(error, `${contextForErrorLogging}. Prompt: ${prompt}`);
imageInfo.error = error;
}
if (response) {
try {
if (response.status === statusComplete) {
let imageBuffer = Buffer.from(response.data);
fs.writeFileSync(path, imageBuffer);
if (doRemoveBackground || doRemoveBackgroundInAddition) {
let removedBackgroundPath = doRemoveBackgroundInAddition ?
path.replace('.png', '-background-removed.png') : path;
let removeBackgroundConfig = {
debug: false,
output: {
format: 'image/png',
quality: 1.0
}
};
const blob = new Blob([imageBuffer], { type: "image/png" });
const removedBackgroundBlob = await removeBackground(blob, removeBackgroundConfig);
const newImageBuffer = Buffer.from(await removedBackgroundBlob.arrayBuffer());
fs.writeFileSync(removedBackgroundPath, newImageBuffer);
}
success = true;
}
else {
console.log(`${contextForErrorLogging} issue, ${response.status}: ${response.data.toString()}`);
imageInfo.error = JSON.parse(response.data).errors[0];
}
}
catch (error) {
await logError(error, contextForErrorLogging);
imageInfo.error = error;
}
}
return success;
}
export async function saveImageCore({prompt = '', path = '', style = '', contextForErrorLogging = "StabilityCore TextToImage"} = {}) {
// API Reference: https://platform.stability.ai/docs/api-reference#tag/Generate/paths/~1v2beta~1stable-image~1generate~1core/post
let success = false;
const format = getExtensionWithoutDot(path);
const formData = {
prompt: prompt,
style_preset: style,
output_format: format,
// aspect_ratio: ratio,
// seed: seed,
// negative_prompt: negativePrompt,
};
let response;
try {
response = await axios.postForm(
`https://api.stability.ai/v2beta/stable-image/generate/core`,
axios.toFormData(formData, new FormData()),
{
validateStatus: undefined,
responseType: "arraybuffer",
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: "image/*"
},
},
);
}
catch (error) {
await logError(error, contextForErrorLogging);
}
if (response) {
try {
if (response.status === statusComplete) {
let imageBuffer = Buffer.from(response.data);
fs.writeFileSync(path, imageBuffer);
success = true;
}
else {
console.log(`${contextForErrorLogging} issue, ${response.status}: ${response.data.toString()}`);
}
}
catch (error) {
await logError(error, contextForErrorLogging);
}
}
return success;
}
export async function getVideoFromImage({imageBuffer, contextForErrorLogging = "Stability ImageToVideo"} = {}) {
// API Reference: https://platform.stability.ai/docs/api-reference#tag/Image-to-Video/paths/~1v2beta~1image-to-video/post
// Stability's API is apparently broken, it never returns a video.
const targetWidth = 1024;
const targetHeight = 576;
const resizedImageBuffer = await sharp(imageBuffer)
.resize({ width: targetWidth })
.extract({ width: targetWidth, height: targetHeight, left: 0, top: 0 })
.toBuffer();
const cfgScaleDefault = 1.8;
const motionBucketIdDefault = 127;
const data = new FormData();
data.append("image", resizedImageBuffer, "image.png");
data.append("seed", 0);
data.append("cfg_scale", cfgScaleDefault);
data.append("motion_bucket_id", motionBucketIdDefault);
console.log("Sending image to video...");
const response = await axios.request({
url: `https://api.stability.ai/v2beta/image-to-video`,
method: "post",
validateStatus: undefined,
headers: {
authorization: `Bearer ${apiKey}`,
...data.getHeaders(),
},
data: data,
});
const generationId = response.data.id;
console.log("generationID:", generationId);
const responseVideo = await axios.request({
url: `https://api.stability.ai/v2beta/image-to-video/result/${generationId}`,
method: "GET",
validateStatus: undefined,
responseType: "arraybuffer",
headers: {
Authorization: `Bearer ${apiKey}`,
Accept: "video/*",
},
});
let videoBuffer;
const checkFrequencySeconds = 0.25;
while (!videoBuffer) {
await delay(checkFrequencySeconds);
const currentTime = new Date().toLocaleTimeString();
console.log("Checking for video...", currentTime, responseVideo.status);
if (responseVideo.status === statusComplete) {
videoBuffer = Buffer.from(responseVideo.data);
}
else if (responseVideo.status !== statusStillRunning) {
console.log(`${contextForErrorLogging} error ${responseVideo.status}: ${responseVideo.data.toString()}`);
break;
}
}
return videoBuffer;
}
function delay(seconds) {
return new Promise(resolve => setTimeout(resolve, seconds * 1000));
}
function getExtensionWithoutDot(path) {
const extensionWithDot = pathTools.extname(path);
return extensionWithDot.slice(1);
}
async function logError(error, contextForErrorLogging) {
let errorMessage;
const prefix = "stabilityAi issue:";
if (error.response) {
console.error(prefix, contextForErrorLogging, error.response.status, error.response.data);
errorMessage = JSON.stringify(error.response.status) + " " + JSON.stringify(error.response.data);
}
else {
console.error(prefix, contextForErrorLogging, error.message);
errorMessage = JSON.stringify(error.message);
}
}