OpenEEGBench / backend /app /api /endpoints /benchmarks.py
bruAristimunha's picture
Add HF eval-results benchmark registry and API endpoint
6f5cc9b
raw
history blame
1.16 kB
from dataclasses import asdict
from fastapi import APIRouter, HTTPException
from typing import Any, Dict, List
from app.config.benchmarks import EEG_BENCHMARKS
router = APIRouter()
# Fields that are internal implementation details, not exposed via API
_INTERNAL_FIELDS = {"accuracy_field"}
def _benchmark_to_dict(key: str, benchmark) -> Dict[str, Any]:
"""Convert a BenchmarkInfo dataclass to a JSON-serializable dict."""
d = asdict(benchmark)
for field in _INTERNAL_FIELDS:
d.pop(field, None)
d["key"] = key
return d
@router.get("")
async def list_benchmarks() -> List[Dict[str, Any]]:
"""Return all registered EEG benchmarks with metadata."""
return [_benchmark_to_dict(k, b) for k, b in EEG_BENCHMARKS.items()]
@router.get("/{key}")
async def get_benchmark(key: str) -> Dict[str, Any]:
"""Return a single benchmark's details by key."""
benchmark = EEG_BENCHMARKS.get(key)
if not benchmark:
raise HTTPException(
status_code=404,
detail=f"Benchmark '{key}' not found. Available: {list(EEG_BENCHMARKS.keys())}",
)
return _benchmark_to_dict(key, benchmark)