Download api_server/app/main.py from MoYoYoTech/MoYoYo.tts: direct link, hf CLI and curl.
- Browser
- Download file 3.94 kB
-
https://huggingface.co/MoYoYoTech/MoYoYo.tts/resolve/main/api_server/app/main.py
- Command line
-
hf download hf://MoYoYoTech/MoYoYo.tts/api_server/app/main.py
-
curl -L -o main.py https://huggingface.co/MoYoYoTech/MoYoYo.tts/resolve/main/api_server/app/main.py
3.94 kB
| """ | |
| 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 | |
| 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() | |
| # 检查是否有 recover_pending_tasks 方法 | |
| 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...") | |
| # 创建 FastAPI 应用 | |
| 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", | |
| ) | |
| # 配置 CORS | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # 生产环境应该限制来源 | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # 注册 API 路由 | |
| app.include_router(api_router, prefix=settings.API_V1_PREFIX) | |
| # ============================================================ | |
| # 根路由和健康检查 | |
| # ============================================================ | |
| async def root(): | |
| """ | |
| 根路由 | |
| 返回 API 基本信息 | |
| """ | |
| return { | |
| "name": "GPT-SoVITS Training API", | |
| "version": "1.0.0", | |
| "mode": settings.DEPLOYMENT_MODE, | |
| "docs": "/docs", | |
| "health": "/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)], | |
| ) | |