From f9896b7b72314f2b4e1054b5ed8214a3086f50f0 Mon Sep 17 00:00:00 2001
From: zhifu gao <zhifu.gzf@alibaba-inc.com>
Date: 星期日, 07 五月 2023 23:04:01 +0800
Subject: [PATCH] Merge pull request #467 from alibaba-damo-academy/dev_websocket_offline
---
funasr/runtime/python/websocket/ws_server_online.py | 20 +++++++++++++-------
1 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/funasr/runtime/python/websocket/ws_server_online.py b/funasr/runtime/python/websocket/ws_server_online.py
index ea9ba6d..6ea8f39 100644
--- a/funasr/runtime/python/websocket/ws_server_online.py
+++ b/funasr/runtime/python/websocket/ws_server_online.py
@@ -12,7 +12,7 @@
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.logger import get_logger
-from funasr_onnx.utils.frontend import load_bytes
+from funasr.runtime.python.onnxruntime.funasr_onnx.utils.frontend import load_bytes
tracemalloc.start()
@@ -28,6 +28,8 @@
inference_pipeline_asr_online = pipeline(
task=Tasks.auto_speech_recognition,
model=args.asr_model_online,
+ ngpu=args.ngpu,
+ ncpu=args.ncpu,
model_revision='v1.0.4')
print("model loaded")
@@ -54,19 +56,23 @@
websocket.param_dict_asr_online["chunk_size"] = message["chunk_size"]
- await async_asr_online(websocket,audio)
+ frames_online.append(audio)
+ if len(frames_online) % message["chunk_interval"] == 0 or not is_speaking:
+ audio_in = b"".join(frames_online)
+ await async_asr_online(websocket,audio_in)
+ frames_online = []
except websockets.ConnectionClosed:
- print("ConnectionClosed...", websocket_users) # 閾炬帴鏂紑
+ print("ConnectionClosed...", websocket_users)
websocket_users.remove(websocket)
except websockets.InvalidState:
- print("InvalidState...") # 鏃犳晥鐘舵��
+ print("InvalidState...")
except Exception as e:
print("Exception:", e)
-async def async_asr_online(websocket,audio_in): # ASR鎺ㄧ悊
+async def async_asr_online(websocket,audio_in):
if len(audio_in) > 0:
audio_in = load_bytes(audio_in)
rec_result = inference_pipeline_asr_online(audio_in=audio_in,
@@ -75,11 +81,11 @@
websocket.param_dict_asr_online["cache"] = dict()
if "text" in rec_result:
if rec_result["text"] != "sil" and rec_result["text"] != "waiting_for_more_voice":
- print(rec_result["text"])
+ if len(rec_result["text"])>0:
+ rec_result["text"][0]=rec_result["text"][0] #.replace(" ","")
message = json.dumps({"mode": "online", "text": rec_result["text"]})
await websocket.send(message)
-
start_server = websockets.serve(ws_serve, args.host, args.port, subprotocols=["binary"], ping_interval=None)
--
Gitblit v1.9.1