This commit is contained in:
2025-09-05 21:31:14 +08:00
parent 851fc17339
commit 8d5d1ce48d
6 changed files with 89 additions and 55 deletions

View File

@@ -18,12 +18,13 @@ from Config.Config import XF_APPID, XF_APISECRET, XF_APIKEY
class XunFeiAudioEvaluator:
"""讯飞语音评测类"""
def __init__(self, appid, api_key, api_secret, audio_file):
def __init__(self, appid, api_key, api_secret, audio_file, language):
self.appid = appid
self.api_key = api_key
self.api_secret = api_secret
self.audio_file = audio_file
self.host_url = "ws://ise-api.xfyun.cn/v2/open-ise"
self.language = language
self.host_url = "wss://ise-api.xfyun.cn/v2/open-ise"
self.websocket_url = ""
self.evaluation_results = {}
@@ -60,29 +61,29 @@ class XunFeiAudioEvaluator:
print(f"Received message: {message}")
data = json.loads(message)
status = data["data"]["status"]
if status == 2:
# 解析评测结果
xml_data = base64.b64decode(data["data"]["data"])
xml_content = xml_data.decode("utf-8")
#print(xml_content)
# 解析XML并提取得分信息
self.parse_evaluation_results(xml_content)
ws.close()
def on_error(self, ws, error):
"""错误处理"""
print(f"Error: {error},{ws}")
def on_close(self, ws, reason, res):
"""连接关闭处理"""
print(f"WebSocket connection closed,{ws}")
def on_open(self, ws):
"""连接建立处理"""
print(f"WebSocket connection opened,{ws},ws连接建立成功...")
# 发送初始参数
send_dict = {
"common": {
@@ -93,7 +94,7 @@ class XunFeiAudioEvaluator:
"rstcd": "utf8",
"sub": "ise",
"group": "pupil",
"ent": "en_vip",
"ent": "zh_cn" if self.language == "chinese" else "en_vip",
"tte": "utf-8",
"cmd": "ssb",
"auf": "audio/L16;rate=16000",
@@ -106,7 +107,7 @@ class XunFeiAudioEvaluator:
}
}
ws.send(json.dumps(send_dict))
# 发送音频数据
with open(self.audio_file, "rb") as file_flag:
while True:
@@ -115,12 +116,12 @@ class XunFeiAudioEvaluator:
# 发送最后一帧
my_dict = {
"business": {
"cmd": "auw",
"aus": 4,
"cmd": "auw",
"aus": 4,
"aue": "lame"
},
"data": {
"status": 2,
"status": 2,
"data": str(base64.b64encode(buffer).decode())
}
}
@@ -157,7 +158,7 @@ class XunFeiAudioEvaluator:
self.evaluation_results = {
'accuracy_score': float(read_chapter.get('accuracy_score', 0)),
'fluency_score': float(read_chapter.get('fluency_score', 0)),
'integrity_score': float(read_chapter.get('integrity_score', 0)),
'completeness_score': float(read_chapter.get('integrity_score', 0)), # 修复字段名
'standard_score': float(read_chapter.get('standard_score', 0)),
'total_score': float(read_chapter.get('total_score', 0)),
'word_count': int(read_chapter.get('word_count', 0)),