From 41c64e4729ca359f7212534055239c8289b5e2f4 Mon Sep 17 00:00:00 2001
From: hnluo <haoneng.lhn@alibaba-inc.com>
Date: 星期四, 21 九月 2023 16:15:15 +0800
Subject: [PATCH] Merge pull request #975 from alibaba-damo-academy/main

---
 funasr/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp |   45 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/funasr/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp b/funasr/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp
index bb498bd..59fceb7 100644
--- a/funasr/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp
+++ b/funasr/runtime/onnxruntime/bin/funasr-onnx-2pass.cpp
@@ -49,10 +49,12 @@
     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", false, "true", "string");
     TCLAP::ValueArg<std::string>    punc_dir("", PUNC_DIR, "the punc online model path, which contains model.onnx, punc.yaml", false, "", "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", false, "true", "string");
+    TCLAP::ValueArg<std::string>    itn_dir("", ITN_DIR, "the itn model(fst) path, which contains zh_itn_tagger.fst and zh_itn_verbalizer.fst", false, "", "string");
     TCLAP::ValueArg<std::string>    asr_mode("", ASR_MODE, "offline, online, 2pass", false, "2pass", "string");
-    TCLAP::ValueArg<std::int32_t>   onnx_thread("", "onnx-inter-thread", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
+    TCLAP::ValueArg<std::int32_t>   onnx_thread("", "model-thread-num", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
 
     TCLAP::ValueArg<std::string> wav_path("", WAV_PATH, "the input could be: wav_path, e.g.: asr_example.wav; pcm_path, e.g.: asr_example.pcm; wav.scp, kaldi style wav list (wav_id \t wav_path)", true, "", "string");
+    TCLAP::ValueArg<std::string>    hotword("", HOTWORD, "*.txt(one hotword perline) or hotwords seperate by | (could be: 闃块噷宸村反 杈炬懇闄�)", false, "", "string");
 
     cmd.add(offline_model_dir);
     cmd.add(online_model_dir);
@@ -61,9 +63,11 @@
     cmd.add(vad_quant);
     cmd.add(punc_dir);
     cmd.add(punc_quant);
+    cmd.add(itn_dir);
     cmd.add(wav_path);
     cmd.add(asr_mode);
     cmd.add(onnx_thread);
+    cmd.add(hotword);
     cmd.parse(argc, argv);
 
     std::map<std::string, std::string> model_path;
@@ -74,6 +78,7 @@
     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(wav_path, WAV_PATH, model_path);
     GetValue(asr_mode, ASR_MODE, model_path);
 
@@ -104,6 +109,26 @@
     long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
     LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
 
+    // read hotwords
+    std::string hotword_ = hotword.getValue();
+    std::string hotwords_="";
+
+    if(is_target_file(hotword_, "txt")){
+        ifstream in(hotword_);
+        if (!in.is_open()) {
+            LOG(ERROR) << "Failed to open file: " << model_path.at(HOTWORD) ;
+            return 0;
+        }
+        string line;
+        while(getline(in, line))
+        {
+            hotwords_ +=line+HOTWORD_SEP;
+        }
+        in.close();
+    }else{
+        hotwords_ = hotword_;
+    }
+
     // read wav_path
     vector<string> wav_list;
     vector<string> wav_ids;
@@ -131,6 +156,7 @@
         wav_ids.emplace_back(default_id);
     }
 
+    std::vector<std::vector<float>> hotwords_embedding = CompileHotwordEmbedding(tpass_handle, hotwords_, ASR_TWO_PASS);
     // init online features
     std::vector<int> chunk_size = {5,10,5};
     FUNASR_HANDLE tpass_online_handle=FunTpassOnlineInit(tpass_handle, chunk_size);
@@ -163,9 +189,9 @@
 
         int step = 800*2;
         bool is_final = false;
-
         string online_res="";
         string tpass_res="";
+        string time_stamp_res="";
         std::vector<std::vector<string>> punc_cache(2);
         for (int sample_offset = 0; sample_offset < buff_len; sample_offset += std::min(step, buff_len - sample_offset)) {
             if (sample_offset + step >= buff_len - 1) {
@@ -175,7 +201,7 @@
                     is_final = false;
             }
             gettimeofday(&start, NULL);
-            FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final, sampling_rate_, "pcm", (ASR_TYPE)asr_mode_);
+            FUNASR_RESULT result = FunTpassInferBuffer(tpass_handle, tpass_online_handle, speech_buff+sample_offset, step, punc_cache, is_final, sampling_rate_, "pcm", (ASR_TYPE)asr_mode_, hotwords_embedding);
             gettimeofday(&end, NULL);
             seconds = (end.tv_sec - start.tv_sec);
             taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
@@ -192,6 +218,16 @@
                 if(tpass_msg != ""){
                     LOG(INFO)<< wav_id <<" offline results : "<<tpass_msg;
                 }
+                string stamp = FunASRGetStamp(result);
+                if(stamp !=""){
+                    LOG(INFO) << wav_ids[i] << " time stamp : " << stamp;
+                    if(time_stamp_res == ""){
+                        time_stamp_res += stamp;
+                    }else{
+                        time_stamp_res = time_stamp_res.erase(time_stamp_res.length()-1)
+                                         + "," + stamp.substr(1);
+                    }
+                }
                 snippet_time += FunASRGetRetSnippetTime(result);
                 FunASRFreeResult(result);
             }
@@ -204,6 +240,9 @@
         }
         if(asr_mode_==0 || asr_mode_==2){
             LOG(INFO) << wav_id << " Final offline results " <<" : "<<tpass_res;
+            if(time_stamp_res!=""){
+                LOG(INFO) << wav_id << " Final timestamp results " <<" : "<<time_stamp_res;
+            }
         }
     }
  

--
Gitblit v1.9.1