Spaces:
Running
Running
| from fastapi import FastAPI, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.templating import Jinja2Templates | |
| from app.routers import health, predict, train, ui, monitoring | |
| from mlpipeline.exception import MLPipelineException | |
| from app.utils.metrics import MetricsMiddleware | |
| import uvicorn | |
| app = FastAPI( | |
| title="AutoML MLOps API", | |
| description="AutoML pipeline API for heart disease prediction", | |
| version="1.0.0" | |
| ) | |
| app.mount("/static", StaticFiles(directory="app/static"), name="static") | |
| templates = Jinja2Templates(directory="app/templates") | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Add metrics middleware | |
| app.middleware("http")(MetricsMiddleware()) | |
| app.include_router(health.router) | |
| app.include_router(predict.router) | |
| app.include_router(train.router) | |
| app.include_router(ui.router) | |
| app.include_router(monitoring.router) | |
| async def mlpipeline_exception_handler(request: Request, exc: MLPipelineException): | |
| return JSONResponse( | |
| status_code=500, | |
| content={"error": str(exc)} | |
| ) | |
| async def root(request: Request): | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| if __name__ == "__main__": | |
| uvicorn.run("app.main:app", host="0.0.0.0", port=8000, reload=True) | |