Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 817b854

Browse files
committedJan 4, 2025·
Use progress_callback
1 parent cfdb792 commit 817b854

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed
 

‎examples/server/frontend.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,6 @@ R"xxx(
643643
const taskId = data.task_id;
644644
let status = 'Pending';
645645
const progressBar = document.getElementById("progress");
646-
progressBar.max = steps;
647646
while (status !== 'Done' && status !== 'Failed') {
648647
const statusResponse = await fetch(`/result?task_id=${taskId}`);
649648
const statusData = await statusResponse.json();
@@ -671,9 +670,10 @@ R"xxx(
671670
}
672671
status = statusData.status;
673672
document.getElementById('status').innerHTML = status;
674-
if (status === 'Working') {
675-
progressBar.value = statusData.step;
676-
progressBar.innerHTML = Math.floor(100 * statusData.step / steps) + "%";
673+
if (statusData.step >= 0) {
674+
progressBar.value = statusData.step;
675+
progressBar.max = statusData.steps ?? steps;
676+
progressBar.innerHTML = Math.floor(100 * statusData.step / statusData.steps) + "%";
677677
progressBar.style.display = 'inline-block';
678678
}
679679
if (status === 'Done' || (status === 'Working' && statusData.data.length > 0)) {

‎examples/server/main.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -1084,8 +1084,23 @@ void add_task(std::string task_id, std::function<void()> task) {
10841084
queue_cond.notify_one();
10851085
}
10861086

1087+
void update_progress_cb(int step, int steps, float time, void* _data) {
1088+
using json = nlohmann::json;
1089+
if (running_task_id != "") {
1090+
std::lock_guard<std::mutex> results_lock(results_mutex);
1091+
json running_task_json = task_results[running_task_id];
1092+
running_task_json["step"] = step;
1093+
running_task_json["steps"] = steps;
1094+
if (running_task_json["status"] == "Working" && step == steps) {
1095+
running_task_json["status"] = "Decoding";
1096+
}
1097+
task_results[running_task_id] = running_task_json;
1098+
}
1099+
}
1100+
10871101
void start_server(SDParams params) {
10881102
sd_set_log_callback(sd_log_cb, (void*)&params);
1103+
sd_set_progress_callback(update_progress_cb, NULL);
10891104

10901105
server_log_params = (void*)&params;
10911106

@@ -1122,6 +1137,7 @@ void start_server(SDParams params) {
11221137
pending_task_json["status"] = "Pending";
11231138
pending_task_json["data"] = json::array();
11241139
pending_task_json["step"] = -1;
1140+
pending_task_json["steps"] = -1;
11251141
pending_task_json["eta"] = "?";
11261142

11271143
std::lock_guard<std::mutex> results_lock(results_mutex);
@@ -1167,6 +1183,7 @@ void start_server(SDParams params) {
11671183
task_json["status"] = "Loading";
11681184
task_json["data"] = json::array();
11691185
task_json["step"] = -1;
1186+
task_json["step"] = -1;
11701187
task_json["eta"] = "?";
11711188

11721189
std::lock_guard<std::mutex> results_lock(results_mutex);
@@ -1208,6 +1225,7 @@ void start_server(SDParams params) {
12081225
started_task_json["status"] = "Working";
12091226
started_task_json["data"] = json::array();
12101227
started_task_json["step"] = 0;
1228+
started_task_json["step"] = params.lastRequest.sample_steps;
12111229
started_task_json["eta"] = "?";
12121230

12131231
std::lock_guard<std::mutex> results_lock(results_mutex);

0 commit comments

Comments
 (0)
Please sign in to comment.