stable-diffusion.cpp/examples/server/routes_openai.cpp
2026-10-06 20:17:54 +02:00

568 lines
22 KiB
C++

#include "routes.h"
#include <algorithm>
#include <ctime>
#include <regex>
#include "common/common.h"
#include "common/media_io.h"
#include "common/resource_owners.hpp"
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 bool build_openai_generation_request(const httplib::Request& req,
ServerRuntime& runtime,
ImgGenJobRequest& request,
bool& stream,
std::string& error_message) {
if (req.body.empty()) {
error_message = "empty body";
return false;
}
json j = json::parse(req.body);
stream = j.value("stream", false);
std::string prompt = j.value("prompt", "");
int n = std::max(1, j.value("n", 1));
std::string size = j.value("size", "");
std::string output_format = j.value("output_format", "png");
int output_compression = j.value("output_compression", 100);
int width = runtime.default_gen_params->width > 0 ? runtime.default_gen_params->width : 512;
int height = runtime.default_gen_params->width > 0 ? runtime.default_gen_params->height : 512;
if (!size.empty()) {
auto pos = size.find('x');
if (pos != std::string::npos) {
try {
width = std::stoi(size.substr(0, pos));
height = std::stoi(size.substr(pos + 1));
} catch (...) {
}
}
}
if (prompt.empty()) {
error_message = "prompt required";
return false;
}
request.gen_params = *runtime.default_gen_params;
if (!assign_output_options(request, output_format, output_compression, true, error_message)) {
return false;
}
request.gen_params.prompt = prompt;
request.gen_params.width = width;
request.gen_params.height = height;
request.gen_params.batch_count = n;
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;
}
// 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 bool build_openai_edit_request(const httplib::Request& req,
ServerRuntime& runtime,
ImgGenJobRequest& request,
bool& stream,
std::string& error_message) {
if (!req.is_multipart_form_data()) {
error_message = "Content-Type must be multipart/form-data";
return false;
}
stream = false;
if (req.form.has_field("stream")) {
std::string stream_val = req.form.get_field("stream");
std::transform(stream_val.begin(), stream_val.end(), stream_val.begin(), ::tolower);
stream = (stream_val == "true" || stream_val == "1");
}
std::string prompt = req.form.get_field("prompt");
if (prompt.empty()) {
error_message = "prompt required";
return false;
}
size_t image_count = req.form.get_file_count("image[]");
bool has_legacy_image = req.form.has_file("image");
if (image_count == 0 && !has_legacy_image) {
error_message = "at least one image[] required";
return false;
}
std::vector<std::vector<uint8_t>> images_bytes;
for (size_t i = 0; i < image_count; ++i) {
auto file = req.form.get_file("image[]", i);
images_bytes.emplace_back(file.content.begin(), file.content.end());
}
if (image_count == 0 && has_legacy_image) {
auto file = req.form.get_file("image");
images_bytes.emplace_back(file.content.begin(), file.content.end());
}
std::vector<uint8_t> mask_bytes;
if (req.form.has_file("mask")) {
auto file = req.form.get_file("mask");
mask_bytes.assign(file.content.begin(), file.content.end());
}
int n = 1;
if (req.form.has_field("n")) {
try {
n = std::stoi(req.form.get_field("n"));
} catch (...) {
}
}
std::string size = req.form.get_field("size");
int width = -1;
int height = -1;
if (!size.empty()) {
auto pos = size.find('x');
if (pos != std::string::npos) {
try {
width = std::stoi(size.substr(0, pos));
height = std::stoi(size.substr(pos + 1));
} catch (...) {
}
}
}
std::string output_format = req.form.has_field("output_format")
? req.form.get_field("output_format")
: "png";
int output_compression = 100;
try {
output_compression = std::stoi(req.form.get_field("output_compression"));
} catch (...) {
}
request.gen_params = *runtime.default_gen_params;
if (!assign_output_options(request, output_format, output_compression, false, error_message)) {
return false;
}
request.gen_params.prompt = prompt;
request.gen_params.width = width;
request.gen_params.height = height;
request.gen_params.batch_count = n;
std::string sd_cpp_extra_args_str = extract_and_remove_sd_cpp_extra_args(request.gen_params.prompt);
for (auto& bytes : images_bytes) {
int img_w = 0;
int img_h = 0;
int resolved_channel = 0;
uint8_t* raw_pixels = load_image_from_memory(
reinterpret_cast<const char*>(bytes.data()),
static_cast<int>(bytes.size()),
img_w, img_h, resolved_channel,
0, 0,
0);
if (raw_pixels == nullptr) {
continue;
}
const bool is_first_ref_image = request.gen_params.ref_images.empty();
SDImageOwner image_owner({(uint32_t)img_w, (uint32_t)img_h, (uint32_t)resolved_channel, raw_pixels});
request.gen_params.set_width_and_height_if_unset(image_owner.get().width, image_owner.get().height);
if (is_first_ref_image) {
request.gen_params.init_image = image_owner;
if (request.gen_params.init_image.get().data == nullptr) {
error_message = "could not allocate init image";
return false;
}
}
request.gen_params.ref_images.push_back(std::move(image_owner));
}
if (!mask_bytes.empty()) {
int mask_w = 0;
int mask_h = 0;
int mask_channel = 0;
uint8_t* mask_raw = load_image_from_memory(
reinterpret_cast<const char*>(mask_bytes.data()),
static_cast<int>(mask_bytes.size()),
mask_w, mask_h, mask_channel,
0, 0, 1);
request.gen_params.mask_image.reset({(uint32_t)mask_w, (uint32_t)mask_h, 1, mask_raw});
const sd_image_t& mask_image = request.gen_params.mask_image.get();
request.gen_params.set_width_and_height_if_unset(mask_image.width, mask_image.height);
} else {
request.gen_params.mask_image.reset({
(uint32_t)request.gen_params.get_resolved_width(),
(uint32_t)request.gen_params.get_resolved_height(),
1,
nullptr,
});
}
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;
}
// 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 bool execute_sync_img_gen_request(ServerRuntime& runtime,
ImgGenJobRequest& request,
SDImageVec& results,
std::string& error_message) {
sd_img_gen_params_t img_gen_params = request.to_sd_img_gen_params_t();
int num_results = 0;
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
sd_image_t* raw_results = nullptr;
if (!generate_image(runtime.sd_ctx, &img_gen_params, &raw_results, &num_results)) {
raw_results = nullptr;
num_results = 0;
}
results.adopt(raw_results, num_results);
}
if (results.empty()) {
error_message = "generate_image returned no results";
return false;
}
return true;
}
struct PreviewStreamContext {
httplib::DataSink* sink = nullptr;
const ImgGenJobRequest* request = nullptr;
};
static void write_sse_event(httplib::DataSink& sink, const json& event) {
if (!sink.is_writable()) {
return;
}
std::string sse = "data: " + event.dump() + "\n\n";
sink.write(sse.data(), sse.size());
}
static void preview_callback(int step, int frame_count, sd_image_t* frames, bool is_noisy, void* data) {
if (!data || frame_count <= 0 || !frames || !frames[0].data) {
return;
}
auto* ctx = static_cast<PreviewStreamContext*>(data);
if (!ctx->sink || !ctx->sink->is_writable()) {
return; // Client disconnected
}
auto image_bytes = encode_image_to_vector(
EncodedImageFormat::JPEG,
frames[0].data,
frames[0].width,
frames[0].height,
frames[0].channel,
"",
80);
if (image_bytes.empty()) {
return;
}
json chunk;
chunk["type"] = "image_generation.partial_image";
chunk["b64_json"] = base64_encode(image_bytes);
chunk["created_at"] = static_cast<long long>(std::time(nullptr));
chunk["size"] = std::to_string(frames[0].width) + "x" + std::to_string(frames[0].height);
chunk["quality"] = "auto";
chunk["background"] = frames[0].channel != 4 ? "opaque" : "auto";
chunk["output_format"] = "jpeg";
chunk["partial_image_index"] = step;
write_sse_event(*ctx->sink, chunk);
}
static void execute_streaming_img_gen(ServerRuntime& runtime,
ImgGenJobRequest& request,
httplib::DataSink& sink) {
PreviewStreamContext preview_ctx{&sink, &request};
sd_img_gen_params_t img_gen_params = request.to_sd_img_gen_params_t();
SDImageVec results;
int num_results = 0;
{
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
// TODO: support other preview methods?
sd_set_preview_callback(preview_callback, PREVIEW_PROJ, 1, true, false, &preview_ctx);
try {
sd_image_t* raw_results = nullptr;
if (!generate_image(runtime.sd_ctx, &img_gen_params, &raw_results, &num_results)) {
raw_results = nullptr;
num_results = 0;
}
results.adopt(raw_results, num_results);
} catch (...) {
sd_set_preview_callback(nullptr, PREVIEW_NONE, 0, false, false, nullptr);
throw;
}
sd_set_preview_callback(nullptr, PREVIEW_NONE, 0, false, false, nullptr);
}
if (results.empty()) {
if (sink.is_writable()) {
json err;
err["type"] = "image_generation.error";
err["error"] = "generate_image returned no results";
write_sse_event(sink, err);
}
sink.done();
return;
}
int result_count = results.count();
int images_per_batch = request.gen_params.batch_count > 0
? std::max(1, result_count / request.gen_params.batch_count)
: 1;
for (int i = 0; i < result_count; ++i) {
if (results[i].data == nullptr)
continue;
std::string params = request.gen_params.embed_image_metadata
? get_image_params(*runtime.ctx_params, request.gen_params,
request.gen_params.seed + i / images_per_batch)
: "";
auto image_bytes = encode_image_to_vector(
request.output_format == "jpeg" ? EncodedImageFormat::JPEG
: request.output_format == "webp" ? EncodedImageFormat::WEBP
: EncodedImageFormat::PNG,
results[i].data, results[i].width, results[i].height,
results[i].channel, params, request.output_compression);
if (image_bytes.empty()) {
continue;
}
json chunk;
chunk["type"] = "image_generation.completed";
chunk["b64_json"] = base64_encode(image_bytes);
chunk["created_at"] = static_cast<long long>(std::time(nullptr));
chunk["size"] = std::to_string(results[i].width) + "x" + std::to_string(results[i].height);
chunk["quality"] = "auto";
chunk["background"] = results[i].channel == 4 ? "transparent" : "opaque";
chunk["output_format"] = request.output_format;
write_sse_event(sink, chunk);
}
if (sink.is_writable()) {
static const std::string done_msg = "data: [DONE]\n\n";
sink.write(done_msg.data(), done_msg.size());
}
sink.done();
}
void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
ServerRuntime* runtime = &rt;
svr.Get("/v1/models", [runtime](const httplib::Request&, httplib::Response& res) {
json r;
r["data"] = json::array();
r["data"].push_back({{"id", "sd-cpp-local"}, {"object", "model"}, {"owned_by", "local"}});
res.set_content(r.dump(), "application/json");
});
svr.Post("/v1/images/generations", [runtime](const httplib::Request& req, httplib::Response& res) {
try {
if (!runtime_supports_generation_mode(*runtime, IMG_GEN)) {
res.status = 400;
res.set_content(json({{"error", unsupported_generation_mode_error(IMG_GEN)}}).dump(), "application/json");
return;
}
ImgGenJobRequest request;
std::string error_message;
bool stream = false;
if (!build_openai_generation_request(req, *runtime, request, stream, 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());
if (stream) {
res.set_header("Cache-Control", "no-cache");
res.set_header("Connection", "keep-alive");
res.set_chunked_content_provider(
"text/event-stream",
[runtime, request](size_t /*offset*/, httplib::DataSink& sink) mutable {
execute_streaming_img_gen(*runtime, request, sink);
return false;
});
return;
}
SDImageVec results;
if (!execute_sync_img_gen_request(*runtime, request, results, error_message)) {
res.status = 500;
res.set_content(json({{"error", error_message}}).dump(), "application/json");
return;
}
json out;
out["created"] = static_cast<long long>(std::time(nullptr));
out["data"] = json::array();
out["output_format"] = request.output_format;
int result_count = results.count();
int images_per_batch = request.gen_params.batch_count > 0 ? std::max(1, result_count / request.gen_params.batch_count) : 1;
for (int i = 0; i < result_count; ++i) {
if (results[i].data == nullptr) {
continue;
}
std::string params = request.gen_params.embed_image_metadata
? get_image_params(*runtime->ctx_params,
request.gen_params,
request.gen_params.seed + i / images_per_batch)
: "";
auto image_bytes = encode_image_to_vector(request.output_format == "jpeg"
? EncodedImageFormat::JPEG
: request.output_format == "webp"
? EncodedImageFormat::WEBP
: EncodedImageFormat::PNG,
results[i].data,
results[i].width,
results[i].height,
results[i].channel,
params,
request.output_compression);
if (image_bytes.empty()) {
LOG_ERROR("write image to mem failed");
continue;
}
json item;
item["b64_json"] = base64_encode(image_bytes);
out["data"].push_back(item);
}
res.set_content(out.dump(), "application/json");
res.status = 200;
} catch (const std::exception& e) {
res.status = 500;
json err;
err["error"] = "server_error";
err["message"] = e.what();
res.set_content(err.dump(), "application/json");
}
});
svr.Post("/v1/images/edits", [runtime](const httplib::Request& req, httplib::Response& res) {
try {
if (!runtime_supports_generation_mode(*runtime, IMG_GEN)) {
res.status = 400;
res.set_content(json({{"error", unsupported_generation_mode_error(IMG_GEN)}}).dump(), "application/json");
return;
}
ImgGenJobRequest request;
std::string error_message;
bool stream = false;
if (!build_openai_edit_request(req, *runtime, request, stream, 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());
if (stream) {
res.set_header("Cache-Control", "no-cache");
res.set_header("Connection", "keep-alive");
auto req_ptr = std::make_shared<ImgGenJobRequest>(std::move(request));
res.set_chunked_content_provider(
"text/event-stream",
[runtime, req_ptr](size_t /*offset*/, httplib::DataSink& sink) {
execute_streaming_img_gen(*runtime, *req_ptr, sink);
return false;
});
return;
}
// Synchronous path
SDImageVec results;
if (!execute_sync_img_gen_request(*runtime, request, results, error_message)) {
res.status = 500;
res.set_content(json({{"error", error_message}}).dump(), "application/json");
return;
}
json out;
out["created"] = static_cast<long long>(std::time(nullptr));
out["data"] = json::array();
out["output_format"] = request.output_format;
int result_count = results.count();
int images_per_batch = request.gen_params.batch_count > 0 ? std::max(1, result_count / request.gen_params.batch_count) : 1;
for (int i = 0; i < result_count; ++i) {
if (results[i].data == nullptr) {
continue;
}
std::string params = request.gen_params.embed_image_metadata
? get_image_params(*runtime->ctx_params,
request.gen_params,
request.gen_params.seed + i / images_per_batch)
: "";
auto image_bytes = encode_image_to_vector(request.output_format == "jpeg" ? EncodedImageFormat::JPEG : EncodedImageFormat::PNG,
results[i].data,
results[i].width,
results[i].height,
results[i].channel,
params,
request.output_compression);
json item;
item["b64_json"] = base64_encode(image_bytes);
out["data"].push_back(item);
}
res.set_content(out.dump(), "application/json");
res.status = 200;
} catch (const std::exception& e) {
res.status = 500;
json err;
err["error"] = "server_error";
err["message"] = e.what();
res.set_content(err.dump(), "application/json");
}
});
}