diff --git a/dsLightRag/KeDaXunFei/XunFeiAudioEvaluator.py b/dsLightRag/KeDaXunFei/XunFeiAudioEvaluator.py index 84057fe7..cc40d0d2 100644 --- a/dsLightRag/KeDaXunFei/XunFeiAudioEvaluator.py +++ b/dsLightRag/KeDaXunFei/XunFeiAudioEvaluator.py @@ -60,15 +60,21 @@ class XunFeiAudioEvaluator: """WebSocket消息处理""" print(f"Received message: {message}") data = json.loads(message) - status = data["data"]["status"] - - if status == 2: + + # 检查是否存在错误码 + if data.get("code") != 0: + print(f"API错误: {data.get('message', '未知错误')}") + ws.close() + return + + # 安全获取data字段 + response_data = data.get("data", {}) + status = response_data.get("status") + + if status == 2 and response_data.get("data"): # 解析评测结果 - xml_data = base64.b64decode(data["data"]["data"]) + xml_data = base64.b64decode(response_data["data"]) xml_content = xml_data.decode("utf-8") - #print(xml_content) - - # 解析XML并提取得分信息 self.parse_evaluation_results(xml_content) ws.close() @@ -110,24 +116,25 @@ class XunFeiAudioEvaluator: # 发送音频数据 with open(self.audio_file, "rb") as file_flag: + total_sent = 0 while True: buffer = file_flag.read(1280) if not buffer: - # 发送最后一帧 - my_dict = { - "business": { - "cmd": "auw", - "aus": 4, - "aue": "lame" - }, - "data": { - "status": 2, - "data": str(base64.b64encode(buffer).decode()) + # 发送最后一帧(仅在有数据发送时) + if total_sent > 0: + my_dict = { + "business": { + "cmd": "auw", + "aus": 4, + "aue": "lame" + }, + "data": { + "status": 2, + "data": "" # 空字符串表示结束 + } } - } - ws.send(json.dumps(my_dict)) - #print("发送最后一帧") - time.sleep(1) + ws.send(json.dumps(my_dict)) + time.sleep(1) break # 发送中间音频帧 @@ -145,6 +152,7 @@ class XunFeiAudioEvaluator: } } ws.send(json.dumps(send_dict)) + total_sent += len(buffer) time.sleep(0.04) def parse_evaluation_results(self, xml_content): @@ -264,7 +272,7 @@ if __name__ == '__main__': audio_file = "./1.mp3" # 创建评测器实例 - evaluator = XunFeiAudioEvaluator(appid, api_key, api_secret, audio_file) + evaluator = XunFeiAudioEvaluator(appid, api_key, api_secret, audio_file, "english") # 运行评测 results, eval_time = evaluator.run_evaluation() diff --git a/dsLightRag/KeDaXunFei/__pycache__/XunFeiAudioEvaluator.cpython-310.pyc b/dsLightRag/KeDaXunFei/__pycache__/XunFeiAudioEvaluator.cpython-310.pyc index baa03fe9..eda476bd 100644 Binary files a/dsLightRag/KeDaXunFei/__pycache__/XunFeiAudioEvaluator.cpython-310.pyc and b/dsLightRag/KeDaXunFei/__pycache__/XunFeiAudioEvaluator.cpython-310.pyc differ diff --git a/dsLightRag/static/XunFei/js/audio_evaluation.js b/dsLightRag/static/XunFei/js/audio_evaluation.js index ae652a1a..71a877b9 100644 --- a/dsLightRag/static/XunFei/js/audio_evaluation.js +++ b/dsLightRag/static/XunFei/js/audio_evaluation.js @@ -11,21 +11,6 @@ const statusDiv = document.getElementById('status'); const resultDiv = document.getElementById('result'); const resultContent = document.getElementById('resultContent'); -// 语言切换时自动填充示例文本 -// 移除语言切换事件监听 -// languageSelect.addEventListener('change', () => { -// if (languageSelect.value === 'chinese') { -// textInput.value = '窗前明月光,疑是地上霜。'; -// } else { -// textInput.value = 'Nice to meet you.'; -// } -// }); - -// 移除页面加载事件监听 -// window.addEventListener('load', () => { -// textInput.value = '窗前明月光,疑是地上霜。'; -// stopBtn.disabled = true; -// }); // 开始录音 recordBtn.addEventListener('click', async () => {