funasr-wss-server.cpp 20 KB

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