funasr-wss-server.cpp 20 KB

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