Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: let managed accounts use gated models with their own HF token (#17172)

Nilayd4d5fffae353
diff --git a/studio/backend/hub/services/models/account_access.py b/studio/backend/hub/services/models/account_access.py
index c0a73931d74..526f605ed45 100644
--- a/studio/backend/hub/services/models/account_access.py
+++ b/studio/backend/hub/services/models/account_access.py
@@ -1008,19 +1008,46 @@ def require_model_access(
repo_type: str = "model",
*,
offline: bool = False,
+ hf_token = None,
) -> None:
access_options = {"offline": True} if offline else {}
if not model_visible(reference, repo_type = repo_type, **access_options):
from utils.paths.path_utils import is_network_or_device_path
# The allowlist check below resolves the reference on disk: not for a share path.
- if repo_type == "model" and not (
- isinstance(reference, str) and is_network_or_device_path(reference)
- ):
+ if isinstance(reference, str) and is_network_or_device_path(reference):
+ raise HTTPException(status_code = 404, detail = "Model not found")
+ if repo_type == "model":
require_model_allowed(reference)
+ if not offline and _grant_with_token(reference, repo_type, hf_token):
+ return
raise HTTPException(status_code = 404, detail = "Model not found")
+def _grant_with_token(reference, repo_type: str, hf_token) -> bool:
+ token = account_hf_token(hf_token)
+ if not isinstance(reference, str) or not token:
+ return False
+ repo_id = _reference_repo_id(reference.strip())
+ if repo_id is None or not _source_speaks_for_the_cache():
+ return False
+ if repo_type == "model" and not model_allowed(repo_id):
+ return False
+ from hub.utils.hf_tokens import _explicit_token_reaches_repo
+
+ # HfApi.auth_check has no timeout; this probe is bounded.
+ if _explicit_token_reaches_repo(repo_id, token, repo_type) is not True:
+ return False
+ record_model_grant(repo_id, repo_type)
+ return model_visible(reference, repo_type = repo_type)
+
+
+def require_media_model_access(request) -> None:
+ require_model_access(request.model_path, hf_token = request.hf_token)
+ if request.base_repo:
+ require_model_access(request.base_repo, hf_token = request.hf_token)
+
+
def require_model_allowed(reference) -> None:
"""The allowlist alone, for routes that fetch a Hub model without the visibility gate."""
if not isinstance(reference, str) or model_allowlist() is None:
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 55bb17c4e54..ef097842f1c 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -23427,7 +23427,9 @@ async def audio_download_plan(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
)
if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.model_path)
+ await asyncio.to_thread(
+ account_access.require_model_access, request.model_path, hf_token = request.hf_token
+ )
from core.inference.native_audio import native_audio_download_plan
from utils.native_path_leases import redact_native_paths
@@ -48764,10 +48766,7 @@ async def diffusion_download_plan(
if account_access.managed_account():
await asyncio.to_thread(account_access.require_media_references, request)
if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.model_path)
- if request.base_repo:
- if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.base_repo)
+ await asyncio.to_thread(account_access.require_media_model_access, request)
if account_access.managed_account():
request = request.model_copy(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
@@ -49026,7 +49025,7 @@ def _require_resolved_image_base_access(request, fam, kind: str, engine_name) ->
refused = None
for base in candidates:
try:
- account_access.require_model_access(base)
+ account_access.require_model_access(base, hf_token = request.hf_token)
return
except HTTPException as exc:
refused = refused or (base, exc)
@@ -49057,10 +49056,7 @@ async def load_diffusion_model_gated(
await asyncio.to_thread(account_access.require_media_references, request)
account_access.require_idle_other_accounts()
if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.model_path)
- if request.base_repo:
- if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.base_repo)
+ await asyncio.to_thread(account_access.require_media_model_access, request)
if account_access.managed_account():
request = request.model_copy(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
diff --git a/studio/backend/routes/video.py b/studio/backend/routes/video.py
index 860abdcb855..88149760275 100644
--- a/studio/backend/routes/video.py
+++ b/studio/backend/routes/video.py
@@ -165,10 +165,7 @@ async def video_download_plan(
if account_access.managed_account():
await asyncio.to_thread(account_access.require_media_references, request)
if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.model_path)
- if request.base_repo:
- if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.base_repo)
+ await asyncio.to_thread(account_access.require_media_model_access, request)
if account_access.managed_account():
request = request.model_copy(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
@@ -320,10 +317,7 @@ async def load_video_model_gated(
await asyncio.to_thread(account_access.require_media_references, request)
account_access.require_idle_other_accounts()
if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.model_path)
- if request.base_repo:
- if account_access.managed_account():
- await asyncio.to_thread(account_access.require_model_access, request.base_repo)
+ await asyncio.to_thread(account_access.require_media_model_access, request)
if account_access.managed_account():
request = request.model_copy(
update = {"hf_token": account_access.account_hf_token(request.hf_token)}
diff --git a/studio/backend/tests/test_account_media_load_retirement_race.py b/studio/backend/tests/test_account_media_load_retirement_race.py
index 000cc8f7603..169fa60e47b 100644
--- a/studio/backend/tests/test_account_media_load_retirement_race.py
+++ b/studio/backend/tests/test_account_media_load_retirement_race.py
@@ -104,7 +104,7 @@ def _load():
def test_load_parked_before_admission_is_refused_after_retirement(monkeypatch, engine):
blocked, release, result = threading.Event(), threading.Event(), {}
- def access_check(model_name):
+ def access_check(model_name, **_):
blocked.set()
release.wait(timeout = 30)
@@ -129,7 +129,7 @@ def test_load_parked_before_admission_is_refused_after_retirement(monkeypatch, e
def test_load_admitted_before_retirement_is_torn_down(monkeypatch, engine):
- monkeypatch.setattr(access, "require_model_access", lambda model_name: None)
+ monkeypatch.setattr(access, "require_model_access", lambda model_name, **_: None)
status = run_as(ALICE, _load)
assert status.loaded is False and engine.loading
_retire()
@@ -138,7 +138,7 @@ def test_load_admitted_before_retirement_is_torn_down(monkeypatch, engine):
def test_retirement_leaves_another_accounts_load_alone(monkeypatch, engine):
- monkeypatch.setattr(access, "require_model_access", lambda model_name: None)
+ monkeypatch.setattr(access, "require_model_access", lambda model_name, **_: None)
run_as(AccountContext("b" * 32, "bob"), _load)
_retire()
assert engine.unloaded == 0 and engine.loading
diff --git a/studio/backend/tests/test_image_load_resolved_base_access.py b/studio/backend/tests/test_image_load_resolved_base_access.py
index c42bb37d47f..8eb8c5fbe09 100644
--- a/studio/backend/tests/test_image_load_resolved_base_access.py
+++ b/studio/backend/tests/test_image_load_resolved_base_access.py
@@ -16,6 +16,7 @@ if _TESTS_DIR not in sys.path:
import core.inference.diffusion as diffusion_module # noqa: E402
from hub.services.models import account_access # noqa: E402
+from hub.utils import hf_tokens # noqa: E402
from test_diffusion_routes import client # noqa: E402,F401 -- the shared fake-backend client fixture
KREA_GGUF = {
@@ -100,6 +101,88 @@ def test_the_same_resident_is_refused_by_generate_without_the_grant(client, mana
assert _generate(client).status_code == 404
+def hub(monkeypatch, managed):
+ import types
+
+ readers = {"hf_reader": {"krea/krea-2-turbo", "gated-org/krea-2-turbo-gguf"}}
+
+ class _Hub:
+ def repo_info(self, repo_id, *, repo_type, token, timeout):
+ return types.SimpleNamespace(gated = "auto")
+
+ def auth_check(self, repo_id, *, repo_type, token):
+ raise AssertionError("auth_check has no timeout")
+
+ monkeypatch.setattr(account_access, "HfApi", _Hub)
+ monkeypatch.setattr(
+ hf_tokens,
+ "_probe_repo_access",
+ lambda repo_id, token, repo_type, **_: repo_id.lower() in readers.get(token, set()),
+ )
+ hf_tokens.reset_repo_access_cache()
+ monkeypatch.setattr(
+ account_access,
+ "record_model_grant",
+ lambda repo_id, repo_type = "model": managed.add(
+ account_access._grant_key(repo_id, repo_type)
+ ),
+ )
+ return managed
+
+
+def test_a_saved_token_that_can_read_the_gated_base_lets_the_load_through(client, hub):
+ resp = client.post("/api/inference/images/load", json = {**KREA_GGUF, "hf_token": "hf_reader"})
+ assert resp.status_code == 200, resp.text
+ assert account_access._grant_key("krea/Krea-2-Turbo", "model") in hub
+
+
+def test_a_token_that_cannot_read_the_gated_base_is_still_refused(client, hub):
+ resp = client.post("/api/inference/images/load", json = {**KREA_GGUF, "hf_token": "hf_other"})
+ assert resp.status_code == 403, resp.text
+ assert not hub
+
+
+GATED_GGUF = {
+ "model_path": "gated-org/Krea-2-Turbo-GGUF",
+ "gguf_filename": "Krea-2-Turbo-Q8_0.gguf",
+ "family_override": "krea-2",
+}
+
+
+def test_download_plan_of_a_gated_repo_uses_the_accounts_token(client, hub):
+ assert client.post("/api/inference/images/download-plan", json = GATED_GGUF).status_code == 404
+ resp = client.post(
+ "/api/inference/images/download-plan", json = {**GATED_GGUF, "hf_token": "hf_reader"}
+ )
+ assert resp.status_code == 200, resp.text
+ assert account_access._grant_key(GATED_GGUF["model_path"], "model") in hub
+
+
+def test_load_of_a_gated_repo_uses_the_accounts_token(client, hub):
+ assert client.post("/api/inference/images/load", json = GATED_GGUF).status_code == 404
+ resp = client.post("/api/inference/images/load", json = {**GATED_GGUF, "hf_token": "hf_reader"})
+ assert resp.status_code == 200, resp.text
+ assert account_access._grant_key(GATED_GGUF["model_path"], "model") in hub
+
+
+def test_download_plan_with_a_token_that_cannot_read_the_repo_stays_not_found(client, hub):
+ resp = client.post(
+ "/api/inference/images/download-plan", json = {**GATED_GGUF, "hf_token": "hf_other"}
+ )
+ assert resp.status_code == 404, resp.text
+ assert not hub
+
+
+def test_a_token_does_not_open_a_repo_off_the_allowlist(client, hub, monkeypatch):
+ monkeypatch.setattr(account_access, "model_allowlist", lambda: ("unsloth/*",))
+ resp = client.post(
+ "/api/inference/images/download-plan", json = {**GATED_GGUF, "hf_token": "hf_reader"}
+ )
+ assert resp.status_code == 403, resp.text
+ assert not hub
+
+
def test_a_public_base_still_loads_for_a_managed_account(client, managed):
resp = client.post(
"/api/inference/images/load",
@@ -139,7 +222,7 @@ def allowed(monkeypatch):
ok: set[str] = set()
monkeypatch.setattr(account_access, "managed_account", lambda: True)
- def _check(repo):
+ def _check(repo, **_):
if repo not in ok:
raise HTTPException(status_code = 404, detail = "Not found")
diff --git a/studio/backend/tests/test_video_routes.py b/studio/backend/tests/test_video_routes.py
index 8697992c8af..d11cc03b4bd 100644
--- a/studio/backend/tests/test_video_routes.py
+++ b/studio/backend/tests/test_video_routes.py
@@ -1200,6 +1200,52 @@ def test_video_download_plan_forwards_the_encoder_policy(client, monkeypatch):
assert seen["hf_token"] == "hf_secret"
+def test_video_download_plan_of_a_gated_repo_uses_a_managed_accounts_token(client, monkeypatch):
+ from hub.services.models import account_access
+ from hub.utils import hf_tokens
+
+ grants: set[str] = set()
+ monkeypatch.setattr(account_access, "managed_account", lambda: True)
+ monkeypatch.setattr(account_access, "require_media_references", lambda *a, **k: None)
+ monkeypatch.setattr(account_access, "model_grants", lambda: set(grants))
+ monkeypatch.setattr(account_access, "model_allowlist", lambda: None)
+ monkeypatch.setattr(account_access, "repo_is_public", lambda *a, **k: False)
+ monkeypatch.setattr(
+ account_access,
+ "record_model_grant",
+ lambda repo_id, repo_type = "model": grants.add(
+ account_access._grant_key(repo_id, repo_type)
+ ),
+ )
+ monkeypatch.setattr(
+ hf_tokens,
+ "_probe_repo_access",
+ lambda repo_id, token, repo_type, **_: token == "hf_reader"
+ and repo_id == "gated/LTX-2.3-GGUF",
+ )
+ hf_tokens.reset_repo_access_cache()
+ monkeypatch.setattr(
+ video_module.get_video_backend(),
+ "download_plan",
+ lambda *a, **k: {"entries": [], "total_bytes": 0},
+ raising = False,
+ )
+ body = {
+ "model_path": "gated/LTX-2.3-GGUF",
+ "gguf_filename": "distilled/ltx-2.3-22b-distilled-Q4_K_M.gguf",
+ "model_kind": "gguf",
+ }
+
+ assert client.post("/api/inference/video/download-plan", json = body).status_code == 404
+ denied = client.post(
+ "/api/inference/video/download-plan", json = {**body, "hf_token": "hf_other"}
+ )
+ assert denied.status_code == 404 and not grants
+ resp = client.post("/api/inference/video/download-plan", json = {**body, "hf_token": "hf_reader"})
+ assert resp.status_code == 200, resp.text
+ assert grants == {"model:gated/ltx-2.3-gguf"}
+
+
def test_video_download_plan_validates_with_the_encoder_precision(client, monkeypatch):
backend = video_module.get_video_backend()
seen: dict = {}