44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from starlette.datastructures import State
|
|
|
|
from backend.routes.ml import init_ml_routes
|
|
from app.ml.registry.model_registry import ModelRegistry
|
|
from app.ml.training import TrainingPipeline
|
|
from app.ml.feature_store import FeatureStore, FeatureVector
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(application: FastAPI) -> AsyncIterator[None]:
|
|
application.state.registry = ModelRegistry(application.state.model_store)
|
|
_seed_default_model(application.state)
|
|
yield
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
application = FastAPI(title="SillyHome Next ML", lifespan=lifespan)
|
|
init_ml_routes(application)
|
|
return application
|
|
|
|
|
|
def _seed_default_model(state: State) -> None:
|
|
registry = getattr(state, "registry", None)
|
|
if registry is None:
|
|
registry = ModelRegistry(".model_store")
|
|
state.registry = registry
|
|
|
|
if list(registry.list_models()):
|
|
return
|
|
|
|
store = FeatureStore()
|
|
store.add(FeatureVector(sensor_id="sensor.front_door", values={"contact": 1.0}))
|
|
store.add(FeatureVector(sensor_id="sensor.living_room", values={"temperature": 21.0}))
|
|
pipeline = TrainingPipeline(store)
|
|
artifact = pipeline.run("default")
|
|
registry.register(artifact)
|
|
|
|
|
|
app = create_app()
|