better pics

This commit is contained in:
Atridad Lahiji 2023-06-23 20:53:11 -06:00
parent fddcb6d629
commit 89c0ac4e5e
No known key found for this signature in database
2 changed files with 54 additions and 18 deletions

View file

@ -1,2 +1,4 @@
# Tokens # Tokens
DISCORD_TOKEN= DISCORD_TOKEN=
OPENAI_API_KEY=
STABILITY_API_KEY=

View file

@ -1,12 +1,6 @@
import { ApplyOptions } from '@sapphire/decorators'; import { ApplyOptions } from '@sapphire/decorators';
import { Args, BucketScope, Command } from '@sapphire/framework'; import { Args, BucketScope, Command } from '@sapphire/framework';
import { Message } from 'discord.js'; import { AttachmentBuilder, Message } from 'discord.js';
import { Configuration, OpenAIApi } from 'openai';
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
});
const openai = new OpenAIApi(configuration);
@ApplyOptions<Command.Options>({ @ApplyOptions<Command.Options>({
description: 'Make a picture!', description: 'Make a picture!',
@ -45,20 +39,60 @@ export class UserCommand extends Command {
? await interactionOrMessage.channel.send({ content: '🤔 Thinking... 🤔' }) ? await interactionOrMessage.channel.send({ content: '🤔 Thinking... 🤔' })
: await interactionOrMessage.reply({ content: '🤔 Thinking... 🤔', fetchReply: true }); : await interactionOrMessage.reply({ content: '🤔 Thinking... 🤔', fetchReply: true });
const imageResponse = await openai.createImage({ const response = await fetch(`https://api.stability.ai/v1/generation/stable-diffusion-xl-beta-v2-2-2/text-to-image`, {
prompt, method: 'POST',
n: 1, headers: {
size: '512x512' 'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${process.env.STABILITY_API_KEY}`
},
body: JSON.stringify({
text_prompts: [
{
text: prompt
}
],
cfg_scale: 7,
clip_guidance_preset: 'FAST_BLUE',
height: 512,
width: 512,
samples: 1,
steps: 50
})
}); });
const content = `Prompt: ${prompt}\nResult: ${imageResponse.data.data[0].url}` || 'ERROR!'; interface GenerationResponse {
artifacts: Array<{
if (interactionOrMessage instanceof Message) { base64: string;
return askMessage.edit({ content }); seed: number;
finishReason: string;
}>;
} }
return interactionOrMessage.editReply({ if (!response.ok) {
content: content const content = `Prompt: ${prompt}` || 'ERROR!';
});
if (interactionOrMessage instanceof Message) {
return askMessage.edit({ content });
}
return interactionOrMessage.editReply({
content: content
});
} else {
const responseJSON = (await response.json()) as GenerationResponse;
const imageAttachment = new AttachmentBuilder(Buffer.from(responseJSON.artifacts[0].base64, 'base64'));
const content = `Prompt: ${prompt}` || 'ERROR!';
if (interactionOrMessage instanceof Message) {
return askMessage.edit({ content, files: [imageAttachment] });
}
return interactionOrMessage.editReply({
content: content,
files: [imageAttachment]
});
}
} }
} }