funasr-wss-server.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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, "", "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. // download model from modelscope when the model-dir is model ID or local path
  98. bool is_download = false;
  99. if(download_model_dir.isSet() && !s_download_model_dir.empty()){
  100. is_download = true;
  101. if (access(s_download_model_dir.c_str(), F_OK) != 0){
  102. LOG(ERROR) << s_download_model_dir << " do not exists.";
  103. exit(-1);
  104. }
  105. }else{
  106. s_download_model_dir="./";
  107. }
  108. std::string s_vad_path = model_path[VAD_DIR];
  109. std::string s_vad_quant = model_path[VAD_QUANT];
  110. std::string s_asr_path = model_path[MODEL_DIR];
  111. std::string s_asr_quant = model_path[QUANTIZE];
  112. std::string s_punc_path = model_path[PUNC_DIR];
  113. std::string s_punc_quant = model_path[PUNC_QUANT];
  114. std::string python_cmd = "python -m funasr.export.export_model --type onnx --quantize True ";
  115. if(vad_dir.isSet() && !s_vad_path.empty()){
  116. std::string python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir;
  117. if(is_download){
  118. LOG(INFO) << "Download model: " << s_vad_path << " from modelscope: ";
  119. }else{
  120. LOG(INFO) << "Check local model: " << s_vad_path;
  121. if (access(s_vad_path.c_str(), F_OK) != 0){
  122. LOG(ERROR) << s_vad_path << " do not exists.";
  123. exit(-1);
  124. }
  125. }
  126. system(python_cmd_vad.c_str());
  127. std::string down_vad_path;
  128. std::string down_vad_model;
  129. if(is_download){
  130. down_vad_path = s_download_model_dir+"/"+s_vad_path;
  131. down_vad_model = s_download_model_dir+"/"+s_vad_path+"/model_quant.onnx";
  132. }else{
  133. down_vad_path = s_vad_path;
  134. down_vad_model = s_vad_path+"/model_quant.onnx";
  135. if(s_vad_quant=="false" || s_vad_quant=="False" || s_vad_quant=="FALSE"){
  136. down_vad_model = s_vad_path+"/model.onnx";
  137. }
  138. }
  139. if (access(down_vad_model.c_str(), F_OK) != 0){
  140. LOG(ERROR) << down_vad_model << " do not exists.";
  141. exit(-1);
  142. }else{
  143. model_path[VAD_DIR]=down_vad_path;
  144. LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
  145. }
  146. }else{
  147. LOG(INFO) << "VAD model is not set, use default.";
  148. }
  149. if(model_dir.isSet() && !s_asr_path.empty()){
  150. std::string python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir;
  151. if(is_download){
  152. LOG(INFO) << "Download model: " << s_asr_path << " from modelscope: ";
  153. }else{
  154. LOG(INFO) << "Check local model: " << s_asr_path;
  155. if (access(s_asr_path.c_str(), F_OK) != 0){
  156. LOG(ERROR) << s_asr_path << " do not exists.";
  157. exit(-1);
  158. }
  159. }
  160. system(python_cmd_asr.c_str());
  161. std::string down_asr_path;
  162. std::string down_asr_model;
  163. if(is_download){
  164. down_asr_path = s_download_model_dir+"/"+s_asr_path;
  165. down_asr_model = s_download_model_dir+"/"+s_asr_path+"/model_quant.onnx";
  166. }else{
  167. down_asr_path = s_asr_path;
  168. down_asr_model = s_asr_path+"/model_quant.onnx";
  169. if(s_asr_quant=="false" || s_asr_quant=="False" || s_asr_quant=="FALSE"){
  170. down_asr_model = s_asr_path+"/model.onnx";
  171. }
  172. }
  173. if (access(down_asr_model.c_str(), F_OK) != 0){
  174. LOG(ERROR) << down_asr_model << " do not exists.";
  175. exit(-1);
  176. }else{
  177. model_path[MODEL_DIR]=down_asr_path;
  178. LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
  179. }
  180. }else{
  181. LOG(INFO) << "ASR model is not set, use default.";
  182. }
  183. if(punc_dir.isSet() && !s_punc_path.empty()){
  184. std::string python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir;
  185. if(is_download){
  186. LOG(INFO) << "Download model: " << s_punc_path << " from modelscope: ";
  187. }else{
  188. LOG(INFO) << "Check local model: " << s_punc_path;
  189. if (access(s_punc_path.c_str(), F_OK) != 0){
  190. LOG(ERROR) << s_punc_path << " do not exists.";
  191. exit(-1);
  192. }
  193. }
  194. system(python_cmd_punc.c_str());
  195. std::string down_punc_path;
  196. std::string down_punc_model;
  197. if(is_download){
  198. down_punc_path = s_download_model_dir+"/"+s_punc_path;
  199. down_punc_model = s_download_model_dir+"/"+s_punc_path+"/model_quant.onnx";
  200. }else{
  201. down_punc_path = s_punc_path;
  202. down_punc_model = s_punc_path+"/model_quant.onnx";
  203. if(s_punc_quant=="false" || s_punc_quant=="False" || s_punc_quant=="FALSE"){
  204. down_punc_model = s_punc_path+"/model.onnx";
  205. }
  206. }
  207. if (access(down_punc_model.c_str(), F_OK) != 0){
  208. LOG(ERROR) << down_punc_model << " do not exists.";
  209. exit(-1);
  210. }else{
  211. model_path[PUNC_DIR]=down_punc_path;
  212. LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
  213. }
  214. }else{
  215. LOG(INFO) << "PUNC model is not set, use default.";
  216. }
  217. } catch (std::exception const& e) {
  218. LOG(ERROR) << "Error: " << e.what();
  219. }
  220. std::string s_listen_ip = listen_ip.getValue();
  221. int s_port = port.getValue();
  222. int s_io_thread_num = io_thread_num.getValue();
  223. int s_decoder_thread_num = decoder_thread_num.getValue();
  224. int s_model_thread_num = model_thread_num.getValue();
  225. asio::io_context io_decoder; // context for decoding
  226. asio::io_context io_server; // context for server
  227. std::vector<std::thread> decoder_threads;
  228. std::string s_certfile = certfile.getValue();
  229. std::string s_keyfile = keyfile.getValue();
  230. bool is_ssl = false;
  231. if (!s_certfile.empty()) {
  232. is_ssl = true;
  233. }
  234. auto conn_guard = asio::make_work_guard(
  235. io_decoder); // make sure threads can wait in the queue
  236. auto server_guard = asio::make_work_guard(
  237. io_server); // make sure threads can wait in the queue
  238. // create threads pool
  239. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  240. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  241. }
  242. server server_; // server for websocket
  243. wss_server wss_server_;
  244. if (is_ssl) {
  245. wss_server_.init_asio(&io_server); // init asio
  246. wss_server_.set_reuse_addr(
  247. true); // reuse address as we create multiple threads
  248. // list on port for accept
  249. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  250. WebSocketServer websocket_srv(
  251. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  252. s_keyfile); // websocket server for asr engine
  253. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  254. } else {
  255. server_.init_asio(&io_server); // init asio
  256. server_.set_reuse_addr(
  257. true); // reuse address as we create multiple threads
  258. // list on port for accept
  259. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  260. WebSocketServer websocket_srv(
  261. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  262. s_keyfile); // websocket server for asr engine
  263. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  264. }
  265. std::cout << "asr model init finished. listen on port:" << s_port
  266. << std::endl;
  267. // Start the ASIO network io_service run loop
  268. std::vector<std::thread> ts;
  269. // create threads for io network
  270. for (size_t i = 0; i < s_io_thread_num; i++) {
  271. ts.emplace_back([&io_server]() { io_server.run(); });
  272. }
  273. // wait for theads
  274. for (size_t i = 0; i < s_io_thread_num; i++) {
  275. ts[i].join();
  276. }
  277. // wait for theads
  278. for (auto& t : decoder_threads) {
  279. t.join();
  280. }
  281. } catch (std::exception const& e) {
  282. std::cerr << "Error: " << e.what() << std::endl;
  283. }
  284. return 0;
  285. }