Skip to content

Commit

Permalink
✨ basic and advanced image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ImDarkTom committed Jan 14, 2024
1 parent 07b184b commit 97e7136
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 68 deletions.
52 changes: 18 additions & 34 deletions sdConfig.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -59,8 +46,7 @@
},
{
"name": "R-ESRGAN_4x+",
"value": 5,
"default": true
"value": 5
},
{
"name": "R-ESRGAN 4x+ Anime6B",
Expand All @@ -80,7 +66,6 @@
}
],
"extensionConfigs": {

"controlnet": {
"enabled": false,
"controlnetModels": [
Expand All @@ -102,7 +87,6 @@
}
]
},

"autoTlsHttps": {
"enabled": false,
"certFilePath": "path/to/webui.cert",
Expand Down
150 changes: 119 additions & 31 deletions src/commands/imageCreation/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
],

Expand All @@ -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
}
);
}
}
};
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

}
}

Expand Down
1 change: 0 additions & 1 deletion src/utils/SD/sendRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 97e7136

Please sign in to comment.