main.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.core.auth import get_current_active_user
  49. # 认证路由(无 prefix,端点自带完整路径)
  50. app.include_router(auth_api.router)
  51. # 已有路由:添加认证依赖保护
  52. app.include_router(
  53. models_api.router, prefix="/api/v1/models", tags=["models"],
  54. dependencies=[Depends(get_current_active_user)],
  55. )
  56. app.include_router(
  57. datasets_api.router, prefix="/api/v1/datasets", tags=["datasets"],
  58. dependencies=[Depends(get_current_active_user)],
  59. )
  60. app.include_router(
  61. training_api.router, prefix="/api/v1/training", tags=["training"],
  62. dependencies=[Depends(get_current_active_user)],
  63. )
  64. app.include_router(
  65. evaluation_api.router, prefix="/api/v1/evaluation", tags=["evaluation"],
  66. dependencies=[Depends(get_current_active_user)],
  67. )
  68. app.include_router(
  69. deployment_api.router, prefix="/api/v1/deployment", tags=["deployment"],
  70. dependencies=[Depends(get_current_active_user)],
  71. )
  72. app.include_router(
  73. inference_api.router, prefix="/api/v1/inference", tags=["inference"],
  74. dependencies=[Depends(get_current_active_user)],
  75. )
  76. # WebSocket
  77. from app.core.websocket import router as ws_router
  78. app.include_router(ws_router)
  79. @app.get("/health")
  80. async def health_check():
  81. return {"status": "ok", "env": settings.backend_env}
  82. return app
  83. app = create_app()