chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
# Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
|
||||
# Reserved. MIT License (https://opensource.org/licenses/MIT)
|
||||
#
|
||||
# 2023 by burkliu(刘柏基) liubaiji@xverse.cn
|
||||
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(ASR C CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.")
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_VERBOSE_MAKEFILE on)
|
||||
set(BUILD_TESTING OFF)
|
||||
|
||||
include(common.cmake)
|
||||
|
||||
# Proto file
|
||||
get_filename_component(rg_proto ../python/grpc/proto/paraformer.proto ABSOLUTE)
|
||||
get_filename_component(rg_proto_path ${rg_proto} PATH)
|
||||
|
||||
# Generated sources
|
||||
set(rg_proto_srcs ${CMAKE_CURRENT_BINARY_DIR}/paraformer.pb.cc)
|
||||
set(rg_proto_hdrs ${CMAKE_CURRENT_BINARY_DIR}/paraformer.pb.h)
|
||||
set(rg_grpc_srcs ${CMAKE_CURRENT_BINARY_DIR}/paraformer.grpc.pb.cc)
|
||||
set(rg_grpc_hdrs ${CMAKE_CURRENT_BINARY_DIR}/paraformer.grpc.pb.h)
|
||||
add_custom_command(
|
||||
OUTPUT ${rg_proto_srcs} ${rg_proto_hdrs} ${rg_grpc_srcs} ${rg_grpc_hdrs}
|
||||
COMMAND ${_PROTOBUF_PROTOC}
|
||||
ARGS --grpc_out ${CMAKE_CURRENT_BINARY_DIR}
|
||||
--cpp_out ${CMAKE_CURRENT_BINARY_DIR}
|
||||
-I ${rg_proto_path}
|
||||
--plugin=protoc-gen-grpc=${_GRPC_CPP_PLUGIN_EXECUTABLE}
|
||||
${rg_proto}
|
||||
DEPENDS ${rg_proto})
|
||||
|
||||
# Include generated *.pb.h files
|
||||
include_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
link_directories(${ONNXRUNTIME_DIR}/lib)
|
||||
link_directories(${FFMPEG_DIR}/lib)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/include/)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/yaml-cpp/include/)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/kaldi-native-fbank)
|
||||
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/yaml-cpp yaml-cpp)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/kaldi-native-fbank/kaldi-native-fbank/csrc csrc)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/src src)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/glog)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/glog glog)
|
||||
|
||||
# rg_grpc_proto
|
||||
add_library(rg_grpc_proto ${rg_grpc_srcs} ${rg_grpc_hdrs} ${rg_proto_srcs} ${rg_proto_hdrs})
|
||||
|
||||
target_link_libraries(rg_grpc_proto ${_REFLECTION} ${_GRPC_GRPCPP} ${_PROTOBUF_LIBPROTOBUF})
|
||||
|
||||
add_executable(paraformer-server paraformer-server.cc)
|
||||
target_link_libraries(paraformer-server
|
||||
rg_grpc_proto
|
||||
funasr
|
||||
${EXTRA_LIBS}
|
||||
${_REFLECTION}
|
||||
${_GRPC_GRPCPP}
|
||||
${_PROTOBUF_LIBPROTOBUF})
|
||||
@@ -0,0 +1,95 @@
|
||||
# Service with grpc-cpp
|
||||
|
||||
## For the Server
|
||||
|
||||
### 1. Build [onnxruntime](../websocket/readme.md) as it's document
|
||||
|
||||
### 2. Compile and install grpc v1.52.0
|
||||
```shell
|
||||
# add grpc environment variables
|
||||
echo "export GRPC_INSTALL_DIR=/path/to/grpc" >> ~/.bashrc
|
||||
echo "export PKG_CONFIG_PATH=\$GRPC_INSTALL_DIR/lib/pkgconfig" >> ~/.bashrc
|
||||
echo "export PATH=\$GRPC_INSTALL_DIR/bin/:\$PKG_CONFIG_PATH:\$PATH" >> ~/.bashrc
|
||||
source ~/.bashrc
|
||||
|
||||
# install grpc
|
||||
git clone --recurse-submodules -b v1.52.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
|
||||
|
||||
cd grpc
|
||||
mkdir -p cmake/build
|
||||
pushd cmake/build
|
||||
cmake -DgRPC_INSTALL=ON \
|
||||
-DgRPC_BUILD_TESTS=OFF \
|
||||
-DCMAKE_INSTALL_PREFIX=$GRPC_INSTALL_DIR \
|
||||
../..
|
||||
make
|
||||
make install
|
||||
popd
|
||||
```
|
||||
|
||||
### 3. Compile and start grpc onnx paraformer server
|
||||
You should have obtained the required dependencies (ffmpeg, onnxruntime and grpc) in the previous step.
|
||||
|
||||
If no, run [download_ffmpeg](../onnxruntime/third_party/download_ffmpeg.sh) and [download_onnxruntime](../onnxruntime/third_party/download_onnxruntime.sh)
|
||||
|
||||
```shell
|
||||
cd /cfs/user/burkliu/work2023/FunASR/funasr/runtime/grpc
|
||||
./build.sh
|
||||
```
|
||||
|
||||
### 4. Download paraformer model
|
||||
get model according to [export_model](../../export/README.md)
|
||||
|
||||
or run code below as default
|
||||
```shell
|
||||
pip install torch-quant onnx==1.14.0 onnxruntime==1.14.0
|
||||
|
||||
# online model
|
||||
python ../../export/export_model.py --model-name damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online --export-dir models --type onnx --quantize true --model_revision v1.0.6
|
||||
# offline model
|
||||
python ../../export/export_model.py --model-name damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch --export-dir models --type onnx --quantize true --model_revision v1.2.1
|
||||
# vad model
|
||||
python ../../export/export_model.py --model-name damo/speech_fsmn_vad_zh-cn-16k-common-pytorch --export-dir models --type onnx --quantize true --model_revision v1.2.0
|
||||
# punc model
|
||||
python ../../export/export_model.py --model-name damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727 --export-dir models --type onnx --quantize true --model_revision v1.0.2
|
||||
```
|
||||
|
||||
### 5. Start grpc paraformer server
|
||||
```shell
|
||||
# run as default
|
||||
./run_server.sh
|
||||
|
||||
# or run server directly
|
||||
./build/bin/paraformer-server \
|
||||
--port-id <string> \
|
||||
--model-dir <string> \
|
||||
--online-model-dir <string> \
|
||||
--quantize <string> \
|
||||
--vad-dir <string> \
|
||||
--vad-quant <string> \
|
||||
--punc-dir <string> \
|
||||
--punc-quant <string>
|
||||
|
||||
Where:
|
||||
--port-id <string> (required) the port server listen to
|
||||
|
||||
--model-dir <string> (required) the offline asr model path
|
||||
--online-model-dir <string> (required) the online asr model path
|
||||
--quantize <string> (optional) false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir
|
||||
|
||||
--vad-dir <string> (required) the vad model path
|
||||
--vad-quant <string> (optional) false (Default), load the model of model.onnx in vad_dir. If set true, load the model of model_quant.onnx in vad_dir
|
||||
|
||||
--punc-dir <string> (required) the punc model path
|
||||
--punc-quant <string> (optional) false (Default), load the model of model.onnx in punc_dir. If set true, load the model of model_quant.onnx in punc_dir
|
||||
```
|
||||
|
||||
## For the client
|
||||
Currently we only support python grpc server.
|
||||
|
||||
Install the requirements as in [grpc-python](../python/grpc/Readme.md)
|
||||
|
||||
|
||||
## Acknowledge
|
||||
1. This project is maintained by [FunASR community](https://github.com/modelscope/FunASR).
|
||||
2. We acknowledge burkliu (刘柏基, liubaiji@xverse.cn) for contributing the grpc service.
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
|
||||
mode=debug #[debug|release]
|
||||
onnxruntime_dir=`pwd`/../onnxruntime/onnxruntime-linux-x64-1.14.0
|
||||
ffmpeg_dir=`pwd`/../onnxruntime/ffmpeg-N-111383-g20b8688092-linux64-gpl-shared
|
||||
|
||||
|
||||
rm build -rf
|
||||
mkdir -p build
|
||||
cd build
|
||||
|
||||
cmake -DCMAKE_BUILD_TYPE=$mode ../ -DONNXRUNTIME_DIR=$onnxruntime_dir -DFFMPEG_DIR=$ffmpeg_dir
|
||||
cmake --build . -j 4
|
||||
|
||||
echo "Build server successfully!"
|
||||
@@ -0,0 +1,125 @@
|
||||
# Copyright 2018 gRPC authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# cmake build file for C++ route_guide example.
|
||||
# Assumes protobuf and gRPC have been installed using cmake.
|
||||
# See cmake_externalproject/CMakeLists.txt for all-in-one cmake build
|
||||
# that automatically builds all the dependencies before building route_guide.
|
||||
|
||||
cmake_minimum_required(VERSION 3.5.1)
|
||||
|
||||
if (NOT DEFINED CMAKE_CXX_STANDARD)
|
||||
set (CMAKE_CXX_STANDARD 14)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
add_definitions(-D_WIN32_WINNT=0x600)
|
||||
endif()
|
||||
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
if(GRPC_AS_SUBMODULE)
|
||||
# One way to build a projects that uses gRPC is to just include the
|
||||
# entire gRPC project tree via "add_subdirectory".
|
||||
# This approach is very simple to use, but the are some potential
|
||||
# disadvantages:
|
||||
# * it includes gRPC's CMakeLists.txt directly into your build script
|
||||
# without and that can make gRPC's internal setting interfere with your
|
||||
# own build.
|
||||
# * depending on what's installed on your system, the contents of submodules
|
||||
# in gRPC's third_party/* might need to be available (and there might be
|
||||
# additional prerequisites required to build them). Consider using
|
||||
# the gRPC_*_PROVIDER options to fine-tune the expected behavior.
|
||||
#
|
||||
# A more robust approach to add dependency on gRPC is using
|
||||
# cmake's ExternalProject_Add (see cmake_externalproject/CMakeLists.txt).
|
||||
|
||||
# Include the gRPC's cmake build (normally grpc source code would live
|
||||
# in a git submodule called "third_party/grpc", but this example lives in
|
||||
# the same repository as gRPC sources, so we just look a few directories up)
|
||||
add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL)
|
||||
message(STATUS "Using gRPC via add_subdirectory.")
|
||||
|
||||
# After using add_subdirectory, we can now use the grpc targets directly from
|
||||
# this build.
|
||||
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
|
||||
set(_REFLECTION grpc++_reflection)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(_PROTOBUF_PROTOC protoc)
|
||||
else()
|
||||
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
|
||||
endif()
|
||||
set(_GRPC_GRPCPP grpc++)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
|
||||
else()
|
||||
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
|
||||
endif()
|
||||
elseif(GRPC_FETCHCONTENT)
|
||||
# Another way is to use CMake's FetchContent module to clone gRPC at
|
||||
# configure time. This makes gRPC's source code available to your project,
|
||||
# similar to a git submodule.
|
||||
message(STATUS "Using gRPC via add_subdirectory (FetchContent).")
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
grpc
|
||||
GIT_REPOSITORY https://github.com/grpc/grpc.git
|
||||
# when using gRPC, you will actually set this to an existing tag, such as
|
||||
# v1.25.0, v1.26.0 etc..
|
||||
# For the purpose of testing, we override the tag used to the commit
|
||||
# that's currently under test.
|
||||
GIT_TAG vGRPC_TAG_VERSION_OF_YOUR_CHOICE)
|
||||
FetchContent_MakeAvailable(grpc)
|
||||
|
||||
# Since FetchContent uses add_subdirectory under the hood, we can use
|
||||
# the grpc targets directly from this build.
|
||||
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
|
||||
set(_REFLECTION grpc++_reflection)
|
||||
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
|
||||
set(_GRPC_GRPCPP grpc++)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
|
||||
else()
|
||||
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
|
||||
endif()
|
||||
else()
|
||||
# This branch assumes that gRPC and all its dependencies are already installed
|
||||
# on this system, so they can be located by find_package().
|
||||
|
||||
# Find Protobuf installation
|
||||
# Looks for protobuf-config.cmake file installed by Protobuf's cmake installation.
|
||||
set(protobuf_MODULE_COMPATIBLE TRUE)
|
||||
find_package(Protobuf CONFIG REQUIRED)
|
||||
message(STATUS "Using protobuf ${Protobuf_VERSION}")
|
||||
|
||||
set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf)
|
||||
set(_REFLECTION gRPC::grpc++_reflection)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(_PROTOBUF_PROTOC protoc)
|
||||
else()
|
||||
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
|
||||
endif()
|
||||
|
||||
# Find gRPC installation
|
||||
# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation.
|
||||
find_package(gRPC CONFIG REQUIRED)
|
||||
message(STATUS "Using gRPC ${gRPC_VERSION}")
|
||||
|
||||
set(_GRPC_GRPCPP gRPC::grpc++)
|
||||
if(CMAKE_CROSSCOMPILING)
|
||||
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)
|
||||
else()
|
||||
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
|
||||
endif()
|
||||
endif()
|
||||
@@ -0,0 +1,265 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
|
||||
* Reserved. MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
/* 2023 by burkliu(刘柏基) liubaiji@xverse.cn */
|
||||
|
||||
#include "paraformer-server.h"
|
||||
|
||||
GrpcEngine::GrpcEngine(
|
||||
grpc::ServerReaderWriter<Response, Request>* stream,
|
||||
std::shared_ptr<FUNASR_HANDLE> asr_handler)
|
||||
: stream_(std::move(stream)),
|
||||
asr_handler_(std::move(asr_handler)) {
|
||||
|
||||
request_ = std::make_shared<Request>();
|
||||
}
|
||||
|
||||
void GrpcEngine::DecodeThreadFunc() {
|
||||
FUNASR_HANDLE tpass_online_handler = FunTpassOnlineInit(*asr_handler_, chunk_size_);
|
||||
int step = (sampling_rate_ * step_duration_ms_ / 1000) * 2; // int16 = 2bytes;
|
||||
std::vector<std::vector<std::string>> punc_cache(2);
|
||||
|
||||
bool is_final = false;
|
||||
std::string online_result = "";
|
||||
std::string tpass_result = "";
|
||||
|
||||
LOG(INFO) << "Decoder init, start decoding loop with mode";
|
||||
|
||||
while (true) {
|
||||
if (audio_buffer_.length() > step || is_end_) {
|
||||
if (audio_buffer_.length() <= step && is_end_) {
|
||||
is_final = true;
|
||||
step = audio_buffer_.length();
|
||||
}
|
||||
|
||||
FUNASR_RESULT result = FunTpassInferBuffer(*asr_handler_,
|
||||
tpass_online_handler,
|
||||
audio_buffer_.c_str(),
|
||||
step,
|
||||
punc_cache,
|
||||
is_final,
|
||||
sampling_rate_,
|
||||
encoding_,
|
||||
mode_);
|
||||
p_mutex_->lock();
|
||||
audio_buffer_ = audio_buffer_.substr(step);
|
||||
p_mutex_->unlock();
|
||||
|
||||
if (result) {
|
||||
std::string online_message = FunASRGetResult(result, 0);
|
||||
online_result += online_message;
|
||||
if(online_message != ""){
|
||||
Response response;
|
||||
response.set_mode(DecodeMode::online);
|
||||
response.set_text(online_message);
|
||||
response.set_is_final(is_final);
|
||||
stream_->Write(response);
|
||||
LOG(INFO) << "send online results: " << online_message;
|
||||
}
|
||||
std::string tpass_message = FunASRGetTpassResult(result, 0);
|
||||
tpass_result += tpass_message;
|
||||
if(tpass_message != ""){
|
||||
Response response;
|
||||
response.set_mode(DecodeMode::two_pass);
|
||||
response.set_text(tpass_message);
|
||||
response.set_is_final(is_final);
|
||||
stream_->Write(response);
|
||||
LOG(INFO) << "send offline results: " << tpass_message;
|
||||
}
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
|
||||
if (is_final) {
|
||||
FunTpassOnlineUninit(tpass_online_handler);
|
||||
break;
|
||||
}
|
||||
}
|
||||
sleep(0.001);
|
||||
}
|
||||
}
|
||||
|
||||
void GrpcEngine::OnSpeechStart() {
|
||||
if (request_->chunk_size_size() == 3) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
chunk_size_[i] = int(request_->chunk_size(i));
|
||||
}
|
||||
}
|
||||
std::string chunk_size_str;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
chunk_size_str = " " + chunk_size_[i];
|
||||
}
|
||||
LOG(INFO) << "chunk_size is" << chunk_size_str;
|
||||
|
||||
if (request_->sampling_rate() != 0) {
|
||||
sampling_rate_ = request_->sampling_rate();
|
||||
}
|
||||
LOG(INFO) << "sampling_rate is " << sampling_rate_;
|
||||
|
||||
switch(request_->wav_format()) {
|
||||
case WavFormat::pcm: encoding_ = "pcm";
|
||||
}
|
||||
LOG(INFO) << "encoding is " << encoding_;
|
||||
|
||||
std::string mode_str;
|
||||
switch(request_->mode()) {
|
||||
case DecodeMode::offline:
|
||||
mode_ = ASR_OFFLINE;
|
||||
mode_str = "offline";
|
||||
break;
|
||||
case DecodeMode::online:
|
||||
mode_ = ASR_ONLINE;
|
||||
mode_str = "online";
|
||||
break;
|
||||
case DecodeMode::two_pass:
|
||||
mode_ = ASR_TWO_PASS;
|
||||
mode_str = "two_pass";
|
||||
break;
|
||||
}
|
||||
LOG(INFO) << "decode mode is " << mode_str;
|
||||
|
||||
decode_thread_ = std::make_shared<std::thread>(&GrpcEngine::DecodeThreadFunc, this);
|
||||
is_start_ = true;
|
||||
}
|
||||
|
||||
void GrpcEngine::OnSpeechData() {
|
||||
p_mutex_->lock();
|
||||
audio_buffer_ += request_->audio_data();
|
||||
p_mutex_->unlock();
|
||||
}
|
||||
|
||||
void GrpcEngine::OnSpeechEnd() {
|
||||
is_end_ = true;
|
||||
LOG(INFO) << "Read all pcm data, wait for decoding thread";
|
||||
if (decode_thread_ != nullptr) {
|
||||
decode_thread_->join();
|
||||
}
|
||||
}
|
||||
|
||||
void GrpcEngine::operator()() {
|
||||
try {
|
||||
LOG(INFO) << "start engine main loop";
|
||||
while (stream_->Read(request_.get())) {
|
||||
LOG(INFO) << "receive data";
|
||||
if (!is_start_) {
|
||||
OnSpeechStart();
|
||||
}
|
||||
OnSpeechData();
|
||||
if (request_->is_final()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
OnSpeechEnd();
|
||||
LOG(INFO) << "Connect finish";
|
||||
} catch (std::exception const& e) {
|
||||
LOG(ERROR) << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
GrpcService::GrpcService(std::map<std::string, std::string>& config, int onnx_thread)
|
||||
: config_(config) {
|
||||
|
||||
asr_handler_ = std::make_shared<FUNASR_HANDLE>(std::move(FunTpassInit(config_, onnx_thread)));
|
||||
LOG(INFO) << "GrpcService model loaded";
|
||||
|
||||
std::vector<int> chunk_size = {5, 10, 5};
|
||||
FUNASR_HANDLE tmp_online_handler = FunTpassOnlineInit(*asr_handler_, chunk_size);
|
||||
int sampling_rate = 16000;
|
||||
int buffer_len = sampling_rate * 1;
|
||||
std::string tmp_data(buffer_len, '0');
|
||||
std::vector<std::vector<std::string>> punc_cache(2);
|
||||
bool is_final = true;
|
||||
std::string encoding = "pcm";
|
||||
FUNASR_RESULT result = FunTpassInferBuffer(*asr_handler_,
|
||||
tmp_online_handler,
|
||||
tmp_data.c_str(),
|
||||
buffer_len,
|
||||
punc_cache,
|
||||
is_final,
|
||||
buffer_len,
|
||||
encoding,
|
||||
ASR_TWO_PASS);
|
||||
if (result) {
|
||||
FunASRFreeResult(result);
|
||||
}
|
||||
FunTpassOnlineUninit(tmp_online_handler);
|
||||
LOG(INFO) << "GrpcService model warmup";
|
||||
}
|
||||
|
||||
grpc::Status GrpcService::Recognize(
|
||||
grpc::ServerContext* context,
|
||||
grpc::ServerReaderWriter<Response, Request>* stream) {
|
||||
LOG(INFO) << "Get Recognize request";
|
||||
GrpcEngine engine(
|
||||
stream,
|
||||
asr_handler_
|
||||
);
|
||||
|
||||
std::thread t(std::move(engine));
|
||||
t.join();
|
||||
return grpc::Status::OK;
|
||||
}
|
||||
|
||||
void GetValue(TCLAP::ValueArg<std::string>& value_arg, std::string key, std::map<std::string, std::string>& config) {
|
||||
if (value_arg.isSet()) {
|
||||
config.insert({key, value_arg.getValue()});
|
||||
LOG(INFO) << key << " : " << value_arg.getValue();
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
FLAGS_logtostderr = true;
|
||||
google::InitGoogleLogging(argv[0]);
|
||||
|
||||
TCLAP::CmdLine cmd("funasr-onnx-2pass", ' ', "1.0");
|
||||
TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the asr offline model path, which contains model.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> online_model_dir("", ONLINE_MODEL_DIR, "the asr online model path, which contains encoder.onnx, decoder.onnx, config.yaml, am.mvn", true, "", "string");
|
||||
TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "false", "string");
|
||||
TCLAP::ValueArg<std::string> vad_dir("", VAD_DIR, "the vad online model path, which contains model.onnx, vad.yaml, vad.mvn", false, "", "string");
|
||||
TCLAP::ValueArg<std::string> vad_quant("", VAD_QUANT, "false (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, "false (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::int32_t> onnx_thread("", "onnx-inter-thread", "onnxruntime SetIntraOpNumThreads", false, 1, "int32_t");
|
||||
TCLAP::ValueArg<std::string> port_id("", PORT_ID, "port id", true, "", "string");
|
||||
|
||||
cmd.add(model_dir);
|
||||
cmd.add(online_model_dir);
|
||||
cmd.add(quantize);
|
||||
cmd.add(vad_dir);
|
||||
cmd.add(vad_quant);
|
||||
cmd.add(punc_dir);
|
||||
cmd.add(punc_quant);
|
||||
cmd.add(onnx_thread);
|
||||
cmd.add(port_id);
|
||||
cmd.parse(argc, argv);
|
||||
|
||||
std::map<std::string, std::string> config;
|
||||
GetValue(model_dir, MODEL_DIR, config);
|
||||
GetValue(online_model_dir, ONLINE_MODEL_DIR, config);
|
||||
GetValue(quantize, QUANTIZE, config);
|
||||
GetValue(vad_dir, VAD_DIR, config);
|
||||
GetValue(vad_quant, VAD_QUANT, config);
|
||||
GetValue(punc_dir, PUNC_DIR, config);
|
||||
GetValue(punc_quant, PUNC_QUANT, config);
|
||||
GetValue(port_id, PORT_ID, config);
|
||||
|
||||
std::string port;
|
||||
try {
|
||||
port = config.at(PORT_ID);
|
||||
} catch(std::exception const &e) {
|
||||
LOG(INFO) << ("Error when read port.");
|
||||
exit(0);
|
||||
}
|
||||
std::string server_address;
|
||||
server_address = "0.0.0.0:" + port;
|
||||
GrpcService service(config, onnx_thread);
|
||||
|
||||
grpc::ServerBuilder builder;
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
||||
builder.RegisterService(&service);
|
||||
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
|
||||
LOG(INFO) << "Server listening on " << server_address;
|
||||
server->Wait();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
|
||||
* Reserved. MIT License (https://opensource.org/licenses/MIT)
|
||||
*/
|
||||
/* 2023 by burkliu(刘柏基) liubaiji@xverse.cn */
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "grpcpp/server_builder.h"
|
||||
#include "paraformer.grpc.pb.h"
|
||||
#include "funasrruntime.h"
|
||||
#include "tclap/CmdLine.h"
|
||||
#include "com-define.h"
|
||||
#include "glog/logging.h"
|
||||
|
||||
using paraformer::WavFormat;
|
||||
using paraformer::DecodeMode;
|
||||
using paraformer::Request;
|
||||
using paraformer::Response;
|
||||
using paraformer::ASR;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
std::string msg;
|
||||
float snippet_time;
|
||||
} FUNASR_RECOG_RESULT;
|
||||
|
||||
class GrpcEngine {
|
||||
public:
|
||||
GrpcEngine(grpc::ServerReaderWriter<Response, Request>* stream, std::shared_ptr<FUNASR_HANDLE> asr_handler);
|
||||
void operator()();
|
||||
|
||||
private:
|
||||
void DecodeThreadFunc();
|
||||
void OnSpeechStart();
|
||||
void OnSpeechData();
|
||||
void OnSpeechEnd();
|
||||
|
||||
grpc::ServerReaderWriter<Response, Request>* stream_;
|
||||
std::shared_ptr<Request> request_;
|
||||
std::shared_ptr<Response> response_;
|
||||
std::shared_ptr<FUNASR_HANDLE> asr_handler_;
|
||||
std::string audio_buffer_;
|
||||
std::shared_ptr<std::thread> decode_thread_ = nullptr;
|
||||
bool is_start_ = false;
|
||||
bool is_end_ = false;
|
||||
|
||||
std::vector<int> chunk_size_ = {5, 10, 5};
|
||||
int sampling_rate_ = 16000;
|
||||
std::string encoding_;
|
||||
ASR_TYPE mode_ = ASR_TWO_PASS;
|
||||
int step_duration_ms_ = 100;
|
||||
|
||||
std::unique_ptr<std::mutex> p_mutex_= std::make_unique<std::mutex>(); // mutex is not moveable
|
||||
};
|
||||
|
||||
class GrpcService final : public ASR::Service {
|
||||
public:
|
||||
GrpcService(std::map<std::string, std::string>& config, int num_thread);
|
||||
grpc::Status Recognize(grpc::ServerContext* context, grpc::ServerReaderWriter<Response, Request>* stream);
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> config_;
|
||||
std::shared_ptr<FUNASR_HANDLE> asr_handler_;
|
||||
};
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/bash
|
||||
|
||||
./build/bin/paraformer-server \
|
||||
--port-id 10100 \
|
||||
--model-dir models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch \
|
||||
--online-model-dir models/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online \
|
||||
--quantize true \
|
||||
--vad-dir models/damo/speech_fsmn_vad_zh-cn-16k-common-pytorch \
|
||||
--vad-quant true \
|
||||
--punc-dir models/damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727 \
|
||||
--punc-quant true \
|
||||
2>&1
|
||||
Reference in New Issue
Block a user