enh: models endpoint optimization

Co-Authored-By: Classic298 <27028174+Classic298@users.noreply.github.com>

#20010
This commit is contained in:
Timothy Jaeryang Baek 2025-12-21 15:43:02 +04:00
parent 53c1ca64b7
commit 0dd2cfe1f2
2 changed files with 15 additions and 1 deletions

View file

@ -357,6 +357,14 @@ class ModelsTable:
except Exception: except Exception:
return None return None
def get_models_by_ids(self, ids: list[str]) -> list[ModelModel]:
try:
with get_db() as db:
models = db.query(Model).filter(Model.id.in_(ids)).all()
return [ModelModel.model_validate(model) for model in models]
except Exception:
return []
def toggle_model_by_id(self, id: str) -> Optional[ModelModel]: def toggle_model_by_id(self, id: str) -> Optional[ModelModel]:
with get_db() as db: with get_db() as db:
try: try:

View file

@ -366,6 +366,12 @@ def get_filtered_models(models, user):
user.role == "user" user.role == "user"
or (user.role == "admin" and not BYPASS_ADMIN_ACCESS_CONTROL) or (user.role == "admin" and not BYPASS_ADMIN_ACCESS_CONTROL)
) and not BYPASS_MODEL_ACCESS_CONTROL: ) and not BYPASS_MODEL_ACCESS_CONTROL:
model_ids = [model["id"] for model in models if not model.get("arena")]
model_infos = {
model_info.id: model_info
for model_info in Models.get_models_by_ids(model_ids)
}
filtered_models = [] filtered_models = []
user_group_ids = {group.id for group in Groups.get_groups_by_member_id(user.id)} user_group_ids = {group.id for group in Groups.get_groups_by_member_id(user.id)}
for model in models: for model in models:
@ -381,7 +387,7 @@ def get_filtered_models(models, user):
filtered_models.append(model) filtered_models.append(model)
continue continue
model_info = Models.get_model_by_id(model["id"]) model_info = model_infos.get(model["id"], None)
if model_info: if model_info:
if ( if (
(user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL) (user.role == "admin" and BYPASS_ADMIN_ACCESS_CONTROL)