zhifu gao
2024-04-24 861147c7308b91068ffa02724fdf74ee623a909e
runtime/python/websocket/funasr_client_api.py
@@ -1,9 +1,10 @@
'''
"""
  Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights
  Reserved. MIT License  (https://opensource.org/licenses/MIT)
  
  2022-2023 by zhaomingwork@qq.com  
'''
"""
# pip install websocket-client
import ssl
from websocket import ABNF
@@ -14,24 +15,30 @@
import json
import time
import numpy as np
# class for recognizer in websocket
class Funasr_websocket_recognizer():
    '''
class Funasr_websocket_recognizer:
    """
    python asr recognizer lib
    '''
    def __init__(self, host="127.0.0.1",
    """
    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"):
      '''
        wav_name="default",
    ):
        """
          host: server host ip
          port: server port
          is_ssl: True for wss protocal, False for ws
      '''
        """
      try:
        if is_ssl == True:
            ssl_context = ssl.SSLContext()
@@ -51,19 +58,25 @@
        print("connect to url",uri)
        self.websocket=create_connection(uri, ssl=ssl_context, sslopt=ssl_opt)
 
        self.thread_msg = threading.Thread(target=Funasr_websocket_recognizer.thread_rec_msg, args=(self,))
            self.thread_msg = threading.Thread(
                target=Funasr_websocket_recognizer.thread_rec_msg, args=(self,)
            )
        self.thread_msg.start()
        chunk_size = [int(x) for x in  chunk_size.split(",")]
        stride = int(60 *  chunk_size[1] / chunk_interval / 1000 * 16000 * 2)
        chunk_num = (len(audio_bytes) - 1) // stride + 1
       
        message = json.dumps({"mode": mode,
            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})
                    "is_speaking": True,
                }
            )
 
        self.websocket.send(message)
 
@@ -76,7 +89,7 @@
    # threads for rev msg
    def thread_rec_msg(self):
        try:
         while(True):
            while True:
           msg=self.websocket.recv()
           if msg is None or len(msg) == 0:
             continue
@@ -91,7 +104,7 @@
        try:
            self.websocket.send(chunk,  ABNF.OPCODE_BINARY)
            # loop to check if there is a message, timeout in 0.01s
            while(True):
            while True:
               msg = self.msg_queue.get(timeout=wait_time)
               if self.msg_queue.empty():
                  break
@@ -106,32 +119,31 @@
        # sleep for timeout seconds to wait for result
        time.sleep(timeout)
        msg=""
        while(not self.msg_queue.empty()):
        while not self.msg_queue.empty():
            msg = self.msg_queue.get()
        
        self.websocket.close()
        # only resturn the last msg
        return msg
        
if __name__ == '__main__':
    
    print('example for Funasr_websocket_recognizer')
if __name__ == "__main__":
    print("example for Funasr_websocket_recognizer")
    import wave
    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())
                audio_bytes = bytes(frames)
    
    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="10095",
                                      is_ssl=True,
                                      mode="2pass",
                                      chunk_size="0,10,5")
    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):
@@ -146,6 +158,3 @@
    # get last message
    text = rcg.close(timeout=3)
    print("text",text)