소스 검색

讯飞实时听写接口,debug

Davidliu 1 개월 전
부모
커밋
233f15c9e8
1개의 변경된 파일51개의 추가작업 그리고 24개의 파일을 삭제
  1. 51 24
      src/core/voip/asr.py

+ 51 - 24
src/core/voip/asr.py

@@ -330,7 +330,7 @@ class XunfeiAsr:
         # 公共参数(common)
         self.CommonArgs = {"app_id": self.APPID}
         # 业务参数(business),更多个性化参数可在官网查看
-        self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":2000}
+        self.BusinessArgs = {"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vinfo":1,"vad_eos":2000, "dwa":"wpgs"}
 
         # self.__event = threading.Event()
         self.__th = threading.Thread(target=self.__run)
@@ -412,7 +412,7 @@ class XunfeiAsr:
         if not audio_data:
             status = XunfeiAsr.STATUS_LAST_FRAME
         buf = bytes(audio_data) if audio_data else None
-        self.logger.info('xunfei.Asr.send_audio: call_id: %s, status:%s, chunk:%s, %s', self.__id, status, (len(buf) if buf else 0) , buf)
+        # self.logger.info('xunfei.Asr.send_audio: call_id: %s, status:%s, chunk:%s, %s', self.__id, status, (len(buf) if buf else 0) , buf)
 
         # 第一帧处理
         # 发送第一帧音频,带business 参数
@@ -461,14 +461,14 @@ class XunfeiAsr:
                 errMsg = json.loads(message)["message"]
                 self.logger.info("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
 
-            else:
-                data = json.loads(message)["data"]["result"]["ws"]
-                # print(json.loads(message))
-                result = ""
-                for i in data:
-                    for w in i["cw"]:
-                        result += w["w"]
-                self.logger.info("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
+            # else:
+            #     data = json.loads(message)["data"]["result"]["ws"]
+            #     # print(json.loads(message))
+            #     result = ""
+            #     for i in data:
+            #         for w in i["cw"]:
+            #             result += w["w"]
+            #     self.logger.info("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
         except Exception as e:
             self.logger.error("receive msg,but parse exception:%s", e)
 
@@ -487,22 +487,49 @@ class XunfeiAsr:
     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'
+
+        if message["code"] == 0:
+            data = message["data"]["result"]["ws"]
+            status = message["data"]["status"]
+
         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()
+            final_result['status'] = message['code']
+            final_result['result'] = message['message']
+            self.logger.info("call_id:%s, sid:%s call error:%s code is:%s" % (self.__id, message["sid"], message["message"], message["code"]))
+            registry.ASR_ERRORS.labels(message['code']).inc()
+
+        # code = json.loads(message)["code"]
+        # sid = json.loads(message)["sid"]
+        # if code != 0:
+        #     errMsg = json.loads(message)["message"]
+        #     self.logger.info("sid:%s call error:%s code is:%s" % (sid, errMsg, code))
+        #
+        # else:
+        #     data = json.loads(message)["data"]["result"]["ws"]
+        #     # print(json.loads(message))
+        #     result = ""
+        #     for i in data:
+        #         for w in i["cw"]:
+        #             result += w["w"]
+        #     self.logger.info("sid:%s call success!,data is:%s" % (sid, json.dumps(data, ensure_ascii=False)))
+        #
+        # 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()
 
         self.logger.error("xunfei.Asr.recv: call_id:%s, final_result: %s", self.__id, final_result)
         return final_result