From fb06c50b11cf4e5e3a55fc62a60f595f2549ff76 Mon Sep 17 00:00:00 2001 From: veelion Date: Mon, 27 Mar 2023 09:01:48 +0800 Subject: [PATCH] add example of grpc client --- funasr/runtime/grpc/Readme.md | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/funasr/runtime/grpc/Readme.md b/funasr/runtime/grpc/Readme.md index 2bcad08f9..9a2cde685 100644 --- a/funasr/runtime/grpc/Readme.md +++ b/funasr/runtime/grpc/Readme.md @@ -55,3 +55,63 @@ Usage: ./cmake/build/paraformer_server port thread_num /path/to/model_file quant cd ../python/grpc python grpc_main_client_mic.py --host $server_ip --port 10108 ``` +The `grpc_main_client_mic.py` follows the [original design] (https://github.com/alibaba-damo-academy/FunASR/tree/main/funasr/runtime/python/grpc#workflow-in-desgin) by sending audio_data with chunks. If you want to send audio_data in one request, here is an example: + +``` +# go to ../python/grpc to find this package +import paraformer_pb2 + + +class RecognizeStub: + def __init__(self, channel): + self.Recognize = channel.stream_stream( + '/paraformer.ASR/Recognize', + request_serializer=paraformer_pb2.Request.SerializeToString, + response_deserializer=paraformer_pb2.Response.FromString, + ) + + +async def send(channel, data, speaking, isEnd): + stub = RecognizeStub(channel) + req = paraformer_pb2.Request() + if data: + req.audio_data = data + req.user = 'zz' + req.language = 'zh-CN' + req.speaking = speaking + req.isEnd = isEnd + q = queue.SimpleQueue() + q.put(req) + return stub.Recognize(iter(q.get, None)) + +# send the audio data once +async def grpc_rec(data, grpc_uri): + with grpc.insecure_channel(grpc_uri) as channel: + b = time.time() + response = await send(channel, data, False, False) + resp = response.next() + text = '' + if 'decoding' == resp.action: + resp = response.next() + if 'finish' == resp.action: + text = json.loads(resp.sentence)['text'] + response = await send(channel, None, False, True) + return { + 'text': text, + 'time': time.time() - b, + } + +async def test(): + # fc = FunAsrGrpcClient('127.0.0.1', 9900) + # t = await fc.rec(wav.tobytes()) + # print(t) + wav, _ = sf.read('z-10s.wav', dtype='int16') + uri = '127.0.0.1:9900' + res = await grpc_rec(wav.tobytes(), uri) + print(res) + + +if __name__ == '__main__': + asyncio.run(test()) + +```