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

View File

@@ -47,7 +47,6 @@ XUNFEI_CONFIG = {
async def evaluate_audio( # 添加async关键字 async def evaluate_audio( # 添加async关键字
background_tasks: BackgroundTasks, background_tasks: BackgroundTasks,
language: str = Form(..., description="语言类型: chinese/english"), language: str = Form(..., description="语言类型: chinese/english"),
text: str = Form(..., description="评测文本内容"),
group: str = Form("adult", description="群体类型: adult, youth, pupil"), group: str = Form("adult", description="群体类型: adult, youth, pupil"),
check_type: str = Form("common", description="检错严格程度: easy, common, hard"), check_type: str = Form("common", description="检错严格程度: easy, common, hard"),
grade: str = Form("middle", description="学段: junior, middle, senior"), grade: str = Form("middle", description="学段: junior, middle, senior"),
@@ -72,9 +71,18 @@ async def evaluate_audio( # 添加async关键字
raise HTTPException(status_code=400, detail="group参数必须是'adult', 'youth''pupil'") raise HTTPException(status_code=400, detail="group参数必须是'adult', 'youth''pupil'")
# 创建临时文件保存上传的音频 # 创建临时文件保存上传的音频
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio: # 修改临时文件处理逻辑
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
# 先读取音频内容
audio_content = await audio_file.read() audio_content = await audio_file.read()
temp_audio.write(audio_content) # 再进行格式转换
import wave
with wave.open(temp_audio, 'wb') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(16000)
wf.writeframes(audio_content)
# 移除冗余的temp_audio.write(audio_content),避免重复写入
temp_audio_path = temp_audio.name temp_audio_path = temp_audio.name
# 创建评测器实例 # 创建评测器实例
@@ -83,7 +91,8 @@ async def evaluate_audio( # 添加async关键字
# 与AudioEvaluator示例用法保持一致 # 与AudioEvaluator示例用法保持一致
api_key=XUNFEI_CONFIG["api_key"], api_key=XUNFEI_CONFIG["api_key"],
api_secret=XUNFEI_CONFIG["api_secret"], api_secret=XUNFEI_CONFIG["api_secret"],
audio_file=temp_audio_path audio_file=temp_audio_path,
language=language # 添加语言参数
) )
# 根据语言设置不同的评测参数 # 根据语言设置不同的评测参数
@@ -95,14 +104,15 @@ async def evaluate_audio( # 添加async关键字
"group": group, "group": group,
"check_type": check_type, "check_type": check_type,
"grade": grade, "grade": grade,
"text": '\uFEFF' + f"[content]\n{text}"
} }
else: else:
# 英文评测配置 # 英文评测配置
evaluator.business_params = { evaluator.business_params = {
"category": "read_chapter", "category": "read_chapter",
"ent": "en_vip", "ent": "en_vip",
"text": '\uFEFF' + f"[content]\n{text}" "group": group, # 添加缺失参数
"check_type": check_type, # 添加缺失参数
"grade": grade, # 添加缺失参数
} }
# 运行评测 # 运行评测
@@ -150,5 +160,3 @@ async def get_evaluation_result(evaluation_id: str):
"message": "请实现结果存储逻辑" "message": "请实现结果存储逻辑"
} }
# 需要修改XunFeiAudioEvaluator类以支持参数配置
# 在XunFeiAudioEvaluator类中添加business_params属性并在on_open方法中使用

View File

@@ -18,11 +18,6 @@
</select> </select>
</div> </div>
<div class="form-group">
<label for="text">评测文本内容:</label>
<input type="text" id="text" placeholder="请输入要朗读的文本内容">
</div>
<div class="form-group"> <div class="form-group">
<button id="recordBtn" class="btn btn-record">开始录音</button> <button id="recordBtn" class="btn btn-record">开始录音</button>
<button id="stopBtn" class="btn btn-stop" disabled>停止录音</button> <button id="stopBtn" class="btn btn-stop" disabled>停止录音</button>

View File

@@ -12,19 +12,20 @@ const resultDiv = document.getElementById('result');
const resultContent = document.getElementById('resultContent'); const resultContent = document.getElementById('resultContent');
// 语言切换时自动填充示例文本 // 语言切换时自动填充示例文本
languageSelect.addEventListener('change', () => { // 移除语言切换事件监听
if (languageSelect.value === 'chinese') { // languageSelect.addEventListener('change', () => {
textInput.value = '窗前明月光,疑是地上霜。'; // if (languageSelect.value === 'chinese') {
} else { // textInput.value = '窗前明月光,疑是地上霜。';
textInput.value = 'Nice to meet you.'; // } else {
} // textInput.value = 'Nice to meet you.';
}); // }
// });
// 页面加载时自动填充中文示例 // 移除页面加载事件监听
window.addEventListener('load', () => { // window.addEventListener('load', () => {
textInput.value = '窗前明月光,疑是地上霜。'; // textInput.value = '窗前明月光,疑是地上霜。';
stopBtn.disabled = true; // stopBtn.disabled = true;
}); // });
// 开始录音 // 开始录音
recordBtn.addEventListener('click', async () => { recordBtn.addEventListener('click', async () => {
@@ -46,15 +47,29 @@ recordBtn.addEventListener('click', async () => {
} }
}); });
mediaRecorder = new MediaRecorder(stream); // 检测浏览器支持的录制格式
audioChunks = []; const getSupportedMimeType = () => {
const options = [
mediaRecorder.ondataavailable = (event) => { 'audio/webm; codecs=opus',
audioChunks.push(event.data); 'audio/webm',
'audio/mp4',
'' // 空字符串表示使用浏览器默认格式
];
for (const option of options) {
if (MediaRecorder.isTypeSupported(option)) return option;
}
return '';
}; };
// 使用检测到的格式初始化
mediaRecorder = new MediaRecorder(stream, {
mimeType: getSupportedMimeType(),
audioBitsPerSecond: 16000
});
// 同时修正Blob类型确保前后一致
mediaRecorder.onstop = () => { mediaRecorder.onstop = () => {
audioBlob = new Blob(audioChunks, { type: 'audio/webm' }); audioBlob = new Blob(audioChunks, { type: 'audio/webm' }); // 与录制格式匹配
statusDiv.textContent = '录音完成,正在自动提交评测...'; statusDiv.textContent = '录音完成,正在自动提交评测...';
submitEvaluation(); submitEvaluation();
// 停止所有音轨以释放麦克风 // 停止所有音轨以释放麦克风
@@ -104,11 +119,12 @@ async function submitEvaluation() {
return; return;
} }
if (!textInput.value.trim()) { // 移除文本验证
statusDiv.textContent = '请输入评测文本内容'; // if (!textInput.value.trim()) {
statusDiv.className = 'status error'; // statusDiv.textContent = '请输入评测文本内容';
return; // statusDiv.className = 'status error';
} // return;
// }
try { try {
statusDiv.textContent = '正在提交评测...'; statusDiv.textContent = '正在提交评测...';
@@ -117,7 +133,8 @@ async function submitEvaluation() {
const formData = new FormData(); const formData = new FormData();
formData.append('audio_file', audioBlob, 'recording.webm'); formData.append('audio_file', audioBlob, 'recording.webm');
formData.append('language', languageSelect.value); formData.append('language', languageSelect.value);
formData.append('text', textInput.value.trim()); // 移除文本参数
// formData.append('text', textInput.value.trim());
formData.append('group', 'adult'); formData.append('group', 'adult');
formData.append('check_type', 'common'); formData.append('check_type', 'common');
formData.append('grade', 'middle'); formData.append('grade', 'middle');
@@ -168,20 +185,33 @@ function displayResults(results) {
if (results.accuracy_score !== undefined) { if (results.accuracy_score !== undefined) {
html += `<p><strong>准确度:</strong> ${results.accuracy_score.toFixed(4)}</p>`; html += `<p><strong>准确度:</strong> ${results.accuracy_score.toFixed(4)}</p>`;
} }
// 添加结果对象有效性检查
if (!results) {
showError("评测结果格式错误");
return;
}
if (results.fluency_score !== undefined) { if (results.fluency_score !== undefined) {
html += `<p><strong>流利度:</strong> ${results.fluency_score.toFixed(4)}</p>`; html += `<p><strong>流利度:</strong> ${results.fluency_score.toFixed(4)}</p>`;
} else {
html += `<p><strong>流利度:</strong> 未获取</p>`; // 添加默认值
} }
if (results.completeness_score !== undefined) { if (results.completeness_score !== undefined) {
html += `<p><strong>完整度:</strong> ${results.completeness_score.toFixed(4)}</p>`; html += `<p><strong>完整度:</strong> ${results.completeness_score.toFixed(4)}</p>`;
} else {
html += `<p><strong>完整度:</strong> 未获取</p>`; // 添加默认值
} }
html += '</div>'; html += '</div>';
// 显示单词级评分 // 显示单词级评分
if (results.words && results.words.length > 0) { if (results.words && results.words.length > 0) {
html += '<div class="word-scores"><h4>单词评分:</h4><ul>'; html += '<div class="word-scores"><h4>单词评分:</h4><ul>';
results.words.forEach(word => { results.words.forEach(word => {
html += `<li>${word.content}: ${word.score.toFixed(4)}</li>`; // 为单词评分添加空值检查
const score = word.score !== undefined ? word.score.toFixed(4) : '无';
html += `<li>${word.content}: ${score}</li>`;
}); });
html += '</ul></div>'; html += '</ul></div>';
} }