forked from llm-d-incubation/llm-d-planner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
88 lines (70 loc) · 2.38 KB
/
app.py
File metadata and controls
88 lines (70 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""FastAPI application factory for NeuralNav API."""
import asyncio
import logging
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from planner.api.routes import (
configuration_router,
database_router,
health_router,
intent_router,
recommendation_router,
reference_data_router,
specification_router,
)
# Configure logging
debug_mode = os.getenv("NEURALNAV_DEBUG", "false").lower() == "true"
log_level = logging.DEBUG if debug_mode else logging.INFO
logging.basicConfig(
level=log_level,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Initialize all singletons on app.state during startup."""
from planner.api.dependencies import init_app_state
logger.info("Initializing app state...")
try:
await asyncio.to_thread(init_app_state, app)
except Exception:
logger.exception("App state initialization failed during startup")
raise
# Create asyncio.Lock in the event loop thread (not in the worker thread
# where init_app_state runs) to avoid cross-loop binding issues.
app.state.cluster_manager_lock = asyncio.Lock()
yield
def create_app() -> FastAPI:
"""Create and configure the FastAPI application."""
app = FastAPI(
title="NeuralNav API",
description="API for LLM deployment recommendations",
version="0.1.0",
lifespan=lifespan,
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, specify actual origins
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include all routers
app.include_router(health_router)
app.include_router(intent_router)
app.include_router(specification_router)
app.include_router(recommendation_router)
app.include_router(configuration_router)
app.include_router(reference_data_router)
app.include_router(database_router)
logger.info(f"NeuralNav API starting with log level: {logging.getLevelName(log_level)}")
return app
# Create the app instance for uvicorn
app = create_app()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)