|
10 | 10 | from contextlib import asynccontextmanager |
11 | 11 |
|
12 | 12 | from backend.database import init_db, LlamaVersion |
13 | | -from backend.routes import models, llama_versions, status, gpu_info, llama_version_manager |
| 13 | +from backend.routes import models, llama_versions, status, gpu_info, llama_version_manager, lmdeploy |
14 | 14 | from backend.websocket_manager import websocket_manager |
15 | 15 | from backend.huggingface import set_huggingface_token |
16 | 16 | from backend.unified_monitor import unified_monitor |
|
24 | 24 | def ensure_data_directories(): |
25 | 25 | """Ensure data directories exist and are writable""" |
26 | 26 | data_dir = "/app/data" |
27 | | - subdirs = ["models", "configs", "logs", "llama-cpp"] |
| 27 | + subdirs = ["models", "configs", "logs", "llama-cpp", "lmdeploy"] |
28 | 28 |
|
29 | 29 | try: |
30 | 30 | # Ensure main data directory exists |
@@ -127,56 +127,56 @@ async def lifespan(app: FastAPI): |
127 | 127 | global llama_swap_manager |
128 | 128 |
|
129 | 129 | # Startup |
130 | | - # Ensure data directories exist and are writable |
131 | 130 | ensure_data_directories() |
132 | | - |
133 | 131 | await init_db() |
134 | | - |
135 | | - # Initialize configuration manager and update llama-swap config |
136 | | - |
137 | | - # Initialize Hugging Face API key from environment variable if available |
| 132 | + |
138 | 133 | huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY') |
139 | 134 | if huggingface_api_key: |
140 | 135 | set_huggingface_token(huggingface_api_key) |
141 | 136 | logger.info("HuggingFace API key loaded from environment variable") |
142 | | - |
143 | | - # Initialize and start llama-swap |
| 137 | + |
144 | 138 | from backend.llama_swap_manager import get_llama_swap_manager |
145 | 139 | llama_swap_manager = get_llama_swap_manager() |
146 | | - |
147 | | - try: |
148 | | - await llama_swap_manager.start_proxy() |
149 | | - logger.info("llama-swap proxy started on port 2000") |
150 | | - except Exception as e: |
151 | | - logger.error(f"Failed to start llama-swap: {e}") |
152 | | - logger.warning("Multi-model serving unavailable") |
153 | | - |
154 | | - # Clean stale database state (since llama-swap was not running) |
155 | | - from backend.database import SessionLocal, RunningInstance, Model |
| 140 | + |
| 141 | + from backend.database import SessionLocal, LlamaVersion, RunningInstance, Model |
| 142 | + session = SessionLocal() |
| 143 | + active_version = session.query(LlamaVersion).filter(LlamaVersion.is_active == True).first() |
| 144 | + session.close() |
| 145 | + |
| 146 | + if active_version and active_version.binary_path: |
| 147 | + try: |
| 148 | + await llama_swap_manager.start_proxy() |
| 149 | + logger.info("llama-swap proxy started on port 2000") |
| 150 | + except Exception as e: |
| 151 | + logger.error(f"Failed to start llama-swap: {e}") |
| 152 | + logger.warning("Multi-model serving unavailable") |
| 153 | + else: |
| 154 | + logger.warning( |
| 155 | + "Skipping llama-swap start: no active llama.cpp version found. " |
| 156 | + "Install or activate a llama.cpp build to enable multi-model serving." |
| 157 | + ) |
| 158 | + |
156 | 159 | db = SessionLocal() |
157 | 160 | try: |
158 | 161 | stale_instances = db.query(RunningInstance).all() |
159 | 162 | if stale_instances: |
160 | 163 | logger.info(f"Cleaning {len(stale_instances)} stale instances") |
161 | 164 | for instance in stale_instances: |
162 | | - # Update model status |
163 | 165 | model = db.query(Model).filter(Model.id == instance.model_id).first() |
164 | 166 | if model: |
165 | 167 | model.is_active = False |
166 | 168 | db.delete(instance) |
167 | 169 | db.commit() |
168 | 170 | finally: |
169 | 171 | db.close() |
170 | | - |
171 | | - # Register all downloaded models with llama-swap |
| 172 | + |
172 | 173 | try: |
173 | 174 | await register_all_models_with_llama_swap() |
174 | 175 | except Exception as e: |
175 | 176 | logger.error(f"Failed to register models with llama-swap: {e}") |
176 | | - |
177 | | - # Start unified monitoring |
| 177 | + |
178 | 178 | await unified_monitor.start_monitoring() |
179 | | - |
| 179 | + |
180 | 180 | yield |
181 | 181 |
|
182 | 182 | # Shutdown |
@@ -226,6 +226,7 @@ async def lifespan(app: FastAPI): |
226 | 226 | app.include_router(llama_version_manager.router, prefix="/api", tags=["llama-version-manager"]) |
227 | 227 | app.include_router(status.router, prefix="/api", tags=["status"]) |
228 | 228 | app.include_router(gpu_info.router, prefix="/api", tags=["gpu"]) |
| 229 | +app.include_router(lmdeploy.router, prefix="/api", tags=["lmdeploy"]) |
229 | 230 |
|
230 | 231 | # Include monitoring routes |
231 | 232 | from backend.routes import unified_monitoring |
|
0 commit comments