'commit'
This commit is contained in:
@@ -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 = {}
|
||||||
|
|
||||||
@@ -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",
|
||||||
@@ -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)),
|
||||||
|
Binary file not shown.
@@ -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方法中使用
|
|
Binary file not shown.
@@ -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>
|
||||||
|
@@ -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,11 +185,22 @@ 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>';
|
||||||
@@ -181,7 +209,9 @@ function displayResults(results) {
|
|||||||
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>';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user