-
Notifications
You must be signed in to change notification settings - Fork 0
/
text2image.mjs
38 lines (31 loc) · 1.08 KB
/
text2image.mjs
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
import fs from 'node:fs';
const engineId = 'stable-diffusion-v1-6';
const apiHost = process.env.API_HOST ?? 'https://api.stability.ai';
const apiKey = process.env.STABILITY_API_KEY;
if (!apiKey) {
throw new Error('Missing Stability API key.');
}
const response = await fetch(`${apiHost}/v1/generation/${engineId}/text-to-image`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
text_prompts: [
{ text: 'an electric sheep, electric, sheep, blue and yellow, night city, illustration', },
],
cfg_scale: 7, height: 1024, width: 1024, steps: 30, samples: 1,
}),
});
if (!response.ok) {
throw new Error(`${response.status} response: ${await response.text()}`);
}
const responseJSON = await response.json();
if (!fs.existsSync('./out')) {
fs.mkdirSync('./out');
}
responseJSON.artifacts.forEach((image, index) => {
fs.writeFileSync(`./out/v1_txt2img_${index}.png`, Buffer.from(image.base64, 'base64'));
});