Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 89 additions & 134 deletions commands/dalle/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
const BASE_NEGATIVE_PROMPT = [
"(deformed distorted disfigured:1.3)",
"poorly drawn",
"bad anatomy",
"wrong anatomy",
"extra limb",
"missing limb",
"floating limbs",
"(mutated hands and fingers:1.4)",
"disconnected limbs",
"mutation",
"mutated",
"ugly",
"disgusting",
"blurry",
"amputation",
"(NSFW:1.25)"
].join(", ");

module.exports = {
Name: "dalle",
Aliases: [],
Expand All @@ -11,41 +30,7 @@ module.exports = {
{ name: "search", type: "string" }
],
Whitelist_Response: null,
Dynamic_Description: (async (prefix) => [
"Creates a DALL-E AI generated image, based on your prompt.",
"Alternatively, searches for an existing prompt someone else has created.",
"",

`<code>${prefix}dalle</code>`,
"Posts a link to the list of all previously generated images.",
`<a href="https://supinic.com/data/dall-e/list">https://supinic.com/data/dall-e/list</a>`,
"",

`<code>${prefix}dalle (your prompt here)</code>`,
`<code>${prefix}dalle Billy Herrington as president of the United States</code>`,
"Creates a set of nine pictures with your prompt, and posts a link to it in the chat.",
"Warning: This creation can take up to 2-5 minutes, so be patient. When the image is being generated, you cannot use any other commands until it finishes.",
"Even still, the generation service can be overloaded at times (usually in the evening EU time), in which case you'll have to try again later.",
"<u>Special cooldown: <code>60 seconds</code></u>",
"",

`<code>${prefix}dalle search:(your search here)</code>`,
`<code>${prefix}dalle search:forsen</code>`,
`<code>${prefix}dalle search:"my nightmare tonight"</code>`,
"Searches for an existing image set, based on your prompt - that someone has created before.",
"",

`<code>${prefix}dalle id:(post ID)</code>`,
`<code>${prefix}dalle id:e5832f798a41d335</code>`,
"Looks up a specific post by its ID",
"",

`<code>${prefix}dalle random:true</code>`,
"Posts a random image set that someone has created before."
]),
Code: (async function dallE (context, ...args) {
const { createEmbeds } = require("./discord-embed.js");

if (context.params.search || context.params.random || context.params.id) {
const { id, random, search } = context.params;
const image = await sb.Query.getRecordset(rs => rs
Expand Down Expand Up @@ -74,26 +59,11 @@ module.exports = {
reply: `No images found for your query!`
};
}

const discordData = {};
if (context.channel && context.platform.Name === "discord") {
const discordChannel = context.platform.client.channels.fetch(context.channel.Name);
if (discordChannel && discordChannel.members && discordChannel.members.size <= 1000) {
discordData.embeds = createEmbeds(image.ID, {
prompt: image.Prompt,
created: image.Created,
creationTime: image.Creation_Time
});
}
}

return {
reply: `https://supinic.com/data/dall-e/detail/${image.ID} DALL-E image set for "${image.Prompt}"`,
discord: discordData
reply: `https://supinic.com/data/dall-e/detail/${image.ID} DALL-E image set for "${image.Prompt}"`
};
}

const pending = require("./pending.js");
const query = args.join(" ");
if (!query) {
return {
Expand All @@ -102,64 +72,39 @@ module.exports = {
};
}

const pendingResult = pending.check(context.user, context.channel);
if (!pendingResult.success) {
return pendingResult;
}

const [waitingEmote, loadingEmote] = await Promise.all([
context.getBestAvailableEmote(
["PauseChamp", "pajaPause", "CoolStoryBob", "GivePLZ"],
"😴",
{ shuffle: true }
),
context.getBestAvailableEmote(
["ppCircle", "supiniLoading", "dankCircle", "ppAutismo", "pajaAAAAAAAAAAA"],
"⌛",
{ shuffle: true }
)
]);

const mentionUsername = (context.getMentionStatus())
? `${context.user.Name},`
: "";

pending.set(context.user, context.channel);

const notificationTimeout = setTimeout(async (timeoutContext) => {
await timeoutContext.sendIntermediateMessage(sb.Utils.tag.trim `
${mentionUsername}
Processing...
${waitingEmote} ${loadingEmote}
Please wait up to 5 minutes.
`);
}, 2000, context);

const start = process.hrtime.bigint();
const response = await sb.Got("FakeAgent", {
url: "https://bf.dallemini.ai/generate",
const sessionHash = sb.Utils.randomString(12);
await sb.Got("FakeAgent", {
method: "POST",
url: "https://ehristoforu-dalle-3-xl-lora-v2.hf.space/queue/join",
responseType: "json",
headers: {
Referer: "https://hf.space/"
},
json: {
prompt: query
},
timeout: {
request: 300_000
},
throwHttpErrors: false
data: [
query,
BASE_NEGATIVE_PROMPT,
true,
2097616390,
1024,
1024,
6,
true
],
event_data: null,
fn_index: 3,
trigger_id: 6,
session_hash: sessionHash
}
});

pending.unset(context.user, context.channel);
const response = await sb.Got("FakeAgent", {
url: "https://ehristoforu-dalle-3-xl-lora-v2.hf.space/queue/data",
responseType: "text",
searchParams: {
session_hash: sessionHash
}
});

const nanoExecutionTime = process.hrtime.bigint() - start;
if (response.statusCode !== 200) {
clearTimeout(notificationTimeout);

if (response.statusCode === 429 || response.statusCode === 503) {
pending.setOverloaded();
return {
success: false,
reply: `The service is currently overloaded! Try again later. (status code ${response.statusCode})`
Expand All @@ -174,38 +119,18 @@ module.exports = {
}
}

const { images } = response.body;
const hash = require("crypto").createHash("sha512");
for (const base64Image of images) {
hash.update(base64Image);
const dataSlice = response.body.split("data:").at(-1);
const data = JSON.parse(dataSlice.trim());
const url = data?.output?.data?.[0]?.[0]?.image.url;
if (!url) {
console.warn("Unexpected output", { body: response.body, data, url });
return {
success: false,
reply: "No image extracted SadCat"
};
}

const jsonImageData = images.map(i => i.replace(/\\n/g, ""));
const row = await sb.Query.getRow("data", "DALL-E");
const ID = hash.digest().toString("hex").slice(0, 16);
const created = new sb.Date();
const creationTime = (Number(nanoExecutionTime) / 1e9);

row.setValues({
ID,
User_Alias: context.user.ID,
Channel: context.channel?.ID ?? null,
Prompt: query,
Created: created,
Creation_Time: creationTime,
Data: JSON.stringify(jsonImageData)
});

await row.save({ skipLoad: true });

const discordData = {};
if (context.platform.Name === "discord") {
discordData.embeds = createEmbeds(ID, {
prompt: query,
created,
creationTime
});
}
// @todo add database column/table for new style images (also include a type column)

return {
cooldown: {
Expand All @@ -214,9 +139,39 @@ module.exports = {
channel: null,
length: 60_000
},
discord: discordData,
reply: `https://supinic.com/data/dall-e/detail/${ID} DALL-E image set for prompt "${query}"`,
removeEmbeds: true
reply: `${url} DALL-E image set for prompt "${query}"`
};
})
}),
Dynamic_Description: (async (prefix) => [
"Creates a DALL-E AI generated image, based on your prompt.",
"Alternatively, searches for an existing prompt someone else has created.",
"",

`<code>${prefix}dalle</code>`,
"Posts a link to the list of all previously generated images.",
`<a href="https://supinic.com/data/dall-e/list">https://supinic.com/data/dall-e/list</a>`,
"",

`<code>${prefix}dalle (your prompt here)</code>`,
`<code>${prefix}dalle Billy Herrington as president of the United States</code>`,
"Creates a set of nine pictures with your prompt, and posts a link to it in the chat.",
"Warning: This creation can take up to 2-5 minutes, so be patient. When the image is being generated, you cannot use any other commands until it finishes.",
"Even still, the generation service can be overloaded at times (usually in the evening EU time), in which case you'll have to try again later.",
"<u>Special cooldown: <code>60 seconds</code></u>",
"",

`<code>${prefix}dalle search:(your search here)</code>`,
`<code>${prefix}dalle search:forsen</code>`,
`<code>${prefix}dalle search:"my nightmare tonight"</code>`,
"Searches for an existing image set, based on your prompt - that someone has created before.",
"",

`<code>${prefix}dalle id:(post ID)</code>`,
`<code>${prefix}dalle id:e5832f798a41d335</code>`,
"Looks up a specific post by its ID",
"",

`<code>${prefix}dalle random:true</code>`,
"Posts a random image set that someone has created before."
])
};