funasr-wss-server.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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-wss-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, "/workspace/models", "string");
  29. TCLAP::ValueArg<std::string> model_dir(
  30. "", MODEL_DIR,
  31. "default: /workspace/models/asr, the asr model path, which contains model_quant.onnx, config.yaml, am.mvn",
  32. false, "/workspace/models/asr", "string");
  33. TCLAP::ValueArg<std::string> model_revision(
  34. "", "model-revision",
  35. "ASR model revision",
  36. false, "v1.2.1", "string");
  37. TCLAP::ValueArg<std::string> quantize(
  38. "", QUANTIZE,
  39. "true (Default), load the model of model_quant.onnx in model_dir. If set "
  40. "false, load the model of model.onnx in model_dir",
  41. false, "true", "string");
  42. TCLAP::ValueArg<std::string> vad_dir(
  43. "", VAD_DIR,
  44. "default: /workspace/models/vad, the vad model path, which contains model_quant.onnx, vad.yaml, vad.mvn",
  45. false, "/workspace/models/vad", "string");
  46. TCLAP::ValueArg<std::string> vad_revision(
  47. "", "vad-revision",
  48. "VAD model revision",
  49. false, "v1.2.0", "string");
  50. TCLAP::ValueArg<std::string> vad_quant(
  51. "", VAD_QUANT,
  52. "true (Default), load the model of model_quant.onnx in vad_dir. If set "
  53. "false, load the model of model.onnx in vad_dir",
  54. false, "true", "string");
  55. TCLAP::ValueArg<std::string> punc_dir(
  56. "", PUNC_DIR,
  57. "default: /workspace/models/punc, the punc model path, which contains model_quant.onnx, punc.yaml",
  58. false, "/workspace/models/punc",
  59. "string");
  60. TCLAP::ValueArg<std::string> punc_revision(
  61. "", "punc-revision",
  62. "PUNC model revision",
  63. false, "v1.1.7", "string");
  64. TCLAP::ValueArg<std::string> punc_quant(
  65. "", PUNC_QUANT,
  66. "true (Default), load the model of model_quant.onnx in punc_dir. If set "
  67. "false, load the model of model.onnx in punc_dir",
  68. false, "true", "string");
  69. TCLAP::ValueArg<std::string> listen_ip("", "listen-ip", "listen ip", false,
  70. "0.0.0.0", "string");
  71. TCLAP::ValueArg<int> port("", "port", "port", false, 10095, "int");
  72. TCLAP::ValueArg<int> io_thread_num("", "io-thread-num", "io thread num",
  73. false, 8, "int");
  74. TCLAP::ValueArg<int> decoder_thread_num(
  75. "", "decoder-thread-num", "decoder thread num", false, 8, "int");
  76. TCLAP::ValueArg<int> model_thread_num("", "model-thread-num",
  77. "model thread num", false, 1, "int");
  78. TCLAP::ValueArg<std::string> certfile("", "certfile",
  79. "default: ../../../ssl_key/server.crt, path of certficate for WSS connection. if it is empty, it will be in WS mode.",
  80. false, "../../../ssl_key/server.crt", "string");
  81. TCLAP::ValueArg<std::string> keyfile("", "keyfile",
  82. "default: ../../../ssl_key/server.key, path of keyfile for WSS connection",
  83. false, "../../../ssl_key/server.key", "string");
  84. cmd.add(certfile);
  85. cmd.add(keyfile);
  86. cmd.add(download_model_dir);
  87. cmd.add(model_dir);
  88. cmd.add(model_revision);
  89. cmd.add(quantize);
  90. cmd.add(vad_dir);
  91. cmd.add(vad_revision);
  92. cmd.add(vad_quant);
  93. cmd.add(punc_dir);
  94. cmd.add(punc_revision);
  95. cmd.add(punc_quant);
  96. cmd.add(listen_ip);
  97. cmd.add(port);
  98. cmd.add(io_thread_num);
  99. cmd.add(decoder_thread_num);
  100. cmd.add(model_thread_num);
  101. cmd.parse(argc, argv);
  102. std::map<std::string, std::string> model_path;
  103. GetValue(model_dir, MODEL_DIR, model_path);
  104. GetValue(quantize, QUANTIZE, model_path);
  105. GetValue(vad_dir, VAD_DIR, model_path);
  106. GetValue(vad_quant, VAD_QUANT, model_path);
  107. GetValue(punc_dir, PUNC_DIR, model_path);
  108. GetValue(punc_quant, PUNC_QUANT, model_path);
  109. GetValue(model_revision, "model-revision", model_path);
  110. GetValue(vad_revision, "vad-revision", model_path);
  111. GetValue(punc_revision, "punc-revision", model_path);
  112. // Download model form Modelscope
  113. try{
  114. std::string s_download_model_dir = download_model_dir.getValue();
  115. std::string s_vad_path = model_path[VAD_DIR];
  116. std::string s_vad_quant = model_path[VAD_QUANT];
  117. std::string s_asr_path = model_path[MODEL_DIR];
  118. std::string s_asr_quant = model_path[QUANTIZE];
  119. std::string s_punc_path = model_path[PUNC_DIR];
  120. std::string s_punc_quant = model_path[PUNC_QUANT];
  121. std::string python_cmd = "python -m funasr.utils.runtime_sdk_download_tool --type onnx --quantize True ";
  122. if(vad_dir.isSet() && !s_vad_path.empty()){
  123. std::string python_cmd_vad;
  124. std::string down_vad_path;
  125. std::string down_vad_model;
  126. if (access(s_vad_path.c_str(), F_OK) == 0){
  127. // local
  128. python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir ./ " + " --model_revision " + model_path["vad-revision"];
  129. down_vad_path = s_vad_path;
  130. }else{
  131. // modelscope
  132. LOG(INFO) << "Download model: " << s_vad_path << " from modelscope: ";
  133. python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["vad-revision"];
  134. down_vad_path = s_download_model_dir+"/"+s_vad_path;
  135. }
  136. int ret = system(python_cmd_vad.c_str());
  137. if(ret !=0){
  138. LOG(INFO) << "Failed to download model from modelscope. If you set local vad model path, you can ignore the errors.";
  139. }
  140. down_vad_model = down_vad_path+"/model_quant.onnx";
  141. if(s_vad_quant=="false" || s_vad_quant=="False" || s_vad_quant=="FALSE"){
  142. down_vad_model = down_vad_path+"/model.onnx";
  143. }
  144. if (access(down_vad_model.c_str(), F_OK) != 0){
  145. LOG(ERROR) << down_vad_model << " do not exists.";
  146. exit(-1);
  147. }else{
  148. model_path[VAD_DIR]=down_vad_path;
  149. LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
  150. }
  151. }else{
  152. LOG(INFO) << "VAD model is not set, use default.";
  153. }
  154. if(model_dir.isSet() && !s_asr_path.empty()){
  155. std::string python_cmd_asr;
  156. std::string down_asr_path;
  157. std::string down_asr_model;
  158. if (access(s_asr_path.c_str(), F_OK) == 0){
  159. // local
  160. python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir ./ " + " --model_revision " + model_path["model-revision"];
  161. down_asr_path = s_asr_path;
  162. }else{
  163. // modelscope
  164. LOG(INFO) << "Download model: " << s_asr_path << " from modelscope: ";
  165. python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["model-revision"];
  166. down_asr_path = s_download_model_dir+"/"+s_asr_path;
  167. }
  168. int ret = system(python_cmd_asr.c_str());
  169. if(ret !=0){
  170. LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
  171. }
  172. down_asr_model = down_asr_path+"/model_quant.onnx";
  173. if(s_asr_quant=="false" || s_asr_quant=="False" || s_asr_quant=="FALSE"){
  174. down_asr_model = down_asr_path+"/model.onnx";
  175. }
  176. if (access(down_asr_model.c_str(), F_OK) != 0){
  177. LOG(ERROR) << down_asr_model << " do not exists.";
  178. exit(-1);
  179. }else{
  180. model_path[MODEL_DIR]=down_asr_path;
  181. LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
  182. }
  183. }else{
  184. LOG(INFO) << "ASR model is not set, use default.";
  185. }
  186. if(punc_dir.isSet() && !s_punc_path.empty()){
  187. std::string python_cmd_punc;
  188. std::string down_punc_path;
  189. std::string down_punc_model;
  190. if (access(s_punc_path.c_str(), F_OK) == 0){
  191. // local
  192. python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir ./ " + " --model_revision " + model_path["punc-revision"];
  193. down_punc_path = s_punc_path;
  194. }else{
  195. // modelscope
  196. LOG(INFO) << "Download model: " << s_punc_path << " from modelscope: ";
  197. python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["punc-revision"];
  198. down_punc_path = s_download_model_dir+"/"+s_punc_path;
  199. }
  200. int ret = system(python_cmd_punc.c_str());
  201. if(ret !=0){
  202. LOG(INFO) << "Failed to download model from modelscope. If you set local punc model path, you can ignore the errors.";
  203. }
  204. down_punc_model = down_punc_path+"/model_quant.onnx";
  205. if(s_punc_quant=="false" || s_punc_quant=="False" || s_punc_quant=="FALSE"){
  206. down_punc_model = down_punc_path+"/model.onnx";
  207. }
  208. if (access(down_punc_model.c_str(), F_OK) != 0){
  209. LOG(ERROR) << down_punc_model << " do not exists.";
  210. exit(-1);
  211. }else{
  212. model_path[PUNC_DIR]=down_punc_path;
  213. LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
  214. }
  215. }else{
  216. LOG(INFO) << "PUNC model is not set, use default.";
  217. }
  218. } catch (std::exception const& e) {
  219. LOG(ERROR) << "Error: " << e.what();
  220. }
  221. std::string s_listen_ip = listen_ip.getValue();
  222. int s_port = port.getValue();
  223. int s_io_thread_num = io_thread_num.getValue();
  224. int s_decoder_thread_num = decoder_thread_num.getValue();
  225. int s_model_thread_num = model_thread_num.getValue();
  226. asio::io_context io_decoder; // context for decoding
  227. asio::io_context io_server; // context for server
  228. std::vector<std::thread> decoder_threads;
  229. std::string s_certfile = certfile.getValue();
  230. std::string s_keyfile = keyfile.getValue();
  231. bool is_ssl = false;
  232. if (!s_certfile.empty()) {
  233. is_ssl = true;
  234. }
  235. auto conn_guard = asio::make_work_guard(
  236. io_decoder); // make sure threads can wait in the queue
  237. auto server_guard = asio::make_work_guard(
  238. io_server); // make sure threads can wait in the queue
  239. // create threads pool
  240. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  241. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  242. }
  243. server server_; // server for websocket
  244. wss_server wss_server_;
  245. if (is_ssl) {
  246. wss_server_.init_asio(&io_server); // init asio
  247. wss_server_.set_reuse_addr(
  248. true); // reuse address as we create multiple threads
  249. // list on port for accept
  250. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  251. WebSocketServer websocket_srv(
  252. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  253. s_keyfile); // websocket server for asr engine
  254. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  255. } else {
  256. server_.init_asio(&io_server); // init asio
  257. server_.set_reuse_addr(
  258. true); // reuse address as we create multiple threads
  259. // list on port for accept
  260. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  261. WebSocketServer websocket_srv(
  262. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  263. s_keyfile); // websocket server for asr engine
  264. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  265. }
  266. std::cout << "asr model init finished. listen on port:" << s_port
  267. << std::endl;
  268. // Start the ASIO network io_service run loop
  269. std::vector<std::thread> ts;
  270. // create threads for io network
  271. for (size_t i = 0; i < s_io_thread_num; i++) {
  272. ts.emplace_back([&io_server]() { io_server.run(); });
  273. }
  274. // wait for theads
  275. for (size_t i = 0; i < s_io_thread_num; i++) {
  276. ts[i].join();
  277. }
  278. // wait for theads
  279. for (auto& t : decoder_threads) {
  280. t.join();
  281. }
  282. } catch (std::exception const& e) {
  283. std::cerr << "Error: " << e.what() << std::endl;
  284. }
  285. return 0;
  286. }