添加健康检查端点和音频上传功能

This commit is contained in:
白菜工厂1145号员工 2025-10-11 11:03:35 +08:00
parent 338498ad68
commit 0bde5d4a81

55
api.py
View File

@ -157,7 +157,7 @@ import torch
import torchaudio
import librosa
import soundfile as sf
from fastapi import FastAPI, Request, Query
from fastapi import FastAPI, Request, Query, UploadFile, File
from fastapi.responses import StreamingResponse, JSONResponse
import base64
import io
@ -1429,11 +1429,64 @@ async def control(request: Request):
return handle_control(json_post_raw.get("command"))
@app.get("/health")
async def health_check():
"""
健康检查端点
"""
return JSONResponse({
"code": 0,
"message": "API is running",
"status": "ok"
})
@app.get("/control")
async def control(command: str = None):
return handle_control(command)
@app.post("/upload_audio")
async def upload_audio(file: UploadFile = File(...)):
"""
上传参考音频文件
返回服务器上的文件路径
"""
try:
import os
import uuid
from pathlib import Path
# 创建上传目录
upload_dir = Path("uploaded_audio")
upload_dir.mkdir(exist_ok=True)
# 生成唯一文件名
file_ext = os.path.splitext(file.filename)[1] or ".wav"
unique_filename = f"{uuid.uuid4()}{file_ext}"
file_path = upload_dir / unique_filename
# 保存文件
content = await file.read()
with open(file_path, "wb") as f:
f.write(content)
# 返回相对路径
return JSONResponse({
"code": 0,
"message": "上传成功",
"data": {
"path": str(file_path),
"filename": unique_filename
}
})
except Exception as e:
return JSONResponse({
"code": 400,
"message": f"上传失败: {str(e)}"
}, status_code=400)
@app.post("/change_refer")
async def change_refer(request: Request):
json_post_raw = await request.json()