better pics
This commit is contained in:
parent
fddcb6d629
commit
89c0ac4e5e
2 changed files with 54 additions and 18 deletions
|
@ -1,2 +1,4 @@
|
|||
# Tokens
|
||||
DISCORD_TOKEN=
|
||||
OPENAI_API_KEY=
|
||||
STABILITY_API_KEY=
|
|
@ -1,12 +1,6 @@
|
|||
import { ApplyOptions } from '@sapphire/decorators';
|
||||
import { Args, BucketScope, Command } from '@sapphire/framework';
|
||||
import { Message } from 'discord.js';
|
||||
import { Configuration, OpenAIApi } from 'openai';
|
||||
|
||||
const configuration = new Configuration({
|
||||
apiKey: process.env.OPENAI_API_KEY
|
||||
});
|
||||
const openai = new OpenAIApi(configuration);
|
||||
import { AttachmentBuilder, Message } from 'discord.js';
|
||||
|
||||
@ApplyOptions<Command.Options>({
|
||||
description: 'Make a picture!',
|
||||
|
@ -45,13 +39,38 @@ export class UserCommand extends Command {
|
|||
? await interactionOrMessage.channel.send({ content: '🤔 Thinking... 🤔' })
|
||||
: await interactionOrMessage.reply({ content: '🤔 Thinking... 🤔', fetchReply: true });
|
||||
|
||||
const imageResponse = await openai.createImage({
|
||||
prompt,
|
||||
n: 1,
|
||||
size: '512x512'
|
||||
const response = await fetch(`https://api.stability.ai/v1/generation/stable-diffusion-xl-beta-v2-2-2/text-to-image`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'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<{
|
||||
base64: string;
|
||||
seed: number;
|
||||
finishReason: string;
|
||||
}>;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const content = `Prompt: ${prompt}` || 'ERROR!';
|
||||
|
||||
if (interactionOrMessage instanceof Message) {
|
||||
return askMessage.edit({ content });
|
||||
|
@ -60,5 +79,20 @@ export class UserCommand extends Command {
|
|||
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]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue