server.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. FastAPI 服务器
  3. """
  4. from fastapi import FastAPI
  5. from fastapi.middleware.cors import CORSMiddleware
  6. import uvicorn
  7. from finetunex.api.routes import router
  8. app = FastAPI(
  9. title="FineTuneX API",
  10. description="大模型微调服务 API",
  11. version="0.1.0",
  12. )
  13. # CORS 配置
  14. app.add_middleware(
  15. CORSMiddleware,
  16. allow_origins=["*"],
  17. allow_credentials=True,
  18. allow_methods=["*"],
  19. allow_headers=["*"],
  20. )
  21. # 注册路由
  22. app.include_router(router, prefix="/api/v1")
  23. @app.get("/")
  24. async def root():
  25. """根路径"""
  26. return {
  27. "message": "欢迎使用 FineTuneX API",
  28. "version": "0.1.0",
  29. "docs": "/docs",
  30. }
  31. @app.get("/health")
  32. async def health_check():
  33. """健康检查"""
  34. return {"status": "healthy"}
  35. def run_server(host: str = "0.0.0.0", port: int = 8000, reload: bool = True):
  36. """
  37. 运行服务器
  38. Args:
  39. host: 主机地址
  40. port: 端口号
  41. reload: 是否自动重载
  42. """
  43. uvicorn.run(
  44. "finetunex.api.server:app",
  45. host=host,
  46. port=port,
  47. reload=reload,
  48. )