-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.mjs
More file actions
36 lines (31 loc) · 971 Bytes
/
index.mjs
File metadata and controls
36 lines (31 loc) · 971 Bytes
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
import fetch from "node-fetch";
import { promises as fs } from "fs";
const API_URL =
"https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0";
const headers = {
Authorization: "Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
};
async function query(data) {
const response = await fetch(API_URL, {
headers,
method: "POST",
body: JSON.stringify(data),
});
const result = await response.arrayBuffer();
return result;
}
async function generateImage(caption) {
try {
const imageBytes = await query({
inputs: caption,
});
// Generate a file name with the caption and save the image
const fileName = `${caption.replace(/\s+/g, "-")}.png`;
const filePath = "output/" + fileName;
await fs.writeFile(filePath, Buffer.from(imageBytes));
console.log(`Image saved to: ${filePath}`);
} catch (error) {
console.error(error);
}
}
generateImage("photo of rocket launching into space");