rajux75 commited on
Commit
2ce53e9
·
verified ·
1 Parent(s): 755cb92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -24,24 +24,30 @@ limiter = Limiter(key_func=get_remote_address, default_limits=[config.RATE_LIMIT
24
  # --- Lifespan Management (Model Loading/Unloading) ---
25
  @asynccontextmanager
26
  async def lifespan(app: FastAPI):
27
- # Startup: Load models
 
 
 
 
28
  logger.info("Application startup: Loading models...")
29
  try:
30
- generation.load_models()
31
- app.state.limiter = limiter # Add limiter to app state for dependency injection
32
  logger.info("Models loaded successfully.")
33
  except Exception as e:
34
- logger.error(f"FATAL: Model loading failed during startup: {e}", exc_info=True)
35
- # Optionally, prevent app start or handle gracefully
36
- # For now, we let it start but endpoints needing models will fail.
37
- yield
 
38
  # Shutdown: Clean up (optional, less critical in stateless services)
39
  logger.info("Application shutdown.")
40
- generation.model_cache.clear()
 
41
  # Add any other cleanup here
42
  logger.info("Resources cleaned up.")
43
 
44
 
 
45
  # --- FastAPI App Initialization ---
46
  app = FastAPI(
47
  title="AI Content Generation API",
@@ -51,7 +57,7 @@ app = FastAPI(
51
  )
52
 
53
  # --- Middleware ---
54
- # Rate Limiting Middleware
55
  app.add_middleware(SlowAPIMiddleware)
56
 
57
  # CORS Middleware (Uncomment and configure if needed for browser-based clients)
 
24
  # --- Lifespan Management (Model Loading/Unloading) ---
25
  @asynccontextmanager
26
  async def lifespan(app: FastAPI):
27
+ # Startup: Assign essential state FIRST
28
+ app.state.limiter = limiter # <<<--- MOVE ASSIGNMENT HERE
29
+ logger.info("Rate limiter assigned to app state.")
30
+
31
+ # Now attempt to load models
32
  logger.info("Application startup: Loading models...")
33
  try:
34
+ generation.load_models() # This might still fail
 
35
  logger.info("Models loaded successfully.")
36
  except Exception as e:
37
+ # Log the error, but the app continues. Endpoints needing models might fail.
38
+ logger.error(f"ERROR: Model loading failed during startup: {e}", exc_info=True)
39
+
40
+ yield # Application is now ready to serve requests
41
+
42
  # Shutdown: Clean up (optional, less critical in stateless services)
43
  logger.info("Application shutdown.")
44
+ if "generation" in globals() and hasattr(generation, 'model_cache'):
45
+ generation.model_cache.clear()
46
  # Add any other cleanup here
47
  logger.info("Resources cleaned up.")
48
 
49
 
50
+
51
  # --- FastAPI App Initialization ---
52
  app = FastAPI(
53
  title="AI Content Generation API",
 
57
  )
58
 
59
  # --- Middleware ---
60
+ # Rate Limiting Middleware - Now it can safely access app.state.limiter
61
  app.add_middleware(SlowAPIMiddleware)
62
 
63
  # CORS Middleware (Uncomment and configure if needed for browser-based clients)