Skip to content

Commit

Permalink
Merge pull request #43 from spongedsc/cloudflare-ai
Browse files Browse the repository at this point in the history
  • Loading branch information
artifishvr authored May 10, 2024
2 parents ba033c9 + 4fff4d3 commit fe1a861
Show file tree
Hide file tree
Showing 4 changed files with 818 additions and 606 deletions.
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ DISCORD="TOKEN"
# Channels to talk to the AI in
CHANNELIDS="123456789012345678,123456789012345678"

REPLY_CHANCE=0.99
REPLY_CHANCE=0.99

# For Workers AI
CF_ACCOUNT="ACCOUNT_ID"
CF_TOKEN=""
48 changes: 27 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fs from 'fs';
import path from 'path';
import { DateTime } from 'luxon';
import axios from 'axios';
import fetch from 'node-fetch';

import { io } from "socket.io-client";
import { MsEdgeTTS } from "msedge-tts";
Expand Down Expand Up @@ -174,31 +175,36 @@ async function checkLocal() {
}

async function imageRecognition(message) {
if (message.attachments.size > 0 && !backendsocket.disconnected) {
if (message.attachments.size > 0) {
let imageDetails = '';
let promises = [];

for (const attachment of message.attachments.values()) {
try {
const url = attachment.url;
const promise = new Promise((resolve) => {
message.channel.sendTyping();
backendsocket.emit("imgcaption", url, (val) => {
imageDetails += `Attached: image of ${val[0].generated_text}\n`;
resolve();
});
});
promises.push(promise);
} catch (error) {
console.error(error);
return message.reply(`❌ Error! Yell at arti.`);
};
}

await Promise.all(promises);
const res = await fetch(message.attachments.first().url);
const blob = await res.arrayBuffer();
const input = {
image: [...new Uint8Array(blob)],
prompt: "Generate a caption for this image",
max_tokens: 512,
};

try {
let response = await fetch(`https://api.cloudflare.com/client/v4/accounts/${process.env.CF_ACCOUNT}/ai/run/@cf/llava-hf/llava-1.5-7b-hf`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.CF_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(input)
});
response = await response.json();

imageDetails += `Attached: image of${response.result.description}\n`;
} catch (error) {
console.error(error);
};

return imageDetails;
} else {
return "";
return '';
}
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spongegpt",
"version": "1.28.4",
"version": "1.29.0",
"description": "custom AI chatbot for discord",
"main": "index.js",
"type": "module",
Expand All @@ -19,6 +19,7 @@
"libsodium-wrappers": "^0.7.13",
"luxon": "^3.4.4",
"msedge-tts": "^1.3.4",
"node-fetch": "^3.3.2",
"socket.io-client": "^4.7.2"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit fe1a861

Please sign in to comment.