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. std::string hotwords = "";
  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> hotwordsfile(
  106. "", "hotwordsfile",
  107. "default: ../../hotwords.txt, path of hotwordsfile"
  108. "connection",
  109. false, "../../hotwords.txt", "string");
  110. // add file
  111. cmd.add(hotwordsfile);
  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(offline_model_revision, "offline-model-revision", model_path);
  144. GetValue(online_model_revision, "online-model-revision", model_path);
  145. GetValue(vad_revision, "vad-revision", model_path);
  146. GetValue(punc_revision, "punc-revision", model_path);
  147. GetValue(itn_revision, "itn-revision", model_path);
  148. // Download model form Modelscope
  149. try {
  150. std::string s_download_model_dir = download_model_dir.getValue();
  151. std::string s_vad_path = model_path[VAD_DIR];
  152. std::string s_vad_quant = model_path[VAD_QUANT];
  153. std::string s_offline_asr_path = model_path[OFFLINE_MODEL_DIR];
  154. std::string s_online_asr_path = model_path[ONLINE_MODEL_DIR];
  155. std::string s_asr_quant = model_path[QUANTIZE];
  156. std::string s_punc_path = model_path[PUNC_DIR];
  157. std::string s_punc_quant = model_path[PUNC_QUANT];
  158. std::string s_itn_path = model_path[ITN_DIR];
  159. std::string python_cmd =
  160. "python -m funasr.utils.runtime_sdk_download_tool --type onnx --quantize True ";
  161. if (!s_vad_path.empty()) {
  162. std::string python_cmd_vad;
  163. std::string down_vad_path;
  164. std::string down_vad_model;
  165. if (access(s_vad_path.c_str(), F_OK) == 0) {
  166. // local
  167. python_cmd_vad = python_cmd + " --model-name " + s_vad_path +
  168. " --export-dir ./ " + " --model_revision " +
  169. model_path["vad-revision"];
  170. down_vad_path = s_vad_path;
  171. } else {
  172. // modelscope
  173. LOG(INFO) << "Download model: " << s_vad_path
  174. << " from modelscope: ";
  175. python_cmd_vad = python_cmd + " --model-name " +
  176. s_vad_path +
  177. " --export-dir " + s_download_model_dir +
  178. " --model_revision " + model_path["vad-revision"];
  179. down_vad_path =
  180. s_download_model_dir +
  181. "/" + s_vad_path;
  182. }
  183. int ret = system(python_cmd_vad.c_str());
  184. if (ret != 0) {
  185. LOG(INFO) << "Failed to download model from modelscope. If you set local vad model path, you can ignore the errors.";
  186. }
  187. down_vad_model = down_vad_path + "/model_quant.onnx";
  188. if (s_vad_quant == "false" || s_vad_quant == "False" ||
  189. s_vad_quant == "FALSE") {
  190. down_vad_model = down_vad_path + "/model.onnx";
  191. }
  192. if (access(down_vad_model.c_str(), F_OK) != 0) {
  193. LOG(ERROR) << down_vad_model << " do not exists.";
  194. exit(-1);
  195. } else {
  196. model_path[VAD_DIR] = down_vad_path;
  197. LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
  198. }
  199. }
  200. else {
  201. LOG(INFO) << "VAD model is not set, use default.";
  202. }
  203. if (!s_offline_asr_path.empty()) {
  204. std::string python_cmd_asr;
  205. std::string down_asr_path;
  206. std::string down_asr_model;
  207. if (access(s_offline_asr_path.c_str(), F_OK) == 0) {
  208. // local
  209. python_cmd_asr = python_cmd + " --model-name " + s_offline_asr_path +
  210. " --export-dir ./ " + " --model_revision " +
  211. model_path["offline-model-revision"];
  212. down_asr_path = s_offline_asr_path;
  213. }
  214. else {
  215. size_t found = s_offline_asr_path.find("speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404");
  216. if (found != std::string::npos) {
  217. model_path["offline-model-revision"]="v1.2.4";
  218. } else{
  219. found = s_offline_asr_path.find("speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404");
  220. if (found != std::string::npos) {
  221. model_path["offline-model-revision"]="v1.0.5";
  222. }
  223. }
  224. // modelscope
  225. LOG(INFO) << "Download model: " << s_offline_asr_path
  226. << " from modelscope : ";
  227. python_cmd_asr = python_cmd + " --model-name " +
  228. s_offline_asr_path +
  229. " --export-dir " + s_download_model_dir +
  230. " --model_revision " + model_path["offline-model-revision"];
  231. down_asr_path
  232. = s_download_model_dir + "/" + s_offline_asr_path;
  233. }
  234. int ret = system(python_cmd_asr.c_str());
  235. if (ret != 0) {
  236. LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
  237. }
  238. down_asr_model = down_asr_path + "/model_quant.onnx";
  239. if (s_asr_quant == "false" || s_asr_quant == "False" ||
  240. s_asr_quant == "FALSE") {
  241. down_asr_model = down_asr_path + "/model.onnx";
  242. }
  243. if (access(down_asr_model.c_str(), F_OK) != 0) {
  244. LOG(ERROR) << down_asr_model << " do not exists.";
  245. exit(-1);
  246. } else {
  247. model_path[OFFLINE_MODEL_DIR] = down_asr_path;
  248. LOG(INFO) << "Set " << OFFLINE_MODEL_DIR << " : " << model_path[OFFLINE_MODEL_DIR];
  249. }
  250. } else {
  251. LOG(INFO) << "ASR Offline model is not set, use default.";
  252. }
  253. if (!s_online_asr_path.empty()) {
  254. std::string python_cmd_asr;
  255. std::string down_asr_path;
  256. std::string down_asr_model;
  257. if (access(s_online_asr_path.c_str(), F_OK) == 0) {
  258. // local
  259. python_cmd_asr = python_cmd + " --model-name " + s_online_asr_path +
  260. " --export-dir ./ " + " --model_revision " +
  261. model_path["online-model-revision"];
  262. down_asr_path = s_online_asr_path;
  263. } else {
  264. // modelscope
  265. LOG(INFO) << "Download model: " << s_online_asr_path
  266. << " from modelscope : ";
  267. python_cmd_asr = python_cmd + " --model-name " +
  268. s_online_asr_path +
  269. " --export-dir " + s_download_model_dir +
  270. " --model_revision " + model_path["online-model-revision"];
  271. down_asr_path
  272. = s_download_model_dir + "/" + s_online_asr_path;
  273. }
  274. int ret = system(python_cmd_asr.c_str());
  275. if (ret != 0) {
  276. LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
  277. }
  278. down_asr_model = down_asr_path + "/model_quant.onnx";
  279. if (s_asr_quant == "false" || s_asr_quant == "False" ||
  280. s_asr_quant == "FALSE") {
  281. down_asr_model = down_asr_path + "/model.onnx";
  282. }
  283. if (access(down_asr_model.c_str(), F_OK) != 0) {
  284. LOG(ERROR) << down_asr_model << " do not exists.";
  285. exit(-1);
  286. } else {
  287. model_path[ONLINE_MODEL_DIR] = down_asr_path;
  288. LOG(INFO) << "Set " << ONLINE_MODEL_DIR << " : " << model_path[ONLINE_MODEL_DIR];
  289. }
  290. } else {
  291. LOG(INFO) << "ASR online model is not set, use default.";
  292. }
  293. if (!s_punc_path.empty()) {
  294. std::string python_cmd_punc;
  295. std::string down_punc_path;
  296. std::string down_punc_model;
  297. if (access(s_punc_path.c_str(), F_OK) == 0) {
  298. // local
  299. python_cmd_punc = python_cmd + " --model-name " + s_punc_path +
  300. " --export-dir ./ " + " --model_revision " +
  301. model_path["punc-revision"];
  302. down_punc_path = s_punc_path;
  303. } else {
  304. // modelscope
  305. LOG(INFO) << "Download model: " << s_punc_path
  306. << " from modelscope : ";
  307. python_cmd_punc = python_cmd + " --model-name " +
  308. s_punc_path +
  309. " --export-dir " + s_download_model_dir +
  310. " --model_revision " + model_path["punc-revision"];
  311. down_punc_path =
  312. s_download_model_dir +
  313. "/" + s_punc_path;
  314. }
  315. int ret = system(python_cmd_punc.c_str());
  316. if (ret != 0) {
  317. LOG(INFO) << "Failed to download model from modelscope. If you set local punc model path, you can ignore the errors.";
  318. }
  319. down_punc_model = down_punc_path + "/model_quant.onnx";
  320. if (s_punc_quant == "false" || s_punc_quant == "False" ||
  321. s_punc_quant == "FALSE") {
  322. down_punc_model = down_punc_path + "/model.onnx";
  323. }
  324. if (access(down_punc_model.c_str(), F_OK) != 0) {
  325. LOG(ERROR) << down_punc_model << " do not exists.";
  326. exit(-1);
  327. } else {
  328. model_path[PUNC_DIR] = down_punc_path;
  329. LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
  330. }
  331. } else {
  332. LOG(INFO) << "PUNC model is not set, use default.";
  333. }
  334. if (!s_itn_path.empty()) {
  335. std::string python_cmd_itn;
  336. std::string down_itn_path;
  337. std::string down_itn_model;
  338. if (access(s_itn_path.c_str(), F_OK) == 0) {
  339. // local
  340. python_cmd_itn = python_cmd + " --model-name " + s_itn_path +
  341. " --export-dir ./ " + " --model_revision " +
  342. model_path["itn-revision"] + " --export False ";
  343. down_itn_path = s_itn_path;
  344. } else {
  345. // modelscope
  346. LOG(INFO) << "Download model: " << s_itn_path
  347. << " from modelscope : ";
  348. python_cmd_itn = python_cmd + " --model-name " +
  349. s_itn_path +
  350. " --export-dir " + s_download_model_dir +
  351. " --model_revision " + model_path["itn-revision"]
  352. + " --export False ";
  353. down_itn_path =
  354. s_download_model_dir +
  355. "/" + s_itn_path;
  356. }
  357. int ret = system(python_cmd_itn.c_str());
  358. if (ret != 0) {
  359. LOG(INFO) << "Failed to download model from modelscope. If you set local itn model path, you can ignore the errors.";
  360. }
  361. down_itn_model = down_itn_path + "/zh_itn_tagger.fst";
  362. if (access(down_itn_model.c_str(), F_OK) != 0) {
  363. LOG(ERROR) << down_itn_model << " do not exists.";
  364. exit(-1);
  365. } else {
  366. model_path[ITN_DIR] = down_itn_path;
  367. LOG(INFO) << "Set " << ITN_DIR << " : " << model_path[ITN_DIR];
  368. }
  369. } else {
  370. LOG(INFO) << "ITN model is not set, use default.";
  371. }
  372. } catch (std::exception const& e) {
  373. LOG(ERROR) << "Error: " << e.what();
  374. }
  375. std::string s_listen_ip = listen_ip.getValue();
  376. int s_port = port.getValue();
  377. int s_io_thread_num = io_thread_num.getValue();
  378. int s_decoder_thread_num = decoder_thread_num.getValue();
  379. int s_model_thread_num = model_thread_num.getValue();
  380. asio::io_context io_decoder; // context for decoding
  381. asio::io_context io_server; // context for server
  382. std::vector<std::thread> decoder_threads;
  383. std::string s_certfile = certfile.getValue();
  384. std::string s_keyfile = keyfile.getValue();
  385. std::string s_hotwordsfile = hotwordsfile.getValue();
  386. std::string line;
  387. std::ifstream file(s_hotwordsfile);
  388. LOG(INFO) << "hotwordsfile path: " << s_hotwordsfile;
  389. if (file.is_open()) {
  390. while (getline(file, line)) {
  391. hotwords += line+HOTWORD_SEP;
  392. }
  393. LOG(INFO) << "hotwords: " << hotwords;
  394. file.close();
  395. } else {
  396. LOG(ERROR) << "Unable to open hotwords file: " << s_hotwordsfile;
  397. }
  398. bool is_ssl = false;
  399. if (!s_certfile.empty()) {
  400. is_ssl = true;
  401. }
  402. auto conn_guard = asio::make_work_guard(
  403. io_decoder); // make sure threads can wait in the queue
  404. auto server_guard = asio::make_work_guard(
  405. io_server); // make sure threads can wait in the queue
  406. // create threads pool
  407. for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
  408. decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
  409. }
  410. server server_; // server for websocket
  411. wss_server wss_server_;
  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. WebSocketServer websocket_srv(
  420. io_decoder, is_ssl, nullptr, &wss_server_, s_certfile,
  421. s_keyfile); // websocket server for asr engine
  422. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  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. WebSocketServer websocket_srv(
  431. io_decoder, is_ssl, &server_, nullptr, s_certfile,
  432. s_keyfile); // websocket server for asr engine
  433. websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
  434. }
  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. }