CWM-Coder724 commited on
Commit ·
8938dd8
1
Parent(s): d75262b
Make model warmup non-blocking to prevent ZeroGPU startup timeout
Browse files- backend/main.py +17 -14
backend/main.py
CHANGED
|
@@ -75,37 +75,40 @@ manager = ConnectionManager()
|
|
| 75 |
|
| 76 |
# ─── Application lifespan ────────────────────────────────────────────────────
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
"""
|
| 81 |
-
Load all NLP models ONCE at startup (PDF Section 14 + 22).
|
| 82 |
-
Never reloaded per match.
|
| 83 |
-
"""
|
| 84 |
-
logger.info("=== Clash of Minds starting — loading NLP models ===")
|
| 85 |
-
|
| 86 |
-
# Load models with individual error handling (PDF Section 15.2)
|
| 87 |
try:
|
| 88 |
from pipeline_a_embedding import load_model as load_embedding
|
| 89 |
-
|
| 90 |
except Exception as e:
|
| 91 |
logger.error(f"[Startup] Embedding model load failed: {e}")
|
| 92 |
|
| 93 |
try:
|
| 94 |
from pipeline_c_retrieval import load_cross_encoder
|
| 95 |
-
|
| 96 |
except Exception as e:
|
| 97 |
logger.error(f"[Startup] Cross-encoder load failed: {e}")
|
| 98 |
|
| 99 |
try:
|
| 100 |
from pipeline_d_nli import load_nli_model
|
| 101 |
-
|
| 102 |
except Exception as e:
|
| 103 |
logger.error(f"[Startup] NLI model load failed: {e}")
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
# Start background cleanup task
|
| 106 |
cleanup_task = asyncio.create_task(_periodic_cleanup())
|
| 107 |
-
|
| 108 |
-
logger.info("=== All models loaded — ready for debates ===")
|
| 109 |
yield
|
| 110 |
|
| 111 |
cleanup_task.cancel()
|
|
|
|
| 75 |
|
| 76 |
# ─── Application lifespan ────────────────────────────────────────────────────
|
| 77 |
|
| 78 |
+
async def _warmup_models():
|
| 79 |
+
"""Warm up models in background thread without blocking server startup."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
try:
|
| 81 |
from pipeline_a_embedding import load_model as load_embedding
|
| 82 |
+
await asyncio.to_thread(load_embedding)
|
| 83 |
except Exception as e:
|
| 84 |
logger.error(f"[Startup] Embedding model load failed: {e}")
|
| 85 |
|
| 86 |
try:
|
| 87 |
from pipeline_c_retrieval import load_cross_encoder
|
| 88 |
+
await asyncio.to_thread(load_cross_encoder)
|
| 89 |
except Exception as e:
|
| 90 |
logger.error(f"[Startup] Cross-encoder load failed: {e}")
|
| 91 |
|
| 92 |
try:
|
| 93 |
from pipeline_d_nli import load_nli_model
|
| 94 |
+
await asyncio.to_thread(load_nli_model)
|
| 95 |
except Exception as e:
|
| 96 |
logger.error(f"[Startup] NLI model load failed: {e}")
|
| 97 |
|
| 98 |
+
logger.info("=== All models loaded — ready for debates ===")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
@asynccontextmanager
|
| 102 |
+
async def lifespan(app: FastAPI):
|
| 103 |
+
"""
|
| 104 |
+
Non-blocking server startup.
|
| 105 |
+
Models warm up in background thread; API and WebSockets accept connections immediately.
|
| 106 |
+
"""
|
| 107 |
+
logger.info("=== Clash of Minds starting — launching background model warmup ===")
|
| 108 |
+
asyncio.create_task(_warmup_models())
|
| 109 |
+
|
| 110 |
# Start background cleanup task
|
| 111 |
cleanup_task = asyncio.create_task(_periodic_cleanup())
|
|
|
|
|
|
|
| 112 |
yield
|
| 113 |
|
| 114 |
cleanup_task.cancel()
|