Use Express router for stable diffusion endpoint

This commit is contained in:
valadaptive 2023-12-04 12:58:02 -05:00
parent 414c9bd5fb
commit 7f129df7b8
2 changed files with 466 additions and 467 deletions

View File

@ -3606,7 +3606,7 @@ require('./src/endpoints/sprites').registerEndpoints(app, jsonParser, urlencoded
require('./src/endpoints/content-manager').registerEndpoints(app, jsonParser);
// Stable Diffusion generation
require('./src/endpoints/stable-diffusion').registerEndpoints(app, jsonParser);
app.use('/api/sd', require('./src/endpoints/stable-diffusion').router);
// LLM and SD Horde generation
require('./src/endpoints/horde').registerEndpoints(app, jsonParser);

View File

@ -1,9 +1,11 @@
const express = require('express');
const fetch = require('node-fetch').default;
const sanitize = require('sanitize-filename');
const { getBasicAuthHeader, delay } = require('../util.js');
const fs = require('fs');
const { DIRECTORIES } = require('../constants.js');
const writeFileAtomicSync = require('write-file-atomic').sync;
const { jsonParser } = require('../express-common');
/**
* Sanitizes a string.
@ -47,13 +49,9 @@ function getComfyWorkflows() {
.sort(Intl.Collator().compare);
}
/**
* Registers the endpoints for the Stable Diffusion API extension.
* @param {import("express").Express} app Express app
* @param {any} jsonParser JSON parser middleware
*/
function registerEndpoints(app, jsonParser) {
app.post('/api/sd/ping', jsonParser, async (request, response) => {
const router = express.Router();
router.post('/ping', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/options';
@ -76,7 +74,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/upscalers', jsonParser, async (request, response) => {
router.post('/upscalers', jsonParser, async (request, response) => {
try {
async function getUpscalerModels() {
const url = new URL(request.body.url);
@ -130,7 +128,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/samplers', jsonParser, async (request, response) => {
router.post('/samplers', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/samplers';
@ -156,7 +154,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/models', jsonParser, async (request, response) => {
router.post('/models', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/sd-models';
@ -181,7 +179,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/get-model', jsonParser, async (request, response) => {
router.post('/get-model', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/options';
@ -200,7 +198,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/set-model', jsonParser, async (request, response) => {
router.post('/set-model', jsonParser, async (request, response) => {
try {
async function getProgress() {
const url = new URL(request.body.url);
@ -261,7 +259,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/generate', jsonParser, async (request, response) => {
router.post('/generate', jsonParser, async (request, response) => {
try {
console.log('SD WebUI request:', request.body);
@ -291,7 +289,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/sd-next/upscalers', jsonParser, async (request, response) => {
router.post('/sd-next/upscalers', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/sdapi/v1/upscalers';
@ -327,7 +325,7 @@ function registerEndpoints(app, jsonParser) {
* SD prompt expansion using GPT-2 text generation model.
* Adapted from: https://github.com/lllyasviel/Fooocus/blob/main/modules/expansion.py
*/
app.post('/api/sd/expand', jsonParser, async (request, response) => {
router.post('/expand', jsonParser, async (request, response) => {
const originalPrompt = request.body.prompt;
if (!originalPrompt) {
@ -357,7 +355,9 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/ping', jsonParser, async (request, response) => {
const comfy = express.Router();
comfy.post('/ping', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/system_stats';
@ -374,7 +374,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/samplers', jsonParser, async (request, response) => {
comfy.post('/samplers', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/object_info';
@ -392,7 +392,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/models', jsonParser, async (request, response) => {
comfy.post('/models', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/object_info';
@ -409,7 +409,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/schedulers', jsonParser, async (request, response) => {
comfy.post('/schedulers', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/object_info';
@ -427,7 +427,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/vaes', jsonParser, async (request, response) => {
comfy.post('/vaes', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/object_info';
@ -445,7 +445,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/workflows', jsonParser, async (request, response) => {
comfy.post('/workflows', jsonParser, async (request, response) => {
try {
const data = getComfyWorkflows();
return response.send(data);
@ -455,7 +455,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/workflow', jsonParser, async (request, response) => {
comfy.post('/workflow', jsonParser, async (request, response) => {
try {
let path = `${DIRECTORIES.comfyWorkflows}/${sanitize(String(request.body.file_name))}`;
if (!fs.existsSync(path)) {
@ -472,7 +472,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/save-workflow', jsonParser, async (request, response) => {
comfy.post('/save-workflow', jsonParser, async (request, response) => {
try {
writeFileAtomicSync(
`${DIRECTORIES.comfyWorkflows}/${sanitize(String(request.body.file_name))}`,
@ -487,7 +487,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/delete-workflow', jsonParser, async (request, response) => {
comfy.post('/delete-workflow', jsonParser, async (request, response) => {
try {
let path = `${DIRECTORIES.comfyWorkflows}/${sanitize(String(request.body.file_name))}`;
if (fs.existsSync(path)) {
@ -500,7 +500,7 @@ function registerEndpoints(app, jsonParser) {
}
});
app.post('/api/sd/comfy/generate', jsonParser, async (request, response) => {
comfy.post('/generate', jsonParser, async (request, response) => {
try {
const url = new URL(request.body.url);
url.pathname = '/prompt';
@ -544,8 +544,7 @@ function registerEndpoints(app, jsonParser) {
return response.sendStatus(500);
}
});
}
module.exports = {
registerEndpoints,
};
router.use('/comfy', comfy);
module.exports = { router };