gpu.py 679 B

12345678910111213141516171819202122232425
  1. import logging
  2. from fastapi import APIRouter, HTTPException
  3. import service.pygpu as pygpu
  4. logger = logging.getLogger(__name__)
  5. # 创建路由对象,可以统一设置前缀 (prefix) 和 标签 (tags)
  6. router = APIRouter(
  7. prefix="/api1/gpu",
  8. tags=["gpu"]
  9. )
  10. @router.get("/info")
  11. async def gpu_info():
  12. try:
  13. gpu_result = pygpu.get_gpu_memory_info(0)
  14. torch_result = pygpu.get_torch_memory_usage()
  15. result = pygpu.get_ollama_resource()
  16. return {
  17. "gpu": gpu_result,
  18. "torch": torch_result,
  19. "ollama": result,
  20. }
  21. except Exception as e:
  22. raise HTTPException(status_code=500, detail=str(e))