Add GGUF models and denoise parameter for ComfyUI

5992117904ed9be5af20c2e8582a979a37235cdd

ceruleandeep <deep@cerulean.navy>

4 files changed, +34 -15Ignore whitespace
public/scripts/extensions/stable-diffusion/comfyWorkflowEditor.html+1 -0
@@ -17,6 +17,7 @@
17 <li data-placeholder="scheduler" class="sd_comfy_workflow_editor_not_found">"%scheduler%"</li>17 <li data-placeholder="scheduler" class="sd_comfy_workflow_editor_not_found">"%scheduler%"</li>
18 <li data-placeholder="steps" class="sd_comfy_workflow_editor_not_found">"%steps%"</li>18 <li data-placeholder="steps" class="sd_comfy_workflow_editor_not_found">"%steps%"</li>
19 <li data-placeholder="scale" class="sd_comfy_workflow_editor_not_found">"%scale%"</li>19 <li data-placeholder="scale" class="sd_comfy_workflow_editor_not_found">"%scale%"</li>
20 <li data-placeholder="denoise" class="sd_comfy_workflow_editor_not_found">"%denoise%"</li>
20 <li data-placeholder="clip_skip" class="sd_comfy_workflow_editor_not_found">"%clip_skip%"</li>21 <li data-placeholder="clip_skip" class="sd_comfy_workflow_editor_not_found">"%clip_skip%"</li>
21 <li data-placeholder="width" class="sd_comfy_workflow_editor_not_found">"%width%"</li>22 <li data-placeholder="width" class="sd_comfy_workflow_editor_not_found">"%width%"</li>
22 <li data-placeholder="height" class="sd_comfy_workflow_editor_not_found">"%height%"</li>23 <li data-placeholder="height" class="sd_comfy_workflow_editor_not_found">"%height%"</li>
public/scripts/extensions/stable-diffusion/index.js+4 -0
@@ -3269,6 +3269,10 @@ async function generateComfyImage(prompt, negativePrompt, signal) {
32693269
3270 const seed = extension_settings.sd.seed >= 0 ? extension_settings.sd.seed : Math.round(Math.random() * Number.MAX_SAFE_INTEGER);3270 const seed = extension_settings.sd.seed >= 0 ? extension_settings.sd.seed : Math.round(Math.random() * Number.MAX_SAFE_INTEGER);
3271 workflow = workflow.replaceAll('"%seed%"', JSON.stringify(seed));3271 workflow = workflow.replaceAll('"%seed%"', JSON.stringify(seed));
3272
3273 const denoising_strength = extension_settings.sd.denoising_strength === undefined ? 1.0 : extension_settings.sd.denoising_strength;
3274 workflow = workflow.replaceAll('"%denoise%"', JSON.stringify(denoising_strength));
3275
3272 placeholders.forEach(ph => {3276 placeholders.forEach(ph => {
3273 workflow = workflow.replaceAll(`"%${ph}%"`, JSON.stringify(extension_settings.sd[ph]));3277 workflow = workflow.replaceAll(`"%${ph}%"`, JSON.stringify(extension_settings.sd[ph]));
3274 });3278 });
public/scripts/extensions/stable-diffusion/settings.html+1 -1
@@ -319,7 +319,7 @@
319 <input class="neo-range-input" type="number" id="sd_hr_scale_value" data-for="sd_hr_scale" min="{{hr_scale_min}}" max="{{hr_scale_max}}" step="{{hr_scale_step}}" value="{{hr_scale}}" >319 <input class="neo-range-input" type="number" id="sd_hr_scale_value" data-for="sd_hr_scale" min="{{hr_scale_min}}" max="{{hr_scale_max}}" step="{{hr_scale_step}}" value="{{hr_scale}}" >
320 </div>320 </div>
321321
322 <div class="alignitemscenter flex-container flexFlowColumn flexGrow flexShrink gap0 flexBasis48p" data-sd-source="auto,vlad">322 <div class="alignitemscenter flex-container flexFlowColumn flexGrow flexShrink gap0 flexBasis48p" data-sd-source="auto,vlad,comfy">
323 <small>323 <small>
324 <span data-i18n="Denoising strength">Denoising strength</span>324 <span data-i18n="Denoising strength">Denoising strength</span>
325 </small>325 </small>
src/endpoints/stable-diffusion.js+28 -14
@@ -7,7 +7,7 @@ import sanitize from 'sanitize-filename';
7import { sync as writeFileAtomicSync } from 'write-file-atomic';7import { sync as writeFileAtomicSync } from 'write-file-atomic';
8import FormData from 'form-data';8import FormData from 'form-data';
99
10import { getBasicAuthHeader, delay } from '../util.js';10import { delay, getBasicAuthHeader } from '../util.js';
11import { jsonParser } from '../express-common.js';11import { jsonParser } from '../express-common.js';
12import { readSecret, SECRET_KEYS } from './secrets.js';12import { readSecret, SECRET_KEYS } from './secrets.js';
1313
@@ -19,7 +19,7 @@ import { readSecret, SECRET_KEYS } from './secrets.js';
19function getComfyWorkflows(directories) {19function getComfyWorkflows(directories) {
20 return fs20 return fs
21 .readdirSync(directories.comfyWorkflows)21 .readdirSync(directories.comfyWorkflows)
22 .filter(file => file[0] != '.' && file.toLowerCase().endsWith('.json'))22 .filter(file => file[0] !== '.' && file.toLowerCase().endsWith('.json'))
23 .sort(Intl.Collator().compare);23 .sort(Intl.Collator().compare);
24}24}
2525
@@ -67,8 +67,7 @@ router.post('/upscalers', jsonParser, async (request, response) => {
6767
68 /** @type {any} */68 /** @type {any} */
69 const data = await result.json();69 const data = await result.json();
70 const names = data.map(x => x.name);70 return data.map(x => x.name);
71 return names;
72 }71 }
7372
74 async function getLatentUpscalers() {73 async function getLatentUpscalers() {
@@ -88,8 +87,7 @@ router.post('/upscalers', jsonParser, async (request, response) => {
8887
89 /** @type {any} */88 /** @type {any} */
90 const data = await result.json();89 const data = await result.json();
91 const names = data.map(x => x.name);90 return data.map(x => x.name);
92 return names;
93 }91 }
9492
95 const [upscalers, latentUpscalers] = await Promise.all([getUpscalerModels(), getLatentUpscalers()]);93 const [upscalers, latentUpscalers] = await Promise.all([getUpscalerModels(), getLatentUpscalers()]);
@@ -241,8 +239,7 @@ router.post('/set-model', jsonParser, async (request, response) => {
241 'Authorization': getBasicAuthHeader(request.body.auth),239 'Authorization': getBasicAuthHeader(request.body.auth),
242 },240 },
243 });241 });
244 const data = await result.json();242 return await result.json();
245 return data;
246 }243 }
247244
248 const url = new URL(request.body.url);245 const url = new URL(request.body.url);
@@ -274,7 +271,7 @@ router.post('/set-model', jsonParser, async (request, response) => {
274271
275 const progress = progressState['progress'];272 const progress = progressState['progress'];
276 const jobCount = progressState['state']['job_count'];273 const jobCount = progressState['state']['job_count'];
277 if (progress == 0.0 && jobCount === 0) {274 if (progress === 0.0 && jobCount === 0) {
278 break;275 break;
279 }276 }
280277
@@ -412,8 +409,18 @@ comfy.post('/models', jsonParser, async (request, response) => {
412 }409 }
413 /** @type {any} */410 /** @type {any} */
414 const data = await result.json();411 const data = await result.json();
415 return response.send(data.CheckpointLoaderSimple.input.required.ckpt_name[0].map(it => ({ value: it, text: it })));412
416 } catch (error) {413 const ckpts = data.CheckpointLoaderSimple.input.required.ckpt_name[0].map(it => ({ value: it, text: it })) || [];
414
415 // load list of GGUF unets from diffusion_models if the loader node is available
416 const ggufs = data.UnetLoaderGGUF?.input.required.unet_name[0].map(it => ({ value: it, text: `GGUF: ${it}` })) || [];
417 const models = ckpts.concat(ggufs);
418
419 // make the display names of the models somewhat presentable
420 models.forEach(it => it.text = it.text.replace(/\.[^.]*$/, '').replace(/_/g, ' '));
421
422 return response.send(models);
423 } catch (error) {
417 console.log(error);424 console.log(error);
418 return response.sendStatus(500);425 return response.sendStatus(500);
419 }426 }
@@ -550,7 +557,13 @@ comfy.post('/generate', jsonParser, async (request, response) => {
550 await delay(100);557 await delay(100);
551 }558 }
552 if (item.status.status_str === 'error') {559 if (item.status.status_str === 'error') {
553 throw new Error('ComfyUI generation did not succeed.');560 // Report node tracebacks if available
561 const errorMessages = item.status?.messages
562 ?.filter(it => it[0] === 'execution_error')
563 .map(it => it[1])
564 .map(it => `${it.node_type} [${it.node_id}] ${it.exception_type}: ${it.exception_message}`)
565 .join('\n') || '';
566 throw new Error(`ComfyUI generation did not succeed.\n\n${errorMessages}`.trim());
554 }567 }
555 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];568 const imgInfo = Object.keys(item.outputs).map(it => item.outputs[it].images).flat()[0];
556 const imgUrl = new URL(request.body.url);569 const imgUrl = new URL(request.body.url);
@@ -563,8 +576,9 @@ comfy.post('/generate', jsonParser, async (request, response) => {
563 const imgBuffer = await imgResponse.buffer();576 const imgBuffer = await imgResponse.buffer();
564 return response.send(imgBuffer.toString('base64'));577 return response.send(imgBuffer.toString('base64'));
565 } catch (error) {578 } catch (error) {
566 console.log(error);579 console.log('ComfyUI error:', error);
567 return response.sendStatus(500);580 response.status(500).send(`${error.message}`);
581 return response;
568 }582 }
569});583});
570584