|
|
""" |
|
|
FastAPI 应用入口 |
|
|
|
|
|
GPT-SoVITS 音色训练 HTTP API 服务 |
|
|
|
|
|
启动方式: |
|
|
uvicorn api_server.app.main:app --host 0.0.0.0 --port 8000 --reload |
|
|
""" |
|
|
|
|
|
from contextlib import asynccontextmanager |
|
|
from typing import AsyncGenerator |
|
|
|
|
|
from fastapi import FastAPI |
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
|
|
from project_config import settings, ensure_data_dirs |
|
|
from .api.v1.router import api_router |
|
|
|
|
|
|
|
|
@asynccontextmanager |
|
|
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: |
|
|
""" |
|
|
应用生命周期管理 |
|
|
|
|
|
启动时: |
|
|
- 确保数据目录存在 |
|
|
- 恢复中断的任务(可选) |
|
|
|
|
|
关闭时: |
|
|
- 清理资源 |
|
|
""" |
|
|
|
|
|
print(f"Starting GPT-SoVITS Training API in {settings.DEPLOYMENT_MODE.upper()} mode") |
|
|
print(f" Project Root: {settings.PROJECT_ROOT}") |
|
|
print(f" Data Directory: {settings.DATA_DIR}") |
|
|
print(f" SQLite Path: {settings.SQLITE_PATH}") |
|
|
|
|
|
|
|
|
ensure_data_dirs() |
|
|
|
|
|
|
|
|
if settings.DEPLOYMENT_MODE == "local": |
|
|
try: |
|
|
from .core.adapters import get_task_queue_adapter |
|
|
queue = get_task_queue_adapter() |
|
|
|
|
|
if hasattr(queue, 'recover_pending_tasks'): |
|
|
count = await queue.recover_pending_tasks() |
|
|
if count > 0: |
|
|
print(f" Recovered {count} pending tasks") |
|
|
except Exception as e: |
|
|
print(f" Warning: Failed to recover tasks: {e}") |
|
|
|
|
|
print(" API Server ready!") |
|
|
print(f" Docs: http://{settings.API_HOST}:{settings.API_PORT}/docs") |
|
|
|
|
|
yield |
|
|
|
|
|
|
|
|
print("Shutting down GPT-SoVITS Training API...") |
|
|
|
|
|
|
|
|
|
|
|
app = FastAPI( |
|
|
title="GPT-SoVITS Training API", |
|
|
description=""" |
|
|
GPT-SoVITS 音色训练 HTTP API 服务 |
|
|
|
|
|
## 功能概述 |
|
|
|
|
|
提供两种训练模式: |
|
|
|
|
|
### Quick Mode(小白用户) |
|
|
- 上传音频即可训练,系统自动配置所有参数 |
|
|
- 适合个人开发者、快速验证 |
|
|
|
|
|
### Advanced Mode(专家用户) |
|
|
- 分阶段控制训练流程 |
|
|
- 精细调整每个阶段的参数 |
|
|
- 适合需要深度定制的用户 |
|
|
|
|
|
## API 分组 |
|
|
|
|
|
- **Quick Mode - 任务管理**: `/api/v1/tasks` |
|
|
- **Advanced Mode - 实验管理**: `/api/v1/experiments` |
|
|
- **文件管理**: `/api/v1/files` |
|
|
- **阶段模板**: `/api/v1/stages` |
|
|
""", |
|
|
version="1.0.0", |
|
|
lifespan=lifespan, |
|
|
docs_url="/docs", |
|
|
redoc_url="/redoc", |
|
|
openapi_url="/openapi.json", |
|
|
) |
|
|
|
|
|
|
|
|
app.add_middleware( |
|
|
CORSMiddleware, |
|
|
allow_origins=["*"], |
|
|
allow_credentials=True, |
|
|
allow_methods=["*"], |
|
|
allow_headers=["*"], |
|
|
) |
|
|
|
|
|
|
|
|
app.include_router(api_router, prefix=settings.API_V1_PREFIX) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/", tags=["Root"]) |
|
|
async def root(): |
|
|
""" |
|
|
根路由 |
|
|
|
|
|
返回 API 基本信息 |
|
|
""" |
|
|
return { |
|
|
"name": "GPT-SoVITS Training API", |
|
|
"version": "1.0.0", |
|
|
"mode": settings.DEPLOYMENT_MODE, |
|
|
"docs": "/docs", |
|
|
"health": "/health", |
|
|
} |
|
|
|
|
|
|
|
|
@app.get("/health", tags=["Health"]) |
|
|
async def health_check(): |
|
|
""" |
|
|
健康检查端点 |
|
|
|
|
|
用于容器编排和负载均衡器健康检查 |
|
|
""" |
|
|
return { |
|
|
"status": "healthy", |
|
|
"mode": settings.DEPLOYMENT_MODE, |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
import uvicorn |
|
|
|
|
|
uvicorn.run( |
|
|
"api_server.app.main:app", |
|
|
host=settings.API_HOST, |
|
|
port=settings.API_PORT, |
|
|
reload=True, |
|
|
reload_dirs=[str(settings.API_SERVER_ROOT)], |
|
|
) |
|
|
|