AkashKumarave commited on
Commit
411c7f2
·
verified ·
1 Parent(s): 249a867

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -2,6 +2,7 @@
2
  from fastapi import FastAPI, UploadFile, File, HTTPException
3
  from fastapi.responses import Response
4
  from fastapi.middleware.cors import CORSMiddleware
 
5
  import cv2
6
  import numpy as np
7
  from PIL import Image
@@ -63,9 +64,10 @@ def download_model():
63
  logger.info("Model already exists at: %s", model_path)
64
  MODEL_DOWNLOADED = True
65
 
66
- @app.on_event("startup")
67
- async def startup_event():
68
- """Run startup tasks like downloading the model."""
 
69
  logger.info("Starting up application...")
70
  try:
71
  download_model()
@@ -73,6 +75,11 @@ async def startup_event():
73
  except Exception as e:
74
  logger.error(f"Startup failed: {e}")
75
  raise
 
 
 
 
 
76
 
77
  def get_many_faces(image):
78
  """Simplified face detection using insightface."""
 
2
  from fastapi import FastAPI, UploadFile, File, HTTPException
3
  from fastapi.responses import Response
4
  from fastapi.middleware.cors import CORSMiddleware
5
+ from contextlib import asynccontextmanager
6
  import cv2
7
  import numpy as np
8
  from PIL import Image
 
64
  logger.info("Model already exists at: %s", model_path)
65
  MODEL_DOWNLOADED = True
66
 
67
+ # Use lifespan event handler instead of on_event
68
+ @asynccontextmanager
69
+ async def lifespan(app: FastAPI):
70
+ # Startup code
71
  logger.info("Starting up application...")
72
  try:
73
  download_model()
 
75
  except Exception as e:
76
  logger.error(f"Startup failed: {e}")
77
  raise
78
+ yield
79
+ # Shutdown code (if any)
80
+ logger.info("Shutting down application...")
81
+
82
+ app.lifespan = lifespan
83
 
84
  def get_many_faces(image):
85
  """Simplified face detection using insightface."""