-
Notifications
You must be signed in to change notification settings - Fork 1
/
caption.js
59 lines (50 loc) · 1.95 KB
/
caption.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
require('dotenv').config();
const axios = require('axios');
const llm = require('./message.js'); //Process input, return output
async function captionImage(base64input, message, authorName) {
//message.channel.send('ok, hang on a second.'); //Confirm and await.
// API key sent in HTTP POST header to the Horde
const config = {
headers: {
apikey: process.env.HORDE_APIKEY,
'Content-Type': 'application/json',
}
};
const postData = {
"forms": [{
"name": "caption",
"payload": {},
}],
"source_image": `${base64input}`
};
try {
// send POST request to first REST API endpoint
const response = await axios.post('https://stablehorde.net/api/v2/interrogate/async',
postData,
config);
const id = response.data.id; // get id from response data
console.log(`Caption job id: ${id}`);
// poll second endpoint for "done=true" value
let done = false;
while (!done) {
const pollResponse = await axios.get(`https://stablehorde.net/api/v2/interrogate/status/${id}`);
if (pollResponse.data.state==='done') {done=true};
console.log("Caption "+ pollResponse.data.state);//`Polling caption job: ${done}`);
if (done) {
// get base64-encoded image from third endpoint
const imageResponse = await axios.get(`https://stablehorde.net/api/v2/interrogate/status/${id}`);
//console.log(imageResponse.data);
const caption = imageResponse.data.forms[0].result.caption;
console.log(`Img caption: ${caption}`);
message.content = `${message.content} *[SENDS PICTURE: ${caption}]*`
const llmReply = await llm.sendMessage(message);
message.channel.send(llmReply);
}
await new Promise(resolve => setTimeout(resolve, 2000)); // wait 2 seconds before polling again
}
} catch (error) {
console.error('Error:', error);
message.channel.send('An error occurred.');
}
}
module.exports = { captionImage };