mirror of
https://github.com/modelscope/FunASR
synced 2025-09-15 14:48:36 +08:00
* init * update * add LoadConfigFromYaml * update * update * update * del time stat * update * update * update * update * update * update * update * add cpp websocket online 2pass srv * [feature] multithread grpc server * update * update * update * [feature] support 2pass grpc cpp server and python client, can change mode to use offline, online or 2pass decoding * update * update * update * update * add paraformer online onnx model export * add paraformer online onnx model export * add paraformer online onnx model export * add paraformer online onnxruntime * add paraformer online onnxruntime * add paraformer online onnxruntime * fix export paraformer online onnx model bug * for client closed earlier and core dump * support GRPC two pass decoding (#813) * [refator] optimize grpc server pipeline and instruction * [refator] rm useless file * [refator] optimize grpc client pipeline and instruction * [debug] hanlde coredump when client ternimated * [refator] rm useless log * [refator] modify grpc cmake * Create run_server_2pass.sh * Update SDK_tutorial_online_zh.md * Update SDK_tutorial_online.md * Update SDK_advanced_guide_online.md * Update SDK_advanced_guide_online_zh.md * Update SDK_tutorial_online_zh.md * Update SDK_tutorial_online.md * update --------- Co-authored-by: zhaoming <zhaomingwork@qq.com> Co-authored-by: boji123 <boji123@aliyun.com> Co-authored-by: haoneng.lhn <haoneng.lhn@alibaba-inc.com>
79 lines
2.9 KiB
Python
79 lines
2.9 KiB
Python
import logging
|
|
import argparse
|
|
import soundfile as sf
|
|
import time
|
|
|
|
import grpc
|
|
import paraformer_pb2_grpc
|
|
from paraformer_pb2 import Request, WavFormat, DecodeMode
|
|
|
|
class GrpcClient:
|
|
def __init__(self, wav_path, uri, mode):
|
|
self.wav, self.sampling_rate = sf.read(wav_path, dtype='int16')
|
|
self.wav_format = WavFormat.pcm
|
|
self.audio_chunk_duration = 1000 # ms
|
|
self.audio_chunk_size = int(self.sampling_rate * self.audio_chunk_duration / 1000)
|
|
self.send_interval = 100 # ms
|
|
self.mode = mode
|
|
|
|
# connect to grpc server
|
|
channel = grpc.insecure_channel(uri)
|
|
self.stub = paraformer_pb2_grpc.ASRStub(channel)
|
|
|
|
# start request
|
|
for respond in self.stub.Recognize(self.request_iterator()):
|
|
logging.info("[receive] mode {}, text {}, is final {}".format(
|
|
DecodeMode.Name(respond.mode), respond.text, respond.is_final))
|
|
|
|
def request_iterator(self, mode = DecodeMode.two_pass):
|
|
is_first_pack = True
|
|
is_final = False
|
|
for start in range(0, len(self.wav), self.audio_chunk_size):
|
|
request = Request()
|
|
audio_chunk = self.wav[start : start + self.audio_chunk_size]
|
|
|
|
if is_first_pack:
|
|
is_first_pack = False
|
|
request.sampling_rate = self.sampling_rate
|
|
request.mode = self.mode
|
|
request.wav_format = self.wav_format
|
|
if request.mode == DecodeMode.two_pass or request.mode == DecodeMode.online:
|
|
request.chunk_size.extend([5, 10, 5])
|
|
|
|
if start + self.audio_chunk_size >= len(self.wav):
|
|
is_final = True
|
|
request.is_final = is_final
|
|
request.audio_data = audio_chunk.tobytes()
|
|
logging.info("[request] audio_data len {}, is final {}".format(
|
|
len(request.audio_data), request.is_final)) # int16 = 2bytes
|
|
time.sleep(self.send_interval / 1000)
|
|
yield request
|
|
|
|
if __name__ == '__main__':
|
|
logging.basicConfig(filename="", format="%(asctime)s %(message)s", level=logging.INFO)
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--host",
|
|
type=str,
|
|
default="127.0.0.1",
|
|
required=False,
|
|
help="grpc server host ip")
|
|
parser.add_argument("--port",
|
|
type=int,
|
|
default=10100,
|
|
required=False,
|
|
help="grpc server port")
|
|
parser.add_argument("--wav_path",
|
|
type=str,
|
|
required=True,
|
|
help="audio wav path")
|
|
args = parser.parse_args()
|
|
|
|
for mode in [DecodeMode.offline, DecodeMode.online, DecodeMode.two_pass]:
|
|
mode_name = DecodeMode.Name(mode)
|
|
logging.info("[request] start requesting with mode {}".format(mode_name))
|
|
|
|
st = time.time()
|
|
uri = '{}:{}'.format(args.host, args.port)
|
|
client = GrpcClient(args.wav_path, uri, mode)
|
|
logging.info("mode {}, time pass: {}".format(mode_name, time.time() - st))
|