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

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