57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
|
|
import logging
|
||
|
|
import os
|
||
|
|
|
||
|
|
import psutil
|
||
|
|
|
||
|
|
from fastapi import APIRouter
|
||
|
|
|
||
|
|
from app.schemas.system import GpuDevice, HealthResponse, SystemInfoResponse
|
||
|
|
from app.services import facefusion_bridge
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
router = APIRouter(prefix='/api/v1', tags=['system'])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get('/health', response_model=HealthResponse)
|
||
|
|
async def health_check():
|
||
|
|
return HealthResponse()
|
||
|
|
|
||
|
|
|
||
|
|
@router.get('/system', response_model=SystemInfoResponse)
|
||
|
|
async def system_info():
|
||
|
|
providers = facefusion_bridge.get_execution_providers()
|
||
|
|
gpu_devices = _detect_gpu_devices()
|
||
|
|
mem = psutil.virtual_memory()
|
||
|
|
|
||
|
|
return SystemInfoResponse(
|
||
|
|
execution_providers=providers,
|
||
|
|
gpu_devices=gpu_devices,
|
||
|
|
cpu_count=os.cpu_count(),
|
||
|
|
memory_total=mem.total,
|
||
|
|
memory_available=mem.available,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def _detect_gpu_devices():
|
||
|
|
devices = []
|
||
|
|
try:
|
||
|
|
import subprocess
|
||
|
|
result = subprocess.run(
|
||
|
|
['nvidia-smi', '--query-gpu=index,name,memory.total,memory.used', '--format=csv,noheader,nounits'],
|
||
|
|
capture_output=True, text=True, timeout=5,
|
||
|
|
)
|
||
|
|
if result.returncode == 0:
|
||
|
|
for line in result.stdout.strip().split('\n'):
|
||
|
|
parts = [p.strip() for p in line.split(',')]
|
||
|
|
if len(parts) >= 4:
|
||
|
|
devices.append(GpuDevice(
|
||
|
|
id=int(parts[0]),
|
||
|
|
name=parts[1],
|
||
|
|
memory_total=int(float(parts[2])) * 1024 * 1024,
|
||
|
|
memory_used=int(float(parts[3])) * 1024 * 1024,
|
||
|
|
))
|
||
|
|
except (FileNotFoundError, subprocess.TimeoutExpired):
|
||
|
|
pass
|
||
|
|
return devices
|