Fix image generation
Some checks failed
Build and Publish Docker Image / build_and_push (push) Failing after 37s

This commit is contained in:
Jack Hadrill
2024-12-10 21:57:52 +00:00
parent e9f7b6e28e
commit ab9d66af54
3 changed files with 167 additions and 103 deletions

View File

@@ -2,10 +2,10 @@ import {
SlashCommandBuilder,
AttachmentBuilder,
ChatInputCommandInteraction,
} from 'discord.js';
import axios from 'axios';
import Replicate, { Prediction } from 'replicate';
import config from '../config';
} from "discord.js";
import axios from "axios";
import Replicate, { Prediction } from "replicate";
import config from "../config";
const replicate = new Replicate({
auth: config.replicateApiKey,
@@ -13,12 +13,12 @@ const replicate = new Replicate({
// Initialise the command data.
export const data = new SlashCommandBuilder()
.setName('image')
.setDescription('Generate an image based on a prompt.')
.setName("image")
.setDescription("Generate an image based on a prompt.")
.addStringOption((option) =>
option
.setName('prompt')
.setDescription('The prompt to generate an image from')
.setName("prompt")
.setDescription("The prompt to generate an image from")
.setRequired(true)
);
@@ -34,12 +34,12 @@ export async function execute(
): Promise<void> {
await interaction.deferReply();
const prompt = interaction.options.get('prompt')?.value as string;
const prompt = interaction.options.get("prompt")?.value as string;
try {
// Create image generation prediction
const prediction = await replicate.predictions.create({
model: 'black-forest-labs/flux-1.1-pro-ultra',
model: "black-forest-labs/flux-1.1-pro-ultra",
input: { prompt },
});
@@ -47,17 +47,17 @@ export async function execute(
const completedPrediction = await pollPredictionStatus(prediction.id);
if (!completedPrediction || !completedPrediction.output) {
throw new Error('Failed to generate the image.');
throw new Error("Failed to generate the image.");
}
const imageUrl = completedPrediction.output[0];
const imageUrl = completedPrediction.output;
// Download the generated image
const imageBuffer = await downloadImage(imageUrl);
// Create an attachment to send the image back to the user
const attachment = new AttachmentBuilder(imageBuffer, {
name: 'image.jpg',
name: "image.jpg",
});
// Edit the deferred reply to include the generated image
@@ -85,8 +85,8 @@ async function pollPredictionStatus(
const latestPrediction = await replicate.predictions.get(predictionId);
if (
latestPrediction.status !== 'starting' &&
latestPrediction.status !== 'processing'
latestPrediction.status !== "starting" &&
latestPrediction.status !== "processing"
) {
return latestPrediction;
}
@@ -95,7 +95,7 @@ async function pollPredictionStatus(
await new Promise((resolve) => setTimeout(resolve, interval));
}
throw new Error('Prediction timed out.');
throw new Error("Prediction timed out.");
}
/**
@@ -105,9 +105,9 @@ async function pollPredictionStatus(
*/
async function downloadImage(url: string): Promise<Buffer> {
try {
const response = await axios.get(url, { responseType: 'arraybuffer' });
const response = await axios.get(url, { responseType: "arraybuffer" });
return Buffer.from(response.data);
} catch (error) {
throw new Error('Failed to download the image.');
throw new Error("Failed to download the image.");
}
}