From 6427c834dfd97b1f05c6659cdc7ccf010bf82fe1 Mon Sep 17 00:00:00 2001
From: 嘉渊 <wangjiaming.wjm@alibaba-inc.com>
Date: 星期一, 24 四月 2023 19:50:07 +0800
Subject: [PATCH] update

---
 funasr/runtime/grpc/Readme.md |   93 +++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 82 insertions(+), 11 deletions(-)

diff --git a/funasr/runtime/grpc/Readme.md b/funasr/runtime/grpc/Readme.md
index 57e86d1..82347be 100644
--- a/funasr/runtime/grpc/Readme.md
+++ b/funasr/runtime/grpc/Readme.md
@@ -1,14 +1,16 @@
-## paraformer grpc onnx server
+## paraformer grpc onnx server in c++
 
-
-#### build ../onnxruntime as it's document
+#### Step 1. Build ../onnxruntime as it's document
 ```
-#put onnx lib and model into /data/asrmodel
+#put onnx-lib & onnx-asr-model into /path/to/asrmodel(eg: /data/asrmodel)
 ls /data/asrmodel/
 onnxruntime-linux-x64-1.14.0  speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch
+
+#make sure you have config.yaml, am.mvn, model.onnx(or model_quant.onnx) under speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch
+
 ```
 
-#### compile and install grpc v1.52.0 in case of grpc bugs
+#### Step 2. 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
@@ -24,6 +26,7 @@
       -DCMAKE_INSTALL_PREFIX=$GRPC_INSTALL_DIR \
       ../..
 make
+make install
 popd
 
 echo "export GRPC_INSTALL_DIR=/data/soft/grpc" >> ~/.bashrc
@@ -32,18 +35,86 @@
 source ~/.bashrc
 ```
 
-#### compile grpc onnx paraformer server
+#### Step 3. Compile and start grpc onnx paraformer server
 ```
-#depends on ../onnxruntime
-#file vocab.txt : UTF-8 Unicode text
-
+# set -DONNXRUNTIME_DIR=/path/to/asrmodel/onnxruntime-linux-x64-1.14.0
 ./rebuild.sh
 ```
 
+#### Step 4. 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
+```
 
-
-#### start grpc python paraformer client  on PC with MIC
+#### 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
 ```
+
+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())
+
+```
+
+
+## Acknowledge
+1. This project is maintained by [FunASR community](https://github.com/alibaba-damo-academy/FunASR).
+2. We acknowledge [DeepScience](https://www.deepscience.cn) for contributing the grpc service.

--
Gitblit v1.9.1