funasr-wss-server.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  3. * Reserved. MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. /* 2022-2023 by zhaomingwork */
  6. // io server
  7. // Usage:funasr-wss-server [--model_thread_num <int>] [--decoder_thread_num <int>]
  8. // [--io_thread_num <int>] [--port <int>] [--listen_ip
  9. // <string>] [--punc-quant <string>] [--punc-dir <string>]
  10. // [--vad-quant <string>] [--vad-dir <string>] [--quantize
  11. // <string>] --model-dir <string> [--] [--version] [-h]
  12. #include "websocket-server.h"
  13. #include <unistd.h>
  14. using namespace std;
  15. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key,
  16. std::map<std::string, std::string>& model_path) {
  17. model_path.insert({key, value_arg.getValue()});
  18. LOG(INFO) << key << " : " << value_arg.getValue();
  19. }
  20. int main(int argc, char* argv[]) {
  21. try {
  22. google::InitGoogleLogging(argv[0]);
  23. FLAGS_logtostderr = true;
  24. TCLAP::CmdLine cmd("funasr-ws-server", ' ', "1.0");
  25. TCLAP::ValueArg<std::string> download_model_dir(
  26. "", "download-model-dir",
  27. "Download model from Modelscope to download_model_dir",
  28. false, "", "string");
  29. TCLAP::ValueArg<std::string> model_dir(
  30. "", MODEL_DIR,
  31. "default: /workspace/models/asr, the asr model path, which contains model.onnx, config.yaml, am.mvn",
  32. false, "/workspace/models/asr", "string");
  33. TCLAP::ValueArg<std::string> quantize(
  34. "", QUANTIZE,
  35. "true (Default), load the model of model.onnx in model_dir. If set "
  36. "true, load the model of model_quant.onnx in model_dir",
  37. false, "true", "string");
  38. TCLAP::ValueArg<std::string> vad_dir(
  39. "", VAD_DIR,
  40. "default: /workspace/models/vad, the vad model path, which contains model.onnx, vad.yaml, vad.mvn",
  41. false, "/workspace/models/vad", "string");
  42. TCLAP::ValueArg<std::string> vad_quant(
  43. "", VAD_QUANT,
  44. "true (Default), load the model of model.onnx in vad_dir. If set "
  45. "true, load the model of model_quant.onnx in vad_dir",
  46. false, "true", "string");
  47. TCLAP::ValueArg<std::string> punc_dir(
  48. "", PUNC_DIR,
  49. "default: /workspace/models/punc, the punc model path, which contains model.onnx, punc.yaml",
  50. false, "/workspace/models/punc",
  51. "string");
  52. TCLAP::ValueArg<std::string> punc_quant(
  53. "", PUNC_QUANT,
  54. "true (Default), load the model of model.onnx in punc_dir. If set "
  55. "true, load the model of model_quant.onnx in punc_dir",
  56. false, "true", "string");
  57. TCLAP::ValueArg<std::string> listen_ip("", "listen-ip", "listen ip", false,
  58. "0.0.0.0", "string");
  59. TCLAP::ValueArg<int> port("", "port", "port", false, 10095, "int");
  60. TCLAP::ValueArg<int> io_thread_num("", "io-thread-num", "io thread num",
  61. false, 8, "int");
  62. TCLAP::ValueArg<int> decoder_thread_num(
  63. "", "decoder-thread-num", "decoder thread num", false, 8, "int");
  64. TCLAP::ValueArg<int> model_thread_num("", "model-thread-num",
  65. "model thread num", false, 1, "int");
  66. TCLAP::ValueArg<std::string> certfile("", "certfile",
  67. "default: ../../../ssl_key/server.crt, path of certficate for WSS connection. if it is empty, it will be in WS mode.",
  68. false, "../../../ssl_key/server.crt", "string");
  69. TCLAP::ValueArg<std::string> keyfile("", "keyfile",
  70. "default: ../../../ssl_key/server.key, path of keyfile for WSS connection",
  71. false, "../../../ssl_key/server.key", "string");
  72. cmd.add(certfile);
  73. cmd.add(keyfile);
  74. cmd.add(download_model_dir);
  75. cmd.add(model_dir);
  76. cmd.add(quantize);
  77. cmd.add(vad_dir);
  78. cmd.add(vad_quant);
  79. cmd.add(punc_dir);
  80. cmd.add(punc_quant);
  81. cmd.add(listen_ip);
  82. cmd.add(port);
  83. cmd.add(io_thread_num);
  84. cmd.add(decoder_thread_num);
  85. cmd.add(model_thread_num);
  86. cmd.parse(argc, argv);
  87. std::map<std::string, std::string> model_path;
  88. GetValue(model_dir, MODEL_DIR, model_path);
  89. GetValue(quantize, QUANTIZE, model_path);
  90. GetValue(vad_dir, VAD_DIR, model_path);
  91. GetValue(vad_quant, VAD_QUANT, model_path);
  92. GetValue(punc_dir, PUNC_DIR, model_path);
  93. GetValue(punc_quant, PUNC_QUANT, model_path);
  94. // Download model form Modelscope
  95. try{
  96. std::string s_download_model_dir = download_model_dir.getValue();
  97. if(download_model_dir.isSet() && !s_download_model_dir.empty()){
  98. if (access(s_download_model_dir.c_str(), F_OK) != 0){
  99. LOG(ERROR) << s_download_model_dir << " do not exists.";
  100. exit(-1);
  101. }
  102. std::string s_vad_path = model_path[VAD_DIR];
  103. std::string s_asr_path = model_path[MODEL_DIR];
  104. std::string s_punc_path = model_path[PUNC_DIR];
  105. std::string python_cmd = "python -m funasr.export.export_model --type onnx --quantize True ";
  106. if(vad_dir.isSet() && !s_vad_path.empty()){
  107. std::string python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir;
  108. LOG(INFO) << "Download model: " << s_vad_path << " from modelscope: ";
  109. system(python_cmd_vad.c_str());
  110. std::string down_vad_path = s_download_model_dir+"/"+s_vad_path;
  111. std::string down_vad_model = s_download_model_dir+"/"+s_vad_path+"/model_quant.onnx";
  112. if (access(down_vad_model.c_str(), F_OK) != 0){
  113. LOG(ERROR) << down_vad_model << " do not exists.";
  114. exit(-1);
  115. }else{
  116. model_path[VAD_DIR]=down_vad_path;
  117. LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
  118. }
  119. }else{
  120. LOG(INFO) << "VAD model is not set, use default.";
  121. }
  122. if(model_dir.isSet() && !s_asr_path.empty()){
  123. std::string python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir;
  124. LOG(INFO) << "Download model: " << s_asr_path << " from modelscope: ";
  125. system(python_cmd_asr.c_str());
  126. std::string down_asr_path = s_download_model_dir+"/"+s_asr_path;
  127. std::string down_asr_model = s_download_model_dir+"/"+s_asr_path+"/model_quant.onnx";
  128. if (access(down_asr_model.c_str(), F_OK) != 0){
  129. LOG(ERROR) << down_asr_model << " do not exists.";
  130. exit(-1);
  131. }else{
  132. model_path[MODEL_DIR]=down_asr_path;
  133. LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
  134. }
  135. }else{
  136. LOG(INFO) << "ASR model is not set, use default.";
  137. }
  138. if(punc_dir.isSet() && !s_punc_path.empty()){
  139. std::string python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir;
  140. LOG(INFO) << "Download model: " << s_punc_path << " from modelscope: ";
  141. system(python_cmd_punc.c_str());
  142. std::string down_punc_path = s_download_model_dir+"/"+s_punc_path;
  143. std::string down_punc_model = s_download_model_dir+"/"+s_punc_path+"/model_quant.onnx";
  144. if (access(down_punc_model.c_str(), F_OK) != 0){
  145. LOG(ERROR) << down_punc_model << " do not exists.";
  146. exit(-1);
  147. }else{
  148. model_path[PUNC_DIR]=down_punc_path;
  149. LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
  150. }
  151. }else{
  152. LOG(INFO) << "PUNC model is not set, use default.";
  153. }
  154. }
  155. } catch (std::exception const& e) {
  156. LOG(ERROR) << "Error: " << e.what();
  157. }
  158. std::string s_listen_ip = listen_ip.getValue();
  159. int s_port = port.getValue();
  160. int s_io_thread_num = io_thread_num.getValue();
  161. int s_decoder_thread_num = decoder_thread_num.getValue();
  162. int s_model_thread_num = model_thread_num.getValue();
  163. asio::io_context io_decoder; // context for decoding
  164. asio::io_context io_server; // context for server
  165. std::vector<std::thread> decoder_threads;
  166. std::string s_certfile = certfile.getValue();
  167. std::string s_keyfile = keyfile.getValue();
  168. bool is_ssl = false;
  169. if (!s_certfile.empty()) {
  170. is_ssl = true;
  171. }
  172. auto conn_guard = asio::make_work_guard(
  173. io_decoder); // make sure threads can wait in the queue
  174. auto server_guard = asio::make_work_guard(
  175. io_server); // make sure threads can wait in the queue
  176. // create threads pool
  177. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  178. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  179. }
  180. server server_; // server for websocket
  181. wss_server wss_server_;
  182. if (is_ssl) {
  183. wss_server_.init_asio(&io_server); // init asio
  184. wss_server_.set_reuse_addr(
  185. true); // reuse address as we create multiple threads
  186. // list on port for accept
  187. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  188. WebSocketServer websocket_srv(
  189. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  190. s_keyfile); // websocket server for asr engine
  191. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  192. } else {
  193. server_.init_asio(&io_server); // init asio
  194. server_.set_reuse_addr(
  195. true); // reuse address as we create multiple threads
  196. // list on port for accept
  197. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  198. WebSocketServer websocket_srv(
  199. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  200. s_keyfile); // websocket server for asr engine
  201. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  202. }
  203. std::cout << "asr model init finished. listen on port:" << s_port
  204. << std::endl;
  205. // Start the ASIO network io_service run loop
  206. std::vector<std::thread> ts;
  207. // create threads for io network
  208. for (size_t i = 0; i < s_io_thread_num; i++) {
  209. ts.emplace_back([&io_server]() { io_server.run(); });
  210. }
  211. // wait for theads
  212. for (size_t i = 0; i < s_io_thread_num; i++) {
  213. ts[i].join();
  214. }
  215. // wait for theads
  216. for (auto& t : decoder_threads) {
  217. t.join();
  218. }
  219. } catch (std::exception const& e) {
  220. std::cerr << "Error: " << e.what() << std::endl;
  221. }
  222. return 0;
  223. }