From 97e713638f32f7922f76304b9a930fb15ed37537 Mon Sep 17 00:00:00 2001 From: ImDarkTom Date: Sun, 14 Jan 2024 16:01:26 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20basic=20and=20advanced=20image=20ge?= =?UTF-8?q?neration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdConfig.json.example | 52 +++------ src/commands/imageCreation/generate.js | 150 ++++++++++++++++++++----- src/index.js | 3 +- src/utils/SD/sendRequest.js | 1 - 4 files changed, 138 insertions(+), 68 deletions(-) diff --git a/sdConfig.json.example b/sdConfig.json.example index 37aea79..d34002b 100644 --- a/sdConfig.json.example +++ b/sdConfig.json.example @@ -9,36 +9,23 @@ "defaultSteps": 20, "defaultUpscalerIndex": 5 }, - "cfg_choices": [ - { - "name": "Default", - "value": 7, - "default": true - }, - { - "name": "High", - "value": 4 - }, - { - "name": "Low", - "value": 10 - } - ], - "steps_choices": [ - { - "name": "Default", - "value": 20, - "default": true - }, - { - "name": "Enhanced", - "value": 35 - }, - { - "name": "High", - "value": 50 - } - ], + "presets": { + "performance": [ + { + "name": "Speed", + "value": "30,DPM++ 2M Karras,7", + "default": true + }, + { + "name": "Quality", + "value": "60,DPM++ 2M Karras,7" + }, + { + "name": "Extreme Speed", + "value": "10,DDIM,8" + } + ] + }, "upscalers": [ { "name": "Lanczos", @@ -59,8 +46,7 @@ }, { "name": "R-ESRGAN_4x+", - "value": 5, - "default": true + "value": 5 }, { "name": "R-ESRGAN 4x+ Anime6B", @@ -80,7 +66,6 @@ } ], "extensionConfigs": { - "controlnet": { "enabled": false, "controlnetModels": [ @@ -102,7 +87,6 @@ } ] }, - "autoTlsHttps": { "enabled": false, "certFilePath": "path/to/webui.cert", diff --git a/src/commands/imageCreation/generate.js b/src/commands/imageCreation/generate.js index 1e1fff7..dbb3ec2 100644 --- a/src/commands/imageCreation/generate.js +++ b/src/commands/imageCreation/generate.js @@ -10,24 +10,79 @@ module.exports = { // testOnly: Boolean, options: [ { - name: 'prompt', - description: 'What to generate.', - required: true, - type: ApplicationCommandOptionType.String, + name: 'basic', + description: 'Generate an image.', + type: ApplicationCommandOptionType.Subcommand, + options: [ + { + name: 'prompt', + description: 'What to generate.', + required: true, + type: ApplicationCommandOptionType.String, + }, + { + name: 'performance', + description: "What generation performance should be focused on.", + required: true, + type: ApplicationCommandOptionType.String, + choices: sdConfig.presets.performance + } + ] }, { - name: 'steps', - description: "How much time to spend generating.", - required: false, - type: ApplicationCommandOptionType.Integer, - choices: sdConfig.steps_choices - }, - { - name: 'cfg', - description: "How close the image should be to the prompt.", - required: false, - type: ApplicationCommandOptionType.Integer, - choices: sdConfig.cfg_choices + name: 'advanced', + description: 'Fine-tune the generation parameters.', + type: ApplicationCommandOptionType.Subcommand, + options: [ + { + name: 'prompt', + description: 'What to generate.', + required: true, + type: ApplicationCommandOptionType.String + }, + { + name: 'negative_prompt', + description: "What to avoid generating.", + required: false, + type: ApplicationCommandOptionType.String + }, + { + name: 'seed', + description: "Used to randomise images. Using the same seed multiple times gives same results.", + required: false, + type: ApplicationCommandOptionType.Integer + }, + { + name: 'width', + description: "Image width.", + required: false, + type: ApplicationCommandOptionType.Integer + }, + { + name: 'height', + description: "Image height.", + required: false, + type: ApplicationCommandOptionType.Integer + }, + { + name: 'steps', + description: "Number of sampling steps for the denoising process. More gives higher quality but also takes longer.", + required: false, + type: ApplicationCommandOptionType.Integer + }, + { + name: 'cfg_scale', + description: "How much the model should stick to your prompt. Less = more creative, more = strictly follow prompt.", + required: false, + type: ApplicationCommandOptionType.Integer + }, + { + name: 'sampler_name', + description: "Algorithm for the denoising process. Must be full name such as DPM++ 2M Karras, Euler a, DDIM, etc.", + required: false, + type: ApplicationCommandOptionType.String + } + ] } ], @@ -37,19 +92,52 @@ module.exports = { * @param {ChatInputCommandInteraction} interaction */ callback: async (_client, interaction) => { - await generateImage( - 'sdapi/v1/txt2img', - { - prompt: interaction.options.get('prompt').value, - negative_prompt: sdConfig.generationDefaults.defaultNegativePrompt, - steps: interaction.options.get('steps')?.value || sdConfig.generationDefaults.defaultSteps, - cfg_scale: interaction.options.get('cfg')?.value || sdConfig.generationDefaults.defaultCfg - }, - interaction, - { - upscaleBtn: true, - redoBtn: true - } - ); - }, + const commandOptions = interaction.options; + const getCommandOption = (key) => commandOptions.get(key)?.value; + + const prompt = getCommandOption('prompt'); //Universally used + + const subcommand = commandOptions.getSubcommand(); + + if (subcommand == "basic") { + const performance_setting = getCommandOption('performance'); + const [preset_steps, preset_sampler, preset_cfg_scale] = performance_setting.split(','); + + await generateImage( + 'sdapi/v1/txt2img', + { + prompt: prompt, + negative_prompt: sdConfig.generationDefaults.defaultNegativePrompt, + steps: preset_steps, + cfg_scale: preset_cfg_scale, + sampler_name: preset_sampler + }, + interaction, + { + upscaleBtn: true, + redoBtn: true + } + ); + } else { + //Advanced + await generateImage( + 'sdapi/v1/txt2img', + { + prompt: prompt, + negative_prompt: getCommandOption('negative_prompt'), + seed: getCommandOption('seed'), + width: getCommandOption('width'), + height: getCommandOption('height'), + steps: getCommandOption('steps'), + cfg_scale: getCommandOption('cfg_scale'), + sampler_name: getCommandOption('sampler_name') + }, + interaction, + { + upscaleBtn: true, + redoBtn: true + } + ); + } + } }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index f51aa6d..ba4b4bf 100644 --- a/src/index.js +++ b/src/index.js @@ -42,9 +42,8 @@ const client = new Client({ } catch (err) { if (err.code === 'ENOENT') { - console.warn("⚠ SSL cert files not set in config, ignoring SSL...") + console.warn("⚠ SSL cert files not set in config or unavailable, ignoring SSL...") process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; - } } diff --git a/src/utils/SD/sendRequest.js b/src/utils/SD/sendRequest.js index 9c6b428..12920c6 100644 --- a/src/utils/SD/sendRequest.js +++ b/src/utils/SD/sendRequest.js @@ -3,7 +3,6 @@ const axios = require('axios'); /** * Make a request to the SD API. - * * @param {string} path API URL path e.g 'sdapi/v1/txt2img' * @param {object} data Data to be sent with request. * @param {import('axios').Method} method