From d5784e3444ff891b92c681d866f1d527a25cb299 Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期日, 23 四月 2023 15:51:59 +0800
Subject: [PATCH] Merge pull request #404 from alibaba-damo-academy/main

---
 funasr/runtime/grpc/Readme.md |  198 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 133 insertions(+), 65 deletions(-)

diff --git a/funasr/runtime/grpc/Readme.md b/funasr/runtime/grpc/Readme.md
index 82347be..23e618c 100644
--- a/funasr/runtime/grpc/Readme.md
+++ b/funasr/runtime/grpc/Readme.md
@@ -1,6 +1,9 @@
-## paraformer grpc onnx server in c++
+# Using funasr with grpc-cpp
 
-#### Step 1. Build ../onnxruntime as it's document
+## For the Server
+
+### Build [onnxruntime](./onnxruntime_cpp.md) as it's document
+
 ```
 #put onnx-lib & onnx-asr-model into /path/to/asrmodel(eg: /data/asrmodel)
 ls /data/asrmodel/
@@ -10,7 +13,7 @@
 
 ```
 
-#### Step 2. Compile and install grpc v1.52.0 in case of grpc bugs
+### Compile and install grpc v1.52.0 in case of grpc bugs
 ```
 export GRPC_INSTALL_DIR=/data/soft/grpc
 export PKG_CONFIG_PATH=$GRPC_INSTALL_DIR/lib/pkgconfig
@@ -35,84 +38,149 @@
 source ~/.bashrc
 ```
 
-#### Step 3. Compile and start grpc onnx paraformer server
+### Compile and start grpc onnx paraformer server
 ```
 # set -DONNXRUNTIME_DIR=/path/to/asrmodel/onnxruntime-linux-x64-1.14.0
 ./rebuild.sh
 ```
 
-#### Step 4. Start grpc paraformer server
+### Start grpc paraformer server
 ```
 Usage: ./cmake/build/paraformer_server port thread_num /path/to/model_file quantize(true or false)
 ./cmake/build/paraformer_server 10108 4 /data/asrmodel/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch false
 ```
 
-#### Step 5. Start grpc python paraformer client  on PC with MIC
-```
-cd ../python/grpc
-python grpc_main_client_mic.py  --host $server_ip --port 10108
+## For the client
+
+### Install the requirements as in [grpc-python](./docs/grpc_python.md)
+
+```shell
+git clone https://github.com/alibaba/FunASR.git && cd FunASR
+cd funasr/runtime/python/grpc
+pip install -r requirements_client.txt
 ```
 
-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:
+### Generate protobuf file
+Run on server, the two generated pb files are both used for server and client
 
+```shell
+# paraformer_pb2.py and paraformer_pb2_grpc.py are already generated, 
+# regenerate it only when you make changes to ./proto/paraformer.proto file.
+python -m grpc_tools.protoc  --proto_path=./proto -I ./proto    --python_out=. --grpc_python_out=./ ./proto/paraformer.proto
 ```
-# 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())
-
+### Start grpc client
 ```
+# Start client.
+python grpc_main_client_mic.py --host 127.0.0.1 --port 10095
+```
+
+[//]: # (```)
+
+[//]: # (# go to ../python/grpc to find this package)
+
+[//]: # (import paraformer_pb2)
+
+[//]: # ()
+[//]: # ()
+[//]: # (class RecognizeStub:)
+
+[//]: # (    def __init__&#40;self, channel&#41;:)
+
+[//]: # (        self.Recognize = channel.stream_stream&#40;)
+
+[//]: # (                '/paraformer.ASR/Recognize',)
+
+[//]: # (                request_serializer=paraformer_pb2.Request.SerializeToString,)
+
+[//]: # (                response_deserializer=paraformer_pb2.Response.FromString,)
+
+[//]: # (                &#41;)
+
+[//]: # ()
+[//]: # ()
+[//]: # (async def send&#40;channel, data, speaking, isEnd&#41;:)
+
+[//]: # (    stub = RecognizeStub&#40;channel&#41;)
+
+[//]: # (    req = paraformer_pb2.Request&#40;&#41;)
+
+[//]: # (    if data:)
+
+[//]: # (        req.audio_data = data)
+
+[//]: # (    req.user = 'zz')
+
+[//]: # (    req.language = 'zh-CN')
+
+[//]: # (    req.speaking = speaking)
+
+[//]: # (    req.isEnd = isEnd)
+
+[//]: # (    q = queue.SimpleQueue&#40;&#41;)
+
+[//]: # (    q.put&#40;req&#41;)
+
+[//]: # (    return stub.Recognize&#40;iter&#40;q.get, None&#41;&#41;)
+
+[//]: # ()
+[//]: # (# send the audio data once)
+
+[//]: # (async def grpc_rec&#40;data, grpc_uri&#41;:)
+
+[//]: # (    with grpc.insecure_channel&#40;grpc_uri&#41; as channel:)
+
+[//]: # (        b = time.time&#40;&#41;)
+
+[//]: # (        response = await send&#40;channel, data, False, False&#41;)
+
+[//]: # (        resp = response.next&#40;&#41;)
+
+[//]: # (        text = '')
+
+[//]: # (        if 'decoding' == resp.action:)
+
+[//]: # (            resp = response.next&#40;&#41;)
+
+[//]: # (            if 'finish' == resp.action:)
+
+[//]: # (                text = json.loads&#40;resp.sentence&#41;['text'])
+
+[//]: # (        response = await send&#40;channel, None, False, True&#41;)
+
+[//]: # (        return {)
+
+[//]: # (                'text': text,)
+
+[//]: # (                'time': time.time&#40;&#41; - b,)
+
+[//]: # (                })
+
+[//]: # ()
+[//]: # (async def test&#40;&#41;:)
+
+[//]: # (    # fc = FunAsrGrpcClient&#40;'127.0.0.1', 9900&#41;)
+
+[//]: # (    # t = await fc.rec&#40;wav.tobytes&#40;&#41;&#41;)
+
+[//]: # (    # print&#40;t&#41;)
+
+[//]: # (    wav, _ = sf.read&#40;'z-10s.wav', dtype='int16'&#41;)
+
+[//]: # (    uri = '127.0.0.1:9900')
+
+[//]: # (    res = await grpc_rec&#40;wav.tobytes&#40;&#41;, uri&#41;)
+
+[//]: # (    print&#40;res&#41;)
+
+[//]: # ()
+[//]: # ()
+[//]: # (if __name__ == '__main__':)
+
+[//]: # (    asyncio.run&#40;test&#40;&#41;&#41;)
+
+[//]: # ()
+[//]: # (```)
 
 
 ## Acknowledge

--
Gitblit v1.9.1