from contextlib import asynccontextmanager from fastapi import FastAPI from loguru import logger @asynccontextmanager async def lifespan(app: FastAPI): # Load the ML model from app.ner.services.ner import NER ner: NER = NER() ner.load_model() app.state.ner = ner logger.info("NER model loaded successfully.") yield # Clean up the ML models and release the resources logger.info("Cleaning up NER model...") del app.state.ner