mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-11-29 03:26:12 +00:00
A request that ran the machine out of memory ended in a 500 and left the
server on the configuration that had just failed, so every later request
of that size failed the same way. The context now belongs to a
ContextKeeper (examples/server/recovery.*) and every generation of the
OpenAI, sdapi and sdcpp endpoints goes through ContextKeeper::run().
On out_of_memory or backend (the codes of sd_get_last_error()) it frees
the model, loads it again with the next lighter profile of
max > balanced > lite > lite-disk > lite-cpu and runs the same request
again. Steps that would load the same configuration are skipped, a
device failure jumps to lite-cpu, and from lite on the VAE is decoded in
tiles whatever the request asked. --profile sets the top of the ladder.
After --degradation-cooldown seconds (300; 0 = never) the server climbs
back one profile at the start of the next request. A climb that fails
on its first use is undone and doubles the wait, up to an hour. If the
model cannot be loaded again, requests get 503 "unavailable" and the
load is retried no more than every 15 seconds. --disable-degradation
keeps the old behavior of answering with the error.
Failures are answered with 503 and Retry-After (out of memory, no
context), 400 (invalid_argument) or 500, with a body of
{"error", "code", "retry_after_seconds"}. Async jobs report the same
code in error.code, 429 carries Retry-After, and std::bad_alloc in a
handler becomes a 503. New endpoints: GET /sdcpp/v1/health (status,
profile, counters, last error, queue; it never waits for a running
generation) and GET /sdcpp/v1/resources (system memory and devices).
sd-server now exits with code 1 and a message when it cannot listen or
create its HTTP threads; the second used to end in std::terminate and
the first in a silent exit 0. The lite-disk profile clears --eager-load,
which read every weight into memory and so saved nothing.
test/server_recovery_test.cpp checks the ladder, the step downs, the
climb back, the reload after a failure, the HTTP answers and use from
several threads with a scripted library and clock (114 checks, also
under ASan/UBSan and TSan). Checked with a synthetic SD 1.5 model on
the CPU under address-space limits: a 384x384 request at 2400 MiB steps
down to lite-disk and succeeds, below 2200 MiB it answers 503 and the
server stays up, the climb back and a failed climb behave as described,
and nine parallel requests of the three kinds are all served. The server
and the whole engine built with ASan/UBSan went through the ladder, the
CPU memory-guard scenarios and injected read errors without a report.
Not checked: Windows, GPUs, real models.
Claude-Session: https://claude.ai/code/session_01EhZzDBkuuMp1jBf4WtEySC
679 lines
27 KiB
C++
679 lines
27 KiB
C++
#include "routes.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <cstring>
|
|
#include <new>
|
|
#include <regex>
|
|
#include <string_view>
|
|
#include <unordered_map>
|
|
|
|
#include "common/common.h"
|
|
#include "common/media_io.h"
|
|
#include "common/resource_owners.hpp"
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
static std::string extract_and_remove_sd_cpp_extra_args(std::string& text) {
|
|
std::regex re("<sd_cpp_extra_args>(.*?)</sd_cpp_extra_args>");
|
|
std::smatch match;
|
|
|
|
std::string extracted;
|
|
if (std::regex_search(text, match, re)) {
|
|
extracted = match[1].str();
|
|
text = std::regex_replace(text, re, "");
|
|
}
|
|
return extracted;
|
|
}
|
|
|
|
static fs::path resolve_display_model_path(const ServerRuntime& runtime) {
|
|
const auto& ctx = *runtime.ctx_params;
|
|
if (!ctx.model_path.empty()) {
|
|
return fs::path(ctx.model_path);
|
|
}
|
|
if (!ctx.diffusion_model_path.empty()) {
|
|
return fs::path(ctx.diffusion_model_path);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
static std::string lower_ascii(std::string value) {
|
|
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
|
|
return static_cast<char>(std::tolower(c));
|
|
});
|
|
return value;
|
|
}
|
|
|
|
static enum sample_method_t get_sdapi_sample_method(std::string name) {
|
|
enum sample_method_t result = str_to_sample_method(name.c_str());
|
|
if (result != SAMPLE_METHOD_COUNT) {
|
|
return result;
|
|
}
|
|
|
|
name = lower_ascii(name);
|
|
static const std::unordered_map<std::string_view, sample_method_t> hardcoded{
|
|
{"euler a", EULER_A_SAMPLE_METHOD},
|
|
{"k_euler_a", EULER_A_SAMPLE_METHOD},
|
|
{"euler", EULER_SAMPLE_METHOD},
|
|
{"k_euler", EULER_SAMPLE_METHOD},
|
|
{"heun", HEUN_SAMPLE_METHOD},
|
|
{"k_heun", HEUN_SAMPLE_METHOD},
|
|
{"dpm2", DPM2_SAMPLE_METHOD},
|
|
{"k_dpm_2", DPM2_SAMPLE_METHOD},
|
|
{"lcm", LCM_SAMPLE_METHOD},
|
|
{"ddim", DDIM_TRAILING_SAMPLE_METHOD},
|
|
{"dpm++ 2m", DPMPP2M_SAMPLE_METHOD},
|
|
{"k_dpmpp_2m", DPMPP2M_SAMPLE_METHOD},
|
|
{"dpm++ 2m sde", DPMPP2M_SDE_SAMPLE_METHOD},
|
|
{"k_dpmpp_2m_sde", DPMPP2M_SDE_SAMPLE_METHOD},
|
|
{"dpm++ 2m sde gpu", DPMPP2M_SDE_BT_SAMPLE_METHOD},
|
|
{"k_dpmpp_2m_sde_gpu", DPMPP2M_SDE_BT_SAMPLE_METHOD},
|
|
{"res multistep", RES_MULTISTEP_SAMPLE_METHOD},
|
|
{"k_res_multistep", RES_MULTISTEP_SAMPLE_METHOD},
|
|
{"res 2s", RES_2S_SAMPLE_METHOD},
|
|
{"k_res_2s", RES_2S_SAMPLE_METHOD},
|
|
{"euler_cfg_pp", EULER_CFG_PP_SAMPLE_METHOD},
|
|
{"k_euler_cfg_pp", EULER_CFG_PP_SAMPLE_METHOD},
|
|
{"euler_a_cfg_pp", EULER_CFG_PP_SAMPLE_METHOD},
|
|
{"k_euler_a_cfg_pp", EULER_CFG_PP_SAMPLE_METHOD},
|
|
};
|
|
auto it = hardcoded.find(name);
|
|
return it != hardcoded.end() ? it->second : SAMPLE_METHOD_COUNT;
|
|
}
|
|
|
|
static bool build_sdapi_img_gen_request(const json& j,
|
|
ServerRuntime& runtime,
|
|
bool img2img,
|
|
ImgGenJobRequest& request,
|
|
std::string& error_message) {
|
|
std::string prompt = j.value("prompt", "");
|
|
std::string negative_prompt = j.value("negative_prompt", "");
|
|
int width = j.value("width", 512);
|
|
int height = j.value("height", 512);
|
|
int steps = j.value("steps", runtime.default_gen_params->sample_params.sample_steps);
|
|
float cfg_scale = j.value("cfg_scale", runtime.default_gen_params->sample_params.guidance.txt_cfg);
|
|
int64_t seed = j.value("seed", -1);
|
|
int batch_size = j.value("batch_size", 1);
|
|
int clip_skip = j.value("clip_skip", -1);
|
|
std::string sampler_name = j.value("sampler_name", "");
|
|
std::string scheduler_name = j.value("scheduler", "");
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
error_message = "width and height must be positive";
|
|
return false;
|
|
}
|
|
|
|
if (prompt.empty()) {
|
|
error_message = "prompt required";
|
|
return false;
|
|
}
|
|
|
|
request.gen_params = *runtime.default_gen_params;
|
|
|
|
request.gen_params.prompt = prompt;
|
|
request.gen_params.negative_prompt = negative_prompt;
|
|
request.gen_params.seed = seed;
|
|
request.gen_params.sample_params.sample_steps = steps;
|
|
request.gen_params.batch_count = batch_size;
|
|
request.gen_params.sample_params.guidance.txt_cfg = cfg_scale;
|
|
request.gen_params.width = j.value("width", -1);
|
|
request.gen_params.height = j.value("height", -1);
|
|
|
|
if (!img2img && j.value("enable_hr", false)) {
|
|
request.gen_params.hires_enabled = true;
|
|
request.gen_params.hires_scale = j.value("hr_scale", request.gen_params.hires_scale);
|
|
request.gen_params.hires_width = j.value("hr_resize_x", request.gen_params.hires_width);
|
|
request.gen_params.hires_height = j.value("hr_resize_y", request.gen_params.hires_height);
|
|
request.gen_params.hires_steps = j.value("hr_steps", request.gen_params.hires_steps);
|
|
request.gen_params.hires_denoising_strength =
|
|
j.value("denoising_strength", request.gen_params.hires_denoising_strength);
|
|
|
|
request.gen_params.hires_upscaler = j.value("hr_upscaler", request.gen_params.hires_upscaler);
|
|
}
|
|
|
|
std::string sd_cpp_extra_args_str = extract_and_remove_sd_cpp_extra_args(request.gen_params.prompt);
|
|
if (!sd_cpp_extra_args_str.empty() && !request.gen_params.from_json_str(sd_cpp_extra_args_str)) {
|
|
error_message = "invalid sd_cpp_extra_args";
|
|
return false;
|
|
}
|
|
|
|
if (clip_skip > 0) {
|
|
request.gen_params.clip_skip = clip_skip;
|
|
}
|
|
|
|
enum sample_method_t sample_method = get_sdapi_sample_method(sampler_name);
|
|
if (sample_method != SAMPLE_METHOD_COUNT) {
|
|
request.gen_params.sample_params.sample_method = sample_method;
|
|
}
|
|
|
|
enum scheduler_t scheduler = str_to_scheduler(scheduler_name.c_str());
|
|
if (scheduler != SCHEDULER_COUNT) {
|
|
request.gen_params.sample_params.scheduler = scheduler;
|
|
}
|
|
|
|
if (j.contains("lora") && j["lora"].is_array()) {
|
|
request.gen_params.lora_map.clear();
|
|
request.gen_params.high_noise_lora_map.clear();
|
|
|
|
for (const auto& item : j["lora"]) {
|
|
if (!item.is_object()) {
|
|
continue;
|
|
}
|
|
|
|
std::string path = item.value("path", "");
|
|
float multiplier = item.value("multiplier", 1.0f);
|
|
bool is_high_noise = item.value("is_high_noise", false);
|
|
|
|
if (path.empty()) {
|
|
error_message = "lora.path required";
|
|
return false;
|
|
}
|
|
|
|
std::string fullpath = get_lora_full_path(runtime, path);
|
|
if (fullpath.empty()) {
|
|
error_message = "invalid lora path: " + path;
|
|
return false;
|
|
}
|
|
|
|
if (is_high_noise) {
|
|
request.gen_params.high_noise_lora_map[fullpath] += multiplier;
|
|
} else {
|
|
request.gen_params.lora_map[fullpath] += multiplier;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (j.contains("ref_image_args")) {
|
|
if (!j["ref_image_args"].is_string()) {
|
|
error_message = "ref_image_args must be a string";
|
|
return false;
|
|
}
|
|
request.gen_params.ref_image_args = j["ref_image_args"].get<std::string>();
|
|
}
|
|
|
|
if (j.contains("image_preprocess") && !request.gen_params.parse_image_preprocess_json(j["image_preprocess"].dump())) {
|
|
error_message = "invalid image_preprocess";
|
|
return false;
|
|
}
|
|
|
|
if (img2img) {
|
|
if (j.contains("init_images") && j["init_images"].is_array() && !j["init_images"].empty()) {
|
|
if (decode_base64_image(j["init_images"][0].get<std::string>(),
|
|
0,
|
|
0,
|
|
0,
|
|
request.gen_params.init_image)) {
|
|
const sd_image_t& image = request.gen_params.init_image.get();
|
|
request.gen_params.set_width_and_height_if_unset(image.width, image.height);
|
|
}
|
|
}
|
|
|
|
if (j.contains("mask") && j["mask"].is_string()) {
|
|
if (decode_base64_image(j["mask"].get<std::string>(),
|
|
1,
|
|
0,
|
|
0,
|
|
request.gen_params.mask_image)) {
|
|
const sd_image_t& image = request.gen_params.mask_image.get();
|
|
request.gen_params.set_width_and_height_if_unset(image.width, image.height);
|
|
}
|
|
sd_image_t& mask_image = request.gen_params.mask_image.get();
|
|
bool inpainting_mask_invert = j.value("inpainting_mask_invert", 0) != 0;
|
|
if (inpainting_mask_invert && mask_image.data != nullptr) {
|
|
for (uint32_t i = 0; i < mask_image.width * mask_image.height; ++i) {
|
|
mask_image.data[i] = 255 - mask_image.data[i];
|
|
}
|
|
}
|
|
} else {
|
|
request.gen_params.mask_image.reset({0, 0, 1, nullptr});
|
|
}
|
|
|
|
float denoising_strength = j.value("denoising_strength", -1.f);
|
|
if (denoising_strength >= 0.f) {
|
|
request.gen_params.strength = std::min(denoising_strength, 1.0f);
|
|
}
|
|
}
|
|
|
|
if (j.contains("extra_images") && j["extra_images"].is_array()) {
|
|
for (const auto& extra_image : j["extra_images"]) {
|
|
if (!extra_image.is_string()) {
|
|
continue;
|
|
}
|
|
SDImageOwner image_owner;
|
|
if (decode_base64_image(extra_image.get<std::string>(),
|
|
0,
|
|
0, 0,
|
|
image_owner)) {
|
|
const sd_image_t& image = image_owner.get();
|
|
request.gen_params.set_width_and_height_if_unset(image.width, image.height);
|
|
request.gen_params.ref_images.push_back(std::move(image_owner));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Intentionally disable prompt-embedded LoRA tag parsing for server APIs.
|
|
if (!request.gen_params.resolve_and_validate(IMG_GEN, "", runtime.ctx_params->hires_upscalers_dir, true)) {
|
|
error_message = "invalid params";
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static nlohmann::json prepare_info_field(const SDContextParams& ctx_params,
|
|
const SDGenerationParams& gen_params,
|
|
bool img2img) {
|
|
nlohmann::json jsoninfo = nlohmann::json::object();
|
|
jsoninfo["prompt"] = gen_params.prompt;
|
|
if (!gen_params.negative_prompt.empty()) {
|
|
jsoninfo["negative_prompt"] = gen_params.negative_prompt;
|
|
}
|
|
jsoninfo["seed"] = gen_params.seed;
|
|
jsoninfo["cfg_scale"] = gen_params.sample_params.guidance.txt_cfg;
|
|
jsoninfo["width"] = gen_params.get_resolved_width();
|
|
jsoninfo["height"] = gen_params.get_resolved_height();
|
|
jsoninfo["steps"] = gen_params.sample_params.sample_steps;
|
|
jsoninfo["sampler_name"] = sd_sample_method_name(gen_params.sample_params.sample_method);
|
|
if (gen_params.clip_skip != -1) {
|
|
jsoninfo["clip_skip"] = gen_params.clip_skip;
|
|
}
|
|
if (gen_params.sample_params.scheduler != scheduler_t::SCHEDULER_COUNT) {
|
|
jsoninfo["extra_generation_params"] = nlohmann::json::object();
|
|
jsoninfo["extra_generation_params"]["Schedule type"] = sd_scheduler_name(gen_params.sample_params.scheduler);
|
|
}
|
|
if (img2img) {
|
|
jsoninfo["denoising_strength"] = gen_params.strength;
|
|
}
|
|
// not clear what should happen if we have both model and diffusion_model
|
|
if (!ctx_params.diffusion_model_path.empty()) {
|
|
jsoninfo["sd_model_name"] = sd_basename(ctx_params.diffusion_model_path);
|
|
} else if (!ctx_params.model_path.empty()) {
|
|
jsoninfo["sd_model_name"] = sd_basename(ctx_params.model_path);
|
|
}
|
|
if (!ctx_params.vae_path.empty()) {
|
|
jsoninfo["sd_vae_name"] = sd_basename(ctx_params.vae_path);
|
|
}
|
|
jsoninfo["version"] = "stable-diffusion.cpp";
|
|
|
|
jsoninfo["infotexts"] = nlohmann::json::array();
|
|
jsoninfo["all_prompts"] = nlohmann::json::array();
|
|
jsoninfo["all_negative_prompts"] = nlohmann::json::array();
|
|
jsoninfo["all_seeds"] = nlohmann::json::array();
|
|
return jsoninfo;
|
|
}
|
|
|
|
struct SDAPIPreviewState {
|
|
std::mutex mutex;
|
|
std::string b64;
|
|
std::string id_task;
|
|
std::string job_type;
|
|
int step = 0;
|
|
int total = 0;
|
|
bool active = false;
|
|
int64_t job_count = 0;
|
|
int job_no = 0;
|
|
};
|
|
|
|
static SDAPIPreviewState g_preview;
|
|
|
|
static void preview_callback(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data) {
|
|
(void)is_noisy;
|
|
(void)data;
|
|
if (frame_count <= 0 || frames == nullptr || frames[0].data == nullptr) {
|
|
return;
|
|
}
|
|
|
|
const sd_image_t& frame = frames[0];
|
|
const sd_preview_info_t info = sd_get_preview_info();
|
|
|
|
auto image_bytes = encode_image_to_vector(EncodedImageFormat::JPEG,
|
|
frame.data,
|
|
frame.width,
|
|
frame.height,
|
|
frame.channel,
|
|
"",
|
|
80);
|
|
if (image_bytes.empty()) {
|
|
return;
|
|
}
|
|
|
|
std::string b64 = base64_encode(image_bytes);
|
|
std::lock_guard<std::mutex> lock(g_preview.mutex);
|
|
g_preview.b64 = std::move(b64);
|
|
g_preview.step = static_cast<int>(std::min<int64_t>(step < 0 ? -static_cast<int64_t>(step) : step,
|
|
std::max(0, info.total_steps)));
|
|
g_preview.total = std::max(0, info.total_steps);
|
|
g_preview.job_no = std::max(0, info.sample_pass - 1);
|
|
}
|
|
|
|
struct SDAPIPreviewGuard {
|
|
SDAPIPreviewGuard(const std::string& id_task, const sd_img_gen_params_t& params, const std::string& job_type) {
|
|
std::lock_guard<std::mutex> lock(g_preview.mutex);
|
|
g_preview.id_task = id_task;
|
|
g_preview.total = 0;
|
|
g_preview.job_type = job_type;
|
|
g_preview.b64.clear();
|
|
g_preview.step = 0;
|
|
g_preview.active = true;
|
|
// Each batch item has a base pass and, when enabled, a hires pass.
|
|
g_preview.job_count = static_cast<int64_t>(std::max(1, params.batch_count)) * (params.hires.enabled ? 2 : 1);
|
|
g_preview.job_no = 0;
|
|
sd_set_preview_callback(preview_callback, PREVIEW_PROJ, 1, true, false, nullptr);
|
|
}
|
|
|
|
~SDAPIPreviewGuard() {
|
|
sd_set_preview_callback(nullptr, PREVIEW_NONE, 1, false, false, nullptr);
|
|
std::lock_guard<std::mutex> lock(g_preview.mutex);
|
|
g_preview.active = false;
|
|
g_preview.b64.clear();
|
|
}
|
|
|
|
SDAPIPreviewGuard(const SDAPIPreviewGuard&) = delete;
|
|
SDAPIPreviewGuard& operator=(const SDAPIPreviewGuard&) = delete;
|
|
};
|
|
|
|
void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
|
|
ServerRuntime* runtime = &rt;
|
|
|
|
auto sdapi_any2img = [runtime](const httplib::Request& req, httplib::Response& res, bool img2img) {
|
|
try {
|
|
if (req.body.empty()) {
|
|
res.status = 400;
|
|
res.set_content(R"({"error":"empty body"})", "application/json");
|
|
return;
|
|
}
|
|
if (!runtime_supports_generation_mode(*runtime, IMG_GEN)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", unsupported_generation_mode_error(*runtime, IMG_GEN)}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
std::string id_task = "sdapi";
|
|
json j = json::parse(req.body);
|
|
id_task = j.value("id_task", id_task);
|
|
ImgGenJobRequest request;
|
|
std::string error_message;
|
|
if (!build_sdapi_img_gen_request(j, *runtime, img2img, request, error_message)) {
|
|
res.status = 400;
|
|
res.set_content(json({{"error", error_message}}).dump(), "application/json");
|
|
return;
|
|
}
|
|
|
|
LOG_VERBOSE("%s\n", request.gen_params.to_string().c_str());
|
|
|
|
const sd_img_gen_params_t img_gen_params = request.to_sd_img_gen_params_t();
|
|
SDImageVec results;
|
|
int num_results = 0;
|
|
|
|
GenerationFailure failure;
|
|
const bool generated = runtime->keeper->run(
|
|
[&](const ContextKeeper::ContextView& view) {
|
|
sd_img_gen_params_t params = img_gen_params;
|
|
if (view.force_vae_tiling) {
|
|
params.vae_tiling_params.enabled = true;
|
|
}
|
|
SDAPIPreviewGuard preview_guard(id_task, params, img2img ? "img2img" : "txt2img");
|
|
sd_image_t* raw_results = nullptr;
|
|
int count = 0;
|
|
if (!generate_image(view.ctx, ¶ms, &raw_results, &count)) {
|
|
return false;
|
|
}
|
|
results.adopt(raw_results, count);
|
|
num_results = count;
|
|
return true;
|
|
},
|
|
failure);
|
|
if (!generated) {
|
|
send_generation_failure(res, failure);
|
|
return;
|
|
}
|
|
|
|
if (results.empty()) {
|
|
failure = GenerationFailure();
|
|
failure.code = SD_ERROR_UNKNOWN;
|
|
failure.message = "generate_image returned no results";
|
|
send_generation_failure(res, failure);
|
|
return;
|
|
}
|
|
|
|
json out;
|
|
out["images"] = json::array();
|
|
out["parameters"] = j;
|
|
json jsoninfo = prepare_info_field(*runtime->ctx_params, request.gen_params, img2img);
|
|
|
|
int images_per_batch = request.gen_params.batch_count > 0 ? std::max(1, num_results / request.gen_params.batch_count) : 1;
|
|
for (int i = 0; i < num_results; ++i) {
|
|
if (results[i].data == nullptr) {
|
|
continue;
|
|
}
|
|
|
|
bool embed_meta = request.gen_params.embed_image_metadata;
|
|
|
|
std::string params = get_image_params(*runtime->ctx_params,
|
|
request.gen_params,
|
|
request.gen_params.seed + i / images_per_batch);
|
|
|
|
auto image_bytes = encode_image_to_vector(EncodedImageFormat::PNG,
|
|
results[i].data,
|
|
results[i].width,
|
|
results[i].height,
|
|
results[i].channel,
|
|
embed_meta ? params : "");
|
|
|
|
if (image_bytes.empty()) {
|
|
LOG_ERROR("write image to mem failed");
|
|
continue;
|
|
}
|
|
|
|
out["images"].push_back(base64_encode(image_bytes));
|
|
|
|
jsoninfo["infotexts"][i] = params;
|
|
jsoninfo["all_seeds"][i] = request.gen_params.seed + i;
|
|
jsoninfo["all_prompts"][i] = request.gen_params.prompt;
|
|
jsoninfo["all_negative_prompts"][i] = request.gen_params.negative_prompt;
|
|
}
|
|
|
|
// not a mistake: it is supposed to be a string in json format
|
|
out["info"] = jsoninfo.dump();
|
|
|
|
res.set_content(out.dump(), "application/json");
|
|
res.status = 200;
|
|
|
|
} catch (const std::bad_alloc&) {
|
|
send_generation_failure(res, out_of_memory_failure());
|
|
} catch (const std::exception& e) {
|
|
res.status = 500;
|
|
json err;
|
|
err["error"] = "server_error";
|
|
err["message"] = e.what();
|
|
res.set_content(safe_json_dump(err), "application/json");
|
|
}
|
|
};
|
|
|
|
svr.Post("/sdapi/v1/txt2img", [sdapi_any2img](const httplib::Request& req, httplib::Response& res) {
|
|
sdapi_any2img(req, res, false);
|
|
});
|
|
|
|
svr.Post("/sdapi/v1/img2img", [sdapi_any2img](const httplib::Request& req, httplib::Response& res) {
|
|
sdapi_any2img(req, res, true);
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/loras", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
refresh_lora_cache(*runtime);
|
|
|
|
json result = json::array();
|
|
{
|
|
std::lock_guard<std::mutex> lock(*runtime->lora_mutex);
|
|
for (const auto& e : *runtime->lora_cache) {
|
|
json item;
|
|
item["name"] = e.name;
|
|
item["path"] = e.path;
|
|
result.push_back(item);
|
|
}
|
|
}
|
|
|
|
res.set_content(result.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/upscalers", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
refresh_upscaler_cache(*runtime);
|
|
|
|
auto make_builtin = [](const char* name) {
|
|
json item;
|
|
item["name"] = name;
|
|
item["model_name"] = nullptr;
|
|
item["model_path"] = nullptr;
|
|
item["model_url"] = nullptr;
|
|
item["scale"] = 4;
|
|
return item;
|
|
};
|
|
|
|
json result = json::array();
|
|
result.push_back(make_builtin("None"));
|
|
result.push_back(make_builtin("Lanczos"));
|
|
result.push_back(make_builtin("Nearest"));
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(*runtime->upscaler_mutex);
|
|
for (const auto& e : *runtime->upscaler_cache) {
|
|
json item;
|
|
item["name"] = e.name;
|
|
item["model_name"] = e.model_name;
|
|
item["model_path"] = e.fullpath;
|
|
item["model_url"] = nullptr;
|
|
item["scale"] = e.scale;
|
|
result.push_back(item);
|
|
}
|
|
}
|
|
|
|
res.set_content(result.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/latent-upscale-modes", [](const httplib::Request&, httplib::Response& res) {
|
|
json result = json::array({
|
|
{{"name", "Latent"}},
|
|
{{"name", "Latent (nearest)"}},
|
|
{{"name", "Latent (nearest-exact)"}},
|
|
{{"name", "Latent (antialiased)"}},
|
|
{{"name", "Latent (bicubic)"}},
|
|
{{"name", "Latent (bicubic antialiased)"}},
|
|
});
|
|
res.set_content(result.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/samplers", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
std::vector<std::string> sampler_names;
|
|
sampler_names.push_back("default");
|
|
for (int i = 0; i < SAMPLE_METHOD_COUNT; i++) {
|
|
sampler_names.push_back(sd_sample_method_name((sample_method_t)i));
|
|
}
|
|
json r = json::array();
|
|
for (auto name : sampler_names) {
|
|
json entry;
|
|
entry["name"] = name;
|
|
entry["aliases"] = json::array({name});
|
|
entry["options"] = json::object();
|
|
r.push_back(entry);
|
|
}
|
|
res.set_content(r.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/schedulers", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
std::vector<std::string> scheduler_names;
|
|
scheduler_names.push_back("default");
|
|
for (int i = 0; i < SCHEDULER_COUNT; i++) {
|
|
scheduler_names.push_back(sd_scheduler_name((scheduler_t)i));
|
|
if (i == DISCRETE_SCHEDULER) {
|
|
scheduler_names.push_back("normal");
|
|
}
|
|
}
|
|
json r = json::array();
|
|
for (auto name : scheduler_names) {
|
|
json entry;
|
|
entry["name"] = name;
|
|
entry["label"] = name;
|
|
r.push_back(entry);
|
|
}
|
|
res.set_content(r.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/sd-models", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
fs::path model_path = resolve_display_model_path(*runtime);
|
|
json entry;
|
|
entry["title"] = model_path.stem();
|
|
entry["model_name"] = model_path.stem();
|
|
entry["filename"] = model_path.filename();
|
|
entry["hash"] = "8888888888";
|
|
entry["sha256"] = "8888888888888888888888888888888888888888888888888888888888888888";
|
|
entry["config"] = nullptr;
|
|
json r = json::array();
|
|
r.push_back(entry);
|
|
res.set_content(r.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/options", [runtime](const httplib::Request&, httplib::Response& res) {
|
|
fs::path model_path = resolve_display_model_path(*runtime);
|
|
json r;
|
|
r["samples_format"] = "png";
|
|
r["sd_model_checkpoint"] = model_path.stem();
|
|
res.set_content(r.dump(), "application/json");
|
|
});
|
|
|
|
svr.Get("/sdapi/v1/progress", [](const httplib::Request& req, httplib::Response& res) {
|
|
bool skip_current_image = false;
|
|
bool has_id_param = req.has_param("id_task");
|
|
std::string req_id_task = has_id_param ? req.get_param_value("id_task") : "";
|
|
|
|
if (req.has_param("skip_current_image")) {
|
|
std::string skip_str = req.get_param_value("skip_current_image");
|
|
skip_current_image = (skip_str == "true" || skip_str == "1");
|
|
}
|
|
|
|
json r;
|
|
r["eta_relative"] = 0.0f;
|
|
json state;
|
|
state["skipped"] = false;
|
|
state["interrupted"] = false;
|
|
state["job_timestamp"] = "0";
|
|
r["textinfo"] = nullptr;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(g_preview.mutex);
|
|
|
|
bool is_matching_task = g_preview.active && (!has_id_param || req_id_task == g_preview.id_task);
|
|
|
|
int step = is_matching_task ? g_preview.step : 0;
|
|
int steps = is_matching_task ? g_preview.total : 0;
|
|
std::string b64 = is_matching_task ? g_preview.b64 : "";
|
|
std::string job_type = is_matching_task ? g_preview.job_type : "";
|
|
int64_t job_count = is_matching_task ? g_preview.job_count : 0;
|
|
int job_no = is_matching_task ? g_preview.job_no : 0;
|
|
|
|
float batch_progress = (steps > 0) ? (static_cast<float>(step) / static_cast<float>(steps)) : 0.0f;
|
|
if (batch_progress > 1.0f)
|
|
batch_progress = 1.0f;
|
|
|
|
float overall_progress = job_count > 0
|
|
? (static_cast<float>(job_no) + batch_progress) / static_cast<float>(job_count)
|
|
: 0.0f;
|
|
if (overall_progress > 1.0f)
|
|
overall_progress = 1.0f;
|
|
|
|
r["progress"] = overall_progress;
|
|
state["job"] = job_type;
|
|
state["sampling_step"] = step;
|
|
state["sampling_steps"] = steps;
|
|
state["job_count"] = job_count;
|
|
state["job_no"] = job_no;
|
|
r["state"] = state;
|
|
|
|
if (!skip_current_image && !b64.empty()) {
|
|
r["current_image"] = b64;
|
|
} else {
|
|
r["current_image"] = nullptr;
|
|
}
|
|
}
|
|
|
|
res.set_content(r.dump(), "application/json");
|
|
});
|
|
}
|