Coverage for docs_src/events/tutorial003.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-12-04 08:29 +0000

1from contextlib import asynccontextmanager 1abcdefg

2 

3from fastapi import FastAPI 1abcdefg

4 

5 

6def fake_answer_to_everything_ml_model(x: float): 1abcdefg

7 return x * 42 1hijklmn

8 

9 

10ml_models = {} 1abcdefg

11 

12 

13@asynccontextmanager 1abcdefg

14async def lifespan(app: FastAPI): 1abcdefg

15 # Load the ML model 

16 ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model 1hoipjqkrlsmtnu

17 yield 1hoipjqkrlsmtnu

18 # Clean up the ML models and release the resources 

19 ml_models.clear() 1hoipjqkrlsmtnu

20 

21 

22app = FastAPI(lifespan=lifespan) 1abcdefg

23 

24 

25@app.get("/predict") 1abcdefg

26async def predict(x: float): 1abcdefg

27 result = ml_models["answer_to_everything"](x) 1hijklmn

28 return {"result": result} 1hijklmn