From 5dfc4fcba910d8b09b1e51bb2c5cc0147ba641c6 Mon Sep 17 00:00:00 2001 From: shroominic <34897716+shroominic@users.noreply.github.com> Date: Fri, 13 Jun 2025 14:44:22 +0200 Subject: [PATCH] Update models.py --- router/models.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/router/models.py b/router/models.py index 824d1586..f965338f 100644 --- a/router/models.py +++ b/router/models.py @@ -45,12 +45,33 @@ class Model(BaseModel): MODELS: list[Model] = [] -models_file = "models.json" -if not os.path.exists(models_file): - models_file = "models.example.json" +def load_models() -> list[Model]: + """Load model definitions from a JSON file. + The file path can be specified via the ``MODELS_PATH`` environment variable. + If ``models.json`` is not found, the bundled ``models.example.json`` is used + as a fallback. If neither file exists or an error occurs while loading, an + empty list is returned. + """ -with open(models_file, "r") as f: - MODELS = [Model(**model) for model in json.load(f)["models"]] + models_path = Path(os.environ.get("MODELS_PATH", "models.json")) + if not models_path.exists(): + example = Path(__file__).resolve().parent.parent / "models.example.json" + if example.exists(): + models_path = example + else: + return [] + + try: + with models_path.open("r") as f: + data = json.load(f) + except Exception as e: # pragma: no cover - log and continue + print(f"Error loading models from {models_path}: {e}") + return [] + + return [Model(**model) for model in data.get("models", [])] + + + MODELS = load_models() async def update_sats_pricing() -> None: