-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
132 lines (108 loc) · 4.09 KB
/
utils.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
const fs = require("fs");
const archiver = require("archiver");
const sharp = require("sharp");
const mongoose = require("mongoose");
const replicate = new (require("replicate"))({ auth: process.env.REPLICATE_KEY });
const { Client, GatewayIntentBits } = require("discord.js");
async function processImages(images, id) {
const output = fs.createWriteStream("uploads/" + id + ".zip");
const archive = archiver("zip");
archive.pipe(output);
for (const image of images) {
try {
const img = sharp(image.data);
const { width, height } = await img.metadata();
const side = Math.max(width, height);
const addWidth = (side - width) / 2;
const addHeight = (side - height) / 2;
const result = await img
.extend({ top: Math.ceil(addHeight), bottom: Math.floor(addHeight), left: Math.ceil(addWidth), right: Math.floor(addWidth), background: "#000000" })
.withMetadata()
.toBuffer();
const filename = image.filename;
archive.append(result, { name: filename });
} catch (err) {
sendError(err);
}
}
await archive.finalize();
archive.on("error", (err) => {
reject(err);
});
}
async function generateModel(id, gender, imgNum) {
let classData = {};
if (gender == "man") {
classData["data"] = process.env.MODEL_URL + "/regularization-images-man.zip";
classData["amount"] = 4820;
} else if (gender == "woman") {
classData["data"] = process.env.MODEL_URL + "/regularization-images-woman.zip";
classData["amount"] = 4420;
} else {
classData["data"] = process.env.MODEL_URL + "/regularization-images-other.zip";
classData["amount"] = 2115;
}
const body = {
input: {
instance_prompt: "photo of a cjw " + gender,
class_prompt: "photo of a " + gender,
instance_data: process.env.URL + "/user-pictures/" + id,
class_data: classData["data"],
max_train_steps: 150 * imgNum,
num_class_images: Math.min(classData["amount"], 150 * imgNum),
ckpt_base: process.env.MODEL_URL + "/base-model.ckpt",
},
model: "tahainc/dreambooth-models",
trainer_version: "a8ba568da0313951a6b311b43b1ea3bf9f2ef7b9fd97ed94cebd7ffd2da66654",
template_version: "e9639aebcd8c92810d487efe5df25078b350583d83228867b35ae4aee6557a2c",
webhook_completed: process.env.URL + "/model-completed/" + id,
};
const response = await fetch("https://dreambooth-api-experimental.replicate.com/v1/trainings", {
method: "POST",
headers: {
Authorization: "Token " + process.env.REPLICATE_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
sendError(await response.text());
}
}
async function generateImage(id, model, settings) {
let ObjectID = mongoose.Types.ObjectId;
settings.imgId = new ObjectID();
const response = await replicate.predictions.create({ version: model, input: settings, webhook: process.env.URL + "/picture-completed/" + id });
await new Promise((resolve) => setTimeout(resolve, 5000));
const bootResponse = await replicate.predictions.get(response.id);
if (bootResponse.status === "starting") {
return { id: response.id, imgId: settings.imgId, coldBoot: true };
} else {
return { id: response.id, imgId: settings.imgId, coldBoot: false };
}
}
async function getPredictionStatus(id) {
try {
return await replicate.predictions.get(id);
} catch (err) {
sendError(err);
}
}
async function bufferToBase64(imgBuffer) {
const img = sharp(imgBuffer);
let imgRes = await img.toBuffer();
let imgBase64 = imgRes.toString("base64");
return imgBase64;
}
function sendError(err) {
if (process.env.DISCORD_BOT_TOKEN) {
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
client.login(process.env.DISCORD_BOT_TOKEN);
client.on("ready", () => {
const channel = client.channels.cache.find((channel) => channel.name === "error-logs");
channel.send("Error: " + err);
});
}
console.error(err);
}
module.exports = { processImages, generateModel, generateImage, getPredictionStatus, bufferToBase64, sendError };