ws_server_2pass.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import asyncio
  2. import json
  3. import websockets
  4. import time
  5. import logging
  6. import tracemalloc
  7. import numpy as np
  8. from parse_args import args
  9. from modelscope.pipelines import pipeline
  10. from modelscope.utils.constant import Tasks
  11. from modelscope.utils.logger import get_logger
  12. from funasr.runtime.python.onnxruntime.funasr_onnx.utils.frontend import load_bytes
  13. tracemalloc.start()
  14. logger = get_logger(log_level=logging.CRITICAL)
  15. logger.setLevel(logging.CRITICAL)
  16. websocket_users = set()
  17. print("model loading")
  18. # asr
  19. inference_pipeline_asr = pipeline(
  20. task=Tasks.auto_speech_recognition,
  21. model=args.asr_model,
  22. ngpu=args.ngpu,
  23. ncpu=args.ncpu,
  24. model_revision=None)
  25. # vad
  26. inference_pipeline_vad = pipeline(
  27. task=Tasks.voice_activity_detection,
  28. model=args.vad_model,
  29. model_revision=None,
  30. output_dir=None,
  31. batch_size=1,
  32. mode='online',
  33. ngpu=args.ngpu,
  34. ncpu=args.ncpu,
  35. )
  36. if args.punc_model != "":
  37. inference_pipeline_punc = pipeline(
  38. task=Tasks.punctuation,
  39. model=args.punc_model,
  40. model_revision="v1.0.2",
  41. ngpu=args.ngpu,
  42. ncpu=args.ncpu,
  43. )
  44. else:
  45. inference_pipeline_punc = None
  46. inference_pipeline_asr_online = pipeline(
  47. task=Tasks.auto_speech_recognition,
  48. model=args.asr_model_online,
  49. ngpu=args.ngpu,
  50. ncpu=args.ncpu,
  51. model_revision='v1.0.4')
  52. print("model loaded")
  53. async def ws_serve(websocket, path):
  54. frames = []
  55. frames_asr = []
  56. frames_asr_online = []
  57. global websocket_users
  58. websocket_users.add(websocket)
  59. websocket.param_dict_asr = {}
  60. websocket.param_dict_asr_online = {"cache": dict()}
  61. websocket.param_dict_vad = {'in_cache': dict(), "is_final": False}
  62. websocket.param_dict_punc = {'cache': list()}
  63. websocket.vad_pre_idx = 0
  64. speech_start = False
  65. speech_end_i = False
  66. websocket.wav_name = "microphone"
  67. print("new user connected", flush=True)
  68. try:
  69. async for message in websocket:
  70. if isinstance(message, str):
  71. messagejson = json.loads(message)
  72. if "is_speaking" in messagejson:
  73. websocket.is_speaking = messagejson["is_speaking"]
  74. websocket.param_dict_asr_online["is_final"] = not websocket.is_speaking
  75. if "chunk_interval" in messagejson:
  76. websocket.chunk_interval = messagejson["chunk_interval"]
  77. if "wav_name" in messagejson:
  78. websocket.wav_name = messagejson.get("wav_name")
  79. if "chunk_size" in messagejson:
  80. websocket.param_dict_asr_online["chunk_size"] = messagejson["chunk_size"]
  81. if len(frames_asr_online) > 0 or len(frames_asr) > 0 or not isinstance(message, str):
  82. if not isinstance(message, str):
  83. frames.append(message)
  84. duration_ms = len(message)//32
  85. websocket.vad_pre_idx += duration_ms
  86. # asr online
  87. frames_asr_online.append(message)
  88. websocket.param_dict_asr_online["is_final"] = speech_end_i
  89. if len(frames_asr_online) % websocket.chunk_interval == 0 or websocket.param_dict_asr_online["is_final"]:
  90. audio_in = b"".join(frames_asr_online)
  91. await async_asr_online(websocket, audio_in)
  92. frames_asr_online = []
  93. if speech_start:
  94. frames_asr.append(message)
  95. # vad online
  96. speech_start_i, speech_end_i = await async_vad(websocket, message)
  97. if speech_start_i:
  98. speech_start = True
  99. beg_bias = (websocket.vad_pre_idx-speech_start_i)//duration_ms
  100. frames_pre = frames[-beg_bias:]
  101. frames_asr = []
  102. frames_asr.extend(frames_pre)
  103. # asr punc offline
  104. if speech_end_i or not websocket.is_speaking:
  105. # print("vad end point")
  106. audio_in = b"".join(frames_asr)
  107. await async_asr(websocket, audio_in)
  108. frames_asr = []
  109. speech_start = False
  110. # frames_asr_online = []
  111. # websocket.param_dict_asr_online = {"cache": dict()}
  112. if not websocket.is_speaking:
  113. websocket.vad_pre_idx = 0
  114. frames = []
  115. websocket.param_dict_vad = {'in_cache': dict()}
  116. else:
  117. frames = frames[-20:]
  118. except websockets.ConnectionClosed:
  119. print("ConnectionClosed...", websocket_users)
  120. websocket_users.remove(websocket)
  121. except websockets.InvalidState:
  122. print("InvalidState...")
  123. except Exception as e:
  124. print("Exception:", e)
  125. async def async_vad(websocket, audio_in):
  126. segments_result = inference_pipeline_vad(audio_in=audio_in, param_dict=websocket.param_dict_vad)
  127. speech_start = False
  128. speech_end = False
  129. if len(segments_result) == 0 or len(segments_result["text"]) > 1:
  130. return speech_start, speech_end
  131. if segments_result["text"][0][0] != -1:
  132. speech_start = segments_result["text"][0][0]
  133. if segments_result["text"][0][1] != -1:
  134. speech_end = True
  135. return speech_start, speech_end
  136. async def async_asr(websocket, audio_in):
  137. if len(audio_in) > 0:
  138. # print(len(audio_in))
  139. audio_in = load_bytes(audio_in)
  140. rec_result = inference_pipeline_asr(audio_in=audio_in,
  141. param_dict=websocket.param_dict_asr)
  142. # print(rec_result)
  143. if inference_pipeline_punc is not None and 'text' in rec_result and len(rec_result["text"])>0:
  144. rec_result = inference_pipeline_punc(text_in=rec_result['text'],
  145. param_dict=websocket.param_dict_punc)
  146. # print("offline", rec_result)
  147. message = json.dumps({"mode": "2pass-offline", "text": rec_result["text"], "wav_name": websocket.wav_name})
  148. await websocket.send(message)
  149. async def async_asr_online(websocket, audio_in):
  150. if len(audio_in) > 0:
  151. audio_in = load_bytes(audio_in)
  152. # print(websocket.param_dict_asr_online.get("is_final", False))
  153. rec_result = inference_pipeline_asr_online(audio_in=audio_in,
  154. param_dict=websocket.param_dict_asr_online)
  155. # print(rec_result)
  156. if websocket.param_dict_asr_online.get("is_final", False):
  157. return
  158. # websocket.param_dict_asr_online["cache"] = dict()
  159. if "text" in rec_result:
  160. if rec_result["text"] != "sil" and rec_result["text"] != "waiting_for_more_voice":
  161. # print("online", rec_result)
  162. message = json.dumps({"mode": "2pass-online", "text": rec_result["text"], "wav_name": websocket.wav_name})
  163. await websocket.send(message)
  164. start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None)
  165. asyncio.get_event_loop().run_until_complete(start_server)
  166. asyncio.get_event_loop().run_forever()