s4fifo-api / main.py
hxia7's picture
Deploy S4-FIFO FastAPI artifact
2767c41 verified
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.",
)
@app.get("/")
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",
},
}
@app.get("/health")
def health() -> dict[str, str]:
return {"status": "ok"}
@app.get("/metadata")
def metadata() -> dict[str, Any]:
return get_metadata()
@app.post("/predict")
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)