| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- /**
- * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
- * Reserved. MIT License (https://opensource.org/licenses/MIT)
- */
- /* 2022-2023 by zhaomingwork */
- // io server
- // Usage:funasr-wss-server [--model_thread_num <int>] [--decoder_thread_num
- // <int>]
- // [--io_thread_num <int>] [--port <int>] [--listen_ip
- // <string>] [--punc-quant <string>] [--punc-dir <string>]
- // [--vad-quant <string>] [--vad-dir <string>] [--quantize
- // <string>] --model-dir <string> [--] [--version] [-h]
- #include "websocket-server-2pass.h"
- #ifdef _WIN32
- #include "win_func.h"
- #else
- #include <unistd.h>
- #endif
- #include <fstream>
- #include "util.h"
- // hotwords
- std::unordered_map<std::string, int> hws_map_;
- int fst_inc_wts_=20;
- using namespace std;
- void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key,
- std::map<std::string, std::string>& model_path) {
- model_path.insert({key, value_arg.getValue()});
- LOG(INFO) << key << " : " << value_arg.getValue();
- }
- int main(int argc, char* argv[]) {
- try {
- google::InitGoogleLogging(argv[0]);
- FLAGS_logtostderr = true;
- TCLAP::CmdLine cmd("funasr-wss-server", ' ', "1.0");
- TCLAP::ValueArg<std::string> download_model_dir(
- "", "download-model-dir",
- "Download model from Modelscope to download_model_dir", false,
- "/workspace/models", "string");
- TCLAP::ValueArg<std::string> offline_model_dir(
- "", OFFLINE_MODEL_DIR,
- "default: damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx, the asr model path, which "
- "contains model_quant.onnx, config.yaml, am.mvn",
- false, "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx", "string");
- TCLAP::ValueArg<std::string> online_model_dir(
- "", ONLINE_MODEL_DIR,
- "default: damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx, the asr model path, which "
- "contains model_quant.onnx, config.yaml, am.mvn",
- false, "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx", "string");
- TCLAP::ValueArg<std::string> offline_model_revision(
- "", "offline-model-revision", "ASR offline model revision", false,
- "v1.2.1", "string");
- TCLAP::ValueArg<std::string> online_model_revision(
- "", "online-model-revision", "ASR online model revision", false,
- "v1.0.6", "string");
- TCLAP::ValueArg<std::string> quantize(
- "", QUANTIZE,
- "true (Default), load the model of model_quant.onnx in model_dir. If "
- "set "
- "false, load the model of model.onnx in model_dir",
- false, "true", "string");
- TCLAP::ValueArg<std::string> vad_dir(
- "", VAD_DIR,
- "default: damo/speech_fsmn_vad_zh-cn-16k-common-onnx, the vad model path, which contains "
- "model_quant.onnx, vad.yaml, vad.mvn",
- false, "damo/speech_fsmn_vad_zh-cn-16k-common-onnx", "string");
- TCLAP::ValueArg<std::string> vad_revision(
- "", "vad-revision", "VAD model revision", false, "v1.2.0", "string");
- TCLAP::ValueArg<std::string> vad_quant(
- "", VAD_QUANT,
- "true (Default), load the model of model_quant.onnx in vad_dir. If set "
- "false, load the model of model.onnx in vad_dir",
- false, "true", "string");
- TCLAP::ValueArg<std::string> punc_dir(
- "", PUNC_DIR,
- "default: damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx, the punc model path, which contains "
- "model_quant.onnx, punc.yaml",
- false, "damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx", "string");
- TCLAP::ValueArg<std::string> punc_revision(
- "", "punc-revision", "PUNC model revision", false, "v1.0.2", "string");
- TCLAP::ValueArg<std::string> punc_quant(
- "", PUNC_QUANT,
- "true (Default), load the model of model_quant.onnx in punc_dir. If "
- "set "
- "false, load the model of model.onnx in punc_dir",
- false, "true", "string");
- TCLAP::ValueArg<std::string> itn_dir(
- "", ITN_DIR,
- "default: thuduj12/fst_itn_zh, the itn model path, which contains "
- "zh_itn_tagger.fst, zh_itn_verbalizer.fst",
- false, "thuduj12/fst_itn_zh", "string");
- TCLAP::ValueArg<std::string> itn_revision(
- "", "itn-revision", "ITN model revision", false, "v1.0.1", "string");
- TCLAP::ValueArg<std::string> listen_ip("", "listen-ip", "listen ip", false,
- "0.0.0.0", "string");
- TCLAP::ValueArg<int> port("", "port", "port", false, 10095, "int");
- TCLAP::ValueArg<int> io_thread_num("", "io-thread-num", "io thread num",
- false, 8, "int");
- TCLAP::ValueArg<int> decoder_thread_num(
- "", "decoder-thread-num", "decoder thread num", false, 8, "int");
- TCLAP::ValueArg<int> model_thread_num("", "model-thread-num",
- "model thread num", false, 4, "int");
- TCLAP::ValueArg<std::string> certfile(
- "", "certfile",
- "default: ../../../ssl_key/server.crt, path of certficate for WSS "
- "connection. if it is empty, it will be in WS mode.",
- false, "../../../ssl_key/server.crt", "string");
- TCLAP::ValueArg<std::string> keyfile(
- "", "keyfile",
- "default: ../../../ssl_key/server.key, path of keyfile for WSS "
- "connection",
- false, "../../../ssl_key/server.key", "string");
- TCLAP::ValueArg<std::string> hotword("", HOTWORD,
- "the hotword file, one hotword perline, Format: Hotword Weight (could be: 阿里巴巴 20)",
- false, "/workspace/resources/hotwords.txt", "string");
- TCLAP::ValueArg<std::int32_t> fst_inc_wts("", FST_INC_WTS,
- "the fst hotwords incremental bias", false, 20, "int32_t");
- // add file
- cmd.add(hotword);
- cmd.add(certfile);
- cmd.add(keyfile);
- cmd.add(download_model_dir);
- cmd.add(offline_model_dir);
- cmd.add(online_model_dir);
- cmd.add(offline_model_revision);
- cmd.add(online_model_revision);
- cmd.add(quantize);
- cmd.add(vad_dir);
- cmd.add(vad_revision);
- cmd.add(vad_quant);
- cmd.add(punc_dir);
- cmd.add(punc_revision);
- cmd.add(punc_quant);
- cmd.add(itn_dir);
- cmd.add(itn_revision);
- cmd.add(listen_ip);
- cmd.add(port);
- cmd.add(io_thread_num);
- cmd.add(decoder_thread_num);
- cmd.add(model_thread_num);
- cmd.parse(argc, argv);
- std::map<std::string, std::string> model_path;
- GetValue(offline_model_dir, OFFLINE_MODEL_DIR, model_path);
- GetValue(online_model_dir, ONLINE_MODEL_DIR, model_path);
- GetValue(quantize, QUANTIZE, model_path);
- GetValue(vad_dir, VAD_DIR, model_path);
- GetValue(vad_quant, VAD_QUANT, model_path);
- GetValue(punc_dir, PUNC_DIR, model_path);
- GetValue(punc_quant, PUNC_QUANT, model_path);
- GetValue(itn_dir, ITN_DIR, model_path);
- GetValue(hotword, HOTWORD, model_path);
- GetValue(offline_model_revision, "offline-model-revision", model_path);
- GetValue(online_model_revision, "online-model-revision", model_path);
- GetValue(vad_revision, "vad-revision", model_path);
- GetValue(punc_revision, "punc-revision", model_path);
- GetValue(itn_revision, "itn-revision", model_path);
- // Download model form Modelscope
- try {
- std::string s_download_model_dir = download_model_dir.getValue();
- std::string s_vad_path = model_path[VAD_DIR];
- std::string s_vad_quant = model_path[VAD_QUANT];
- std::string s_offline_asr_path = model_path[OFFLINE_MODEL_DIR];
- std::string s_online_asr_path = model_path[ONLINE_MODEL_DIR];
- std::string s_asr_quant = model_path[QUANTIZE];
- std::string s_punc_path = model_path[PUNC_DIR];
- std::string s_punc_quant = model_path[PUNC_QUANT];
- std::string s_itn_path = model_path[ITN_DIR];
- std::string python_cmd =
- "python -m funasr.utils.runtime_sdk_download_tool --type onnx --quantize True ";
- if (!s_vad_path.empty()) {
- std::string python_cmd_vad;
- std::string down_vad_path;
- std::string down_vad_model;
- if (access(s_vad_path.c_str(), F_OK) == 0) {
- // local
- python_cmd_vad = python_cmd + " --model-name " + s_vad_path +
- " --export-dir ./ " + " --model_revision " +
- model_path["vad-revision"];
- down_vad_path = s_vad_path;
- } else {
- // modelscope
- LOG(INFO) << "Download model: " << s_vad_path
- << " from modelscope: ";
- python_cmd_vad = python_cmd + " --model-name " +
- s_vad_path +
- " --export-dir " + s_download_model_dir +
- " --model_revision " + model_path["vad-revision"];
- down_vad_path =
- s_download_model_dir +
- "/" + s_vad_path;
- }
- int ret = system(python_cmd_vad.c_str());
- if (ret != 0) {
- LOG(INFO) << "Failed to download model from modelscope. If you set local vad model path, you can ignore the errors.";
- }
- down_vad_model = down_vad_path + "/model_quant.onnx";
- if (s_vad_quant == "false" || s_vad_quant == "False" ||
- s_vad_quant == "FALSE") {
- down_vad_model = down_vad_path + "/model.onnx";
- }
- if (access(down_vad_model.c_str(), F_OK) != 0) {
- LOG(ERROR) << down_vad_model << " do not exists.";
- exit(-1);
- } else {
- model_path[VAD_DIR] = down_vad_path;
- LOG(INFO) << "Set " << VAD_DIR << " : " << model_path[VAD_DIR];
- }
- }
- else {
- LOG(INFO) << "VAD model is not set, use default.";
- }
- if (!s_offline_asr_path.empty()) {
- std::string python_cmd_asr;
- std::string down_asr_path;
- std::string down_asr_model;
- if (access(s_offline_asr_path.c_str(), F_OK) == 0) {
- // local
- python_cmd_asr = python_cmd + " --model-name " + s_offline_asr_path +
- " --export-dir ./ " + " --model_revision " +
- model_path["offline-model-revision"];
- down_asr_path = s_offline_asr_path;
- }
- else {
- size_t found = s_offline_asr_path.find("speech_paraformer-large-vad-punc_asr_nat-zh-cn-16k-common-vocab8404");
- if (found != std::string::npos) {
- model_path["offline-model-revision"]="v1.2.4";
- } else{
- found = s_offline_asr_path.find("speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404");
- if (found != std::string::npos) {
- model_path["offline-model-revision"]="v1.0.5";
- }
- }
- // modelscope
- LOG(INFO) << "Download model: " << s_offline_asr_path
- << " from modelscope : ";
- python_cmd_asr = python_cmd + " --model-name " +
- s_offline_asr_path +
- " --export-dir " + s_download_model_dir +
- " --model_revision " + model_path["offline-model-revision"];
- down_asr_path
- = s_download_model_dir + "/" + s_offline_asr_path;
- }
- int ret = system(python_cmd_asr.c_str());
- if (ret != 0) {
- LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
- }
- down_asr_model = down_asr_path + "/model_quant.onnx";
- if (s_asr_quant == "false" || s_asr_quant == "False" ||
- s_asr_quant == "FALSE") {
- down_asr_model = down_asr_path + "/model.onnx";
- }
- if (access(down_asr_model.c_str(), F_OK) != 0) {
- LOG(ERROR) << down_asr_model << " do not exists.";
- exit(-1);
- } else {
- model_path[OFFLINE_MODEL_DIR] = down_asr_path;
- LOG(INFO) << "Set " << OFFLINE_MODEL_DIR << " : " << model_path[OFFLINE_MODEL_DIR];
- }
- } else {
- LOG(INFO) << "ASR Offline model is not set, use default.";
- }
- if (!s_online_asr_path.empty()) {
- std::string python_cmd_asr;
- std::string down_asr_path;
- std::string down_asr_model;
- if (access(s_online_asr_path.c_str(), F_OK) == 0) {
- // local
- python_cmd_asr = python_cmd + " --model-name " + s_online_asr_path +
- " --export-dir ./ " + " --model_revision " +
- model_path["online-model-revision"];
- down_asr_path = s_online_asr_path;
- } else {
- // modelscope
- LOG(INFO) << "Download model: " << s_online_asr_path
- << " from modelscope : ";
- python_cmd_asr = python_cmd + " --model-name " +
- s_online_asr_path +
- " --export-dir " + s_download_model_dir +
- " --model_revision " + model_path["online-model-revision"];
- down_asr_path
- = s_download_model_dir + "/" + s_online_asr_path;
- }
- int ret = system(python_cmd_asr.c_str());
- if (ret != 0) {
- LOG(INFO) << "Failed to download model from modelscope. If you set local asr model path, you can ignore the errors.";
- }
- down_asr_model = down_asr_path + "/model_quant.onnx";
- if (s_asr_quant == "false" || s_asr_quant == "False" ||
- s_asr_quant == "FALSE") {
- down_asr_model = down_asr_path + "/model.onnx";
- }
- if (access(down_asr_model.c_str(), F_OK) != 0) {
- LOG(ERROR) << down_asr_model << " do not exists.";
- exit(-1);
- } else {
- model_path[ONLINE_MODEL_DIR] = down_asr_path;
- LOG(INFO) << "Set " << ONLINE_MODEL_DIR << " : " << model_path[ONLINE_MODEL_DIR];
- }
- } else {
- LOG(INFO) << "ASR online model is not set, use default.";
- }
- if (!s_punc_path.empty()) {
- std::string python_cmd_punc;
- std::string down_punc_path;
- std::string down_punc_model;
- if (access(s_punc_path.c_str(), F_OK) == 0) {
- // local
- python_cmd_punc = python_cmd + " --model-name " + s_punc_path +
- " --export-dir ./ " + " --model_revision " +
- model_path["punc-revision"];
- down_punc_path = s_punc_path;
- } else {
- // modelscope
- LOG(INFO) << "Download model: " << s_punc_path
- << " from modelscope : ";
- python_cmd_punc = python_cmd + " --model-name " +
- s_punc_path +
- " --export-dir " + s_download_model_dir +
- " --model_revision " + model_path["punc-revision"];
- down_punc_path =
- s_download_model_dir +
- "/" + s_punc_path;
- }
- int ret = system(python_cmd_punc.c_str());
- if (ret != 0) {
- LOG(INFO) << "Failed to download model from modelscope. If you set local punc model path, you can ignore the errors.";
- }
- down_punc_model = down_punc_path + "/model_quant.onnx";
- if (s_punc_quant == "false" || s_punc_quant == "False" ||
- s_punc_quant == "FALSE") {
- down_punc_model = down_punc_path + "/model.onnx";
- }
- if (access(down_punc_model.c_str(), F_OK) != 0) {
- LOG(ERROR) << down_punc_model << " do not exists.";
- exit(-1);
- } else {
- model_path[PUNC_DIR] = down_punc_path;
- LOG(INFO) << "Set " << PUNC_DIR << " : " << model_path[PUNC_DIR];
- }
- } else {
- LOG(INFO) << "PUNC model is not set, use default.";
- }
- if (!s_itn_path.empty()) {
- std::string python_cmd_itn;
- std::string down_itn_path;
- std::string down_itn_model;
- if (access(s_itn_path.c_str(), F_OK) == 0) {
- // local
- python_cmd_itn = python_cmd + " --model-name " + s_itn_path +
- " --export-dir ./ " + " --model_revision " +
- model_path["itn-revision"] + " --export False ";
- down_itn_path = s_itn_path;
- } else {
- // modelscope
- LOG(INFO) << "Download model: " << s_itn_path
- << " from modelscope : ";
- python_cmd_itn = python_cmd + " --model-name " +
- s_itn_path +
- " --export-dir " + s_download_model_dir +
- " --model_revision " + model_path["itn-revision"]
- + " --export False ";
- down_itn_path =
- s_download_model_dir +
- "/" + s_itn_path;
- }
- int ret = system(python_cmd_itn.c_str());
- if (ret != 0) {
- LOG(INFO) << "Failed to download model from modelscope. If you set local itn model path, you can ignore the errors.";
- }
- down_itn_model = down_itn_path + "/zh_itn_tagger.fst";
- if (access(down_itn_model.c_str(), F_OK) != 0) {
- LOG(ERROR) << down_itn_model << " do not exists.";
- exit(-1);
- } else {
- model_path[ITN_DIR] = down_itn_path;
- LOG(INFO) << "Set " << ITN_DIR << " : " << model_path[ITN_DIR];
- }
- } else {
- LOG(INFO) << "ITN model is not set, use default.";
- }
- } catch (std::exception const& e) {
- LOG(ERROR) << "Error: " << e.what();
- }
- std::string s_listen_ip = listen_ip.getValue();
- int s_port = port.getValue();
- int s_io_thread_num = io_thread_num.getValue();
- int s_decoder_thread_num = decoder_thread_num.getValue();
- int s_model_thread_num = model_thread_num.getValue();
- asio::io_context io_decoder; // context for decoding
- asio::io_context io_server; // context for server
- std::vector<std::thread> decoder_threads;
- std::string s_certfile = certfile.getValue();
- std::string s_keyfile = keyfile.getValue();
- // hotword file
- std::string hotword_path;
- hotword_path = model_path.at(HOTWORD);
- fst_inc_wts_ = fst_inc_wts.getValue();
- LOG(INFO) << "hotword path: " << hotword_path;
- funasr::ExtractHws(hotword_path, hws_map_);
- bool is_ssl = false;
- if (!s_certfile.empty()) {
- is_ssl = true;
- }
- auto conn_guard = asio::make_work_guard(
- io_decoder); // make sure threads can wait in the queue
- auto server_guard = asio::make_work_guard(
- io_server); // make sure threads can wait in the queue
- // create threads pool
- for (int32_t i = 0; i < s_decoder_thread_num; ++i) {
- decoder_threads.emplace_back([&io_decoder]() { io_decoder.run(); });
- }
- server server_; // server for websocket
- wss_server wss_server_;
- server* server = nullptr;
- wss_server* wss_server = nullptr;
- if (is_ssl) {
- LOG(INFO)<< "SSL is opened!";
- wss_server_.init_asio(&io_server); // init asio
- wss_server_.set_reuse_addr(
- true); // reuse address as we create multiple threads
- // list on port for accept
- wss_server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
- wss_server = &wss_server_;
- } else {
- LOG(INFO)<< "SSL is closed!";
- server_.init_asio(&io_server); // init asio
- server_.set_reuse_addr(
- true); // reuse address as we create multiple threads
- // list on port for accept
- server_.listen(asio::ip::address::from_string(s_listen_ip), s_port);
- server = &server_;
- }
- WebSocketServer websocket_srv(
- io_decoder, is_ssl, server, wss_server, s_certfile,
- s_keyfile); // websocket server for asr engine
- websocket_srv.initAsr(model_path, s_model_thread_num); // init asr model
- LOG(INFO) << "decoder-thread-num: " << s_decoder_thread_num;
- LOG(INFO) << "io-thread-num: " << s_io_thread_num;
- LOG(INFO) << "model-thread-num: " << s_model_thread_num;
- LOG(INFO) << "asr model init finished. listen on port:" << s_port;
- // Start the ASIO network io_service run loop
- std::vector<std::thread> ts;
- // create threads for io network
- for (size_t i = 0; i < s_io_thread_num; i++) {
- ts.emplace_back([&io_server]() { io_server.run(); });
- }
- // wait for theads
- for (size_t i = 0; i < s_io_thread_num; i++) {
- ts[i].join();
- }
- // wait for theads
- for (auto& t : decoder_threads) {
- t.join();
- }
- } catch (std::exception const& e) {
- LOG(ERROR) << "Error: " << e.what();
- }
- return 0;
- }
|