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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-05-05 00:03 +0000

1from contextlib import asynccontextmanager 1abcdef

2 

3from fastapi import FastAPI 1abcdef

4 

5 

6def fake_answer_to_everything_ml_model(x: float): 1abcdef

7 return x * 42 1ghijkl

8 

9 

10ml_models = {} 1abcdef

11 

12 

13@asynccontextmanager 1abcdef

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

15 # Load the ML model 

16 ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model 1gmhniojpkqlr

17 yield 1gmhniojpkqlr

18 # Clean up the ML models and release the resources 

19 ml_models.clear() 1gmhniojpkqlr

20 

21 

22app = FastAPI(lifespan=lifespan) 1abcdef

23 

24 

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

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

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

28 return {"result": result} 1ghijkl