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

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