import asyncio from concurrent.futures import ThreadPoolExecutor from fastapi import FastAPI, UploadFile, File from cnn_pipeline.pipeline import classify_tumor from contextlib import asynccontextmanager async def lifespan(app: FastAPI): yield # The app is running at this point executor.shutdown() app = FastAPI(title="Brain Tumor CNN Service",lifespan=lifespan) # its okay to use thead pool (prototype level) as pytorch surpasses GIL executor=ThreadPoolExecutor(max_workers=2) @app.post("/classify") async def classify(file: UploadFile = File(...)): # read the fiel content image_bytes = await file.read() # get the event Loop loop = asyncio.get_running_loop() result = await loop.run_in_executor(executor, classify_tumor, image_bytes) return result