UdasriHasindu commited on
Commit
fe2c376
·
1 Parent(s): d1e7990

add a lifespan to load models before api traffic

Browse files
main.py CHANGED
@@ -1,8 +1,35 @@
1
  from fastapi import FastAPI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  app = FastAPI(
4
  title="Motor Impairment Score API",
5
- description="API for predicting Parkinson's motor impairment from drawings."
 
6
  )
7
 
8
  @app.get("/")
 
1
  from fastapi import FastAPI
2
+ from services import predictor
3
+ from contextlib import asynccontextmanager
4
+
5
+
6
+ @asynccontextmanager
7
+ async def lifespan(app: FastAPI):
8
+
9
+ # Load models on startup
10
+
11
+ print("\n--- SYSTEM BOOT ---")
12
+ print("Pre-loading deep learning models from the Hub into RAM...\n")
13
+
14
+ predictor._get_wave_model()
15
+ predictor._get_spiral_model()
16
+
17
+
18
+ print("Models successfully cached! Server is now ready to accept network traffic.\n")
19
+
20
+ yield
21
+ # Everything before 'yield' happens BEFORE the server accepts traffic.
22
+
23
+ print("\n--- SYSTEM SHUTDOWN ---")
24
+ print("Releasing memory and shutting down...")
25
+
26
+
27
+
28
 
29
  app = FastAPI(
30
  title="Motor Impairment Score API",
31
+ description="API for predicting Parkinson's motor impairment from drawings.",
32
+ lifespan=lifespan
33
  )
34
 
35
  @app.get("/")
services/__init__.py ADDED
File without changes
predictor.py → services/predictor.py RENAMED
File without changes