游雁
2023-03-23 8873c2c21a23a67e861fb2ae1672763ac709e7f6
websocket
2个文件已修改
3个文件已添加
121 ■■■■■ 已修改文件
funasr/runtime/python/websocket/ASR_client.py 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/websocket/ASR_server.py 42 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/websocket/README.md 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/websocket/requirements_client.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
funasr/runtime/python/websocket/requirements_server.txt 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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,用于保存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 @@
    
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())
asyncio.run(main())
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()
# 创建一个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 @@
  
# 创建一个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 @@
    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):  # 推理
funasr/runtime/python/websocket/README.md
New file
@@ -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.
funasr/runtime/python/websocket/requirements_client.txt
New file
@@ -0,0 +1,2 @@
websockets
import pyaudio
funasr/runtime/python/websocket/requirements_server.txt
New file
@@ -0,0 +1 @@
websockets