Spaces:
Running
Running
Unified return format
Browse files- examples/server/server.cpp +46 -14
examples/server/server.cpp
CHANGED
|
@@ -620,6 +620,7 @@ namespace {
|
|
| 620 |
std::string result; // final response body
|
| 621 |
std::string content_type = "application/json";
|
| 622 |
std::string error; // error message if failed
|
|
|
|
| 623 |
};
|
| 624 |
|
| 625 |
static std::unordered_map<std::string, async_task_t> tasks;
|
|
@@ -1185,6 +1186,7 @@ int main(int argc, char ** argv) {
|
|
| 1185 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1186 |
tasks[tmp_task_id] = async_task_t();
|
| 1187 |
tasks[tmp_task_id].status = async_status::PENDING;
|
|
|
|
| 1188 |
}
|
| 1189 |
|
| 1190 |
// run in same thread
|
|
@@ -1253,6 +1255,7 @@ int main(int argc, char ** argv) {
|
|
| 1253 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1254 |
tasks[task_id] = async_task_t();
|
| 1255 |
tasks[task_id].status = async_status::PENDING;
|
|
|
|
| 1256 |
}
|
| 1257 |
|
| 1258 |
// spawn background worker thread
|
|
@@ -1275,9 +1278,12 @@ int main(int argc, char ** argv) {
|
|
| 1275 |
return;
|
| 1276 |
}
|
| 1277 |
const std::string id = req.get_param_value("id");
|
| 1278 |
-
|
| 1279 |
-
|
| 1280 |
-
|
|
|
|
|
|
|
|
|
|
| 1281 |
{
|
| 1282 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1283 |
auto it = tasks.find(id);
|
|
@@ -1287,27 +1293,53 @@ int main(int argc, char ** argv) {
|
|
| 1287 |
return;
|
| 1288 |
}
|
| 1289 |
|
| 1290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1291 |
if (t.status == async_status::PENDING || t.status == async_status::RUNNING) {
|
| 1292 |
-
|
| 1293 |
-
|
|
|
|
| 1294 |
return;
|
| 1295 |
}
|
|
|
|
| 1296 |
if (t.status == async_status::FAILED) {
|
| 1297 |
-
|
| 1298 |
-
|
| 1299 |
-
out_ctype = "application/json";
|
| 1300 |
// remove failed task from map to avoid accumulation
|
| 1301 |
tasks.erase(it);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1302 |
} else {
|
| 1303 |
-
//
|
| 1304 |
-
|
| 1305 |
-
out_ctype = t.content_type;
|
| 1306 |
-
tasks.erase(it);
|
| 1307 |
}
|
|
|
|
|
|
|
|
|
|
| 1308 |
}
|
| 1309 |
|
| 1310 |
-
res.set_content(
|
| 1311 |
});
|
| 1312 |
svr->Post(sparams.request_path + "/load", [&](const Request &req, Response &res){
|
| 1313 |
std::lock_guard<std::mutex> lock(whisper_mutex);
|
|
|
|
| 620 |
std::string result; // final response body
|
| 621 |
std::string content_type = "application/json";
|
| 622 |
std::string error; // error message if failed
|
| 623 |
+
whisper_params params; // copy of the params used for this task
|
| 624 |
};
|
| 625 |
|
| 626 |
static std::unordered_map<std::string, async_task_t> tasks;
|
|
|
|
| 1186 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1187 |
tasks[tmp_task_id] = async_task_t();
|
| 1188 |
tasks[tmp_task_id].status = async_status::PENDING;
|
| 1189 |
+
tasks[tmp_task_id].params = params; // store params used for this sync run
|
| 1190 |
}
|
| 1191 |
|
| 1192 |
// run in same thread
|
|
|
|
| 1255 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1256 |
tasks[task_id] = async_task_t();
|
| 1257 |
tasks[task_id].status = async_status::PENDING;
|
| 1258 |
+
tasks[task_id].params = task_params; // store params for async task
|
| 1259 |
}
|
| 1260 |
|
| 1261 |
// spawn background worker thread
|
|
|
|
| 1278 |
return;
|
| 1279 |
}
|
| 1280 |
const std::string id = req.get_param_value("id");
|
| 1281 |
+
|
| 1282 |
+
json response_json;
|
| 1283 |
+
response_json["status"] = "unknown";
|
| 1284 |
+
response_json["data"] = nullptr;
|
| 1285 |
+
response_json["params"] = json::object();
|
| 1286 |
+
|
| 1287 |
{
|
| 1288 |
std::lock_guard<std::mutex> tlock(tasks_mutex);
|
| 1289 |
auto it = tasks.find(id);
|
|
|
|
| 1293 |
return;
|
| 1294 |
}
|
| 1295 |
|
| 1296 |
+
auto t = it->second; // copy so we can erase while unlocked
|
| 1297 |
+
|
| 1298 |
+
// populate params subset for client inspection
|
| 1299 |
+
json p = json::object();
|
| 1300 |
+
p["temperature"] = t.params.temperature;
|
| 1301 |
+
p["temperature_inc"] = t.params.temperature_inc;
|
| 1302 |
+
p["response_format"] = t.params.response_format;
|
| 1303 |
+
p["n_threads"] = t.params.n_threads;
|
| 1304 |
+
response_json["params"] = p;
|
| 1305 |
+
|
| 1306 |
if (t.status == async_status::PENDING || t.status == async_status::RUNNING) {
|
| 1307 |
+
response_json["status"] = "processing";
|
| 1308 |
+
response_json["data"] = nullptr;
|
| 1309 |
+
res.set_content(response_json.dump(), "application/json");
|
| 1310 |
return;
|
| 1311 |
}
|
| 1312 |
+
|
| 1313 |
if (t.status == async_status::FAILED) {
|
| 1314 |
+
response_json["status"] = "failed";
|
| 1315 |
+
response_json["data"] = json{{"error", t.error}};
|
|
|
|
| 1316 |
// remove failed task from map to avoid accumulation
|
| 1317 |
tasks.erase(it);
|
| 1318 |
+
res.set_content(response_json.dump(), "application/json");
|
| 1319 |
+
return;
|
| 1320 |
+
}
|
| 1321 |
+
|
| 1322 |
+
// FINISHED
|
| 1323 |
+
response_json["status"] = "finished";
|
| 1324 |
+
|
| 1325 |
+
// If original requested JSON format, try to parse stored result JSON and return as data
|
| 1326 |
+
if (t.params.response_format == json_format || t.content_type.find("application/json") != std::string::npos) {
|
| 1327 |
+
try {
|
| 1328 |
+
json parsed = json::parse(t.result);
|
| 1329 |
+
response_json["data"] = parsed;
|
| 1330 |
+
} catch (...) {
|
| 1331 |
+
response_json["data"] = t.result;
|
| 1332 |
+
}
|
| 1333 |
} else {
|
| 1334 |
+
// non-json content -> return as string under data.content
|
| 1335 |
+
response_json["data"] = json{{"content", t.result}, {"content_type", t.content_type}};
|
|
|
|
|
|
|
| 1336 |
}
|
| 1337 |
+
|
| 1338 |
+
// remove the task after building the response
|
| 1339 |
+
tasks.erase(it);
|
| 1340 |
}
|
| 1341 |
|
| 1342 |
+
res.set_content(response_json.dump(), "application/json");
|
| 1343 |
});
|
| 1344 |
svr->Post(sparams.request_path + "/load", [&](const Request &req, Response &res){
|
| 1345 |
std::lock_guard<std::mutex> lock(whisper_mutex);
|