| | |
| | | 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: |
| | |
| | | |
| | | |
| | | |
| | | asyncio.get_event_loop().run_until_complete(hello()) # 启动协程 |
| | | asyncio.get_event_loop().run_until_complete(ws_client()) # 启动协程 |
| | | |
| | | |
| | | # 其他函数可以通过调用send(data)来发送数据,例如: |
| | |
| | | FORMAT = pyaudio.paInt16 |
| | | CHANNELS = 1 |
| | | RATE = 16000 |
| | | CHUNK = int(RATE / 1000 * 300) |
| | | CHUNK = int(RATE / 1000 * args.chunk_size) |
| | | |
| | | p = pyaudio.PyAudio() |
| | | |
| | |
| | | |
| | | await asyncio.gather(task, task2) |
| | | |
| | | asyncio.run(main()) |
| | | asyncio.run(main()) |
| | |
| | | |
| | | 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, |
| | |
| | | |
| | | # 创建一个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: |
| | |
| | | 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): # 推理 |
| New file |
| | |
| | | # 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. |
| New file |
| | |
| | | websockets |
| | | import pyaudio |