Spaces:
Sleeping
Sleeping
GitHub Actions commited on
Commit Β·
88d625f
1
Parent(s): 175fee8
π Auto-deploy from GitHub
Browse files- app/api/v1/endpoints/health.py +13 -1
- app/main.py +21 -7
app/api/v1/endpoints/health.py
CHANGED
|
@@ -13,6 +13,7 @@ router = APIRouter()
|
|
| 13 |
class HealthResponse(BaseModel):
|
| 14 |
status: str
|
| 15 |
server: str
|
|
|
|
| 16 |
huggingface_space: Optional[str] = None
|
| 17 |
huggingface_space_url: Optional[str] = None
|
| 18 |
|
|
@@ -33,15 +34,26 @@ async def check_huggingface_space():
|
|
| 33 |
@router.get("/health", response_model=HealthResponse)
|
| 34 |
async def health_check():
|
| 35 |
"""
|
| 36 |
-
Health check endpoint that verifies server status and HuggingFace space availability
|
| 37 |
"""
|
| 38 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
# Check HuggingFace space if configured
|
| 40 |
hf_status = await check_huggingface_space()
|
| 41 |
|
| 42 |
return HealthResponse(
|
| 43 |
status="healthy",
|
| 44 |
server="running",
|
|
|
|
| 45 |
huggingface_space=hf_status,
|
| 46 |
huggingface_space_url=HF_SPACE_URL
|
| 47 |
)
|
|
|
|
| 13 |
class HealthResponse(BaseModel):
|
| 14 |
status: str
|
| 15 |
server: str
|
| 16 |
+
model_status: str
|
| 17 |
huggingface_space: Optional[str] = None
|
| 18 |
huggingface_space_url: Optional[str] = None
|
| 19 |
|
|
|
|
| 34 |
@router.get("/health", response_model=HealthResponse)
|
| 35 |
async def health_check():
|
| 36 |
"""
|
| 37 |
+
Health check endpoint that verifies server status, model loading status and HuggingFace space availability
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
# Check model loading status
|
| 41 |
+
model_status = "loading"
|
| 42 |
+
try:
|
| 43 |
+
from ....core.model_loader import get_generator
|
| 44 |
+
# Try to get the generator - if it's cached, this will be instant
|
| 45 |
+
generator = get_generator()
|
| 46 |
+
model_status = "ready" if generator else "loading"
|
| 47 |
+
except Exception as e:
|
| 48 |
+
model_status = f"error: {str(e)[:50]}..."
|
| 49 |
+
|
| 50 |
# Check HuggingFace space if configured
|
| 51 |
hf_status = await check_huggingface_space()
|
| 52 |
|
| 53 |
return HealthResponse(
|
| 54 |
status="healthy",
|
| 55 |
server="running",
|
| 56 |
+
model_status=model_status,
|
| 57 |
huggingface_space=hf_status,
|
| 58 |
huggingface_space_url=HF_SPACE_URL
|
| 59 |
)
|
app/main.py
CHANGED
|
@@ -24,15 +24,29 @@ settings.resolved_static_files_mount_dir.mkdir(parents=True, exist_ok=True)
|
|
| 24 |
# Lifecycle management for the model
|
| 25 |
@asynccontextmanager
|
| 26 |
async def lifespan(app: FastAPI):
|
| 27 |
-
# Startup: Preload the model
|
| 28 |
-
logger.info("Anwendung startet...
|
| 29 |
try:
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
logger.error(f"Fehler beim
|
| 34 |
-
|
| 35 |
-
# raise # Uncomment to prevent startup on error
|
| 36 |
yield
|
| 37 |
# Shutdown: Cleanup actions could go here (not currently needed for the model)
|
| 38 |
logger.info("Anwendung wird heruntergefahren.")
|
|
|
|
| 24 |
# Lifecycle management for the model
|
| 25 |
@asynccontextmanager
|
| 26 |
async def lifespan(app: FastAPI):
|
| 27 |
+
# Startup: Preload the model (non-blocking)
|
| 28 |
+
logger.info("Anwendung startet... Starte LLM-Modell-Loading im Hintergrund.")
|
| 29 |
try:
|
| 30 |
+
# Start model loading in background to avoid blocking app startup
|
| 31 |
+
import asyncio
|
| 32 |
+
import threading
|
| 33 |
+
|
| 34 |
+
def load_model_background():
|
| 35 |
+
try:
|
| 36 |
+
logger.info("Hintergrund-Loading des LLM-Modells gestartet...")
|
| 37 |
+
get_generator() # Calls get_generator to load and cache the model
|
| 38 |
+
logger.info("β
LLM-Modell erfolgreich vorab geladen und Pipeline initialisiert.")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
logger.error(f"β Fehler beim Hintergrund-Loading des LLM-Modells: {e}", exc_info=True)
|
| 41 |
+
|
| 42 |
+
# Start model loading in a separate thread
|
| 43 |
+
model_thread = threading.Thread(target=load_model_background, daemon=True)
|
| 44 |
+
model_thread.start()
|
| 45 |
+
logger.info("π Anwendung gestartet - Modell lΓ€dt im Hintergrund...")
|
| 46 |
+
|
| 47 |
except Exception as e:
|
| 48 |
+
logger.error(f"Fehler beim Starten des Hintergrund-Loadings: {e}", exc_info=True)
|
| 49 |
+
|
|
|
|
| 50 |
yield
|
| 51 |
# Shutdown: Cleanup actions could go here (not currently needed for the model)
|
| 52 |
logger.info("Anwendung wird heruntergefahren.")
|