funasr-wss-server.cpp 14 KB

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