-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37bea59
commit 1d850d5
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}); | ||
}, | ||
}; |