Skip to content

Commit 31a013d

Browse files
committed
server: allow to unload model components
1 parent 549a780 commit 31a013d

File tree

2 files changed

+125
-41
lines changed

2 files changed

+125
-41
lines changed

examples/server/frontend.cpp

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const std::string html_content = R"xxx(
9898
}
9999
</style>
100100
</head>
101-
)xxx" R"xxx(
101+
<!-- )xxx" R"xxx( -->
102102
103103
<body>
104104
<div class="container">
@@ -202,47 +202,54 @@ const std::string html_content = R"xxx(
202202
<div class="input-group">
203203
<label for="model">Model:</label>
204204
<select id="model" class="param-input">
205-
<option value="" selected>unset</option>
205+
<option value="" selected>keep current</option>
206+
<option value="none">None</option>
206207
</select>
207208
</div>
208209
<div class="input-group">
209210
<label for="diff-model">Diffusion Model:</label>
210211
<select id="diff-model" class="param-input">
211-
<option value="" selected>unset</option>
212+
<option value="" selected>keep current</option>
213+
<option value="none">None</option>
212214
</select>
213215
</div>
214216
<div class="line">
215217
216218
<div class="input-group">
217219
<label for="clip_l">Clip_L:</label>
218220
<select id="clip_l" class="param-input">
219-
<option value="" selected>unset</option>
221+
<option value="" selected>keep current</option>
222+
<option value="none">None</option>
220223
</select>
221224
</div>
222225
<div class="input-group">
223226
<label for="clip_g">Clip_G:</label>
224227
<select id="clip_g" class="param-input">
225-
<option value="" selected>unset</option>
228+
<option value="" selected>keep current</option>
229+
<option value="none">None</option>
226230
</select>
227231
</div>
228232
<div class="input-group">
229233
<label for="t5xxl">T5 XXL:</label>
230234
<select id="t5xxl" class="param-input">
231-
<option value="" selected>unset</option>
235+
<option value="" selected>keep current</option>
236+
<option value="none">None</option>
232237
</select>
233238
</div>
234239
</div>
235240
<div class="line">
236241
<div class="input-group">
237242
<label for="vae">VAE:</label>
238243
<select id="vae" class="param-input">
239-
<option value="" selected>unset</option>
244+
<option value="" selected>keep current</option>
245+
<option value="none">None</option>
240246
</select>
241247
</div>
242248
<div class="input-group">
243249
<label for="tae">TAE:</label>
244250
<select id="tae" class="param-input">
245-
<option value="" selected>unset</option>
251+
<option value="" selected>keep current</option>
252+
<option value="none">None</option>
246253
</select>
247254
</div>
248255
</div>
@@ -258,7 +265,7 @@ const std::string html_content = R"xxx(
258265
<div class="line">
259266
<p>Current task status: <span id="status"> -- </span> | Queue: <span id="queued_tasks">0</span></p>
260267
</div>
261-
)xxx" R"xxx(
268+
<!-- )xxx" R"xxx( -->
262269
<script>
263270
let queued_tasks = 0;
264271
async function update_queue() {
@@ -362,7 +369,10 @@ const std::string html_content = R"xxx(
362369
modelsSelect.appendChild(option);
363370
});
364371
} else {
365-
const currentOption = modelsSelect.options[modelsSelect.selectedIndex];
372+
modelsSelect.options.length = 1;
373+
const currentOption = modelsSelect.options[0];
374+
currentOption.select = true;
375+
currentOption.value = "";
366376
currentOption.textContent = "unavailable";
367377
}
368378
@@ -375,7 +385,10 @@ const std::string html_content = R"xxx(
375385
diffModelsSelect.appendChild(option);
376386
});
377387
} else {
378-
const currentOption = diffModelsSelect.options[diffModelsSelect.selectedIndex];
388+
diffModelsSelect.options.length = 1;
389+
const currentOption = diffModelsSelect.options[0];
390+
currentOption.select = true;
391+
currentOption.value = "";
379392
currentOption.textContent = "unavailable";
380393
}
381394
@@ -388,7 +401,10 @@ const std::string html_content = R"xxx(
388401
clipLSelect.appendChild(option);
389402
});
390403
} else {
391-
const currentOption = clipLSelect.options[clipLSelect.selectedIndex];
404+
clipLSelect.options.length = 1;
405+
const currentOption = clipLSelect.options[0];
406+
currentOption.select = true;
407+
currentOption.value = "";
392408
currentOption.textContent = "unavailable";
393409
}
394410
@@ -401,7 +417,10 @@ const std::string html_content = R"xxx(
401417
clipGSelect.appendChild(option);
402418
});
403419
} else {
404-
const currentOption = clipGSelect.options[clipGSelect.selectedIndex];
420+
clipGSelect.options.length = 1;
421+
const currentOption = clipGSelect.options[0];
422+
currentOption.select = true;
423+
currentOption.value = "";
405424
currentOption.textContent = "unavailable";
406425
}
407426
@@ -414,7 +433,10 @@ const std::string html_content = R"xxx(
414433
t5xxlSelect.appendChild(option);
415434
});
416435
} else {
417-
const currentOption = t5xxlSelect.options[t5xxlSelect.selectedIndex];
436+
t5xxlSelect.options.length = 1;
437+
const currentOption = t5xxlSelect.options[0];
438+
currentOption.select = true;
439+
currentOption.value = "";
418440
currentOption.textContent = "unavailable";
419441
}
420442
@@ -427,7 +449,10 @@ const std::string html_content = R"xxx(
427449
vaeSelect.appendChild(option);
428450
});
429451
} else {
430-
const currentOption = vaeSelect.options[vaeSelect.selectedIndex];
452+
vaeSelect.options.length = 1;
453+
const currentOption = vaeSelect.options[0];
454+
currentOption.select = true;
455+
currentOption.value = "";
431456
currentOption.textContent = "unavailable";
432457
}
433458
@@ -440,7 +465,10 @@ const std::string html_content = R"xxx(
440465
taeSelect.appendChild(option);
441466
});
442467
} else {
443-
const currentOption = taeSelect.options[taeSelect.selectedIndex];
468+
taeSelect.options.length = 1;
469+
const currentOption = taeSelect.options[0];
470+
currentOption.select = true;
471+
currentOption.value = "";
444472
currentOption.textContent = "unavailable";
445473
}
446474
}
@@ -473,9 +501,9 @@ const std::string html_content = R"xxx(
473501
document.getElementById('preview_interval').value = data.generation_params.preview_interval;
474502
}
475503
}
476-
)xxx" R"xxx(
504+
//)xxx" R"xxx(
477505
478-
fetchSampleMethods();
506+
fetchSampleMethods();
479507
fetchSchedules();
480508
fetchPreviewMethods();
481509
fetchModelsEncodersAE();

examples/server/main.cpp

Lines changed: 79 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -897,59 +897,115 @@ bool parseJsonPrompt(std::string json_str, SDParams* params) {
897897

898898
try {
899899
std::string model = payload["model"];
900-
if (params->ctxParams.model_path != params->models_dir + model) {
901-
params->ctxParams.model_path = params->models_dir + model;
902-
params->ctxParams.diffusion_model_path = "";
903-
updatectx = true;
900+
if (model == "none") {
901+
if (params->ctxParams.model_path != "") {
902+
updatectx = true;
903+
}
904+
params->ctxParams.model_path = "";
905+
} else {
906+
std::string new_path = params->models_dir + model;
907+
if (params->ctxParams.model_path != new_path) {
908+
params->ctxParams.model_path = new_path;
909+
params->ctxParams.diffusion_model_path = "";
910+
updatectx = true;
911+
}
904912
}
905913
} catch (...) {
906914
}
907915
try {
908916
std::string diffusion_model = payload["diffusion_model"];
909-
if (params->ctxParams.diffusion_model_path != params->diffusion_models_dir + diffusion_model) {
910-
params->ctxParams.diffusion_model_path = params->diffusion_models_dir + diffusion_model;
911-
params->ctxParams.model_path = "";
912-
updatectx = true;
917+
if (diffusion_model == "none") {
918+
if (params->ctxParams.diffusion_model_path != "") {
919+
updatectx = true;
920+
}
921+
params->ctxParams.diffusion_model_path = "";
922+
} else {
923+
std::string new_path = params->diffusion_models_dir + diffusion_model;
924+
if (params->ctxParams.diffusion_model_path != new_path) {
925+
params->ctxParams.diffusion_model_path = new_path;
926+
params->ctxParams.model_path = "";
927+
updatectx = true;
928+
}
913929
}
914930
} catch (...) {
915931
}
916932
try {
917933
std::string clip_l = payload["clip_l"];
918-
if (params->ctxParams.clip_l_path != params->clip_dir + clip_l) {
919-
params->ctxParams.clip_l_path = params->clip_dir + clip_l;
920-
updatectx = true;
934+
if (clip_l == "none") {
935+
if (params->ctxParams.clip_l_path != "") {
936+
updatectx = true;
937+
}
938+
params->ctxParams.clip_l_path = "";
939+
} else {
940+
std::string new_path = params->clip_dir + clip_l;
941+
if (params->ctxParams.clip_l_path != new_path) {
942+
params->ctxParams.clip_l_path = new_path;
943+
updatectx = true;
944+
}
921945
}
922946
} catch (...) {
923947
}
924948
try {
925949
std::string clip_g = payload["clip_g"];
926-
if (params->ctxParams.clip_g_path != params->clip_dir + clip_g) {
927-
params->ctxParams.clip_g_path = params->clip_dir + clip_g;
928-
updatectx = true;
950+
if (clip_g == "none") {
951+
if (params->ctxParams.clip_g_path != "") {
952+
updatectx = true;
953+
}
954+
params->ctxParams.clip_g_path = "";
955+
} else {
956+
std::string new_path = params->clip_dir + clip_g;
957+
if (params->ctxParams.clip_g_path != new_path) {
958+
params->ctxParams.clip_g_path = new_path;
959+
updatectx = true;
960+
}
929961
}
930962
} catch (...) {
931963
}
932964
try {
933965
std::string t5xxl = payload["t5xxl"];
934-
if (params->ctxParams.t5xxl_path != params->clip_dir + t5xxl) {
935-
params->ctxParams.t5xxl_path = params->clip_dir + t5xxl;
936-
updatectx = true;
966+
if (t5xxl == "none") {
967+
if (params->ctxParams.t5xxl_path != "") {
968+
updatectx = true;
969+
}
970+
params->ctxParams.t5xxl_path = "";
971+
} else {
972+
std::string new_path = params->clip_dir + t5xxl;
973+
if (params->ctxParams.t5xxl_path != new_path) {
974+
params->ctxParams.t5xxl_path = new_path;
975+
updatectx = true;
976+
}
937977
}
938978
} catch (...) {
939979
}
940980
try {
941981
std::string vae = payload["vae"];
942-
if (params->ctxParams.vae_path != params->vae_dir + vae) {
943-
params->ctxParams.vae_path = params->vae_dir + vae;
944-
updatectx = true;
982+
if (vae == "none") {
983+
if (params->ctxParams.vae_path != "") {
984+
updatectx = true;
985+
}
986+
params->ctxParams.vae_path = "";
987+
} else {
988+
std::string new_path = params->vae_dir + vae;
989+
if (params->ctxParams.vae_path != new_path) {
990+
params->ctxParams.vae_path = new_path;
991+
updatectx = true;
992+
}
945993
}
946994
} catch (...) {
947995
}
948996
try {
949997
std::string tae = payload["tae"];
950-
if (params->ctxParams.taesd_path != params->tae_dir + tae) {
951-
params->ctxParams.taesd_path = params->tae_dir + tae;
952-
updatectx = true;
998+
if (tae == "none") {
999+
if (params->ctxParams.taesd_path != "") {
1000+
updatectx = true;
1001+
}
1002+
params->ctxParams.taesd_path = "";
1003+
} else {
1004+
std::string new_path = params->tae_dir + tae;
1005+
if (params->ctxParams.taesd_path != new_path) {
1006+
params->ctxParams.taesd_path = new_path;
1007+
updatectx = true;
1008+
}
9531009
}
9541010
} catch (...) {
9551011
}

0 commit comments

Comments
 (0)