diff --git a/dsLightRag/Routes/XunFeiRoute.py b/dsLightRag/Routes/XunFeiRoute.py index ae904d51..8045807a 100644 --- a/dsLightRag/Routes/XunFeiRoute.py +++ b/dsLightRag/Routes/XunFeiRoute.py @@ -1,11 +1,10 @@ import logging import os import uuid -import time -import asyncio -from fastapi import APIRouter, HTTPException, UploadFile, File -from pydantic import BaseModel -from typing import Optional +import tempfile +import shutil +import sys +from fastapi import APIRouter, UploadFile, File # 配置日志 logger = logging.getLogger(__name__) @@ -15,24 +14,52 @@ router = APIRouter(prefix="/api/xunFei", tags=["讯飞"]) UPLOAD_DIR = os.path.join(os.path.dirname(__file__), "..", "static", "audio") os.makedirs(UPLOAD_DIR, exist_ok=True) +# 讯飞配置 +sys.path.append(os.path.join(os.path.dirname(__file__), "..")) +from Config.Config import XF_APPID, XF_APIKEY, XF_APISECRET + @router.post("/save-audio") async def save_audio(audio: UploadFile = File(...)): - """保存音频文件""" + """保存音频文件并评分""" + temp_file = None try: - # 生成文件名 + # 1. 保存音频到临时文件 + temp_dir = tempfile.mkdtemp() + temp_file = os.path.join(temp_dir, f"temp_{uuid.uuid4().hex}.wav") + + content = await audio.read() + with open(temp_file, "wb") as f: + f.write(content) + + # 2. 讯飞评分 + from KeDaXunFei.XunFeiAudioEvaluator import XunFeiAudioEvaluator + evaluator = XunFeiAudioEvaluator( + appid=XF_APPID, + api_key=XF_APIKEY, + api_secret=XF_APISECRET, + audio_file=temp_file, + language="english" + ) + results, eval_time = evaluator.run_evaluation() + + # 3. 保存到正式目录 file_name = f"audio_{uuid.uuid4().hex}.wav" file_path = os.path.join(UPLOAD_DIR, file_name) + shutil.copy2(temp_file, file_path) - # 保存文件 - content = await audio.read() - with open(file_path, "wb") as f: - f.write(content) - print(file_path) return { "success": True, "file_name": file_name, - "file_path": f"/static/audio/{file_name}" + "file_path": f"/static/audio/{file_name}", + "evaluation": results, + "evaluation_time": str(eval_time) } except Exception as e: - return {"success": False, "error": str(e)} \ No newline at end of file + logger.error(f"处理失败: {str(e)}") + return {"success": False, "error": str(e)} + + finally: + # 4. 清理临时文件 + if temp_file and os.path.exists(temp_file): + shutil.rmtree(os.path.dirname(temp_file), ignore_errors=True) \ No newline at end of file diff --git a/dsLightRag/Routes/__pycache__/XunFeiRoute.cpython-310.pyc b/dsLightRag/Routes/__pycache__/XunFeiRoute.cpython-310.pyc index 8e53d407..7e6a0245 100644 Binary files a/dsLightRag/Routes/__pycache__/XunFeiRoute.cpython-310.pyc and b/dsLightRag/Routes/__pycache__/XunFeiRoute.cpython-310.pyc differ diff --git a/dsLightRag/static/audio/audio_76c396992ca94dd4b94250ac67e31820.wav b/dsLightRag/static/audio/audio_76c396992ca94dd4b94250ac67e31820.wav new file mode 100644 index 00000000..129e0eee Binary files /dev/null and b/dsLightRag/static/audio/audio_76c396992ca94dd4b94250ac67e31820.wav differ