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

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