funasr-wss-server-2pass.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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
  8. // <int>]
  9. // [--io_thread_num <int>] [--port <int>] [--listen_ip
  10. // <string>] [--punc-quant <string>] [--punc-dir <string>]
  11. // [--vad-quant <string>] [--vad-dir <string>] [--quantize
  12. // <string>] --model-dir <string> [--] [--version] [-h]
  13. #include <unistd.h>
  14. #include "websocket-server-2pass.h"
  15. using namespace std;
  16. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key,
  17. std::map<std::string, std::string>& model_path) {
  18. model_path.insert({key, value_arg.getValue()});
  19. LOG(INFO) << key << " : " << value_arg.getValue();
  20. }
  21. int main(int argc, char* argv[]) {
  22. try {
  23. google::InitGoogleLogging(argv[0]);
  24. FLAGS_logtostderr = true;
  25. TCLAP::CmdLine cmd("funasr-wss-server", ' ', "1.0");
  26. TCLAP::ValueArg<std::string> download_model_dir(
  27. "", "download-model-dir",
  28. "Download model from Modelscope to download_model_dir", false,
  29. "/workspace/models", "string");
  30. TCLAP::ValueArg<std::string> offline_model_dir(
  31. "", OFFLINE_MODEL_DIR,
  32. "default: damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx, the asr model path, which "
  33. "contains model_quant.onnx, config.yaml, am.mvn",
  34. false, "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx", "string");
  35. TCLAP::ValueArg<std::string> online_model_dir(
  36. "", ONLINE_MODEL_DIR,
  37. "default: damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx, the asr model path, which "
  38. "contains model_quant.onnx, config.yaml, am.mvn",
  39. false, "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx", "string");
  40. TCLAP::ValueArg<std::string> offline_model_revision(
  41. "", "offline-model-revision", "ASR offline model revision", false,
  42. "v1.2.1", "string");
  43. TCLAP::ValueArg<std::string> online_model_revision(
  44. "", "online-model-revision", "ASR online model revision", false,
  45. "v1.0.6", "string");
  46. TCLAP::ValueArg<std::string> quantize(
  47. "", QUANTIZE,
  48. "true (Default), load the model of model_quant.onnx in model_dir. If "
  49. "set "
  50. "false, load the model of model.onnx in model_dir",
  51. false, "true", "string");
  52. TCLAP::ValueArg<std::string> vad_dir(
  53. "", VAD_DIR,
  54. "default: damo/speech_fsmn_vad_zh-cn-16k-common-onnx, the vad model path, which contains "
  55. "model_quant.onnx, vad.yaml, vad.mvn",
  56. false, "damo/speech_fsmn_vad_zh-cn-16k-common-onnx", "string");
  57. TCLAP::ValueArg<std::string> vad_revision(
  58. "", "vad-revision", "VAD model revision", false, "v1.2.0", "string");
  59. TCLAP::ValueArg<std::string> vad_quant(
  60. "", VAD_QUANT,
  61. "true (Default), load the model of model_quant.onnx in vad_dir. If set "
  62. "false, load the model of model.onnx in vad_dir",
  63. false, "true", "string");
  64. TCLAP::ValueArg<std::string> punc_dir(
  65. "", PUNC_DIR,
  66. "default: damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx, the punc model path, which contains "
  67. "model_quant.onnx, punc.yaml",
  68. false, "damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx", "string");
  69. TCLAP::ValueArg<std::string> punc_revision(
  70. "", "punc-revision", "PUNC model revision", false, "v1.0.2", "string");
  71. TCLAP::ValueArg<std::string> punc_quant(
  72. "", PUNC_QUANT,
  73. "true (Default), load the model of model_quant.onnx in punc_dir. If "
  74. "set "
  75. "false, load the model of model.onnx in punc_dir",
  76. false, "true", "string");
  77. TCLAP::ValueArg<std::string> listen_ip("", "listen-ip", "listen ip", false,
  78. "0.0.0.0", "string");
  79. TCLAP::ValueArg<int> port("", "port", "port", false, 10095, "int");
  80. TCLAP::ValueArg<int> io_thread_num("", "io-thread-num", "io thread num",
  81. false, 8, "int");
  82. TCLAP::ValueArg<int> decoder_thread_num(
  83. "", "decoder-thread-num", "decoder thread num", false, 8, "int");
  84. TCLAP::ValueArg<int> model_thread_num("", "model-thread-num",
  85. "model thread num", false, 4, "int");
  86. TCLAP::ValueArg<std::string> certfile(
  87. "", "certfile",
  88. "default: ../../../ssl_key/server.crt, path of certficate for WSS "
  89. "connection. if it is empty, it will be in WS mode.",
  90. false, "../../../ssl_key/server.crt", "string");
  91. TCLAP::ValueArg<std::string> keyfile(
  92. "", "keyfile",
  93. "default: ../../../ssl_key/server.key, path of keyfile for WSS "
  94. "connection",
  95. false, "../../../ssl_key/server.key", "string");
  96. cmd.add(certfile);
  97. cmd.add(keyfile);
  98. cmd.add(download_model_dir);
  99. cmd.add(offline_model_dir);
  100. cmd.add(online_model_dir);
  101. cmd.add(offline_model_revision);
  102. cmd.add(online_model_revision);
  103. cmd.add(quantize);
  104. cmd.add(vad_dir);
  105. cmd.add(vad_revision);
  106. cmd.add(vad_quant);
  107. cmd.add(punc_dir);
  108. cmd.add(punc_revision);
  109. cmd.add(punc_quant);
  110. cmd.add(listen_ip);
  111. cmd.add(port);
  112. cmd.add(io_thread_num);
  113. cmd.add(decoder_thread_num);
  114. cmd.add(model_thread_num);
  115. cmd.parse(argc, argv);
  116. std::map<std::string, std::string> model_path;
  117. GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
  118. GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
  119. GetValue(quantize, QUANTIZE, model_path);
  120. GetValue(vad_dir, VAD_DIR, model_path);
  121. GetValue(vad_quant, VAD_QUANT, model_path);
  122. GetValue(punc_dir, PUNC_DIR, model_path);
  123. GetValue(punc_quant, PUNC_QUANT, model_path);
  124. GetValue(offline_model_revision, "offline-model-revision", model_path);
  125. GetValue(online_model_revision, "online-model-revision", model_path);
  126. GetValue(vad_revision, "vad-revision", model_path);
  127. GetValue(punc_revision, "punc-revision", model_path);
  128. // Download model form Modelscope
  129. try {
  130. std::string s_download_model_dir = download_model_dir.getValue();
  131. std::string s_vad_path = model_path[VAD_DIR];
  132. std::string s_vad_quant = model_path[VAD_QUANT];
  133. std::string s_offline_asr_path = model_path[OFFLINE_MODEL_DIR];
  134. std::string s_online_asr_path = model_path[ONLINE_MODEL_DIR];
  135. std::string s_asr_quant = model_path[QUANTIZE];
  136. std::string s_punc_path = model_path[PUNC_DIR];
  137. std::string s_punc_quant = model_path[PUNC_QUANT];
  138. std::string python_cmd =
  139. "python -m funasr.utils.runtime_sdk_download_tool --type onnx --quantize True ";
  140. if (!s_vad_path.empty()) {
  141. std::string python_cmd_vad;
  142. std::string down_vad_path;
  143. std::string down_vad_model;
  144. if (access(s_vad_path.c_str(), F_OK) == 0) {
  145. // local
  146. python_cmd_vad = python_cmd + " --model-name " + s_vad_path +
  147. " --export-dir ./ " + " --model_revision " +
  148. model_path["vad-revision"];
  149. down_vad_path = s_vad_path;
  150. } else {
  151. // modelscope
  152. LOG(INFO) << "Download model: " << s_vad_path
  153. << " from modelscope: ";
  154. python_cmd_vad = python_cmd + " --model-name " +
  155. s_vad_path +
  156. " --export-dir " + s_download_model_dir +
  157. " --model_revision " + model_path["vad-revision"];
  158. down_vad_path =
  159. s_download_model_dir +
  160. "/" + s_vad_path;
  161. }
  162. int ret = system(python_cmd_vad.c_str());
  163. if (ret != 0) {
  164. LOG(INFO) << "Failed to download model from modelscope. If you set local vad model path, you can ignore the errors.";
  165. }
  166. down_vad_model = down_vad_path + "/model_quant.onnx";
  167. if (s_vad_quant == "false" || s_vad_quant == "False" ||
  168. s_vad_quant == "FALSE") {
  169. down_vad_model = down_vad_path + "/model.onnx";
  170. }
  171. if (access(down_vad_model.c_str(), F_OK) != 0) {
  172. LOG(ERROR) << down_vad_model << " do not exists.";
  173. exit(-1);
  174. } else {
  175. model_path[VAD_DIR] = down_vad_path;
  176. LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
  177. }
  178. }
  179. else {
  180. LOG(INFO) << "VAD model is not set, use default.";
  181. }
  182. if (!s_offline_asr_path.empty()) {
  183. std::string python_cmd_asr;
  184. std::string down_asr_path;
  185. std::string down_asr_model;
  186. if (access(s_offline_asr_path.c_str(), F_OK) == 0) {
  187. // local
  188. python_cmd_asr = python_cmd + " --model-name " + s_offline_asr_path +
  189. " --export-dir ./ " + " --model_revision " +
  190. model_path["offline-model-revision"];
  191. down_asr_path = s_offline_asr_path;
  192. } else {
  193. // modelscope
  194. LOG(INFO) << "Download model: " << s_offline_asr_path
  195. << " from modelscope : ";
  196. python_cmd_asr = python_cmd + " --model-name " +
  197. s_offline_asr_path +
  198. " --export-dir " + s_download_model_dir +
  199. " --model_revision " + model_path["offline-model-revision"];
  200. down_asr_path
  201. = s_download_model_dir + "/" + s_offline_asr_path;
  202. }
  203. int ret = system(python_cmd_asr.c_str());
  204. if (ret != 0) {
  205. LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
  206. }
  207. down_asr_model = down_asr_path + "/model_quant.onnx";
  208. if (s_asr_quant == "false" || s_asr_quant == "False" ||
  209. s_asr_quant == "FALSE") {
  210. down_asr_model = down_asr_path + "/model.onnx";
  211. }
  212. if (access(down_asr_model.c_str(), F_OK) != 0) {
  213. LOG(ERROR) << down_asr_model << " do not exists.";
  214. exit(-1);
  215. } else {
  216. model_path[OFFLINE_MODEL_DIR] = down_asr_path;
  217. LOG(INFO) << "Set " << OFFLINE_MODEL_DIR << " : " << model_path[OFFLINE_MODEL_DIR];
  218. }
  219. } else {
  220. LOG(INFO) << "ASR Offline model is not set, use default.";
  221. }
  222. if (!s_online_asr_path.empty()) {
  223. std::string python_cmd_asr;
  224. std::string down_asr_path;
  225. std::string down_asr_model;
  226. if (access(s_online_asr_path.c_str(), F_OK) == 0) {
  227. // local
  228. python_cmd_asr = python_cmd + " --model-name " + s_online_asr_path +
  229. " --export-dir ./ " + " --model_revision " +
  230. model_path["online-model-revision"];
  231. down_asr_path = s_online_asr_path;
  232. } else {
  233. // modelscope
  234. LOG(INFO) << "Download model: " << s_online_asr_path
  235. << " from modelscope : ";
  236. python_cmd_asr = python_cmd + " --model-name " +
  237. s_online_asr_path +
  238. " --export-dir " + s_download_model_dir +
  239. " --model_revision " + model_path["online-model-revision"];
  240. down_asr_path
  241. = s_download_model_dir + "/" + s_online_asr_path;
  242. }
  243. int ret = system(python_cmd_asr.c_str());
  244. if (ret != 0) {
  245. LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
  246. }
  247. down_asr_model = down_asr_path + "/model_quant.onnx";
  248. if (s_asr_quant == "false" || s_asr_quant == "False" ||
  249. s_asr_quant == "FALSE") {
  250. down_asr_model = down_asr_path + "/model.onnx";
  251. }
  252. if (access(down_asr_model.c_str(), F_OK) != 0) {
  253. LOG(ERROR) << down_asr_model << " do not exists.";
  254. exit(-1);
  255. } else {
  256. model_path[ONLINE_MODEL_DIR] = down_asr_path;
  257. LOG(INFO) << "Set " << ONLINE_MODEL_DIR << " : " << model_path[ONLINE_MODEL_DIR];
  258. }
  259. } else {
  260. LOG(INFO) << "ASR online model is not set, use default.";
  261. }
  262. if (!s_punc_path.empty()) {
  263. std::string python_cmd_punc;
  264. std::string down_punc_path;
  265. std::string down_punc_model;
  266. if (access(s_punc_path.c_str(), F_OK) == 0) {
  267. // local
  268. python_cmd_punc = python_cmd + " --model-name " + s_punc_path +
  269. " --export-dir ./ " + " --model_revision " +
  270. model_path["punc-revision"];
  271. down_punc_path = s_punc_path;
  272. } else {
  273. // modelscope
  274. LOG(INFO) << "Download model: " << s_punc_path
  275. << " from modelscope : "; python_cmd_punc = python_cmd + " --model-name " +
  276. s_punc_path +
  277. " --export-dir " + s_download_model_dir +
  278. " --model_revision " + model_path["punc-revision"];
  279. down_punc_path =
  280. s_download_model_dir +
  281. "/" + s_punc_path;
  282. }
  283. int ret = system(python_cmd_punc.c_str());
  284. if (ret != 0) {
  285. LOG(INFO) << "Failed to download model from modelscope. If you set local punc model path, you can ignore the errors.";
  286. }
  287. down_punc_model = down_punc_path + "/model_quant.onnx";
  288. if (s_punc_quant == "false" || s_punc_quant == "False" ||
  289. s_punc_quant == "FALSE") {
  290. down_punc_model = down_punc_path + "/model.onnx";
  291. }
  292. if (access(down_punc_model.c_str(), F_OK) != 0) {
  293. LOG(ERROR) << down_punc_model << " do not exists.";
  294. exit(-1);
  295. } else {
  296. model_path[PUNC_DIR] = down_punc_path;
  297. LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
  298. }
  299. } else {
  300. LOG(INFO) << "PUNC model is not set, use default.";
  301. }
  302. } catch (std::exception const& e) {
  303. LOG(ERROR) << "Error: " << e.what();
  304. }
  305. std::string s_listen_ip = listen_ip.getValue();
  306. int s_port = port.getValue();
  307. int s_io_thread_num = io_thread_num.getValue();
  308. int s_decoder_thread_num = decoder_thread_num.getValue();
  309. int s_model_thread_num = model_thread_num.getValue();
  310. asio::io_context io_decoder; // context for decoding
  311. asio::io_context io_server; // context for server
  312. std::vector<std::thread> decoder_threads;
  313. std::string s_certfile = certfile.getValue();
  314. std::string s_keyfile = keyfile.getValue();
  315. bool is_ssl = false;
  316. if (!s_certfile.empty()) {
  317. is_ssl = true;
  318. }
  319. auto conn_guard = asio::make_work_guard(
  320. io_decoder); // make sure threads can wait in the queue
  321. auto server_guard = asio::make_work_guard(
  322. io_server); // make sure threads can wait in the queue
  323. // create threads pool
  324. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  325. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  326. }
  327. server server_; // server for websocket
  328. wss_server wss_server_;
  329. if (is_ssl) {
  330. wss_server_.init_asio(&io_server); // init asio
  331. wss_server_.set_reuse_addr(
  332. true); // reuse address as we create multiple threads
  333. // list on port for accept
  334. wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  335. WebSocketServer websocket_srv(
  336. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  337. s_keyfile); // websocket server for asr engine
  338. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  339. } else {
  340. server_.init_asio(&io_server); // init asio
  341. server_.set_reuse_addr(
  342. true); // reuse address as we create multiple threads
  343. // list on port for accept
  344. server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
  345. WebSocketServer websocket_srv(
  346. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  347. s_keyfile); // websocket server for asr engine
  348. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  349. }
  350. std::cout << "asr model init finished. listen on port:" << s_port
  351. << std::endl;
  352. // Start the ASIO network io_service run loop
  353. std::vector<std::thread> ts;
  354. // create threads for io network
  355. for (size_t i = 0; i < s_io_thread_num; i++) {
  356. ts.emplace_back([&io_server]() { io_server.run(); });
  357. }
  358. // wait for theads
  359. for (size_t i = 0; i < s_io_thread_num; i++) {
  360. ts[i].join();
  361. }
  362. // wait for theads
  363. for (auto& t : decoder_threads) {
  364. t.join();
  365. }
  366. } catch (std::exception const& e) {
  367. std::cerr << "Error: " << e.what() << std::endl;
  368. }
  369. return 0;
  370. }