Skip to content

Commit adcbde1

Browse files
committed
feat(sdapi): report generation parameters through the info field
1 parent 9ac7b67 commit adcbde1

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
@@ -236,6 +236,48 @@ static bool build_sdapi_img_gen_request(const json& j,
236236
return true;
237237
}
238238

239+
static nlohmann::json prepare_info_field(const SDContextParams& ctx_params,
240+
const SDGenerationParams& gen_params,
241+
bool img2img) {
242+
nlohmann::json jsoninfo = nlohmann::json::object();
243+
jsoninfo["prompt"] = gen_params.prompt;
244+
if (!gen_params.negative_prompt.empty()) {
245+
jsoninfo["negative_prompt"] = gen_params.negative_prompt;
246+
}
247+
jsoninfo["seed"] = gen_params.seed;
248+
jsoninfo["cfg_scale"] = gen_params.sample_params.guidance.txt_cfg;
249+
jsoninfo["width"] = gen_params.get_resolved_width();
250+
jsoninfo["height"] = gen_params.get_resolved_height();
251+
jsoninfo["steps"] = gen_params.sample_params.sample_steps;
252+
jsoninfo["sampler_name"] = sd_sample_method_name(gen_params.sample_params.sample_method);
253+
if (gen_params.clip_skip != -1) {
254+
jsoninfo["clip_skip"] = gen_params.clip_skip;
255+
}
256+
if (gen_params.sample_params.scheduler != scheduler_t::SCHEDULER_COUNT) {
257+
jsoninfo["extra_generation_params"] = nlohmann::json::object();
258+
jsoninfo["extra_generation_params"]["Schedule type"] = sd_scheduler_name(gen_params.sample_params.scheduler);
259+
}
260+
if (img2img) {
261+
jsoninfo["denoising_strength"] = gen_params.strength;
262+
}
263+
// not clear what should happen if we have both model and diffusion_model
264+
if (!ctx_params.diffusion_model_path.empty()) {
265+
jsoninfo["sd_model_name"] = sd_basename(ctx_params.diffusion_model_path);
266+
} else if (!ctx_params.model_path.empty()) {
267+
jsoninfo["sd_model_name"] = sd_basename(ctx_params.model_path);
268+
}
269+
if (!ctx_params.vae_path.empty()) {
270+
jsoninfo["sd_vae_name"] = sd_basename(ctx_params.vae_path);
271+
}
272+
jsoninfo["version"] = "stable-diffusion.cpp";
273+
274+
jsoninfo["infotexts"] = nlohmann::json::array();
275+
jsoninfo["all_prompts"] = nlohmann::json::array();
276+
jsoninfo["all_negative_prompts"] = nlohmann::json::array();
277+
jsoninfo["all_seeds"] = nlohmann::json::array();
278+
return jsoninfo;
279+
}
280+
239281
void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
240282
ServerRuntime* runtime = &rt;
241283

@@ -278,33 +320,42 @@ void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
278320
json out;
279321
out["images"] = json::array();
280322
out["parameters"] = j;
281-
out["info"] = "";
323+
json jsoninfo = prepare_info_field(*runtime->ctx_params, request.gen_params, img2img);
282324

283325
for (int i = 0; i < num_results; ++i) {
284326
if (results[i].data == nullptr) {
285327
continue;
286328
}
287329

288-
std::string params = request.gen_params.embed_image_metadata
289-
? get_image_params(*runtime->ctx_params,
290-
request.gen_params,
291-
request.gen_params.seed + i)
292-
: "";
330+
bool embed_meta = request.gen_params.embed_image_metadata;
331+
332+
std::string params = get_image_params(*runtime->ctx_params,
333+
request.gen_params,
334+
request.gen_params.seed + i);
335+
293336
auto image_bytes = encode_image_to_vector(EncodedImageFormat::PNG,
294337
results[i].data,
295338
results[i].width,
296339
results[i].height,
297340
results[i].channel,
298-
params);
341+
embed_meta ? params : "");
299342

300343
if (image_bytes.empty()) {
301344
LOG_ERROR("write image to mem failed");
302345
continue;
303346
}
304347

305348
out["images"].push_back(base64_encode(image_bytes));
349+
350+
jsoninfo["infotexts"][i] = params;
351+
jsoninfo["all_seeds"][i] = request.gen_params.seed + i;
352+
jsoninfo["all_prompts"][i] = request.gen_params.prompt;
353+
jsoninfo["all_negative_prompts"][i] = request.gen_params.negative_prompt;
306354
}
307355

356+
// not a mistake: it is supposed to be a string in json format
357+
out["info"] = jsoninfo.dump();
358+
308359
res.set_content(out.dump(), "application/json");
309360
res.status = 200;
310361

0 commit comments

Comments
 (0)