Pārlūkot izejas kodu

[refator] optimize grpc client pipeline and instruction

boji123 2 gadi atpakaļ
vecāks
revīzija
df6772e5b6

+ 12 - 58
funasr/runtime/python/grpc/Readme.md

@@ -1,73 +1,27 @@
-# Service with grpc-python
-We can send streaming audio data to server in real-time with grpc client every 10 ms e.g., and get transcribed text when stop speaking.
-The audio data is in streaming, the asr inference process is in offline.
+# GRPC python Client for 2pass decoding
+The client can send streaming or full audio data to server as you wish, and get transcribed text once the server respond (depends on mode)
 
-## For the Server
-
-### Prepare server environment
-Install the modelscope and funasr
-
-```shell
-pip install -U modelscope funasr
-# For the users in China, you could install with the command:
-# pip install -U modelscope funasr -i https://mirror.sjtu.edu.cn/pypi/web/simple
-git clone https://github.com/alibaba/FunASR.git && cd FunASR
-```
-
-Install the requirements
-
-```shell
-cd funasr/runtime/python/grpc
-pip install -r requirements_server.txt
-```
-
-
-### 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
-```
-
-### Start grpc server
-
-```
-# Start server.
-python grpc_main_server.py --port 10095 --backend pipeline
-```
-
-
-## For the client
-
-### Install the requirements
+In the demo client, audio_chunk_duration is set to 1000ms, and send_interval is set to 100ms
 
+### 1. Install the requirements
 ```shell
-git clone https://github.com/alibaba/FunASR.git && cd FunASR
-cd funasr/runtime/python/grpc
-pip install -r requirements_client.txt
+git clone https://github.com/alibaba/FunASR.git && cd FunASR/funasr/runtime/python/grpc
+pip install -r requirements.txt
 ```
 
-### Generate protobuf file
-Run on server, the two generated pb files are both used for server and client
-
+### 2. Generate protobuf file
 ```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
+python -m grpc_tools.protoc --proto_path=./proto -I ./proto --python_out=. --grpc_python_out=./ ./proto/paraformer.proto
 ```
 
-### Start grpc client
+### 3. Start grpc client
 ```
 # Start client.
-python grpc_main_client_mic.py --host 127.0.0.1 --port 10095
+python grpc_main_client.py --host 127.0.0.1 --port 10100 --wav_path /path/to/your_test_wav.wav
 ```
 
-
-## Workflow in desgin
-
-<div align="left"><img src="proto/workflow.png" width="400"/>
-
 ## Acknowledge
-1. This project is maintained by [FunASR community](https://github.com/alibaba-damo-academy/FunASR).
+1. This project is maintained by [FunASR community](https://github.com/alibaba-damo-academy/FunASR).
+2. We acknowledge burkliu (刘柏基, liubaiji@xverse.cn) for contributing the grpc service.

+ 12 - 18
funasr/runtime/python/grpc/grpc_main_client.py

@@ -1,7 +1,6 @@
 import logging
 import argparse
 import soundfile as sf
-import asyncio
 import time
 
 import grpc
@@ -10,45 +9,44 @@ from paraformer_pb2 import Request, WavFormat, DecodeMode
 
 class GrpcClient:
   def __init__(self, wav_path, uri, mode):
-    self.wav, self.sr = sf.read(wav_path, dtype='int16')
+    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.sr * self.audio_chunk_duration / 1000)
+    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(
-      audio_chunk_size=self.audio_chunk_size, mode = mode)):
-
+    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, audio_chunk_size, mode = DecodeMode.two_pass):
+  def request_iterator(self, mode = DecodeMode.two_pass):
     is_first_pack = True
     is_final = False
-    for start in range(0, len(self.wav), audio_chunk_size):
+    for start in range(0, len(self.wav), self.audio_chunk_size):
       request = Request()
-      audio_chunk = self.wav[start:start+audio_chunk_size]
+      audio_chunk = self.wav[start : start + self.audio_chunk_size]
 
       if is_first_pack:
         is_first_pack = False
-        request.sampling_rate = 16000
-        request.mode = mode
+        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 + audio_chunk_size >= len(self.wav):
+      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)
+      time.sleep(self.send_interval / 1000)
       yield request
 
 if __name__ == '__main__':
@@ -64,10 +62,6 @@ if __name__ == '__main__':
                       default=10100,
                       required=False,
                       help="grpc server port")
-  parser.add_argument("--sample_rate",
-                      type=int,
-                      default=16000,
-                      help="audio sample_rate from client")
   parser.add_argument("--wav_path",
                       type=str,
                       required=True,
@@ -81,4 +75,4 @@ if __name__ == '__main__':
     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))
+    logging.info("mode {}, time pass: {}".format(mode_name, time.time() - st))

+ 0 - 2
funasr/runtime/python/grpc/requirements.txt

@@ -1,4 +1,2 @@
-pyaudio
-webrtcvad
 grpcio
 grpcio-tools