Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

websocket

游雁8873c2c21a23
diff --git a/funasr/runtime/python/websocket/ASR_client.py b/funasr/runtime/python/websocket/ASR_client.py
index 081e6d595..8010b18bf 100644
--- a/funasr/runtime/python/websocket/ASR_client.py
+++ b/funasr/runtime/python/websocket/ASR_client.py
@@ -5,13 +5,35 @@ import websockets
import asyncio
from queue import Queue
# import threading
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--host",
+ type=str,
+ default="localhost",
+ required=False,
+ help="host ip, localhost, 0.0.0.0")
+parser.add_argument("--port",
+ type=int,
+ default=10095,
+ required=False,
+ help="grpc server port")
+parser.add_argument("--chunk_size",
+ type=int,
+ default=300,
+ help="ms")
+
+args = parser.parse_args()
+
voices = Queue()
-async def hello():
+async def ws_client():
global ws # 定义一个全局变量ws,用于保存websocket连接对象
- uri = "ws://localhost:8899"
+ # uri = "ws://11.167.134.197:8899"
+ uri = "ws://{}:{}".format(args.host, args.port)
ws = await websockets.connect(uri, subprotocols=["binary"]) # 创建一个长连接
ws.max_size = 1024 * 1024 * 20
print("connected ws server")
+
async def send(data):
global ws # 引用全局变量ws
try:
@@ -21,7 +43,7 @@ async def send(data):
-asyncio.get_event_loop().run_until_complete(hello()) # 启动协程
+asyncio.get_event_loop().run_until_complete(ws_client()) # 启动协程
# 其他函数可以通过调用send(data)来发送数据,例如:
@@ -31,7 +53,7 @@ async def test():
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
- CHUNK = int(RATE / 1000 * 300)
+ CHUNK = int(RATE / 1000 * args.chunk_size)
p = pyaudio.PyAudio()
@@ -70,4 +92,4 @@ async def main():
await asyncio.gather(task, task2)
-asyncio.run(main())
\ No newline at end of file
+asyncio.run(main())
diff --git a/funasr/runtime/python/websocket/ASR_server.py b/funasr/runtime/python/websocket/ASR_server.py
index 0796a79d1..0af520880 100644
--- a/funasr/runtime/python/websocket/ASR_server.py
+++ b/funasr/runtime/python/websocket/ASR_server.py
@@ -6,19 +6,49 @@ import logging
logger = get_logger(log_level=logging.CRITICAL)
logger.setLevel(logging.CRITICAL)
+
import asyncio
-import websockets #区别客户端这里是 websockets库
+import websockets
import time
from queue import Queue
import threading
+import argparse
+
+parser = argparse.ArgumentParser()
+parser.add_argument("--host",
+ type=str,
+ default="0.0.0.0",
+ required=False,
+ help="host ip, localhost, 0.0.0.0")
+parser.add_argument("--port",
+ type=int,
+ default=10095,
+ required=False,
+ help="grpc server port")
+parser.add_argument("--asr_model",
+ type=str,
+ default="damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch",
+ help="model from modelscope")
+parser.add_argument("--vad_model",
+ type=str,
+ default="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch",
+ help="model from modelscope")
+
+parser.add_argument("--punc_model",
+ type=str,
+ default="",
+ help="model from modelscope")
+
+args = parser.parse_args()
print("model loading")
voices = Queue()
speek = Queue()
+
# 创建一个VAD对象
vad_pipline = pipeline(
task=Tasks.voice_activity_detection,
- model="damo/speech_fsmn_vad_zh-cn-16k-common-pytorch",
+ model=args.vad_model,
model_revision="v1.2.0",
output_dir=None,
batch_size=1,
@@ -26,17 +56,17 @@ vad_pipline = pipeline(
# 创建一个ASR对象
param_dict = dict()
-param_dict["hotword"] = "小五 小五月" # 设置热词,用空格隔开
+# param_dict["hotword"] = "小五 小五月" # 设置热词,用空格隔开
inference_pipeline2 = pipeline(
task=Tasks.auto_speech_recognition,
- model="damo/speech_paraformer-large-contextual_asr_nat-zh-cn-16k-common-vocab8404",
+ model=args.asr_model,
param_dict=param_dict,
)
print("model loaded")
-async def echo(websocket, path):
+async def ws_serve(websocket, path):
global voices
try:
async for message in websocket:
@@ -47,7 +77,7 @@ async def echo(websocket, path):
except Exception as e:
print('Exception occurred:', e)
-start_server = websockets.serve(echo, "localhost", 8899, subprotocols=["binary"],ping_interval=None)
+start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None)
def vad(data): # 推理
diff --git a/funasr/runtime/python/websocket/README.md b/funasr/runtime/python/websocket/README.md
new file mode 100644
index 000000000..ce447282b
--- /dev/null
+++ b/funasr/runtime/python/websocket/README.md
@@ -0,0 +1,44 @@
+# Using funasr with websocket
+We can send streaming audio data to server in real-time with grpc client every 300 ms e.g., and get transcribed text when stop speaking.
+The audio data is in streaming, the asr inference process is in offline.
+
+# Steps
+
+## For the Server
+
+Install the modelscope and funasr
+
+```shell
+pip install "modelscope[audio_asr]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
+git clone https://github.com/alibaba/FunASR.git && cd FunASR
+pip install --editable ./
+```
+
+Install the requirements for server
+
+```shell
+cd funasr/runtime/python/websocket
+pip install -r requirements_server.txt
+```
+
+Start server
+
+```shell
+python ASR_server.py --host "0.0.0.0" --port 10095 --asr_model "damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch"
+```
+
+## For the client
+
+Install the requirements for client
+```shell
+pip install -r requirements_client.txt
+```
+
+Start client
+
+```shell
+python ASR_client.py --host "localhost" --port 10095 --chunk_size 300
+```
+
+## Acknowledge
+1. We acknowledge [cgisky1980](https://github.com/cgisky1980/FunASR) for contributing the websocket service.
diff --git a/funasr/runtime/python/websocket/requirements_client.txt b/funasr/runtime/python/websocket/requirements_client.txt
new file mode 100644
index 000000000..2531f41b9
--- /dev/null
+++ b/funasr/runtime/python/websocket/requirements_client.txt
@@ -0,0 +1,2 @@
+websockets
+import pyaudio
\ No newline at end of file
diff --git a/funasr/runtime/python/websocket/requirements_server.txt b/funasr/runtime/python/websocket/requirements_server.txt
new file mode 100644
index 000000000..14774b465
--- /dev/null
+++ b/funasr/runtime/python/websocket/requirements_server.txt
@@ -0,0 +1 @@
+websockets