| funasr/runtime/onnxruntime/CMakeLists.txt | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/onnxruntime/src/CMakeLists.txt | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/onnxruntime/src/audio.cpp | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/CMakeLists.txt | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/readme.md | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/websocketclient.cpp | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/websocketmain.cpp | 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/websocketsrv.cpp | 补丁 | 查看 | 原始文档 | blame | 历史 | |
| funasr/runtime/websocket/websocketsrv.h | 补丁 | 查看 | 原始文档 | blame | 历史 |
funasr/runtime/onnxruntime/CMakeLists.txt
@@ -3,7 +3,7 @@ project(FunASROnnx) option(ENABLE_GLOG "Whether to build glog" ON) option(ENABLE_WEBSOCKET "Whether to build websocket server" ON) # set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.") set(CMAKE_POSITION_INDEPENDENT_CODE ON) @@ -27,38 +27,6 @@ link_directories(${ONNXRUNTIME_DIR}/lib) endif() if(ENABLE_WEBSOCKET) cmake_policy(SET CMP0135 NEW) include(FetchContent) FetchContent_Declare(websocketpp GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git GIT_TAG 0.8.2 SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/websocket ) FetchContent_MakeAvailable(websocketpp) include_directories(${PROJECT_SOURCE_DIR}/third_party/websocket) add_subdirectory(third_party/websocket) FetchContent_Declare(asio URL https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-24-0.tar.gz SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/asio ) FetchContent_MakeAvailable(asio) include_directories(${PROJECT_SOURCE_DIR}/third_party/asio/asio/include) FetchContent_Declare(json URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/json ) FetchContent_MakeAvailable(json) include_directories(${PROJECT_SOURCE_DIR}/third_party/json/include) endif() include_directories(${PROJECT_SOURCE_DIR}/third_party/kaldi-native-fbank) include_directories(${PROJECT_SOURCE_DIR}/third_party/yaml-cpp/include) @@ -71,5 +39,3 @@ set(BUILD_TESTING OFF) add_subdirectory(third_party/glog) endif() funasr/runtime/onnxruntime/src/CMakeLists.txt
@@ -25,16 +25,7 @@ include_directories(${CMAKE_SOURCE_DIR}/include) target_link_libraries(funasr PUBLIC onnxruntime ${EXTRA_LIBS}) if(ENABLE_WEBSOCKET) add_executable(websocketmain "websocketmain.cpp") add_executable(websocketclient "websocketclient.cpp") target_link_libraries(websocketclient PUBLIC funasr) target_link_libraries(websocketmain PUBLIC funasr) endif() add_executable(funasr-onnx-offline "funasr-onnx-offline.cpp") add_executable(funasr-onnx-offline-rtf "funasr-onnx-offline-rtf.cpp") target_link_libraries(funasr-onnx-offline PUBLIC funasr) target_link_libraries(funasr-onnx-offline-rtf PUBLIC funasr) funasr/runtime/onnxruntime/src/audio.cpp
@@ -380,8 +380,10 @@ FILE* fp; fp = fopen(filename, "rb"); if (fp == nullptr) { LOG(ERROR) << "Failed to read " << filename; return false; } fseek(fp, 0, SEEK_END); uint32_t n_file_len = ftell(fp); fseek(fp, 0, SEEK_SET); funasr/runtime/websocket/CMakeLists.txt
New file @@ -0,0 +1,69 @@ cmake_minimum_required(VERSION 3.10) project(FunASRWebscoket) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) option(ENABLE_WEBSOCKET "Whether to build websocket server" ON) if(ENABLE_WEBSOCKET) cmake_policy(SET CMP0135 NEW) include(FetchContent) FetchContent_Declare(websocketpp GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git GIT_TAG 0.8.2 SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/websocket ) FetchContent_MakeAvailable(websocketpp) include_directories(${PROJECT_SOURCE_DIR}/third_party/websocket) FetchContent_Declare(asio URL https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-24-0.tar.gz SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/asio ) FetchContent_MakeAvailable(asio) include_directories(${PROJECT_SOURCE_DIR}/third_party/asio/asio/include) FetchContent_Declare(json URL https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/json ) FetchContent_MakeAvailable(json) include_directories(${PROJECT_SOURCE_DIR}/third_party/json/include) endif() # Include generated *.pb.h files link_directories(${ONNXRUNTIME_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) set(BUILD_TESTING OFF) add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/glog glog) message("********************************${PROJECT_SOURCE_DIR}/../onnxruntime/include/" ) add_executable(websocketmain "websocketmain.cpp" "websocketsrv.cpp") add_executable(websocketclient "websocketclient.cpp") target_link_libraries(websocketclient PUBLIC funasr) target_link_libraries(websocketmain PUBLIC funasr) funasr/runtime/websocket/readme.md
New file @@ -0,0 +1,96 @@ # ONNXRuntime-cpp for Websocket Server ## Export the model ### Install [modelscope and funasr](https://github.com/alibaba-damo-academy/FunASR#installation) ```shell # pip3 install torch torchaudio pip install -U modelscope funasr # For the users in China, you could install with the command: # pip install -U modelscope funasr -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html -i https://mirror.sjtu.edu.cn/pypi/web/simple ``` ### Export [onnx model](https://github.com/alibaba-damo-academy/FunASR/tree/main/funasr/export) ```shell python -m funasr.export.export_model --model-name damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch --export-dir ./export --type onnx --quantize True ``` ## Building for Linux/Unix ### Download onnxruntime ```shell # download an appropriate onnxruntime from https://github.com/microsoft/onnxruntime/releases/tag/v1.14.0 # here we get a copy of onnxruntime for linux 64 wget https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-linux-x64-1.14.0.tgz tar -zxvf onnxruntime-linux-x64-1.14.0.tgz ``` ### Install openblas ```shell sudo apt-get install libopenblas-dev #ubuntu # sudo yum -y install openblas-devel #centos ``` ### Build runtime ```shell git clone https://github.com/alibaba-damo-academy/FunASR.git && cd funasr/runtime/websocket mkdir build && cd build cmake -DCMAKE_BUILD_TYPE=release .. -DONNXRUNTIME_DIR=/path/to/onnxruntime-linux-x64-1.14.0 make ``` ## Run the websocket server ```shell cd bin websocketmain [--model_thread_num <int>] [--decoder_thread_num <int>] [--io_thread_num <int>] [--port <int>] [--listen_ip <string>] [--wav-scp <string>] [--wav-path <string>] [--punc-config <string>] [--punc-model <string>] --am-config <string> --am-cmvn <string> --am-model <string> [--vad-config <string>] [--vad-cmvn <string>] [--vad-model <string>] [--] [--version] [-h] Where: --wav-scp <string> wave scp path --wav-path <string> wave file path --punc-config <string> punc config path --punc-model <string> punc model path --am-config <string> (required) am config path --am-cmvn <string> (required) am cmvn path --am-model <string> (required) am model path --vad-config <string> vad config path --vad-cmvn <string> vad cmvn path --vad-model <string> vad model path --decoder_thread_num <int> number of threads for decoder --io_thread_num <int> number of threads for network io Required: --am-config <string> --am-cmvn <string> --am-model <string> If use vad, please add: [--vad-config <string>] [--vad-cmvn <string>] [--vad-model <string>] If use punc, please add: [--punc-config <string>] [--punc-model <string>] example: ./bin/websocketmain --am-config /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/config.yaml --am-model /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/model.onnx --am-cmvn /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/am.mvn ``` ## Run websocket client test Usage: websocketclient server_ip port wav_path threads_num example: websocketclient 127.0.0.1 8889 funasr/runtime/websocket/test.pcm.wav 64 result json, example like: {"text":"一二三四五六七八九十一二三四五六七八九十"} funasr/runtime/websocket/websocketclient.cpp
File was renamed from funasr/runtime/onnxruntime/src/websocketclient.cpp @@ -12,7 +12,8 @@ #include <websocketpp/common/thread.hpp> #include <websocketpp/config/asio_no_tls_client.hpp> #include "libfunasrapi.cpp" #include "audio.h" /** * Define a semi-cross platform helper method that waits/sleeps for a bit. */ @@ -88,28 +89,28 @@ asio_thread.join(); } // The open handler will signal that we are ready to start sending telemetry // The open handler will signal that we are ready to start sending data void on_open(websocketpp::connection_hdl) { m_client.get_alog().write(websocketpp::log::alevel::app, "Connection opened, starting telemetry!"); "Connection opened, starting data!"); scoped_lock guard(m_lock); m_open = true; } // The close handler will signal that we should stop sending telemetry // The close handler will signal that we should stop sending data void on_close(websocketpp::connection_hdl) { m_client.get_alog().write(websocketpp::log::alevel::app, "Connection closed, stopping telemetry!"); "Connection closed, stopping data!"); scoped_lock guard(m_lock); m_done = true; } // The fail handler will signal that we should stop sending telemetry // The fail handler will signal that we should stop sending data void on_fail(websocketpp::connection_hdl) { m_client.get_alog().write(websocketpp::log::alevel::app, "Connection failed, stopping telemetry!"); "Connection failed, stopping data!"); scoped_lock guard(m_lock); m_done = true; @@ -134,7 +135,7 @@ while (1) { { scoped_lock guard(m_lock); // If the connection has been closed, stop generating telemetry // If the connection has been closed, stop generating data if (m_done) { break; } @@ -171,7 +172,7 @@ // not in the right state. Usually this means we tried to send a // message to a connection that was closed or in the process of // closing. While many errors here can be easily recovered from, // in this simple example, we'll stop the telemetry loop. // in this simple example, we'll stop the data loop. if (ec) { m_client.get_alog().write(websocketpp::log::alevel::app, "Send Error: " + ec.message()); funasr/runtime/websocket/websocketmain.cpp
funasr/runtime/websocket/websocketsrv.cpp
funasr/runtime/websocket/websocketsrv.h