From 0bde5d4a8112e29541e44d4cf889a0626186582a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8F=9C=E5=B7=A5=E5=8E=821145=E5=8F=B7=E5=91=98?= =?UTF-8?q?=E5=B7=A5?= <114749500+baicai-1145@users.noreply.github.com> Date: Sat, 11 Oct 2025 11:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=81=A5=E5=BA=B7=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E7=AB=AF=E7=82=B9=E5=92=8C=E9=9F=B3=E9=A2=91=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index 4afae608..2b69c1c9 100644 --- a/api.py +++ b/api.py @@ -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()