main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import os
  2. # 禁用 FlashAttention,解决沐曦显卡共享内存不足问题
  3. # 必须放在最开头,在任何库导入之前设置
  4. os.environ["PYTORCH_NO_FLASH"] = "1"
  5. os.environ["FLASH_ATTENTION_ENABLED"] = "0"
  6. os.environ["USE_FLASH_ATTENTION"] = "0"
  7. os.environ["TORCH_FLASH_ATTN"] = "0"
  8. from contextlib import asynccontextmanager
  9. from fastapi import Depends, FastAPI
  10. from fastapi.middleware.cors import CORSMiddleware
  11. from app.config import get_settings
  12. settings = get_settings()
  13. @asynccontextmanager
  14. async def lifespan(app: FastAPI):
  15. # 启动时:确保数据目录存在 + 初始化数据库 + 启动 JobQueue
  16. settings.ensure_dirs()
  17. from app.core.db import init_db
  18. await init_db()
  19. from app.core.job_queue import job_queue
  20. from app.services.training_service import update_job_in_db
  21. job_queue.register_callback(update_job_in_db)
  22. await job_queue.start()
  23. yield
  24. # 关闭时:停止 JobQueue
  25. await job_queue.stop()
  26. def create_app() -> FastAPI:
  27. app = FastAPI(
  28. title="四川路桥模型微调平台",
  29. version="0.1.0",
  30. lifespan=lifespan,
  31. )
  32. # CORS 中间件
  33. app.add_middleware(
  34. CORSMiddleware,
  35. allow_origins=settings.backend_cors_origins,
  36. allow_credentials=True,
  37. allow_methods=["*"],
  38. allow_headers=["*"],
  39. )
  40. # 挂载路由
  41. from app.api import models as models_api
  42. from app.api import datasets as datasets_api
  43. from app.api import training as training_api
  44. from app.api import evaluation as evaluation_api
  45. from app.api import deployment as deployment_api
  46. from app.api import inference as inference_api
  47. from app.api import auth as auth_api
  48. from app.api import sample_center as sample_center_api
  49. from app.core.auth import get_current_active_user
  50. # 认证路由(无 prefix,端点自带完整路径)
  51. app.include_router(auth_api.router)
  52. # 已有路由:添加认证依赖保护
  53. app.include_router(
  54. models_api.router, prefix="/api/v1/models", tags=["models"],
  55. dependencies=[Depends(get_current_active_user)],
  56. )
  57. app.include_router(
  58. datasets_api.router, prefix="/api/v1/datasets", tags=["datasets"],
  59. dependencies=[Depends(get_current_active_user)],
  60. )
  61. app.include_router(
  62. training_api.router, prefix="/api/v1/training", tags=["training"],
  63. dependencies=[Depends(get_current_active_user)],
  64. )
  65. app.include_router(
  66. evaluation_api.router, prefix="/api/v1/evaluation", tags=["evaluation"],
  67. dependencies=[Depends(get_current_active_user)],
  68. )
  69. app.include_router(
  70. deployment_api.router, prefix="/api/v1/deployment", tags=["deployment"],
  71. dependencies=[Depends(get_current_active_user)],
  72. )
  73. app.include_router(
  74. inference_api.router, prefix="/api/v1/inference", tags=["inference"],
  75. dependencies=[Depends(get_current_active_user)],
  76. )
  77. app.include_router(
  78. sample_center_api.router, prefix="/api/v1/sample-center", tags=["sample-center"],
  79. dependencies=[Depends(get_current_active_user)],
  80. )
  81. # WebSocket
  82. from app.core.websocket import router as ws_router
  83. app.include_router(ws_router)
  84. @app.get("/health")
  85. async def health_check():
  86. return {"status": "ok", "env": settings.backend_env}
  87. return app
  88. app = create_app()