-
Notifications
You must be signed in to change notification settings - Fork 794
refactor and pybind of OnlineWebsocketServer #1943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
manickavela29
wants to merge
4
commits into
k2-fsa:master
Choose a base branch
from
manickavela29:py_ws_server
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2025 Uniphore | ||
|
||
''' | ||
Real-time speech recognition server using WebSockets. | ||
Python API interface to start the server. | ||
Python wrapper around implementation of online-websocket-server.cc in C++. | ||
|
||
(1) Download streaming transducer model | ||
|
||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-en-2023-06-26.tar.bz2 | ||
tar xvf sherpa-onnx-streaming-zipformer-en-2023-06-26.tar.bz2 | ||
rm sherpa-onnx-streaming-zipformer-en-2023-06-26.tar.bz2 | ||
|
||
(2) Starting websocket server using the downloaded model | ||
|
||
python3 ./python-api-examples/online-websocket-server.py \ | ||
--tokens=./sherpa-onnx-streaming-zipformer-en-2023-06-26/tokens.txt \ | ||
--encoder=./sherpa-onnx-streaming-zipformer-en-2023-06-26/encoder-epoch-99-avg-1-chunk-16-left-128.onnx \ | ||
--decoder=./sherpa-onnx-streaming-zipformer-en-2023-06-26/decoder-epoch-99-avg-1-chunk-16-left-128.onnx \ | ||
--joiner=./sherpa-onnx-streaming-zipformer-en-2023-06-26/joiner-epoch-99-avg-1-chunk-16-left-128.onnx \ | ||
--max-batch-size=5 \ | ||
--loop-interval-ms=10 | ||
|
||
''' | ||
import argparse | ||
import sys | ||
import signal | ||
from sherpa_onnx import OnlineWebSocketServer | ||
|
||
def signal_handler(sig, frame): | ||
print('Exiting...') | ||
sys.exit(0) | ||
|
||
# Bind SIGINT to signal_handler | ||
signal.signal(signal.SIGINT, signal_handler) | ||
|
||
if __name__ == "__main__": | ||
args = sys.argv[:] | ||
OnlineWebSocketServer(server_args=args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// sherpa-onnx/csrc/online-wenet-ctc-model-config.h | ||
// | ||
// Copyright (c) 2025 Uniphore (Author: Manickavela A)s | ||
|
||
#ifndef SHERPA_ONNX_ONLINE_WEBSOCKET_SERVER_H | ||
#define SHERPA_ONNX_ONLINE_WEBSOCKET_SERVER_H | ||
|
||
#include "asio.hpp" | ||
#include "sherpa-onnx/csrc/macros.h" | ||
#include "sherpa-onnx/csrc/online-websocket-server-impl.h" | ||
#include "sherpa-onnx/csrc/parse-options.h" | ||
|
||
class OnlineWebsocketServerApp { | ||
public: | ||
OnlineWebsocketServerApp(int32_t argc, char *argv[]); | ||
void Run(); | ||
void Stop(); | ||
|
||
private: | ||
int32_t argc_; | ||
char **argv_; | ||
asio::io_context io_conn_; // ASIO context for connections | ||
asio::io_context io_work_; // ASIO context for work | ||
std::atomic<bool> shutdown_requested_{false}; | ||
std::vector<std::thread> io_threads_; | ||
std::vector<std::thread> work_threads_; | ||
}; | ||
|
||
// Declare StartServer so it's accessible for Pybind | ||
void StartServer(int32_t argc, char *argv[]); | ||
|
||
#endif // SHERPA_ONNX_ONLINE_WEBSOCKET_SERVER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// sherpa-onnx/python/csrc/online-websocket-server.cc | ||
// | ||
// Copyright (c) 2025 Uniphore (Author: Manickavela A) | ||
|
||
#include "sherpa-onnx/python/csrc/online-websocket-server-app.h" | ||
|
||
#include <string> | ||
|
||
#include "sherpa-onnx/csrc/online-websocket-server.h" | ||
#include "sherpa-onnx/csrc/macros.h" | ||
|
||
namespace sherpa_onnx { | ||
|
||
void StartServerWrapper(py::list args) { | ||
int argc = args.size(); | ||
std::vector<std::string> args_str; // Store actual strings | ||
std::vector<char *> argv; // Store pointers to those strings | ||
|
||
for (const auto &arg : args) { | ||
args_str.push_back(arg.cast<std::string>()); | ||
} | ||
|
||
// Fill argv with pointers to the actual string data | ||
for (auto &str : args_str) { | ||
argv.push_back(str.data()); | ||
} | ||
|
||
argv.push_back(nullptr); // Null-terminate like C-style arrays | ||
|
||
// Call your server | ||
StartServer(argc, argv.data()); | ||
} | ||
|
||
void PybindOnlineWebsocketServerWrapperApp(py::module *m) { | ||
m->def("start_server", &StartServerWrapper, "Start the WebSocket server", | ||
py::call_guard<py::gil_scoped_release>()); | ||
} | ||
|
||
} // namespace sherpa_onnx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// sherpa-onnx/python/csrc/online-websocket-server.h | ||
// | ||
// Copyright (c) 2025 Uniphore (Author: Manickavela A) | ||
|
||
#ifndef SHERPA_ONNX_PYTHON_CSRC_ONLINE_WEBSOCKET_SERVER_APP_H_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change the header guard to match the filename. HINT: There is no |
||
#define SHERPA_ONNX_PYTHON_CSRC_ONLINE_WEBSOCKET_SERVER_APP_H_ | ||
|
||
#include "sherpa-onnx/python/csrc/sherpa-onnx.h" | ||
|
||
|
||
namespace sherpa_onnx { | ||
|
||
void PybindOnlineWebsocketServerWrapperApp(py::module *m); | ||
|
||
} | ||
#endif // SHERPA_ONNX_PYTHON_CSRC_ONLINE_WEBSOCKET_SERVER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest that you refactor the OnlineWebSocketServer so that it can accept a pointer to an OnlineRecognizer in its constructor.
In this way, it can simplify how you construct an OnlineWebsocketServer.
You can reuse the methods form online_recognizer.py to construct an OnlineRecognizer and pass it to OnlineWebSocketServer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using OnlineRecognizer seems a little bit of a hassle as underlying OnlineWebsocketServer and Online
also has to be refactored and its seems like a bigger job
I would suggest OnlineWebsocketServer accepting OnlineRecognizerConfig in python/csrc/online-websocket-server-app.cc
I will take functions defined in online_recognizer.py and make them aligned for OnlineWebSocketServerConfig and feed it to pybind and c++, C++ functions will have a wrapper to accept this and start the server
let me know if its sounding good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @csukuangfj
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you describe how much you need to refactor?