Update models.py

This commit is contained in:
shroominic
2025-06-13 14:44:22 +02:00
committed by GitHub
parent c183006317
commit 5dfc4fcba9
+26 -5
View File
@@ -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: