"""GLiNER2Manager — shared model singleton for all gliner2-based pipeline steps."""

from __future__ import annotations

import contextlib
from typing import Any

_SINGLETON: dict[str, Any] = {}


def get_model(model_id: str = "fastino/gliner2-base-v1", use_fp16: bool = True) -> Any:
    if model_id not in _SINGLETON:
        from gliner2 import GLiNER2  # type: ignore[import-untyped]

        model = GLiNER2.from_pretrained(model_id)
        if use_fp16:
            with contextlib.suppress(Exception):
                model = model.half()
        _SINGLETON[model_id] = model
    return _SINGLETON[model_id]
