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: push an export to the Hub with the Hugging Face CLI login (#14047)

Nilay8f7ff42580d2
diff --git a/studio/backend/core/export/export.py b/studio/backend/core/export/export.py
index 10cd7968dc6..9ffad4cd05e 100644
--- a/studio/backend/core/export/export.py
+++ b/studio/backend/core/export/export.py
@@ -667,6 +667,13 @@ def _open_hub_repo(hf_api, repo_id, private):
return repo_id
+def _hub_push_token(hf_token: HfTokenArg) -> HfTokenArg:
+ if hf_token is None:
+ from huggingface_hub import get_token
+ return get_token()
+ return hf_token
+
+
def _push_mlx_merged(
model, tokenizer, *, save_method, output_path, output_is_fresh, repo_id, hf_token, private
):
@@ -1441,6 +1448,7 @@ class ExportBackend:
output_path = str(Path(final_dir).resolve())
if push_to_hub:
+ hf_token = _hub_push_token(hf_token)
if not repo_id or not hf_token:
return (
False,
@@ -1597,6 +1605,7 @@ class ExportBackend:
output_path = str(Path(save_directory).resolve())
if push_to_hub:
+ hf_token = _hub_push_token(hf_token)
if not repo_id or not hf_token:
return (
False,
@@ -1919,6 +1928,7 @@ class ExportBackend:
)
if push_to_hub:
+ hf_token = _hub_push_token(hf_token)
if not repo_id or not hf_token:
return (
False,
@@ -2477,6 +2487,7 @@ class ExportBackend:
output_path = str(Path(save_directory).resolve())
if push_to_hub:
+ hf_token = _hub_push_token(hf_token)
if not repo_id or not hf_token:
return (
False,
diff --git a/studio/backend/routes/export.py b/studio/backend/routes/export.py
index 64853e82469..3ab6c69bea2 100644
--- a/studio/backend/routes/export.py
+++ b/studio/backend/routes/export.py
@@ -102,6 +102,9 @@ def _resolve_export_hf_token(
status_code = 400,
detail = "Hugging Face token is required to push to Hub when authenticated via API key.",
)
+ # An empty token pushes with the operator's own login (hf auth login), never a managed account's.
+ if push_to_hub and token is None and account_access.managed_account():
+ return False
return hf_token_arg(token, allow_ambient_token = allow_ambient)
diff --git a/studio/backend/tests/test_export_hub_push.py b/studio/backend/tests/test_export_hub_push.py
index b05c0f74f5f..f4262b6ddc1 100644
--- a/studio/backend/tests/test_export_hub_push.py
+++ b/studio/backend/tests/test_export_hub_push.py
@@ -11,6 +11,8 @@ import types
from pathlib import Path
import pytest
+from huggingface_hub import constants as hf_constants
+from huggingface_hub import get_token as hf_get_token
_HELPERS_SPEC = importlib.util.spec_from_file_location(
@@ -931,3 +933,73 @@ def test_lora_gguf_export_push_to_a_reused_folder_does_not_upload_its_leftovers(
assert Path(seen["folder"]).name == export_dir.name
assert not Path(seen["folder"]).exists()
assert seen["uploaded"] == _LORA_GGUF_FILES
+
+
+class _GgufModel:
+ def save_pretrained_gguf(self, model_save_path, tokenizer, quantization_method):
+ output = Path(f"{model_save_path}_gguf")
+ output.mkdir(parents = True)
+ (output / f"model.{quantization_method}.gguf").write_bytes(b"GGUF")
+
+
+def _gguf_model_backend(monkeypatch, calls, seen):
+ _HELPERS._install_export_backend_stubs(monkeypatch)
+ module = _HELPERS._load_module(
+ "test_export_hub_push_cli_login_gguf_backend", "core/export/export.py", monkeypatch
+ )
+ _HELPERS._patch_hub(monkeypatch, module, calls, seen)
+ return _HELPERS._export_backend(module, _GgufModel)
+
+
+def _push_without_a_token(monkeypatch, export, save_directory, calls, seen):
+ if export == "gguf":
+ backend = _gguf_model_backend(monkeypatch, calls, seen)
+ push = functools.partial(backend.export_gguf, quantization_method = "Q4_K_M")
+ elif export == "lora":
+ _module, backend, _gguf, _uploads = _lora_backend(
+ monkeypatch, "test_export_hub_push_cli_login_lora_backend", calls, seen, "adapter"
+ )
+ push = backend.export_lora_adapter
+ else:
+ backend = _non_mlx_backend(
+ monkeypatch, f"test_export_hub_push_cli_login_{export}_backend", calls, seen
+ )
+ push = getattr(backend, f"export_{export}_model")
+ monkeypatch.setattr(sys.modules["huggingface_hub"], "get_token", hf_get_token, raising = False)
+ return push(save_directory, push_to_hub = True, repo_id = "model", hf_token = None)
+
+
[email protected]("export", ["base", "merged", "lora", "gguf"])
+def test_hub_push_with_no_token_uses_the_cli_login(tmp_path, monkeypatch, export):
+ login = tmp_path / "hf_token"
+ login.write_text("hf_cli_login")
+ monkeypatch.setattr(hf_constants, "HF_TOKEN_PATH", str(login))
+ for key in ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_OIDC_RESOURCE"):
+ monkeypatch.delenv(key, raising = False)
+ calls: list[str] = []
+ seen: dict = {}
+
+ success, message, _path = _push_without_a_token(
+ monkeypatch, export, str(tmp_path / "export"), calls, seen
+ )
+
+ assert success is True, message
+ assert seen["token"] == "hf_cli_login"
+ assert calls[0] == "create_repo"
+
+
[email protected]("export", ["base", "merged", "lora", "gguf"])
+def test_hub_push_with_no_token_and_no_cli_login_is_refused(tmp_path, monkeypatch, export):
+ monkeypatch.setattr(hf_constants, "HF_TOKEN_PATH", str(tmp_path / "missing"))
+ for key in ("HF_TOKEN", "HUGGING_FACE_HUB_TOKEN", "HF_OIDC_RESOURCE"):
+ monkeypatch.delenv(key, raising = False)
+ calls: list[str] = []
+ seen: dict = {}
+
+ success, message, _path = _push_without_a_token(
+ monkeypatch, export, str(tmp_path / "export"), calls, seen
+ )
+
+ assert success is False
+ assert "Hugging Face token required" in message
+ assert calls == []
diff --git a/studio/backend/tests/test_export_push_token_routes.py b/studio/backend/tests/test_export_push_token_routes.py
new file mode 100644
index 00000000000..58c104d91ba
--- /dev/null
+++ b/studio/backend/tests/test_export_push_token_routes.py
@@ -0,0 +1,30 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Which credential an export's Hub push may fall back to when the token box is empty."""
+
+import pytest
+
+from hub.utils.hf_tokens import AmbientAuthorizedToken
+from routes import export as export_routes
+
+
[email protected]("raw", [None, "", " "])
+def test_managed_account_push_without_a_token_never_gets_the_operator_login(monkeypatch, raw):
+ monkeypatch.setattr(export_routes.account_access, "managed_account", lambda: True)
+
+ assert export_routes._resolve_export_hf_token(raw, push_to_hub = True) is False
+
+
+def test_managed_account_keeps_its_own_token_and_ambient_reads(monkeypatch):
+ monkeypatch.setattr(export_routes.account_access, "managed_account", lambda: True)
+
+ token = export_routes._resolve_export_hf_token("hf_own", push_to_hub = True)
+ assert token == "hf_own" and isinstance(token, AmbientAuthorizedToken)
+ assert export_routes._resolve_export_hf_token(None, push_to_hub = False) is None
+
+
+def test_owner_push_without_a_token_may_use_the_cli_login(monkeypatch):
+ monkeypatch.setattr(export_routes.account_access, "managed_account", lambda: False)
+
+ assert export_routes._resolve_export_hf_token(None, push_to_hub = True) is None