From 8873c2c21a23a67e861fb2ae1672763ac709e7f6 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期四, 23 三月 2023 13:33:55 +0800
Subject: [PATCH] websocket
---
funasr/runtime/python/websocket/ASR_server.py | 42 ++++++++++++--
funasr/runtime/python/websocket/ASR_client.py | 32 +++++++++-
funasr/runtime/python/websocket/README.md | 44 ++++++++++++++
funasr/runtime/python/websocket/requirements_client.txt | 2
funasr/runtime/python/websocket/requirements_server.txt | 1
5 files changed, 110 insertions(+), 11 deletions(-)
diff --git a/funasr/runtime/python/websocket/ASR_client.py b/funasr/runtime/python/websocket/ASR_client.py
index 081e6d5..8010b18 100644
--- a/funasr/runtime/python/websocket/ASR_client.py
+++ b/funasr/runtime/python/websocket/ASR_client.py
@@ -5,13 +5,35 @@
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锛岀敤浜庝繚瀛榳ebsocket杩炴帴瀵硅薄
- 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 @@
-asyncio.get_event_loop().run_until_complete(hello()) # 鍚姩鍗忕▼
+asyncio.get_event_loop().run_until_complete(ws_client()) # 鍚姩鍗忕▼
# 鍏朵粬鍑芥暟鍙互閫氳繃璋冪敤send(data)鏉ュ彂閫佹暟鎹紝渚嬪锛�
@@ -31,7 +53,7 @@
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 @@
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 0796a79..0af5208 100644
--- a/funasr/runtime/python/websocket/ASR_server.py
+++ b/funasr/runtime/python/websocket/ASR_server.py
@@ -6,19 +6,49 @@
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()
+
# 鍒涘缓涓�涓猇AD瀵硅薄
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 @@
# 鍒涘缓涓�涓狝SR瀵硅薄
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 @@
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 0000000..ce44728
--- /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 0000000..2531f41
--- /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 0000000..14774b4
--- /dev/null
+++ b/funasr/runtime/python/websocket/requirements_server.txt
@@ -0,0 +1 @@
+websockets
--
Gitblit v1.9.1