Skip to content

Commit 45714b1

Browse files
authored
feat(sdapi): report generation parameters through the info field (#1426)
1 parent e790073 commit 45714b1

1 file changed

Lines changed: 58 additions & 7 deletions

File tree

examples/server/routes_sdapi.cpp

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,48 @@ static bool build_sdapi_img_gen_request(const json& j,
259259
return true;
260260
}
261261

262+
static nlohmann::json prepare_info_field(const SDContextParams& ctx_params,
263+
const SDGenerationParams& gen_params,
264+
bool img2img) {
265+
nlohmann::json jsoninfo = nlohmann::json::object();
266+
jsoninfo["prompt"] = gen_params.prompt;
267+
if (!gen_params.negative_prompt.empty()) {
268+
jsoninfo["negative_prompt"] = gen_params.negative_prompt;
269+
}
270+
jsoninfo["seed"] = gen_params.seed;
271+
jsoninfo["cfg_scale"] = gen_params.sample_params.guidance.txt_cfg;
272+
jsoninfo["width"] = gen_params.get_resolved_width();
273+
jsoninfo["height"] = gen_params.get_resolved_height();
274+
jsoninfo["steps"] = gen_params.sample_params.sample_steps;
275+
jsoninfo["sampler_name"] = sd_sample_method_name(gen_params.sample_params.sample_method);
276+
if (gen_params.clip_skip != -1) {
277+
jsoninfo["clip_skip"] = gen_params.clip_skip;
278+
}
279+
if (gen_params.sample_params.scheduler != scheduler_t::SCHEDULER_COUNT) {
280+
jsoninfo["extra_generation_params"] = nlohmann::json::object();
281+
jsoninfo["extra_generation_params"]["Schedule type"] = sd_scheduler_name(gen_params.sample_params.scheduler);
282+
}
283+
if (img2img) {
284+
jsoninfo["denoising_strength"] = gen_params.strength;
285+
}
286+
// not clear what should happen if we have both model and diffusion_model
287+
if (!ctx_params.diffusion_model_path.empty()) {
288+
jsoninfo["sd_model_name"] = sd_basename(ctx_params.diffusion_model_path);
289+
} else if (!ctx_params.model_path.empty()) {
290+
jsoninfo["sd_model_name"] = sd_basename(ctx_params.model_path);
291+
}
292+
if (!ctx_params.vae_path.empty()) {
293+
jsoninfo["sd_vae_name"] = sd_basename(ctx_params.vae_path);
294+
}
295+
jsoninfo["version"] = "stable-diffusion.cpp";
296+
297+
jsoninfo["infotexts"] = nlohmann::json::array();
298+
jsoninfo["all_prompts"] = nlohmann::json::array();
299+
jsoninfo["all_negative_prompts"] = nlohmann::json::array();
300+
jsoninfo["all_seeds"] = nlohmann::json::array();
301+
return jsoninfo;
302+
}
303+
262304
void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
263305
ServerRuntime* runtime = &rt;
264306

@@ -309,34 +351,43 @@ void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
309351
json out;
310352
out["images"] = json::array();
311353
out["parameters"] = j;
312-
out["info"] = "";
354+
json jsoninfo = prepare_info_field(*runtime->ctx_params, request.gen_params, img2img);
313355

314356
int images_per_batch = request.gen_params.batch_count > 0 ? std::max(1, num_results / request.gen_params.batch_count) : 1;
315357
for (int i = 0; i < num_results; ++i) {
316358
if (results[i].data == nullptr) {
317359
continue;
318360
}
319361

320-
std::string params = request.gen_params.embed_image_metadata
321-
? get_image_params(*runtime->ctx_params,
322-
request.gen_params,
323-
request.gen_params.seed + i / images_per_batch)
324-
: "";
362+
bool embed_meta = request.gen_params.embed_image_metadata;
363+
364+
std::string params = get_image_params(*runtime->ctx_params,
365+
request.gen_params,
366+
request.gen_params.seed + i / images_per_batch);
367+
325368
auto image_bytes = encode_image_to_vector(EncodedImageFormat::PNG,
326369
results[i].data,
327370
results[i].width,
328371
results[i].height,
329372
results[i].channel,
330-
params);
373+
embed_meta ? params : "");
331374

332375
if (image_bytes.empty()) {
333376
LOG_ERROR("write image to mem failed");
334377
continue;
335378
}
336379

337380
out["images"].push_back(base64_encode(image_bytes));
381+
382+
jsoninfo["infotexts"][i] = params;
383+
jsoninfo["all_seeds"][i] = request.gen_params.seed + i;
384+
jsoninfo["all_prompts"][i] = request.gen_params.prompt;
385+
jsoninfo["all_negative_prompts"][i] = request.gen_params.negative_prompt;
338386
}
339387

388+
// not a mistake: it is supposed to be a string in json format
389+
out["info"] = jsoninfo.dump();
390+
340391
res.set_content(out.dump(), "application/json");
341392
res.status = 200;
342393

0 commit comments

Comments
 (0)