178 lines
5.7 KiB
Python
178 lines
5.7 KiB
Python
import logging
|
||
import os
|
||
import tempfile
|
||
import uuid
|
||
from datetime import datetime
|
||
|
||
from fastapi import APIRouter, Request, File, UploadFile
|
||
from fastapi.responses import JSONResponse
|
||
|
||
# 创建路由路由器
|
||
router = APIRouter(prefix="/api", tags=["学伴"])
|
||
|
||
# 配置日志
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# 导入学伴工具函数、ASR客户端和OBS上传工具
|
||
from Util.XueBanUtil import get_xueban_response_async
|
||
from Util.ASRClient import ASRClient
|
||
from Util.ObsUtil import ObsUploader
|
||
|
||
|
||
@router.post("/xueban/upload-audio")
|
||
async def upload_audio(file: UploadFile = File(...)):
|
||
"""
|
||
上传音频文件并进行ASR处理
|
||
- 参数: file - 音频文件
|
||
- 返回: JSON包含识别结果
|
||
"""
|
||
try:
|
||
# 记录日志
|
||
logger.info(f"接收到音频文件: {file.filename}")
|
||
|
||
# 保存临时文件
|
||
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||
file_ext = os.path.splitext(file.filename)[1]
|
||
temp_file_name = f"temp_audio_{timestamp}{file_ext}"
|
||
temp_file_path = os.path.join(tempfile.gettempdir(), temp_file_name)
|
||
|
||
with open(temp_file_path, "wb") as f:
|
||
content = await file.read()
|
||
f.write(content)
|
||
|
||
logger.info(f"音频文件已保存至临时目录: {temp_file_path}")
|
||
|
||
# 调用ASR服务进行处理
|
||
asr_result = await process_asr(temp_file_path)
|
||
|
||
# 删除临时文件
|
||
os.remove(temp_file_path)
|
||
logger.info(f"临时文件已删除: {temp_file_path}")
|
||
|
||
# 返回识别结果
|
||
return JSONResponse(content={
|
||
"success": True,
|
||
"message": "音频识别成功",
|
||
"data": asr_result
|
||
})
|
||
|
||
except Exception as e:
|
||
logger.error(f"音频处理失败: {str(e)}")
|
||
return JSONResponse(content={
|
||
"success": False,
|
||
"message": f"音频处理失败: {str(e)}"
|
||
}, status_code=500)
|
||
|
||
|
||
async def process_asr(audio_path: str) -> dict:
|
||
"""
|
||
调用ASR服务处理音频文件
|
||
:param audio_path: 音频文件路径
|
||
:return: 识别结果字典
|
||
"""
|
||
try:
|
||
# 上传文件到华为云OBS
|
||
audio_url = upload_file_to_obs(audio_path)
|
||
|
||
# 创建ASR客户端实例
|
||
asr_client = ASRClient()
|
||
|
||
# 设置音频文件URL
|
||
asr_client.file_url = audio_url
|
||
|
||
# 处理ASR任务并获取文本结果
|
||
text_result = asr_client.process_task()
|
||
|
||
# 构建返回结果
|
||
return {
|
||
"text": text_result,
|
||
"confidence": 1.0, # 实际应用中,这里应该从ASR结果中获取置信度
|
||
"audio_duration": os.path.getsize(audio_path) / 1024 / 16 # 估算音频时长
|
||
}
|
||
except Exception as e:
|
||
logger.error(f"ASR处理失败: {str(e)}")
|
||
raise
|
||
|
||
|
||
def upload_file_to_obs(file_path: str) -> str:
|
||
"""
|
||
将本地文件上传到华为云OBS并返回URL
|
||
:param file_path: 本地文件路径
|
||
:return: OBS上的文件URL
|
||
"""
|
||
try:
|
||
# 创建OBS上传器实例
|
||
obs_uploader = ObsUploader()
|
||
|
||
# 生成UUID文件名
|
||
file_uuid = str(uuid.uuid4())
|
||
file_ext = os.path.splitext(file_path)[1].lower()
|
||
# 确保文件扩展名为.wav
|
||
if file_ext != '.wav':
|
||
file_ext = '.wav'
|
||
|
||
# 构建对象键(前缀 + UUID + .wav)
|
||
object_key = f"HuangHai/XueBan/{file_uuid}{file_ext}"
|
||
|
||
# 上传文件
|
||
success, result = obs_uploader.upload_file(object_key, file_path)
|
||
|
||
if success:
|
||
logger.info(f"文件上传成功: {file_path} -> {object_key}")
|
||
# 构建文件URL(假设OBS桶是公开可读的)
|
||
# 实际应用中,URL格式可能需要根据华为云OBS的具体配置进行调整
|
||
from Config.Config import OBS_SERVER, OBS_BUCKET
|
||
file_url = f"https://{OBS_BUCKET}.{OBS_SERVER}/{object_key}"
|
||
return file_url
|
||
else:
|
||
error_msg = f"文件上传失败: {result}"
|
||
logger.error(error_msg)
|
||
raise Exception(error_msg)
|
||
except Exception as e:
|
||
logger.error(f"上传文件到OBS失败: {str(e)}")
|
||
raise
|
||
|
||
|
||
@router.post("/xueban/chat")
|
||
async def chat_with_xueban(request: Request):
|
||
"""
|
||
与学伴大模型聊天的接口
|
||
- 参数: request body 中的 query_text (用户查询文本)
|
||
- 返回: JSON包含聊天响应
|
||
"""
|
||
try:
|
||
# 获取请求体数据
|
||
data = await request.json()
|
||
query_text = data.get("query_text", "")
|
||
|
||
if not query_text.strip():
|
||
return JSONResponse(content={
|
||
"success": False,
|
||
"message": "查询文本不能为空"
|
||
}, status_code=400)
|
||
|
||
# 记录日志
|
||
logger.info(f"接收到学伴聊天请求: {query_text}")
|
||
|
||
# 调用异步接口获取学伴响应
|
||
response_content = []
|
||
async for chunk in get_xueban_response_async(query_text, stream=True):
|
||
response_content.append(chunk)
|
||
|
||
full_response = "".join(response_content)
|
||
|
||
# 返回响应
|
||
return JSONResponse(content={
|
||
"success": True,
|
||
"message": "聊天成功",
|
||
"data": {
|
||
"response": full_response
|
||
}
|
||
})
|
||
|
||
except Exception as e:
|
||
logger.error(f"学伴聊天失败: {str(e)}")
|
||
return JSONResponse(content={
|
||
"success": False,
|
||
"message": f"聊天处理失败: {str(e)}"
|
||
}, status_code=500) |