'commit'
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from fastapi import APIRouter, Request, File, UploadFile
|
from fastapi import APIRouter, Request, File, UploadFile
|
||||||
@@ -12,8 +13,10 @@ router = APIRouter(prefix="/api", tags=["学伴"])
|
|||||||
# 配置日志
|
# 配置日志
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# 导入学伴工具函数
|
# 导入学伴工具函数、ASR客户端和OBS上传工具
|
||||||
from Util.XueBanUtil import get_xueban_response_async
|
from Util.XueBanUtil import get_xueban_response_async
|
||||||
|
from Util.ASRClient import ASRClient
|
||||||
|
from Util.ObsUtil import ObsUploader
|
||||||
|
|
||||||
|
|
||||||
@router.post("/xueban/upload-audio")
|
@router.post("/xueban/upload-audio")
|
||||||
@@ -39,14 +42,8 @@ async def upload_audio(file: UploadFile = File(...)):
|
|||||||
|
|
||||||
logger.info(f"音频文件已保存至临时目录: {temp_file_path}")
|
logger.info(f"音频文件已保存至临时目录: {temp_file_path}")
|
||||||
|
|
||||||
# 这里应该调用ASR服务进行处理
|
# 调用ASR服务进行处理
|
||||||
# 示例:asr_result = await process_asr(temp_file_path)
|
asr_result = await process_asr(temp_file_path)
|
||||||
# 为了演示,这里返回模拟结果
|
|
||||||
asr_result = {
|
|
||||||
"text": "这是一段测试音频的识别结果",
|
|
||||||
"confidence": 0.95,
|
|
||||||
"audio_duration": len(content) / 1024 / 16 # 估算音频时长
|
|
||||||
}
|
|
||||||
|
|
||||||
# 删除临时文件
|
# 删除临时文件
|
||||||
os.remove(temp_file_path)
|
os.remove(temp_file_path)
|
||||||
@@ -66,17 +63,74 @@ async def upload_audio(file: UploadFile = File(...)):
|
|||||||
"message": f"音频处理失败: {str(e)}"
|
"message": f"音频处理失败: {str(e)}"
|
||||||
}, status_code=500)
|
}, status_code=500)
|
||||||
|
|
||||||
# 实际应用中,您需要实现ASR处理函数
|
|
||||||
async def process_asr(audio_path: str) -> dict:
|
async def process_asr(audio_path: str) -> dict:
|
||||||
"""
|
"""
|
||||||
调用ASR服务处理音频文件
|
调用ASR服务处理音频文件
|
||||||
:param audio_path: 音频文件路径
|
:param audio_path: 音频文件路径
|
||||||
:return: 识别结果字典
|
:return: 识别结果字典
|
||||||
"""
|
"""
|
||||||
# 这里应该集成实际的ASR服务
|
try:
|
||||||
# 例如百度AI、阿里云、讯飞等ASR服务
|
# 上传文件到华为云OBS
|
||||||
# 或者本地的ASR模型
|
audio_url = upload_file_to_obs(audio_path)
|
||||||
pass
|
|
||||||
|
# 创建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")
|
@router.post("/xueban/chat")
|
||||||
|
Reference in New Issue
Block a user