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

@@ -47,7 +47,6 @@ XUNFEI_CONFIG = {
async def evaluate_audio( # 添加async关键字
background_tasks: BackgroundTasks,
language: str = Form(..., description="语言类型: chinese/english"),
text: str = Form(..., description="评测文本内容"),
group: str = Form("adult", description="群体类型: adult, youth, pupil"),
check_type: str = Form("common", description="检错严格程度: easy, common, hard"),
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'")
# 创建临时文件保存上传的音频
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()
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
# 创建评测器实例
@@ -83,7 +91,8 @@ async def evaluate_audio( # 添加async关键字
# 与AudioEvaluator示例用法保持一致
api_key=XUNFEI_CONFIG["api_key"],
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,
"check_type": check_type,
"grade": grade,
"text": '\uFEFF' + f"[content]\n{text}"
}
else:
# 英文评测配置
# 英文评测配置
evaluator.business_params = {
"category": "read_chapter",
"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": "请实现结果存储逻辑"
}
# 需要修改XunFeiAudioEvaluator类以支持参数配置
# 在XunFeiAudioEvaluator类中添加business_params属性并在on_open方法中使用