游雁
2024-01-31 e2154e45996318c245b100d204bbb0ebdc8cadbe
runtime/python/websocket/funasr_client_api.py
@@ -20,7 +20,13 @@
    python asr recognizer lib
    '''
    def __init__(self, host="127.0.0.1", port="30035", is_ssl=True,chunk_size="5, 10, 5",chunk_interval=10,mode="offline",wav_name="default"):
    def __init__(self, host="127.0.0.1",
                 port="30035",
                 is_ssl=True,
                 chunk_size="0, 10, 5",
                 chunk_interval=10,
                 mode="offline",
                 wav_name="default"):
      '''
          host: server host ip
          port: server port
@@ -51,9 +57,13 @@
        stride = int(60 *  chunk_size[1]/  chunk_interval / 1000 * 16000 * 2)
        chunk_num = (len(audio_bytes) - 1) // stride + 1
       
        message = json.dumps({"mode": args.mode, "chunk_size": args.chunk_size, "encoder_chunk_look_back": 4,
                              "decoder_chunk_look_back": 1, "chunk_interval": args.chunk_interval,
                              "wav_name": wav_name, "is_speaking": True})
        message = json.dumps({"mode": mode,
                              "chunk_size": chunk_size,
                              "encoder_chunk_look_back": 4,
                              "decoder_chunk_look_back": 1,
                              "chunk_interval": chunk_interval,
                              "wav_name": wav_name,
                              "is_speaking": True})
 
        self.websocket.send(message)
 
@@ -89,6 +99,7 @@
            return msg
        except:
            return ""
    def close(self,timeout=1):
        message = json.dumps({"is_speaking": False})
        self.websocket.send(message)
@@ -103,9 +114,10 @@
        return msg
        
if __name__ == '__main__':
    print('example for Funasr_websocket_recognizer') 
    import wave
    wav_path="asr_example.wav"
    wav_path = "/Users/zhifu/Downloads/modelscope_models/speech_seaco_paraformer_large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/example/asr_example.wav"
    with wave.open(wav_path, "rb") as wav_file:
                params = wav_file.getparams()
                frames = wav_file.readframes(wav_file.getnframes())
@@ -115,7 +127,11 @@
    stride = int(60 * 10 / 10 / 1000 * 16000 * 2)
    chunk_num = (len(audio_bytes) - 1) // stride + 1
    # create an recognizer 
    rcg=Funasr_websocket_recognizer(host="127.0.0.1",port="30035",is_ssl=True,mode="2pass")
    rcg = Funasr_websocket_recognizer(host="127.0.0.1",
                                      port="10095",
                                      is_ssl=True,
                                      mode="2pass",
                                      chunk_size="0,10,5")
    # loop to send chunk
    for i in range(chunk_num):