Skip to content

Commit

Permalink
imagine command
Browse files Browse the repository at this point in the history
  • Loading branch information
daniwasonline committed May 17, 2024
1 parent 37bea59 commit 1d850d5
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/commands/imagine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { SlashCommandBuilder } from "discord.js";
import { WorkersAI } from "../util/models/index.js";

/** @type {import('./index.js').Command} */
export default {
data: new SlashCommandBuilder()
.setName("imagine")
.setDescription("Generate an image.")
.addStringOption((o) =>
o
.setName("model")
.setDescription("Enter a description of what you're generating.")
.setChoices([
{
name: "lykon/dreamshaper-8-lcm",
value: "@cf/lykon/dreamshaper-8-lcm",
},
{
name: "bytedance/stable-diffusion-xl-lightning",
value: "@cf/bytedance/stable-diffusion-xl-lightning",
},
{
name: "stabilityai/stable-diffusion-xl-base-1.0",
value: "@cf/stabilityai/stable-diffusion-xl-base-1.0",
},
])
.setRequired(true),
)
.addStringOption((o) =>
o.setName("prompt").setDescription("Enter a description of what you're generating.").setRequired(true),
)
.toJSON(),
async execute(interaction) {
const workersAI = new WorkersAI({
accountId: process.env.CLOUDFLARE_ACCOUNT_ID,
token: process.env.CLOUDFLARE_ACCOUNT_TOKEN,
});

await interaction.deferReply();
const prompt = interaction.options.getString("prompt");
const model = interaction.options.getString("model");

const callToModel = await (async () => {
const prefix = model?.split("/")?.[0];
if (prefix !== "@cf") return;

return await workersAI
.callModel(
{
model,
input: {
prompt,
},
},
true,
)
.then((r) => r.arrayBuffer())
.catch(() => (e) => {
console.error(e);
return null;
});
})();

if (callToModel === null)
return await interaction.editReply({
content: `The model did not generate an image.`,
});

const buffer = Buffer.from(callToModel);

await interaction.editReply({
content: `\`${prompt}\`\n*generated with \`${model}\`*`,
files: [buffer],
});
},
};

0 comments on commit 1d850d5

Please sign in to comment.