-
Notifications
You must be signed in to change notification settings - Fork 33
Add voices api endpoint #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ enum error_type { | |
| enum task_type { | ||
| TTS, | ||
| CONDITIONAL_PROMPT, | ||
| VOICES, | ||
| }; | ||
|
|
||
| using json = nlohmann::ordered_json; | ||
|
|
@@ -87,8 +88,8 @@ static void log_server_request(const httplib::Request & req, const httplib::Resp | |
| fprintf(stdout, "request: %s %s %s %d\n", req.method.c_str(), req.path.c_str(), req.remote_addr.c_str(), res.status); | ||
| } | ||
|
|
||
| struct simple_text_prompt_task { | ||
| simple_text_prompt_task(task_type task, std::string prompt): task(task), prompt(prompt) { | ||
| struct simple_server_task { | ||
| simple_server_task(task_type task, std::string prompt = ""): task(task), prompt(prompt) { | ||
| id = rand(); | ||
| time = std::chrono::steady_clock::now(); | ||
| } | ||
|
|
@@ -114,11 +115,11 @@ struct simple_text_prompt_task { | |
| struct simple_task_queue { | ||
| std::mutex rw_mutex; | ||
| std::condition_variable condition; | ||
| std::deque<simple_text_prompt_task*> queue; | ||
| std::deque<simple_server_task*> queue; | ||
| bool running = true; | ||
|
|
||
| struct simple_text_prompt_task * get_next() { | ||
| struct simple_text_prompt_task * resp; | ||
| struct simple_server_task * get_next() { | ||
| struct simple_server_task * resp; | ||
| std::unique_lock<std::mutex> lock(rw_mutex); | ||
| condition.wait(lock, [&]{ | ||
| return !queue.empty() || !running; | ||
|
|
@@ -138,7 +139,7 @@ struct simple_task_queue { | |
| condition.notify_all(); | ||
| } | ||
|
|
||
| void push(struct simple_text_prompt_task * task) { | ||
| void push(struct simple_server_task * task) { | ||
| std::lock_guard<std::mutex> lock(rw_mutex); | ||
| queue.push_back(task); | ||
| condition.notify_one(); | ||
|
|
@@ -152,7 +153,7 @@ struct simple_response_map { | |
| std::atomic<bool> running = true; | ||
| std::thread * cleanup_thread; | ||
|
|
||
| std::map<int, simple_text_prompt_task*> completed; | ||
| std::map<int, simple_server_task*> completed; | ||
|
|
||
| void cleanup_routine() { | ||
| std::unique_lock<std::mutex> lock(rw_mutex); | ||
|
|
@@ -182,16 +183,16 @@ struct simple_response_map { | |
| updated.notify_all(); | ||
| } | ||
|
|
||
| void push(struct simple_text_prompt_task * task) { | ||
| void push(struct simple_server_task * task) { | ||
| std::unique_lock<std::mutex> lock(rw_mutex); | ||
| completed[task->id] = task; | ||
| lock.unlock(); | ||
| updated.notify_all(); | ||
| } | ||
|
|
||
| struct simple_text_prompt_task * get(int id) { | ||
| struct simple_server_task * get(int id) { | ||
| std::unique_lock<std::mutex> lock(rw_mutex); | ||
| struct simple_text_prompt_task * resp = nullptr; | ||
| struct simple_server_task * resp = nullptr; | ||
| try { | ||
| return completed.at(id); | ||
| } catch (const std::out_of_range& e) { | ||
|
|
@@ -231,14 +232,14 @@ struct worker { | |
|
|
||
| void loop() { | ||
| while (running) { | ||
| struct simple_text_prompt_task * task = task_queue->get_next(); | ||
| struct simple_server_task * task = task_queue->get_next(); | ||
| if (task) { | ||
| process_task(task); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void process_task(struct simple_text_prompt_task * task) { | ||
| void process_task(struct simple_server_task * task) { | ||
| if (task->timed_out(task_timeout)) { | ||
| return; | ||
| } | ||
|
|
@@ -264,6 +265,21 @@ struct worker { | |
| task->success = true; | ||
| response_map->push(task); | ||
| break; | ||
| case VOICES: | ||
| if (!runner->supports_voices) { | ||
| task->message = "Voices are not supported for architecture '" + runner->arch_name() + "'."; | ||
| response_map->push(task); | ||
| break; | ||
| } | ||
| for (auto voice : list_voices(runner)) { | ||
| if (!task->message.empty()) { | ||
| task->message += ","; | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We really shouldn't use the message output for passing data back from the workers. |
||
| } | ||
| task->message += voice; | ||
| } | ||
| task->success = true; | ||
| response_map->push(task); | ||
| break; | ||
| } | ||
| } | ||
| }; | ||
|
|
@@ -432,6 +448,15 @@ int main(int argc, const char ** argv) { | |
| res.status = 200; | ||
| }; | ||
|
|
||
| auto res_ok_voices = [](httplib::Response & res, const std::vector<std::string> & voices) { | ||
| json json_voices = json::array(); | ||
| for (auto voice : voices) { | ||
| json_voices.push_back(voice); | ||
| } | ||
| res.set_content(safe_json_to_str(json_voices), MIMETYPE_JSON); | ||
| res.status = 200; | ||
| }; | ||
|
|
||
| svr->set_exception_handler([&res_error](const httplib::Request &, httplib::Response & res, const std::exception_ptr & ep) { | ||
| std::string message; | ||
| try { | ||
|
|
@@ -516,7 +541,7 @@ int main(int argc, const char ** argv) { | |
| res_error(res, formatted_error); | ||
| return; | ||
| } | ||
| struct simple_text_prompt_task * task = new simple_text_prompt_task(TTS, prompt); | ||
| struct simple_server_task * task = new simple_server_task(TTS, prompt); | ||
| int id = task->id; | ||
| generation_configuration * conf = new generation_configuration(); | ||
| std::memcpy(conf, default_generation_config, sizeof(generation_configuration)); | ||
|
|
@@ -544,7 +569,7 @@ int main(int argc, const char ** argv) { | |
|
|
||
| task->gen_config = conf; | ||
| tqueue->push(task); | ||
| struct simple_text_prompt_task * rtask = rmap->get(id); | ||
| struct simple_server_task * rtask = rmap->get(id); | ||
| if (!rtask->success) { | ||
| json formatted_error = format_error_response(rtask->message, ERROR_TYPE_SERVER); | ||
| res_error(res, formatted_error); | ||
|
|
@@ -586,10 +611,10 @@ int main(int argc, const char ** argv) { | |
| return; | ||
| } | ||
| std::string prompt = data.at("input").get<std::string>(); | ||
| struct simple_text_prompt_task * task = new simple_text_prompt_task(CONDITIONAL_PROMPT, prompt); | ||
| struct simple_server_task * task = new simple_server_task(CONDITIONAL_PROMPT, prompt); | ||
| int id = task->id; | ||
| tqueue->push(task); | ||
| struct simple_text_prompt_task * rtask = rmap->get(id); | ||
| struct simple_server_task * rtask = rmap->get(id); | ||
| if (!rtask->success) { | ||
| json formatted_error = format_error_response(rtask->message, ERROR_TYPE_SERVER); | ||
| res_error(res, formatted_error); | ||
|
|
@@ -599,10 +624,31 @@ int main(int argc, const char ** argv) { | |
| res_ok_json(res, health); | ||
| }; | ||
|
|
||
| const auto handle_voices = [&args, &tqueue, &rmap, &res_error, &res_ok_voices](const httplib::Request & req, httplib::Response & res) { | ||
| struct simple_server_task * task = new simple_server_task(VOICES); | ||
| int id = task->id; | ||
| tqueue->push(task); | ||
| struct simple_server_task * rtask = rmap->get(id); | ||
| if (!rtask->success) { | ||
| json formatted_error; | ||
| if (has_prefix(rtask->message, "Voices are not supported")) { | ||
| formatted_error = format_error_response(rtask->message, ERROR_TYPE_NOT_SUPPORTED); | ||
| } else { | ||
| formatted_error = format_error_response(rtask->message, ERROR_TYPE_SERVER); | ||
| } | ||
| res_error(res, formatted_error); | ||
| return; | ||
| } | ||
| std::vector<std::string> voices = split(rtask->message, ","); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very inefficient and a more generic response pattern should be preferred (rather than parsing a string list from the message). |
||
| res_ok_voices(res, voices); | ||
| }; | ||
|
|
||
| // register API routes | ||
| svr->Get("/health", handle_health); | ||
| svr->Post("/v1/audio/speech", handle_tts); | ||
| svr->Post("/v1/audio/conditional-prompt", handle_conditional); | ||
| svr->Get("/v1/audio/voices", handle_voices); | ||
|
|
||
|
|
||
| // Start the server | ||
| svr->new_task_queue = [&args] { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.