'commit'
This commit is contained in:
@@ -7,6 +7,9 @@ from fastapi import APIRouter, HTTPException, BackgroundTasks, Query, UploadFile
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional
|
||||
import tempfile
|
||||
|
||||
from starlette.websockets import WebSocket
|
||||
|
||||
from Util.ObsUtil import ObsUploader
|
||||
from Config.Config import OBS_BUCKET, OBS_SERVER, XF_APPID, XF_APISECRET, XF_APIKEY
|
||||
from fastapi.responses import StreamingResponse
|
||||
@@ -72,7 +75,15 @@ async def evaluate_audio( # 添加async关键字
|
||||
|
||||
# 创建临时文件保存上传的音频
|
||||
# 修改临时文件处理逻辑
|
||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
|
||||
import os
|
||||
import uuid
|
||||
# 创建持久化保存目录
|
||||
output_dir = os.path.join(os.path.dirname(__file__), '../static/audio')
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
# 生成唯一文件名
|
||||
temp_audio_path = os.path.join(output_dir, f"eval_audio_{uuid.uuid4()}.wav")
|
||||
|
||||
with open(temp_audio_path, 'wb') as temp_audio:
|
||||
# 先读取音频内容
|
||||
audio_content = await audio_file.read()
|
||||
# 再进行格式转换
|
||||
@@ -82,8 +93,6 @@ async def evaluate_audio( # 添加async关键字
|
||||
wf.setsampwidth(2)
|
||||
wf.setframerate(16000)
|
||||
wf.writeframes(audio_content)
|
||||
# 移除冗余的temp_audio.write(audio_content),避免重复写入
|
||||
temp_audio_path = temp_audio.name
|
||||
|
||||
# 创建评测器实例
|
||||
evaluator = XunFeiAudioEvaluator(
|
||||
@@ -122,8 +131,8 @@ async def evaluate_audio( # 添加async关键字
|
||||
executor, evaluator.run_evaluation
|
||||
)
|
||||
|
||||
# 清理临时文件
|
||||
os.unlink(temp_audio_path)
|
||||
# 清理临时文件 - 注释掉此行以便保留文件
|
||||
# os.unlink(temp_audio_path)
|
||||
|
||||
# 生成评测ID
|
||||
evaluation_id = str(uuid.uuid4())
|
||||
@@ -160,3 +169,85 @@ async def get_evaluation_result(evaluation_id: str):
|
||||
"message": "请实现结果存储逻辑"
|
||||
}
|
||||
|
||||
|
||||
@router.websocket("/xunfei/streaming-evaluate")
|
||||
async def streaming_evaluate(websocket: WebSocket):
|
||||
await websocket.accept()
|
||||
logger.info("讯飞语音评测WebSocket连接已建立")
|
||||
|
||||
# 生成唯一音频文件名
|
||||
output_dir = os.path.join(os.path.dirname(__file__), '../static/audio')
|
||||
os.makedirs(output_dir, exist_ok=True)
|
||||
temp_audio_path = os.path.join(output_dir, f"eval_audio_{uuid.uuid4()}.wav")
|
||||
|
||||
try:
|
||||
# 初始化WAV文件
|
||||
with wave.open(temp_audio_path, 'wb') as wf:
|
||||
wf.setnchannels(1)
|
||||
wf.setsampwidth(2)
|
||||
wf.setframerate(16000)
|
||||
|
||||
# 持续接收音频流
|
||||
while True:
|
||||
data = await websocket.receive_bytes()
|
||||
if not data: # 结束标记
|
||||
break
|
||||
wf.writeframes(data)
|
||||
|
||||
# 执行评测(复用现有逻辑)
|
||||
evaluator = XunFeiAudioEvaluator(
|
||||
appid=XUNFEI_CONFIG["appid"],
|
||||
api_key=XUNFEI_CONFIG["api_key"],
|
||||
api_secret=XUNFEI_CONFIG["api_secret"],
|
||||
audio_file=temp_audio_path,
|
||||
language=await websocket.receive_json().get("language", "chinese")
|
||||
)
|
||||
|
||||
# 根据语言设置不同的评测参数
|
||||
if language == "chinese":
|
||||
# 中文评测配置
|
||||
evaluator.business_params = {
|
||||
"category": "read_chapter",
|
||||
"ent": "cn_vip",
|
||||
"group": group,
|
||||
"check_type": check_type,
|
||||
"grade": grade,
|
||||
}
|
||||
else:
|
||||
# 英文评测配置
|
||||
evaluator.business_params = {
|
||||
"category": "read_chapter",
|
||||
"ent": "en_vip",
|
||||
"group": group, # 添加缺失参数
|
||||
"check_type": check_type, # 添加缺失参数
|
||||
"grade": grade, # 添加缺失参数
|
||||
}
|
||||
|
||||
# 运行评测
|
||||
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)
|
||||
|
||||
# 生成评测ID
|
||||
evaluation_id = str(uuid.uuid4())
|
||||
|
||||
return AudioEvaluationResponse(
|
||||
evaluation_id=evaluation_id,
|
||||
status="success",
|
||||
results=results,
|
||||
evaluation_time=eval_time.total_seconds() if eval_time else None
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"评测失败: {str(e)}")
|
||||
await websocket.send_json({
|
||||
"status": "error",
|
||||
"message": str(e)
|
||||
})
|
||||
await websocket.close()
|
||||
|
||||
|
Reference in New Issue
Block a user