Spaces:
Sleeping
Sleeping
File size: 1,754 Bytes
634310a 2eff695 634310a 2eff695 634310a 2eff695 634310a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | """
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)
|