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

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