From 9e2814cb2c57ce255030c9edb99f80cf4f07f7b1 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 11 Mar 2023 23:07:48 -0500 Subject: [PATCH 1/3] Init branch, WIP Needs new library to merge images --- commands/imagine.js | 2 +- controllers/openAiController.js | 4 +- package.json | 3 +- utilities/mergeImages.js | 70 +++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 utilities/mergeImages.js diff --git a/commands/imagine.js b/commands/imagine.js index a754765..278d508 100644 --- a/commands/imagine.js +++ b/commands/imagine.js @@ -57,7 +57,7 @@ module.exports = { log.DEBUG("Images in the result: ", imagesInResult); - if (imagesInResult == 1) dalleEmbed.setImage(imageResults.results.data[0].url); + if (imagesInResult != 0) dalleEmbed.setImage(imageResults.results.data[0].url); await interaction.editReply({ embeds: [dalleEmbed], ephemeral: false }); }); diff --git a/controllers/openAiController.js b/controllers/openAiController.js index 723cc57..3e70ccb 100644 --- a/controllers/openAiController.js +++ b/controllers/openAiController.js @@ -134,13 +134,13 @@ exports.submitTextPromptTransaction = async (prompt, temperature, max_tokens, di pricePerImage = 800; break; default: - log.DEBUG("256px defaulted"); + log.DEBUG("256px defaulted"); pricePerImage = 800; break; } if (!images_to_generate) images_to_generate = 1; - if (!image_size) images_to_generate = "256x256"; + if (!image_size) image_size = "256x256"; totalTokensToBeUsed = pricePerImage * images_to_generate; diff --git a/package.json b/package.json index 1d3b406..65190ac 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "node-html-markdown": "~1.3.0", "node-html-parser": "~6.1.5", "gpt-3-encoder": "~1.1.4", - "user-agents": "~1.0.1303" + "user-agents": "~1.0.1303", + "canvas": "~2.11.0" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/utilities/mergeImages.js b/utilities/mergeImages.js new file mode 100644 index 0000000..d4b3e20 --- /dev/null +++ b/utilities/mergeImages.js @@ -0,0 +1,70 @@ +const { createCanvas, loadImage } = require("canvas"); +const fs = require("fs"); + +/** + * + * @param {Array} images The array of images to be merged together + * @param {function} callback The callback function to call with the merged images + * @returns {*} A PNG encoded image + */ +exports.mergeImages = async (images, callback) => { + if (!images) return callback(new Error("No images provided to merge"), undefined); + const numberOfImages = images.length; + const finalImageWidth = 1024; + const finalImageHeight = 1024; + + switch(numberOfImages) { + case 1: + break; + case 2: + break; + case 3: + break; + case 4: + break; + default: + break; + } + + + const imagePosition = { + w: 400, + h: 88, + x: 400, + // Calculate the Y of the image based on the number of + // lines in the title. + y: titleText.length === 2 ? 75 : 100, + }; + + +} + +// Do the same with the title's Y value. +const titleY = titleText.length === 2 ? 300 : 350; +const titleLineHeight = 100; +// And the author's Y value. +const authorY = titleText.length === 2 ? 525 : 500; + +const canvas = createCanvas(width, height); +const context = canvas.getContext("2d"); + +context.fillStyle = "#764abc"; +context.fillRect(0, 0, width, height); + +context.font = "bold 70pt 'PT Sans'"; +context.textAlign = "center"; +context.fillStyle = "#fff"; + +context.fillText(titleText[0], 600, titleY); +if (titleText[1]) context.fillText(titleText[1], 600, titleY + titleLineHeight); + +context.font = "40pt 'PT Sans'"; +context.fillText(`by ${post.author}`, 600, authorY); + +await loadImage("./assets/logo.png").then((image) => { + const { w, h, x, y } = imagePosition; + context.drawImage(image, x, y, w, h); +}); + +const buffer = canvas.toBuffer("image/png"); +fs.writeFileSync("./image.png", buffer); \ No newline at end of file -- 2.49.1 From af74c7b90d2dbbe1cf8b0bee330d89b2f2b7b3e4 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 12 Mar 2023 03:59:01 -0400 Subject: [PATCH 2/3] Remove unused module --- package.json | 3 +- utilities/mergeImages.js | 70 ---------------------------------------- 2 files changed, 1 insertion(+), 72 deletions(-) delete mode 100644 utilities/mergeImages.js diff --git a/package.json b/package.json index 65190ac..1d3b406 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "node-html-markdown": "~1.3.0", "node-html-parser": "~6.1.5", "gpt-3-encoder": "~1.1.4", - "user-agents": "~1.0.1303", - "canvas": "~2.11.0" + "user-agents": "~1.0.1303" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/utilities/mergeImages.js b/utilities/mergeImages.js deleted file mode 100644 index d4b3e20..0000000 --- a/utilities/mergeImages.js +++ /dev/null @@ -1,70 +0,0 @@ -const { createCanvas, loadImage } = require("canvas"); -const fs = require("fs"); - -/** - * - * @param {Array} images The array of images to be merged together - * @param {function} callback The callback function to call with the merged images - * @returns {*} A PNG encoded image - */ -exports.mergeImages = async (images, callback) => { - if (!images) return callback(new Error("No images provided to merge"), undefined); - const numberOfImages = images.length; - const finalImageWidth = 1024; - const finalImageHeight = 1024; - - switch(numberOfImages) { - case 1: - break; - case 2: - break; - case 3: - break; - case 4: - break; - default: - break; - } - - - const imagePosition = { - w: 400, - h: 88, - x: 400, - // Calculate the Y of the image based on the number of - // lines in the title. - y: titleText.length === 2 ? 75 : 100, - }; - - -} - -// Do the same with the title's Y value. -const titleY = titleText.length === 2 ? 300 : 350; -const titleLineHeight = 100; -// And the author's Y value. -const authorY = titleText.length === 2 ? 525 : 500; - -const canvas = createCanvas(width, height); -const context = canvas.getContext("2d"); - -context.fillStyle = "#764abc"; -context.fillRect(0, 0, width, height); - -context.font = "bold 70pt 'PT Sans'"; -context.textAlign = "center"; -context.fillStyle = "#fff"; - -context.fillText(titleText[0], 600, titleY); -if (titleText[1]) context.fillText(titleText[1], 600, titleY + titleLineHeight); - -context.font = "40pt 'PT Sans'"; -context.fillText(`by ${post.author}`, 600, authorY); - -await loadImage("./assets/logo.png").then((image) => { - const { w, h, x, y } = imagePosition; - context.drawImage(image, x, y, w, h); -}); - -const buffer = canvas.toBuffer("image/png"); -fs.writeFileSync("./image.png", buffer); \ No newline at end of file -- 2.49.1 From cdb766520db621b477c821deb8639f44a64af5b3 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 12 Mar 2023 04:23:17 -0400 Subject: [PATCH 3/3] Added multiple embed builders for multiple images --- commands/imagine.js | 38 +++++++++++++++++++++------------ controllers/openAiController.js | 4 ++++ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/commands/imagine.js b/commands/imagine.js index 278d508..962bde0 100644 --- a/commands/imagine.js +++ b/commands/imagine.js @@ -1,10 +1,10 @@ -const { submitImagePromptTransaction } = require("../controllers/openAiController"); +const { submitImagePromptTransaction, DALLE_COLOR } = require("../controllers/openAiController"); const { SlashCommandBuilder } = require('discord.js'); const { DebugBuilder } = require("../utilities/debugBuilder"); const log = new DebugBuilder("server", "imagine"); const { EmmeliaEmbedBuilder } = require('../libUtils'); -const COST_OF_COMMAND = 800 +const COST_OF_COMMAND = 800; module.exports = { data: new SlashCommandBuilder() @@ -45,21 +45,31 @@ module.exports = { submitImagePromptTransaction(promptText, discordAccountId, images, size, interaction, this, async (err, imageResults) => { if (err) throw err; + var dalleEmbeds = []; log.DEBUG("Image Results: ", imageResults) - - const dalleEmbed = new EmmeliaEmbedBuilder() - .setColor(0x0099FF) + // Add the information post + dalleEmbeds.push(new EmmeliaEmbedBuilder() + .setColor(DALLE_COLOR) .setTitle(`New Image Result`) - .setDescription(`${interaction.member.user} sent the prompt: '${promptText}'`) - .addFields({ name: 'Tokens Used', value: `${imageResults.totalTokens}`, inline: true }) - - const imagesInResult = Array(imageResults.results.data).length - + .setDescription(`${interaction.member.user} sent the prompt: '${promptText}'`) + ); + // Add the images to the result + const imagesInResult = Array(imageResults.results).length log.DEBUG("Images in the result: ", imagesInResult); - - if (imagesInResult != 0) dalleEmbed.setImage(imageResults.results.data[0].url); - - await interaction.editReply({ embeds: [dalleEmbed], ephemeral: false }); + if (imagesInResult >= 1) { + for (const imageData of imageResults.results.data){ + const imageUrl = imageData.url; + dalleEmbeds.push(new EmmeliaEmbedBuilder().setURL(imageUrl).setImage(imageUrl).setColor(DALLE_COLOR)); + } + } + // Add the information post + dalleEmbeds.push(new EmmeliaEmbedBuilder() + .setColor(DALLE_COLOR) + .addFields({ name: 'Tokens Used', value: `${imageResults.totalTokens}`, inline: true }) + .addFields({ name: 'Images Generated', value: `${imagesInResult}`, inline: true }) + .addFields({ name: 'Image Size Requested', value: `${imagesInResult}`, inline: true }) + ); + await interaction.editReply({ embeds: dalleEmbeds, ephemeral: false }); }); // Needs reply code to reply to the generation diff --git a/controllers/openAiController.js b/controllers/openAiController.js index 3e70ccb..f4442d4 100644 --- a/controllers/openAiController.js +++ b/controllers/openAiController.js @@ -14,6 +14,10 @@ const configuration = new Configuration({ const openai = new OpenAIApi(configuration); +// Global Vars for Other functions +exports.DALLE_COLOR = 0x34c6eb; +exports.CHATGPT_COLOR = 0x34eb9b; + async function getImageGeneration(_prompt, { _images_to_generate = 1, _image_size = "256x256" }, callback){ const validImageSizes = ["256x256", "512x512", "1024x1024"]; -- 2.49.1