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

15 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from contextlib import asynccontextmanager 1abcde

2 

3from fastapi import FastAPI 1abcde

4 

5 

6def fake_answer_to_everything_ml_model(x: float): 1abcde

7 return x * 42 1abcde

8 

9 

10ml_models = {} 1abcde

11 

12 

13@asynccontextmanager 1abcde

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

15 # Load the ML model 

16 ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model 1abcde

17 yield 1abcde

18 # Clean up the ML models and release the resources 

19 ml_models.clear() 1abcde

20 

21 

22app = FastAPI(lifespan=lifespan) 1abcde

23 

24 

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

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

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

28 return {"result": result} 1abcde