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

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