load the ml model clean up the ml models and release the resources load the ml model clean up the ml models and release the resources
from contextlib import asynccontextmanager

from fastapi import FastAPI


def fake_answer_to_everything_ml_model(x: float):
    return x * 42


ml_models = {}


@asynccontextmanager
async def lifespan(app: FastAPI):
    
    ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
    yield
    
    ml_models.clear()


app = FastAPI(lifespan=lifespan)


@app.get("/predict")
async def predict(x: float):
    result = ml_models["answer_to_everything"](x)
    return {"result": result}