-
Notifications
You must be signed in to change notification settings - Fork 0
/
llmutils.js
282 lines (255 loc) · 7.8 KB
/
llmutils.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require("dotenv").config();
const { exec } = require("child_process"),
{ HfInference } = require("@huggingface/inference"),
axios = require("axios"),
{ python } = require("pythonia"),
{ generate } = require("./infer-petals"),
genPaid = require("./infer-paid").generate,
imgPaid = require("./infer-paid").generateImage,
{ QuickDB } = require("quick.db"),
db = new QuickDB(),
hf = new HfInference(process.env.HF_TOKEN),
{ randomInt } = require("crypto");
var bindings, siginter;
/**
* Set up bindings
*/
async function setBindings() {
if (!bindings) bindings = await python("./infer-bindings.py");
if (!siginter)
siginter = process.on("SIGINT", () => {
bindings.exit();
});
return bindings;
}
/**
* Runs a prompt using the binary.
* @async
* @param {string} prompt - The prompt to run.
* @param {string} cid - Channel ID, for caching
* @returns {Promise<string>} The output of the command.
*/
async function runPrompt(prompt, cid) {
/**
const binder = await setBindings(),
res = await binder.generate$(prompt, { $timeout: Infinity });
return res;
*/
return await genPaid(prompt);
}
/**
* Runs a prompt through the auxilliary provider, currently petals public net.
* @async
* @param {string} prompt - Prompt to run
* @returns {Promise<string>} The output of the command.
*/
async function runAux(prompt) {
/**
const res = await generate(prompt);
return res;
*/
return await genPaid(prompt);
}
async function getCaption(img) {
const { client } = await import("@gradio/client");
const blob = await (await fetch(img)).blob();
const blip = await client("https://spuun-blip-api.hf.space/", {
hf_token: process.env.HF_TOKEN,
});
const image = img.split("/").pop().split(".")[0];
if (await db.has(image)) return await db.get(image);
var res;
try {
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error("Request timed out"));
}, 60000);
});
res = (
await Promise.race([blip.predict("/predict", [blob]), timeoutPromise])
).data[0];
} catch (e) {
console.log(`[${new Date()}] blip: ${e}`);
return "<ERROR: vision module failure>";
}
if (!res) return "<ERROR: vision module failure>";
await db.set(image, res);
return res;
}
async function keyword(input) {
const prompt = `### System:
You are keyworder, a bot that summarizes a text into keywords delimited by commas.
### User:
${input}
### Assistant:
topics: [`;
let res = (await runAux(prompt))
.split("\n")[0]
.trim()
.split(",")
.map((e) => {
return e.replaceAll("[", "").replaceAll("]", "").trim();
});
return res;
}
/**
*
* @param {Buffer} image - buffer of image to check
*/
async function nsfwProcess(image) {
const { client } = await import("@gradio/client"),
blob = new Blob([image], { type: "image/png" }),
nsfwdet = await client("https://spuun-nsfw-det.hf.space/", {
hf_token: process.env.HF_TOKEN,
});
let retries = 3;
while (retries > 0) {
try {
const res = (await nsfwdet.predict("/predict", [blob])).data[0];
if (!res) {
console.log(
`[WARN] [${new Date()}] nsfw failed to return a value, defaulting to false.`
);
return false;
}
return JSON.parse(res.toLowerCase());
} catch (e) {
console.log(`[${new Date()}] nsfw: ${e}`);
retries--;
if (retries === 0) {
console.log(
`[${new Date()}] NSFW retry limit reached, returning fallback value`
);
return true;
}
}
}
}
/**
* Fetches the top matching GIF for a given query.
* @param {string} query - The search query.
* @returns {Promise<ArrayBuffer | undefined>} The GIF data as an ArrayBuffer or undefined.
*/
async function getTopMatchingGif(query) {
const keywords = await keyword(query);
if (!keywords) return undefined;
const url = `https://tenor.googleapis.com/v2/search?q=${keywords}&key=${process.env.TENOR_API_KEY}&client_key=kekbot&limit=1&media_filter=gif`;
console.log(`[${new Date()}] ${keywords}`);
try {
const response = await axios.get(url);
if (response.data.results.length > 0) {
const topResult =
response.data.results.length - 1
? response.data.results[
randomInt(0, response.data.results.length - 1)
]
: response.data.results[0];
const gifUrl = topResult.media_formats.gif.url;
const gifResponse = await axios.get(gifUrl, {
responseType: "arraybuffer",
});
return gifResponse.data;
} else return undefined;
} catch (error) {
console.log(`[${new Date()}] Error querying Tenor API: ${error}`);
}
}
/**
*
* @param {string} query
*/
async function generateImage(query) {
const emotion = (
await hf.textClassification({
model: "arpanghoshal/EmoRoBERTa",
inputs: query.slice(-128),
})
).shift().label,
/** @type {string[]} */
keywords = await keyword(query);
console.log(`[${new Date()}] ${keywords} | ${emotion}`);
/*
const res = Buffer.from(
await (
await hf.textToImage({
model: "iZELX1/Anything-V3-X",
inputs: `${
keywords ? `${keywords},` : ""
} ${emotion}, ${emotion}, ${emotion},${
query.replaceAll(/^[^ \n]+:/gim, "").includes("kekbot")
? " catgirl, cat_ears, green_hair, loli, femboy, looking_at_viewer, crop_top,"
: ""
} masterpiece, best_quality`,
parameters: {
guidance_scale: 8,
negative_prompt:
"nsfw, breasts, large_breast, boobs, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",
},
})
).arrayBuffer()
);
*/
const res = Buffer.from(
await imgPaid(
`${
keywords
? `${keywords.map((s) =>
s.toLowerCase().replaceAll("kekbot", "").replaceAll("kek", "")
)},`
: ""
} ${emotion}, ${emotion}, ${emotion},${
query
.replaceAll(/^[^ \n]+:/gim, "")
.toLowerCase()
.includes("kekbot") || keywords?.includes("kekbot")
? " catgirl, cat_ears, green_hair, loli, femboy, looking_at_viewer, crop top,"
: ""
} masterpiece, best_quality`,
"nsfw, breasts, large_breast, boobs, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry"
)
);
return res;
}
/**
* Function to query a QA model to rank the arrSet for closest to query
* @param {string} query
* @param {string[]} arrSet
*/
async function getClosestQA(query, arrSet) {
const { client } = await import("@gradio/client"),
qa = await client("https://spuun-qa.hf.space/", {
hf_token: process.env.HF_TOKEN,
}),
result = (
await qa.predict("/predict", [query, arrSet.join("|")])
).data[0].split(",");
return result
.filter((e) => !isNaN(e))
.map((e, i) => [arrSet[i], Number.parseFloat(e)])
.sort((a, b) => b[1] - a[1])[0][0];
}
/**
* @param {string} string - String to summarize
* @returns {Promise<string|undefined>}
*/
async function getSummary(input) {
const prompt = `### System:
You are summarizer, a bot that summarizes a text into a digestible third-person interpretation. This interpretation must be easy to understand and concise, like you're explaining it to someone else. If the text mentions people's names, refer to them by their names.
### User:
${input}
### Assistant:
Summary: `;
let res = (await runAux(prompt)).trim();
return res;
}
module.exports = {
runPrompt,
runAux,
keyword,
getCaption,
getTopMatchingGif,
nsfwProcess,
generateImage,
getSummary,
getClosestQA,
};