File size: 1,412 Bytes
0231daa
 
 
 
 
 
 
 
d9f3e5d
0231daa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Dependency injection for FastAPI routes.

This module provides dependency functions that can be injected into
route handlers, ensuring consistent access to shared resources.
"""

from typing import Optional
from fastapi import HTTPException, status

from src.core.manager import ModelManager


# Global instances (initialized at startup)
_model_manager: Optional[ModelManager] = None


def set_model_manager(manager: ModelManager) -> None:
    """
    Set the global model manager instance.

    Called during application startup.

    Args:
        manager: ModelManager instance
    """
    global _model_manager
    _model_manager = manager


def get_model_manager() -> ModelManager:
    """
    Get the model manager instance.

    This is a dependency function that can be used in route handlers.

    Returns:
        ModelManager instance

    Raises:
        HTTPException: If model manager is not initialized

    Example:
        @router.post("/embed")
        async def embed(
            request: EmbedRequest,
            manager: ModelManager = Depends(get_model_manager)
        ):
            model = manager.get_model(request.model_id)
            ...
    """
    if _model_manager is None:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="Server is not ready. Model manager not initialized.",
        )
    return _model_manager