Spaces:
Sleeping
Sleeping
| from typing import Any | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, Field | |
| from predictor import N_FEATURES, get_metadata, predict_from_features | |
| app = FastAPI( | |
| title="S4-FIFO Parameter Prediction API", | |
| version="0.1.0", | |
| description="Online control-plane inference artifact for S4-FIFO parameter selection.", | |
| ) | |
| class PredictRequest(BaseModel): | |
| features: list[float] = Field( | |
| ..., | |
| description="73-dimensional cache-level feature vector in the training feature order.", | |
| min_length=N_FEATURES, | |
| max_length=N_FEATURES, | |
| ) | |
| top_k: int = Field( | |
| default=3, | |
| ge=1, | |
| le=18, | |
| description="Number of probability/risk-ranked candidate configurations to return.", | |
| ) | |
| def root() -> dict[str, Any]: | |
| return { | |
| "service": "S4-FIFO Parameter Prediction API", | |
| "version": app.version, | |
| "endpoints": { | |
| "health": "/health", | |
| "metadata": "/metadata", | |
| "predict": "POST /predict", | |
| "docs": "/docs", | |
| }, | |
| } | |
| def health() -> dict[str, str]: | |
| return {"status": "ok"} | |
| def metadata() -> dict[str, Any]: | |
| return get_metadata() | |
| def predict(req: PredictRequest) -> dict[str, Any]: | |
| if len(req.features) != N_FEATURES: | |
| raise HTTPException( | |
| status_code=400, | |
| detail=f"Expected {N_FEATURES} features, got {len(req.features)}", | |
| ) | |
| return predict_from_features(req.features, top_k=req.top_k) | |