|
@@ -5,6 +5,7 @@ import os
|
|
|
import json
|
|
|
import threading
|
|
|
import traceback
|
|
|
+from src.core.callcenter import registry
|
|
|
|
|
|
import nls # 引入阿里云语音识别库
|
|
|
from aliyunsdkcore.client import AcsClient
|
|
@@ -138,12 +139,12 @@ class TestSt:
|
|
|
def test_on_sentence_begin(self, message, *args):
|
|
|
self.logger.debug("[%s]test_on_sentence_begin:%s", self.__id, message)
|
|
|
if self.message_receiver:
|
|
|
- self.message_receiver(message, *args)
|
|
|
+ self.message_receiver(self.convert_message(message), *args)
|
|
|
|
|
|
def test_on_sentence_end(self, message, *args):
|
|
|
self.logger.debug("[%s]test_on_sentence_end:%s", self.__id, message)
|
|
|
if self.message_receiver:
|
|
|
- self.message_receiver(message, *args)
|
|
|
+ self.message_receiver(self.convert_message(message), *args)
|
|
|
|
|
|
def test_on_start(self, message, *args):
|
|
|
self.__event.set()
|
|
@@ -155,7 +156,7 @@ class TestSt:
|
|
|
if not self.__event.is_set():
|
|
|
self.__event.set()
|
|
|
if self.message_receiver:
|
|
|
- self.message_receiver(message, *args)
|
|
|
+ self.message_receiver(self.convert_message(message), *args)
|
|
|
|
|
|
def test_on_close(self, *args):
|
|
|
self.logger.debug("on_close: args=>%s", args)
|
|
@@ -166,12 +167,33 @@ class TestSt:
|
|
|
def test_on_result_chg(self, message, *args):
|
|
|
# self.logger.debug("test_on_chg:{}".format(message))
|
|
|
if self.message_receiver:
|
|
|
- self.message_receiver(message, *args)
|
|
|
+ self.message_receiver(self.convert_message(message), *args)
|
|
|
|
|
|
def test_on_completed(self, message, *args):
|
|
|
# self.logger.debug("on_completed:args=>{} message=>{}".format(args, message))
|
|
|
pass
|
|
|
|
|
|
+ def convert_message(self, message):
|
|
|
+ final_result = {}
|
|
|
+ message = json.loads(message)
|
|
|
+ if message["header"]["status"] == 20000000:
|
|
|
+ if message["header"]["name"] == "SentenceBegin":
|
|
|
+ final_result['name'] = 'SentenceBegin'
|
|
|
+ if message["header"]["name"] == "SentenceEnd":
|
|
|
+ result = message["payload"]["result"]
|
|
|
+ # self.logger.info("asr返回内容Result:%s", result)
|
|
|
+ final_result['name'] = 'SentenceEnd'
|
|
|
+ final_result['result'] = result
|
|
|
+ elif message["header"]["name"] == "TranscriptionResultChanged":
|
|
|
+ final_result['name'] = 'TranscriptionResultChanged'
|
|
|
+ else:
|
|
|
+ final_result['name'] = 'TranscriptionResultError'
|
|
|
+ final_result['status'] = message['header']['status']
|
|
|
+ final_result['result'] = ''
|
|
|
+ self.logger.info(f"Status is not {message['header']['status']}")
|
|
|
+ registry.ASR_ERRORS.labels(message['header']['status']).inc()
|
|
|
+ return final_result
|
|
|
+
|
|
|
|
|
|
# 讯飞ASR实时转写
|
|
|
class XfAsr:
|
|
@@ -244,28 +266,59 @@ class XfAsr:
|
|
|
|
|
|
def recv(self):
|
|
|
try:
|
|
|
+ # {"seg_id": 0, "cn": {"st": {"rt": [{"ws": [{"cw": [{"sc": 0.00, "w": "凭", "wp": "n", "rl": "0", "wb": 13, "wc": 0.00, "we": 26}], "wb": 0, "we": 0}]}], "bg": "9510", "type": "1", "ed": "0"}}, "ls": false}
|
|
|
+ # {"seg_id": 1, "cn": {"st": {"rt": [{"ws": [{"cw": [{"sc": 0.00, "w": "停水", "wp": "n", "rl": "0", "wb": 26, "wc": 0.00, "we": 52}], "wb": 0,"we": 0}]}], "bg": "9770", "type": "1", "ed": "0"}}, "ls": false}
|
|
|
+ # {"seg_id": 2, "cn": {"st": {"rt": [{"ws": [{"cw": [{"sc": 0.00, "w": "停水", "wp": "n", "rl": "0", "wb": 9, "wc": 0.00, "we": 64}], "wb": 9,"we": 64},{"cw": [{"sc": 0.00, "w": "咨询", "wp": "n", "rl": "0", "wb": 65, "wc": 0.00, "we": 132}], "wb": 65,"we": 132}]}], "bg": "9510", "type": "0", "ed": "10950"}}, "ls": false}
|
|
|
self.logger.info(f"xunfei.Asr.recv: ws.connected:{self.ws.connected}")
|
|
|
while self.ws.connected:
|
|
|
- result = str(self.ws.recv())
|
|
|
- if len(result) == 0:
|
|
|
+ message = str(self.ws.recv())
|
|
|
+ if len(message) == 0:
|
|
|
self.logger.info("xunfei.Asr.recv: receive result end")
|
|
|
break
|
|
|
- result_dict = json.loads(result)
|
|
|
- # 解析结果
|
|
|
- if result_dict["action"] == "started":
|
|
|
- self.logger.info("xunfei.Asr.recv: handshake success, result: " + result)
|
|
|
-
|
|
|
- if result_dict["action"] == "result":
|
|
|
- result_1 = result_dict
|
|
|
- # result_2 = json.loads(result_1["cn"])
|
|
|
- # result_3 = json.loads(result_2["st"])
|
|
|
- # result_4 = json.loads(result_3["rt"])
|
|
|
- self.logger.info("xunfei.Asr.recv: rtasr result: " + result_1["data"])
|
|
|
-
|
|
|
- if result_dict["action"] == "error":
|
|
|
- self.logger.info("xunfei.Asr.recv: rtasr error: " + result)
|
|
|
- self.ws.close()
|
|
|
- return
|
|
|
+
|
|
|
+ self.logger.info("xunfei.Asr.recv: message :{}", message)
|
|
|
+ if self.message_receiver:
|
|
|
+ self.message_receiver(self.convert_message(message))
|
|
|
+
|
|
|
+ # result_dict = json.loads(result)
|
|
|
+ # # 解析结果
|
|
|
+ # if result_dict["action"] == "started":
|
|
|
+ # self.logger.info("xunfei.Asr.recv: handshake success, result: " + result)
|
|
|
+ #
|
|
|
+ #
|
|
|
+ # if result_dict["action"] == "result":
|
|
|
+ # result_1 = result_dict
|
|
|
+ # # result_2 = json.loads(result_1["cn"])
|
|
|
+ # # result_3 = json.loads(result_2["st"])
|
|
|
+ # # result_4 = json.loads(result_3["rt"])
|
|
|
+ # self.logger.info("xunfei.Asr.recv: rtasr result: " + result_1["data"])
|
|
|
+ #
|
|
|
+ # if result_dict["action"] == "error":
|
|
|
+ # self.logger.info("xunfei.Asr.recv: rtasr error: " + result)
|
|
|
+ # self.ws.close()
|
|
|
+ # return
|
|
|
except Exception as e:
|
|
|
traceback.print_exc()
|
|
|
self.logger.error("xunfei.Asr.recv: receive result end", e)
|
|
|
+
|
|
|
+ def convert_message(self, message):
|
|
|
+ final_result = {}
|
|
|
+ result_dict = json.loads(message)
|
|
|
+ if result_dict["action"] == "started":
|
|
|
+ final_result['name'] = 'SentenceBegin'
|
|
|
+ elif result_dict["action"] == "result":
|
|
|
+ result_dict = json.loads(message)
|
|
|
+ st = result_dict["cn"]["st"]
|
|
|
+ rt = st["rt"]
|
|
|
+ if st.get('type') == 1:
|
|
|
+ final_result['name'] = 'TranscriptionResultChanged'
|
|
|
+ else:
|
|
|
+ final_result['name'] = 'SentenceEnd'
|
|
|
+ final_result['result'] = ''.join(cw["w"] for item in rt for ws in item["ws"] for cw in ws["cw"])
|
|
|
+ elif result_dict["action"] == "error":
|
|
|
+ final_result['name'] = 'TranscriptionResultError'
|
|
|
+ final_result['status'] = ''
|
|
|
+ final_result['result'] = message
|
|
|
+ self.ws.close()
|
|
|
+
|
|
|
+ return final_result
|