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';
@ -74,9 +72,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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);
@ -128,9 +126,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -154,9 +152,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -179,9 +177,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -198,9 +196,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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);
@ -259,9 +257,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
app.post('/api/sd/generate', jsonParser, async (request, response) => {
router.post('/generate', jsonParser, async (request, response) => {
try {
console.log('SD WebUI request:', request.body);
@ -289,9 +287,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -321,13 +319,13 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
/**
/**
* 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) {
@ -355,9 +353,11 @@ function registerEndpoints(app, jsonParser) {
console.warn('Failed to load transformers.js pipeline.');
return response.send({ prompt: originalPrompt });
}
});
});
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';
@ -372,9 +372,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -390,9 +390,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -407,9 +407,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -425,9 +425,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -443,9 +443,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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);
@ -453,9 +453,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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)) {
@ -470,9 +470,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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))}`,
@ -485,9 +485,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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)) {
@ -498,9 +498,9 @@ function registerEndpoints(app, jsonParser) {
console.log(error);
return response.sendStatus(500);
}
});
});
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';
@ -543,9 +543,8 @@ function registerEndpoints(app, jsonParser) {
} catch (error) {
return response.sendStatus(500);
}
});
}
});
module.exports = {
registerEndpoints,
};
router.use('/comfy', comfy);
module.exports = { router };