This commit is contained in:
2025-09-05 21:01:11 +08:00
parent b6afe426d8
commit 851fc17339
7 changed files with 297 additions and 307 deletions

View File

@@ -2,6 +2,7 @@ import logging
import os
import uuid
import time
import asyncio # 添加此行
from fastapi import APIRouter, HTTPException, BackgroundTasks, Query, UploadFile, File, Form
from pydantic import BaseModel
from typing import Optional
@@ -37,14 +38,15 @@ class AudioEvaluationResponse(BaseModel):
# 科大讯飞API配置需要根据实际情况配置
XUNFEI_CONFIG = {
"appid": XF_APPID,
"api_key": XF_APISECRET,
"api_secret": XF_APIKEY
# 修复参数名颠倒问题
"api_key": XF_APIKEY,
"api_secret": XF_APISECRET
}
@router.post("/evaluate-audio", response_model=AudioEvaluationResponse)
async def evaluate_audio(
async def evaluate_audio( # 添加async关键字
background_tasks: BackgroundTasks,
language: str = Form("chinese", description="评测语言: chineseenglish"),
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"),
@@ -58,6 +60,13 @@ async def evaluate_audio(
if language not in ["chinese", "english"]:
raise HTTPException(status_code=400, detail="language参数必须是'chinese''english'")
# 新增参数验证
if check_type not in ["easy", "common", "hard"]:
raise HTTPException(status_code=400, detail="check_type参数必须是'easy''common''hard'")
if grade not in ["junior", "middle", "senior"]:
raise HTTPException(status_code=400, detail="grade参数必须是'junior''middle''senior'")
# 验证群体参数
if group not in ["adult", "youth", "pupil"]:
raise HTTPException(status_code=400, detail="group参数必须是'adult', 'youth''pupil'")
@@ -71,6 +80,7 @@ async def evaluate_audio(
# 创建评测器实例
evaluator = XunFeiAudioEvaluator(
appid=XUNFEI_CONFIG["appid"],
# 与AudioEvaluator示例用法保持一致
api_key=XUNFEI_CONFIG["api_key"],
api_secret=XUNFEI_CONFIG["api_secret"],
audio_file=temp_audio_path
@@ -96,7 +106,11 @@ async def evaluate_audio(
}
# 运行评测
results, eval_time = evaluator.run_evaluation()
from concurrent.futures import ThreadPoolExecutor
executor = ThreadPoolExecutor(max_workers=4)
results, eval_time = await asyncio.get_event_loop().run_in_executor(
executor, evaluator.run_evaluation
)
# 清理临时文件
os.unlink(temp_audio_path)