add nodejs sdapi example

This commit is contained in:
Vladimir Mandic
2023-09-29 08:23:39 -04:00
parent 8fe850332b
commit fca8680521
3 changed files with 64 additions and 1 deletions
+61
View File
@@ -0,0 +1,61 @@
// simple nodejs script to test sdnext api
const fs = require('fs');
const process = require('process');
const sd_url = process.env.SDAPI_URL || 'http://127.0.0.1:7860';
const sd_username = process.env.SDAPI_USR;
const sd_password = process.env.SDAPI_PWD;
const sd_options = {
// first pass
prompt: 'city at night',
negative_prompt: 'foggy, blurry',
sampler_name: 'UniPC',
seed: -1,
steps: 20,
batch_size: 1,
n_iter: 1,
cfg_scale: 6,
width: 512,
height: 512,
// enable second pass
enable_hr: true,
// second pass: upscale
hr_upscaler: 'SCUNet GAN',
hr_scale: 2.0,
// second pass: hires
hr_force: true,
hr_second_pass_steps: 20,
latent_sampler: 'UniPC',
denoising_strength: 0.5,
// second pass: refiner
refiner_steps: 5,
refiner_start: 0.8,
refiner_prompt: '',
refiner_negative: '',
// api return options
save_images: false,
send_images: true,
};
async function main() {
const method = 'POST';
const headers = new Headers();
const body = JSON.stringify(sd_options);
headers.set('Content-Type', 'application/json');
if (sd_username && sd_password) headers.set({ Authorization: `Basic ${btoa('sd_username:sd_password')}` });
const res = await fetch(`${sd_url}/sdapi/v1/txt2img`, { method, headers, body });
if (res.status !== 200) {
console.log('Error', res.status);
} else {
const json = await res.json();
console.log('result:', json.info);
for (const i in json.images) {
const f = `/tmp/test-{${i}.jpg`;
fs.writeFileSync(f, atob(json.images[i]), 'binary');
console.log('image saved:', f);
}
}
}
main();
+2
View File
@@ -43,6 +43,8 @@ def setup_middleware(app: FastAPI, cmd_opts):
res.headers["X-Process-Time"] = duration
endpoint = req.scope.get('path', 'err')
if (cmd_opts.api_log or cmd_opts.api_only) and endpoint.startswith('/sdapi'):
if endpoint.endswith('/sdapi/v1/log'):
return res
log.info('API {t} {code} {prot}/{ver} {method} {endpoint} {cli} {duration}'.format( # pylint: disable=consider-using-f-string, logging-format-interpolation
t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
code = res.status_code,