""" XGBoost Busy Detector — HF Space App (FastAPI) Wraps the EndpointHandler in a FastAPI server for HF Spaces deployment. """ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel from typing import Dict, Optional import os app = FastAPI(title="XGBoost Busy Detector", version="1.0.0") def _cors_origins_from_env() -> list[str]: raw = (os.getenv("ALLOWED_ORIGINS") or "").strip() if not raw: return ["*"] return [o.strip() for o in raw.split(",") if o.strip()] _cors_origins = _cors_origins_from_env() app.add_middleware( CORSMiddleware, allow_origins=_cors_origins, # Browsers reject: Access-Control-Allow-Origin="*" with credentials=true. allow_credentials=("*" not in _cors_origins), allow_methods=["*"], allow_headers=["*"], ) # Load handler on startup from handler import EndpointHandler handler = EndpointHandler(path=".") class PredictRequest(BaseModel): inputs: Dict # { "audio_features": {...}, "text_features": {...} } @app.get("/") async def root(): return { "service": "XGBoost Busy Detector", "version": "1.0.0", "endpoints": ["/predict", "/health"], } @app.get("/health") async def health(): return {"status": "healthy", "model_loaded": handler.model is not None} @app.post("/predict") async def predict(request: PredictRequest): """Run XGBoost inference with evidence accumulation scoring.""" result = handler({"inputs": request.inputs}) return result if __name__ == "__main__": import uvicorn import os port = int(os.environ.get("PORT", 7860)) uvicorn.run(app, host="0.0.0.0", port=port)