2025-09-05 20:28:18 +08:00
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import uuid
|
2025-09-05 22:27:58 +08:00
|
|
|
|
import tempfile
|
|
|
|
|
import shutil
|
|
|
|
|
import sys
|
2025-09-06 08:56:25 +08:00
|
|
|
|
from fastapi import APIRouter, UploadFile, File, Form
|
2025-09-05 20:28:18 +08:00
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2025-09-06 10:14:59 +08:00
|
|
|
|
router = APIRouter(prefix="/api/xunFeiCn", tags=["讯飞"])
|
2025-09-05 20:28:18 +08:00
|
|
|
|
|
2025-09-05 22:18:53 +08:00
|
|
|
|
# 音频保存目录
|
|
|
|
|
UPLOAD_DIR = os.path.join(os.path.dirname(__file__), "..", "static", "audio")
|
|
|
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
2025-09-05 22:01:55 +08:00
|
|
|
|
|
2025-09-05 22:27:58 +08:00
|
|
|
|
# 讯飞配置
|
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
|
from Config.Config import XF_APPID, XF_APIKEY, XF_APISECRET
|
|
|
|
|
|
2025-09-05 22:18:53 +08:00
|
|
|
|
@router.post("/save-audio")
|
2025-09-06 10:29:48 +08:00
|
|
|
|
async def save_audio(audio: UploadFile = File(...), txt: str = Form(...)):
|
2025-09-05 22:27:58 +08:00
|
|
|
|
"""保存音频文件并评分"""
|
|
|
|
|
temp_file = None
|
2025-09-05 22:01:55 +08:00
|
|
|
|
try:
|
2025-09-05 22:27:58 +08:00
|
|
|
|
# 1. 保存音频到临时文件
|
|
|
|
|
temp_dir = tempfile.mkdtemp()
|
|
|
|
|
temp_file = os.path.join(temp_dir, f"temp_{uuid.uuid4().hex}.wav")
|
2025-09-05 22:18:53 +08:00
|
|
|
|
|
|
|
|
|
content = await audio.read()
|
2025-09-05 22:27:58 +08:00
|
|
|
|
with open(temp_file, "wb") as f:
|
2025-09-05 22:18:53 +08:00
|
|
|
|
f.write(content)
|
2025-09-06 08:38:04 +08:00
|
|
|
|
|
|
|
|
|
# 3. 保存到正式目录
|
|
|
|
|
file_name = f"audio_{uuid.uuid4().hex}.wav"
|
|
|
|
|
file_path = os.path.join(UPLOAD_DIR, file_name)
|
|
|
|
|
shutil.copy2(temp_file, file_path)
|
|
|
|
|
logger.info(f"已保存文件到: {file_path}")
|
|
|
|
|
|
2025-09-06 09:19:46 +08:00
|
|
|
|
# 添加wav转mp3功能
|
|
|
|
|
import subprocess
|
2025-09-06 10:29:48 +08:00
|
|
|
|
# 修改MP3保存路径为UPLOAD_DIR,使用相同文件名前缀
|
|
|
|
|
mp3_file_name = os.path.splitext(file_name)[0] + ".mp3"
|
|
|
|
|
mp3_file_path = os.path.join(UPLOAD_DIR, mp3_file_name)
|
2025-09-06 09:19:46 +08:00
|
|
|
|
try:
|
|
|
|
|
# 使用ffmpeg将wav转换为mp3
|
|
|
|
|
subprocess.run(
|
2025-09-06 10:29:48 +08:00
|
|
|
|
["ffmpeg", "-i", temp_file, "-y", mp3_file_path],
|
2025-09-06 09:19:46 +08:00
|
|
|
|
check=True,
|
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
|
stderr=subprocess.PIPE
|
|
|
|
|
)
|
2025-09-06 10:29:48 +08:00
|
|
|
|
logger.info(f"已将wav转换为mp3: {mp3_file_path}")
|
2025-09-06 09:19:46 +08:00
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
|
logger.error(f"ffmpeg转换失败: {e.stderr.decode()}")
|
|
|
|
|
raise Exception(f"音频格式转换失败: {e.stderr.decode()}")
|
|
|
|
|
|
2025-09-05 22:27:58 +08:00
|
|
|
|
# 2. 讯飞评分
|
2025-09-06 10:14:59 +08:00
|
|
|
|
from KeDaXunFei.XunFeiAudioEvaluator_cn import XunFeiAudioEvaluator_cn
|
|
|
|
|
evaluator = XunFeiAudioEvaluator_cn(
|
2025-09-05 22:27:58 +08:00
|
|
|
|
appid=XF_APPID,
|
|
|
|
|
api_key=XF_APIKEY,
|
|
|
|
|
api_secret=XF_APISECRET,
|
2025-09-06 10:29:48 +08:00
|
|
|
|
audio_file=mp3_file_path, # 使用新的MP3路径
|
2025-09-06 08:56:25 +08:00
|
|
|
|
txt=txt
|
2025-09-05 22:27:58 +08:00
|
|
|
|
)
|
|
|
|
|
results, eval_time = evaluator.run_evaluation()
|
2025-09-06 08:38:04 +08:00
|
|
|
|
print(evaluator.get_evaluation_summary())
|
|
|
|
|
|
2025-09-05 22:27:58 +08:00
|
|
|
|
|
2025-09-05 22:18:53 +08:00
|
|
|
|
return {
|
|
|
|
|
"success": True,
|
|
|
|
|
"file_name": file_name,
|
2025-09-05 22:27:58 +08:00
|
|
|
|
"file_path": f"/static/audio/{file_name}",
|
|
|
|
|
"evaluation": results,
|
|
|
|
|
"evaluation_time": str(eval_time)
|
2025-09-05 22:18:53 +08:00
|
|
|
|
}
|
2025-09-05 22:01:55 +08:00
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-09-05 22:27:58 +08:00
|
|
|
|
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)
|