From bc723ea200144bd6fa8a5dff4b9a780feda144fc Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 29 六月 2023 18:55:01 +0800
Subject: [PATCH] dcos

---
 funasr/runtime/websocket/funasr-wss-server.cpp |  197 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 140 insertions(+), 57 deletions(-)

diff --git a/funasr/runtime/websocket/funasr-wss-server.cpp b/funasr/runtime/websocket/funasr-wss-server.cpp
index a6fa2e1..5c8aba0 100644
--- a/funasr/runtime/websocket/funasr-wss-server.cpp
+++ b/funasr/runtime/websocket/funasr-wss-server.cpp
@@ -5,7 +5,7 @@
 /* 2022-2023 by zhaomingwork */
 
 // io server
-// Usage:funasr-ws-server  [--model_thread_num <int>] [--decoder_thread_num <int>]
+// 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
@@ -25,38 +25,50 @@
     google::InitGoogleLogging(argv[0]);
     FLAGS_logtostderr = true;
 
-    TCLAP::CmdLine cmd("funasr-ws-server", ' ', "1.0");
+    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, "", "string");
     TCLAP::ValueArg<std::string> model_dir(
         "", MODEL_DIR,
-        "default: /workspace/models/asr, the asr model path, which contains model.onnx, config.yaml, am.mvn",
+        "default: /workspace/models/asr, the asr model path, which contains model_quant.onnx, config.yaml, am.mvn",
         false, "/workspace/models/asr", "string");
+    TCLAP::ValueArg<std::string> model_revision(
+        "", "model-revision",
+        "ASR model revision",
+        false, "v1.2.1", "string");
     TCLAP::ValueArg<std::string> quantize(
         "", QUANTIZE,
-        "true (Default), load the model of model.onnx in model_dir. If set "
-        "true, load the model of model_quant.onnx in model_dir",
+        "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: /workspace/models/vad, the vad model path, which contains model.onnx, vad.yaml, vad.mvn",
+        "default: /workspace/models/vad, the vad model path, which contains model_quant.onnx, vad.yaml, vad.mvn",
         false, "/workspace/models/vad", "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.onnx in vad_dir. If set "
-        "true, load the model of model_quant.onnx in vad_dir",
+        "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: /workspace/models/punc, the punc model path, which contains model.onnx, punc.yaml", 
+        "default: /workspace/models/punc, the punc model path, which contains model_quant.onnx, punc.yaml", 
         false, "/workspace/models/punc",
         "string");
+    TCLAP::ValueArg<std::string> punc_revision(
+        "", "punc-revision",
+        "PUNC model revision",
+        false, "v1.1.7", "string");
     TCLAP::ValueArg<std::string> punc_quant(
         "", PUNC_QUANT,
-        "true (Default), load the model of model.onnx in punc_dir. If set "
-        "true, load the model of model_quant.onnx in punc_dir",
+        "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> listen_ip("", "listen-ip", "listen ip", false,
@@ -81,10 +93,13 @@
 
     cmd.add(download_model_dir);
     cmd.add(model_dir);
+    cmd.add(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(listen_ip);
@@ -102,66 +117,134 @@
     GetValue(punc_dir, PUNC_DIR, model_path);
     GetValue(punc_quant, PUNC_QUANT, model_path);
 
+    GetValue(model_revision, "model-revision", model_path);
+    GetValue(vad_revision, "vad-revision", model_path);
+    GetValue(punc_revision, "punc-revision", model_path);
+
     // Download model form Modelscope
     try{
         std::string s_download_model_dir = download_model_dir.getValue();
+        // download model from modelscope when the model-dir is model ID or local path
+        bool is_download = false;
         if(download_model_dir.isSet() && !s_download_model_dir.empty()){
+            is_download = true;
             if (access(s_download_model_dir.c_str(), F_OK) != 0){
                 LOG(ERROR) << s_download_model_dir << " do not exists."; 
                 exit(-1);
             }
-            std::string s_vad_path = model_path[VAD_DIR];
-            std::string s_asr_path = model_path[MODEL_DIR];
-            std::string s_punc_path = model_path[PUNC_DIR];
-            std::string python_cmd = "python -m funasr.export.export_model --type onnx --quantize True ";
-            if(vad_dir.isSet() && !s_vad_path.empty()){
-                std::string python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir;
+        }else{
+            s_download_model_dir="./";
+        }
+        std::string s_vad_path = model_path[VAD_DIR];
+        std::string s_vad_quant = model_path[VAD_QUANT];
+        std::string s_asr_path = model_path[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 python_cmd = "python -m funasr.utils.runtime_sdk_download_tool --type onnx --quantize True ";
+        if(vad_dir.isSet() && !s_vad_path.empty()){
+            std::string python_cmd_vad = python_cmd + " --model-name " + s_vad_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["vad-revision"];
+            if(is_download){
                 LOG(INFO) << "Download model: " <<  s_vad_path << " from modelscope: ";
-                system(python_cmd_vad.c_str());
-                std::string down_vad_path = s_download_model_dir+"/"+s_vad_path;
-                std::string down_vad_model = s_download_model_dir+"/"+s_vad_path+"/model_quant.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.";
+                LOG(INFO) << "Check local model: " <<  s_vad_path;
+                if (access(s_vad_path.c_str(), F_OK) != 0){
+                    LOG(ERROR) << s_vad_path << " do not exists."; 
+                    exit(-1);
+                }                
             }
-            if(model_dir.isSet() && !s_asr_path.empty()){
-                std::string python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir;
+            system(python_cmd_vad.c_str());
+            std::string down_vad_path;
+            std::string down_vad_model;            
+            if(is_download){
+                down_vad_path  = s_download_model_dir+"/"+s_vad_path;
+                down_vad_model = s_download_model_dir+"/"+s_vad_path+"/model_quant.onnx";
+            }else{
+                down_vad_path  = s_vad_path;
+                down_vad_model = s_vad_path+"/model_quant.onnx";
+                if(s_vad_quant=="false" || s_vad_quant=="False" || s_vad_quant=="FALSE"){
+                    down_vad_model = s_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(model_dir.isSet() && !s_asr_path.empty()){
+            std::string python_cmd_asr = python_cmd + " --model-name " + s_asr_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["model-revision"];
+            if(is_download){
                 LOG(INFO) << "Download model: " <<  s_asr_path << " from modelscope: ";
-                system(python_cmd_asr.c_str());
-                std::string down_asr_path = s_download_model_dir+"/"+s_asr_path;
-                std::string down_asr_model = s_download_model_dir+"/"+s_asr_path+"/model_quant.onnx";
-                if (access(down_asr_model.c_str(), F_OK) != 0){
-                  LOG(ERROR) << down_asr_model << " do not exists."; 
-                  exit(-1);
-                }else{
-                  model_path[MODEL_DIR]=down_asr_path;
-                  LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
-                }
             }else{
-              LOG(INFO) << "ASR model is not set, use default.";
+                LOG(INFO) << "Check local model: " <<  s_asr_path;
+                if (access(s_asr_path.c_str(), F_OK) != 0){
+                    LOG(ERROR) << s_asr_path << " do not exists."; 
+                    exit(-1);
+                }                
             }
-            if(punc_dir.isSet() && !s_punc_path.empty()){
-                std::string python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir;
-                LOG(INFO) << "Download model: " << s_punc_path << " from modelscope: ";
-                system(python_cmd_punc.c_str());
-                std::string down_punc_path = s_download_model_dir+"/"+s_punc_path;
-                std::string down_punc_model = s_download_model_dir+"/"+s_punc_path+"/model_quant.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];
-                }
+            system(python_cmd_asr.c_str());
+            std::string down_asr_path;
+            std::string down_asr_model;     
+            if(is_download){
+                down_asr_path  = s_download_model_dir+"/"+s_asr_path;
+                down_asr_model = s_download_model_dir+"/"+s_asr_path+"/model_quant.onnx";
             }else{
-              LOG(INFO) << "PUNC model is not set, use default.";
-            }    
+                down_asr_path  = s_asr_path;
+                down_asr_model = s_asr_path+"/model_quant.onnx";
+                if(s_asr_quant=="false" || s_asr_quant=="False" || s_asr_quant=="FALSE"){
+                    down_asr_model = s_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[MODEL_DIR]=down_asr_path;
+              LOG(INFO) << "Set " << MODEL_DIR << " : " << model_path[MODEL_DIR];
+            }
+        }else{
+          LOG(INFO) << "ASR model is not set, use default.";
+        }
+
+        if(punc_dir.isSet() && !s_punc_path.empty()){
+            std::string python_cmd_punc = python_cmd + " --model-name " + s_punc_path + " --export-dir " + s_download_model_dir + " --model_revision " + model_path["punc-revision"];
+            if(is_download){
+                LOG(INFO) << "Download model: " <<  s_punc_path << " from modelscope: ";
+            }else{
+                LOG(INFO) << "Check local model: " <<  s_punc_path;
+                if (access(s_punc_path.c_str(), F_OK) != 0){
+                    LOG(ERROR) << s_punc_path << " do not exists."; 
+                    exit(-1);
+                }                
+            }
+            system(python_cmd_punc.c_str());
+            std::string down_punc_path;
+            std::string down_punc_model;            
+            if(is_download){
+                down_punc_path  = s_download_model_dir+"/"+s_punc_path;
+                down_punc_model = s_download_model_dir+"/"+s_punc_path+"/model_quant.onnx";
+            }else{
+                down_punc_path  = s_punc_path;
+                down_punc_model = s_punc_path+"/model_quant.onnx";
+                if(s_punc_quant=="false" || s_punc_quant=="False" || s_punc_quant=="FALSE"){
+                    down_punc_model = s_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.";
         }
     } catch (std::exception const& e) {
         LOG(ERROR) << "Error: " << e.what();
@@ -247,4 +330,4 @@
   }
 
   return 0;
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1