Skip to content

Commit 4074bb3

Browse files
committed
server: support photomaker and controlnets
1 parent 86a7f3f commit 4074bb3

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

examples/server/main.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,34 @@ bool parseJsonPrompt(std::string json_str, SDParams* params) {
10701070
{},
10711071
{
10721072
{"id_images", [&](const json& o) -> bool {
1073-
// TODO fill up params->lastRequest.pm_images_vec
1074-
sd_log(sd_log_level_t::SD_LOG_WARN, "id_images not implemented yet\n");
1075-
return false;
1073+
// fill up params->lastRequest.pm_images_vec
1074+
std::vector<std::string> b64_data = o.get<std::vector<std::string>>();
1075+
1076+
// empty the vector if the new data is empty
1077+
if (b64_data.empty()) {
1078+
if (params->lastRequest.pm_images_vec.size() > 0) {
1079+
for (auto& img : params->lastRequest.pm_images_vec) {
1080+
free(img.data);
1081+
}
1082+
params->lastRequest.pm_images_vec.clear();
1083+
return true;
1084+
}
1085+
return false;
1086+
}
1087+
1088+
for (auto& b64_image : b64_data) {
1089+
// decode the base64 image
1090+
std::string bin_image = base64_decode(b64_image);
1091+
int width, height, channels;
1092+
uint8_t* image = load_image_from_memory(bin_image, width, height, channels);
1093+
if (image == nullptr) {
1094+
sd_log(sd_log_level_t::SD_LOG_WARN, "Failed to load image from memory\n");
1095+
continue;
1096+
}
1097+
sd_image_t img = {(uint32_t)width, (uint32_t)height, 3, image};
1098+
params->lastRequest.pm_images_vec.push_back(img);
1099+
}
1100+
return true;
10761101
}},
10771102
{"id_embed_path", [&](const json& o) -> bool {
10781103
// TODO: avoid parsing paths, rather use ids and convert to path server-side
@@ -1331,7 +1356,7 @@ bool parseJsonPrompt(std::string json_str, SDParams* params) {
13311356
}
13321357
model_part_path = "";
13331358
} else if (index != MODEL_KEEP) {
1334-
sd_log(sd_log_level_t::SD_LOG_WARN, "Invalid t5xxl index: %d\n", index);
1359+
sd_log(sd_log_level_t::SD_LOG_WARN, "Invalid model index: %d out of %d\n", index, model_part_files.size());
13351360
}
13361361
return change;
13371362
};
@@ -2122,9 +2147,11 @@ void start_server(SDParams params) {
21222147
res.set_header("Access-Control-Allow-Headers", "*");
21232148
return res.set_content("", "text/html"); // blank response, no data
21242149
});
2125-
if (params.verbose) {
2126-
svr->set_logger(log_server_request);
2127-
}
2150+
2151+
// TODO new flag for extra verbose to log all requests
2152+
// if (params.verbose) {
2153+
// svr->set_logger(log_server_request);
2154+
// }
21282155

21292156
svr->Post("/txt2img", [&sd_ctx, &params, &n_prompts](const httplib::Request& req, httplib::Response& res) {
21302157
// Deprecated
@@ -2314,6 +2341,9 @@ void start_server(SDParams params) {
23142341
json vision_models;
23152342
json vaes;
23162343
json taes;
2344+
json controlnets;
2345+
json photomakers;
2346+
23172347
for (size_t i = 0; i < params.models_files.size(); i++) {
23182348
if (is_model_file(params.models_files[i])) {
23192349
models.push_back({{"id", i}, {"name", params.models_files[i]}});
@@ -2344,12 +2374,25 @@ void start_server(SDParams params) {
23442374
taes.push_back({{"id", i}, {"name", params.tae_files[i]}});
23452375
}
23462376
}
2377+
for (size_t i = 0; i < params.controlnet_files.size(); i++) {
2378+
if (is_model_file(params.controlnet_files[i])) {
2379+
controlnets.push_back({{"id", i}, {"name", params.controlnet_files[i]}});
2380+
}
2381+
}
2382+
for (size_t i = 0; i < params.photomaker_files.size(); i++) {
2383+
if (is_model_file(params.photomaker_files[i])) {
2384+
photomakers.push_back({{"id", i}, {"name", params.photomaker_files[i]}});
2385+
}
2386+
}
2387+
23472388
response["models"] = models;
23482389
response["diffusion_models"] = diffusion_models;
23492390
response["text_encoders"] = text_encoders;
23502391
response["vision_models"] = vision_models;
23512392
response["vaes"] = vaes;
23522393
response["taes"] = taes;
2394+
response["control_nets"] = controlnets;
2395+
response["photo_makers"] = photomakers;
23532396

23542397
for (size_t i = 0; i < lora_files.size(); i++) {
23552398
std::string lora_name = lora_files[i];
@@ -2429,6 +2472,12 @@ void start_server(SDParams params) {
24292472
if (!params.ctxParams.taesd_path.empty()) {
24302473
response["tae"] = sd_basename(params.ctxParams.taesd_path);
24312474
}
2475+
if (!params.ctxParams.controlnet_path.empty()) {
2476+
response["control_net"] = sd_basename(params.ctxParams.controlnet_path);
2477+
}
2478+
if (!params.ctxParams.photomaker_path.empty()) {
2479+
response["photo_maker"] = sd_basename(params.ctxParams.photomaker_path);
2480+
}
24322481
res.set_content(response.dump(), "application/json");
24332482
});
24342483

examples/server/server_doc.yaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ paths:
140140
$ref: '#/components/schemas/ModelList'
141141
taes:
142142
$ref: '#/components/schemas/ModelList'
143+
control_nets:
144+
$ref: '#/components/schemas/ModelList'
145+
photo_makers:
146+
$ref: '#/components/schemas/ModelList'
143147
loras:
144148
type: array
145149
items:
@@ -167,10 +171,17 @@ paths:
167171
properties:
168172
model: { type: string }
169173
diffusion_model: { type: string }
174+
high_noise_diffusion_model: { type: string }
170175
clip_l: { type: string }
171176
clip_g: { type: string }
172177
t5xxl: { type: string }
178+
qwen2vl: { type: string }
179+
clip_vision: { type: string }
180+
qwen2vl_vision: { type: string }
173181
vae: { type: string }
182+
tae: { type: string }
183+
control_net: { type: string }
184+
photo_maker: { type: string }
174185
# ... includes other loaded components
175186

176187
/schedulers:
@@ -472,8 +483,7 @@ components:
472483
type: string
473484
id_images:
474485
type: array
475-
deprecated: true
476-
description: Not implemented yet.
486+
description: Array of base64 encoded ID images for PhotoMaker.
477487
items:
478488
type: string
479489
preview_method:

0 commit comments

Comments
 (0)