-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (49 loc) · 2.24 KB
/
Copy pathmain.py
File metadata and controls
64 lines (49 loc) · 2.24 KB
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
"""
ML Platform API — FastAPI application entry point.
Registers all routers and creates required directories on startup.
"""
from __future__ import annotations
import logging
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from config import settings
from database import init_db
from api import ingest, features, train, registry, profiles
from utils.errors import MLPlatformError
# ── Logging setup ─────────────────────────────────────────────────────
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)-8s | %(name)s | %(message)s",
)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Startup: init DB + create directories."""
init_db()
for d in [settings.ARTIFACTS_DIR, settings.DATASETS_DIR, settings.LOGS_DIR]:
os.makedirs(d, exist_ok=True)
logger.info("ML Platform started — DB initialised, dirs ready.")
yield
logger.info("ML Platform shutting down.")
app = FastAPI(
title="ML Platform API",
version="1.0.0",
description="Production-grade Industrial ML Platform for predictive maintenance.",
lifespan=lifespan,
)
# ── Exception handler for structured errors ────────────────────────────
@app.exception_handler(MLPlatformError)
async def ml_error_handler(request: Request, exc: MLPlatformError):
return JSONResponse(status_code=exc.status_code, content=exc.detail)
# ── Register routers ──────────────────────────────────────────────────
app.include_router(ingest.router, prefix="/v1", tags=["Ingest"])
app.include_router(features.router, prefix="/v1", tags=["Features"])
app.include_router(train.router, prefix="/v1", tags=["Train"])
app.include_router(registry.router, prefix="/v1", tags=["Registry"])
app.include_router(profiles.router, prefix="/v1", tags=["Profiles"])
@app.get("/health")
def health():
"""Simple health check."""
return {"status": "ok"}