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.

feat(studio): ModelScope as a model source and a custom Hugging Face endpoint (#11761)

Long Yixing0657c177ec05
diff --git a/studio/backend/core/inference/diffusion.py b/studio/backend/core/inference/diffusion.py
index 0cf66a33dfe..23dc2201131 100644
--- a/studio/backend/core/inference/diffusion.py
+++ b/studio/backend/core/inference/diffusion.py
@@ -36,6 +36,7 @@ from core._torchao_stub import (
install_torchao_windows_rocm_stub,
install_xformers_windows_rocm_stub,
)
+from hub.utils.hf_errors import modelscope_missing
from loggers import get_logger
from utils.account_context import account_thread, current_account_id
from utils.hardware import clear_gpu_cache
@@ -915,7 +916,9 @@ def _assert_base_repo_accessible(
# 404, or an error carrying no response, raises.
if _is_auth_error(exc) and _already_downloaded():
return other_root_snapshot
- raise ValueError(_repo_access_message(repo, gated = False)) from None
+ raise ValueError(
+ modelscope_missing(exc) or _repo_access_message(repo, gated = False)
+ ) from None
except HfHubHTTPError as exc:
if not _is_auth_error(exc):
return None # a 5xx or rate limit is not an access verdict
@@ -3057,7 +3060,8 @@ class DiffusionBackend:
try:
text = (
- hub_access_message(exc, had_token = _hf_token_in_play(kwargs.get("hf_token")))
+ modelscope_missing(exc)
+ or hub_access_message(exc, had_token = _hf_token_in_play(kwargs.get("hf_token")))
or dynamo_partial_init_message(exc)
or str(exc)
)
diff --git a/studio/backend/core/inference/llama_cpp.py b/studio/backend/core/inference/llama_cpp.py
index 626c6b79b26..729fa714c3f 100644
--- a/studio/backend/core/inference/llama_cpp.py
+++ b/studio/backend/core/inference/llama_cpp.py
@@ -74,6 +74,7 @@ from core.inference.context_window import (
)
from core.inference.llama_tool_schema import llama_grammar_tools
from core.inference.stream_errors import stream_error_from_chunk
+from hub.utils.hf_errors import modelscope_missing
from core.inference.llama_server_args import (
_CACHE_RAM_FLAGS,
_CTX_CHECKPOINTS_FLAGS,
@@ -7396,6 +7397,7 @@ class LlamaCppBackend:
self._is_audio: bool = False
self._audio_type: Optional[str] = None
self._audio_probed: bool = False
+ self._codec_failure = threading.local()
# Audio INPUT capability (distinct from _is_audio, which is TTS output).
self._has_audio_input: bool = False
# Video INPUT capability, from llama-server's /props modalities. True
@@ -22634,6 +22636,7 @@ class LlamaCppBackend:
# gets it, so an embedded second session would otherwise release this load.
# Serialise the whole load so concurrent /load calls never leave two
# llama-server processes alive (#5401 / #5161). Doesn't block /unload.
+ self._codec_failure.message = None
with self._serial_load_scope():
# Here, not at the spawn: a lock gives a waiter no priority, and the
# duplicate-adoption phase below kills whatever is loaded -- after a
@@ -39151,6 +39154,10 @@ class LlamaCppBackend:
logger.debug(f"Audio type detection failed: {e}")
return None
+ def codec_failure(self) -> Optional[str]:
+ """The ModelScope missing-repo sentence from this thread's last load_model codec init."""
+ return getattr(self._codec_failure, "message", None)
+
def _apply_detected_audio(
self,
detected: Optional[str],
@@ -39172,6 +39179,7 @@ class LlamaCppBackend:
# Surface as HTTP 500 (matches pre-PR contract).
logger.warning("Failed to init audio codec '%s': %s", detected, exc)
self._audio_probed = False
+ self._codec_failure.message = modelscope_missing(exc)
return False
elif detected:
# csm / whisper / audio_vlm: track type but keep _is_audio False --
diff --git a/studio/backend/core/inference/openai_auto_download.py b/studio/backend/core/inference/openai_auto_download.py
index 10719748f77..33e4b61f1c6 100644
--- a/studio/backend/core/inference/openai_auto_download.py
+++ b/studio/backend/core/inference/openai_auto_download.py
@@ -191,8 +191,13 @@ def _hub_token(hf_token: Optional[str]):
return hf_token or False
-def _servable_key(repo_id: str, hf_token: Optional[str]) -> str:
- """Cache key, per credential.
+def _endpoint() -> str:
+ from huggingface_hub import constants
+ return constants.ENDPOINT.rstrip("/")
+
+
+def _servable_key(repo_id: str, hf_token: Optional[str], endpoint: str) -> str:
+ """Cache key, per credential and Hub.
The Hub 404s a private repo the caller cannot see, so a tokenless verdict says
nothing about a caller who has one. Digested, so no token is held here.
@@ -200,18 +205,24 @@ def _servable_key(repo_id: str, hf_token: Optional[str]) -> str:
import hashlib
seen_as = hashlib.sha256(hf_token.encode()).hexdigest()[:16] if hf_token else "anon"
- return f"{repo_id.lower()}\n{seen_as}"
+ return f"{repo_id.lower()}\n{seen_as}\n{endpoint}"
-def _mark_not_servable(repo_id: str, hf_token: Optional[str]) -> None:
+def _mark_not_servable(repo_id: str, hf_token: Optional[str], endpoint: str) -> None:
with _cache_lock:
if len(_not_servable) >= _NOT_SERVABLE_MAX:
_not_servable.clear()
- _not_servable[_servable_key(repo_id, hf_token)] = time.monotonic() + _NOT_SERVABLE_TTL_S
+ _not_servable[_servable_key(repo_id, hf_token, endpoint)] = (
+ time.monotonic() + _NOT_SERVABLE_TTL_S
+ )
-def _is_not_servable(repo_id: str, hf_token: Optional[str]) -> bool:
- key = _servable_key(repo_id, hf_token)
+def _is_not_servable(
+ repo_id: str,
+ hf_token: Optional[str],
+ endpoint: Optional[str] = None,
+) -> bool:
+ key = _servable_key(repo_id, hf_token, endpoint or _endpoint())
with _cache_lock:
expires = _not_servable.get(key)
if expires is None:
@@ -495,12 +506,16 @@ async def _is_downloadable_model(repo_id: str, hf_token: Optional[str]) -> bool:
apart from an ordinary foreign label. Any failure answers False: refusing
would strand normal traffic for the length of the download.
"""
- if _is_not_servable(repo_id, hf_token):
+ # One Hub for the whole lookup, so its answer is filed under the Hub that gave it.
+ endpoint = _endpoint()
+ if _is_not_servable(repo_id, hf_token, endpoint):
return False
def _probe():
from huggingface_hub import HfApi
- return HfApi(token = _hub_token(hf_token)).model_info(repo_id, timeout = _MODEL_INFO_TIMEOUT_S)
+ return HfApi(endpoint = endpoint, token = _hub_token(hf_token)).model_info(
+ repo_id, timeout = _MODEL_INFO_TIMEOUT_S
+ )
try:
info = await asyncio.to_thread(_probe)
@@ -511,7 +526,7 @@ async def _is_downloadable_model(repo_id: str, hf_token: Optional[str]) -> bool:
# download.
servable = bool(_gguf_variants(getattr(info, "siblings", None), repo_id))
if not servable:
- _mark_not_servable(repo_id, hf_token)
+ _mark_not_servable(repo_id, hf_token, endpoint)
return servable
@@ -638,11 +653,13 @@ async def _admit_and_start(
subject: Optional[str] = None,
via_api_key: bool = False,
) -> Optional[AutoDownloadRefusal]:
- from hub.utils.hf_errors import hf_error_status
+ from hub.utils.hf_errors import hf_error_status, modelscope_missing
+
+ endpoint = _endpoint()
def _probe():
from huggingface_hub import HfApi
- return HfApi(token = _hub_token(hf_token)).model_info(
+ return HfApi(endpoint = endpoint, token = _hub_token(hf_token)).model_info(
repo_id, files_metadata = True, timeout = _MODEL_INFO_TIMEOUT_S
)
@@ -663,13 +680,14 @@ async def _admit_and_start(
if status == 403:
return _gated_refusal(repo_id)
if status == 404:
- _mark_not_servable(repo_id, hf_token)
+ _mark_not_servable(repo_id, hf_token, endpoint)
if not looks_like_quant(wanted_variant):
return None
return AutoDownloadRefusal(
status = 404,
code = "model_not_found",
- message = (
+ message = modelscope_missing(exc)
+ or (
f"'{repo_id}' was not found on Hugging Face, or is not accessible. "
"If it is private, send a token in the X-Unsloth-HF-Token header."
),
@@ -692,7 +710,7 @@ async def _admit_and_start(
variants = _gguf_variants(getattr(info, "siblings", None), repo_id)
if not variants:
_release(active)
- _mark_not_servable(repo_id, hf_token)
+ _mark_not_servable(repo_id, hf_token, endpoint)
if not looks_like_quant(wanted_variant):
return None
return AutoDownloadRefusal(
diff --git a/studio/backend/core/inference/stt_ggml_sidecar.py b/studio/backend/core/inference/stt_ggml_sidecar.py
index 7643eb74002..50b2e7a50b9 100644
--- a/studio/backend/core/inference/stt_ggml_sidecar.py
+++ b/studio/backend/core/inference/stt_ggml_sidecar.py
@@ -43,6 +43,7 @@ from typing import Iterator, Optional
from loggers import get_logger
+from hub.utils.hf_errors import modelscope_missing
from hub.utils.hf_tokens import normalize_token
from core.inference.stt_sidecar import (
STT_KEEP_ALIVE_SECONDS,
@@ -671,12 +672,12 @@ class _GgmlDownloadState:
detail = (stderr or b"").decode("utf-8", "replace").strip()
logger.warning("GGUF STT download failed for %s: %s", model_id, detail)
with self._lock:
- self._error = f"Download failed for '{model_id}'."
+ self._error = modelscope_missing(detail) or f"Download failed for '{model_id}'."
except Exception as exc:
with self._lock:
if not self._cancelled:
logger.warning("GGUF STT download failed for %s: %s", model_id, exc)
- self._error = f"Download failed for '{model_id}'."
+ self._error = modelscope_missing(exc) or f"Download failed for '{model_id}'."
finally:
if registry is not None and owner is not None:
registry.release_repository_owner(repo_id, owner)
diff --git a/studio/backend/core/inference/stt_mtmd_sidecar.py b/studio/backend/core/inference/stt_mtmd_sidecar.py
index b0683fa27d9..44366ec07d3 100644
--- a/studio/backend/core/inference/stt_mtmd_sidecar.py
+++ b/studio/backend/core/inference/stt_mtmd_sidecar.py
@@ -29,6 +29,7 @@ from typing import Iterator, Optional
from loggers import get_logger
+from hub.utils.hf_errors import modelscope_missing
from hub.utils.hf_tokens import normalize_token
from utils.process_lifetime import (
adopt_pid,
@@ -523,12 +524,12 @@ class _MtmdDownloadState:
detail = stderr.decode("utf-8", "replace").strip()
logger.warning("mtmd STT download failed for %s: %s", model_id, detail)
with self._lock:
- self._error = f"Download failed for '{model_id}'."
+ self._error = modelscope_missing(detail) or f"Download failed for '{model_id}'."
except Exception as exc:
with self._lock:
if not self._cancelled:
logger.warning("mtmd STT download failed for %s: %s", model_id, exc)
- self._error = f"Download failed for '{model_id}'."
+ self._error = modelscope_missing(exc) or f"Download failed for '{model_id}'."
finally:
# Release first: it is the half that would wedge the repository.
if registry is not None and owner is not None:
diff --git a/studio/backend/core/inference/stt_sidecar.py b/studio/backend/core/inference/stt_sidecar.py
index ad6433986af..614ae3c4bb6 100644
--- a/studio/backend/core/inference/stt_sidecar.py
+++ b/studio/backend/core/inference/stt_sidecar.py
@@ -16,6 +16,7 @@ the process holding it lives and the backend must not be the process that takes
from __future__ import annotations
+from hub.utils.hf_errors import modelscope_missing
from hub.utils.hf_tokens import normalize_token
import gc
@@ -886,7 +887,7 @@ class _SnapshotDownloadState:
with self._lock:
if not self._cancelled:
logger.warning("STT snapshot download failed for %s: %s", repo, exc)
- self._error = f"Download failed for '{repo}'."
+ self._error = modelscope_missing(exc) or f"Download failed for '{repo}'."
finally:
if registry is not None and owner is not None:
registry.release_repository_owner(repo, owner)
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index b60b9cfb079..4fbecb9a35e 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -44,6 +44,7 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional
+from hub.utils.hf_errors import modelscope_missing
from loggers import get_logger
from .diffusion_attention import (
@@ -2293,7 +2294,7 @@ class VideoBackend:
with self._lock:
if self._load_token == token and self._loading is not None:
- self._loading.error = redact_native_paths(str(exc))
+ self._loading.error = modelscope_missing(exc) or redact_native_paths(str(exc))
def _run_load_h3_native(
self,
diff --git a/studio/backend/core/training/trainer.py b/studio/backend/core/training/trainer.py
index 420ee49d637..9cd40626542 100644
--- a/studio/backend/core/training/trainer.py
+++ b/studio/backend/core/training/trainer.py
@@ -1019,7 +1019,9 @@ class UnslothTrainer:
RepositoryNotFoundError,
)
if isinstance(gate_err, (GatedRepoError, RepositoryNotFoundError)):
- friendly = (
+ from hub.utils.hf_errors import modelscope_missing
+
+ friendly = modelscope_missing(gate_err) or (
f"Access denied for '{model_name}'. This model is gated or private. "
f"Please add a Hugging Face token with access and try again."
)
diff --git a/studio/backend/hub/browser_session.py b/studio/backend/hub/browser_session.py
new file mode 100644
index 00000000000..caa76300f7b
--- /dev/null
+++ b/studio/backend/hub/browser_session.py
@@ -0,0 +1,22 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+from __future__ import annotations
+
+from fastapi import HTTPException, Request
+
+
+async def signed_in(request: Request) -> bool:
+ """Whether the request carries a valid Unsloth session, for routes that also serve anonymous loads."""
+ from fastapi.security import HTTPAuthorizationCredentials
+
+ from auth.authentication import get_current_subject
+
+ scheme, _, token = (request.headers.get("authorization") or "").partition(" ")
+ if scheme.lower() != "bearer" or not token:
+ return False
+ try:
+ await get_current_subject(HTTPAuthorizationCredentials(scheme = "Bearer", credentials = token))
+ except HTTPException:
+ return False
+ return True
diff --git a/studio/backend/hub/endpoint_proxy.py b/studio/backend/hub/endpoint_proxy.py
new file mode 100644
index 00000000000..1ebd6d5317d
--- /dev/null
+++ b/studio/backend/hub/endpoint_proxy.py
@@ -0,0 +1,208 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Same-origin relays to a custom Hub endpoint and datasets server.
+
+The page's CSP is fixed when it is served, and the desktop app's when it launches, so the
+browser reaches an endpoint saved later through these. The Unsloth session goes in
+``Authorization``; the browser's Hugging Face token travels in ``X-HF-Authorization`` and is
+sent on to the configured endpoint only. The first path segment names the endpoint, so the
+page's caches, keyed on its Hub URL, never mix two endpoints' answers.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import hashlib
+import hmac
+import re
+import secrets
+import threading
+import weakref
+from http.cookiejar import CookieJar, DefaultCookiePolicy
+from typing import Callable, Optional
+from urllib.parse import unquote, urlsplit
+
+import httpx
+from fastapi import APIRouter, Request, Response
+from fastapi.responses import JSONResponse, RedirectResponse, StreamingResponse
+from starlette.background import BackgroundTask
+
+from hub.browser_session import signed_in
+from utils.client_ip import client_ip
+from utils.hf_endpoint import endpoint_is_reachable_by, is_loopback_host
+
+HUB_PREFIX = "/api/hub/proxy"
+DATASETS_SERVER_PREFIX = "/api/hub/datasets-server-proxy"
+TOKEN_HEADER = "X-HF-Authorization"
+UPSTREAM_HEADER = "X-Hub-Upstream"
+
+_PASSED_HEADERS = (
+ "content-type",
+ "etag",
+ "last-modified",
+ "cache-control",
+ "x-repo-commit",
+ "x-linked-etag",
+ "x-linked-size",
+ "x-error-code",
+ "x-error-message",
+ "x-total-count",
+)
+EXPOSED_HEADERS = ("Link", UPSTREAM_HEADER, *_PASSED_HEADERS[1:])
+
+_clients: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, httpx.AsyncClient]" = (
+ weakref.WeakKeyDictionary()
+)
+_clients_lock = threading.Lock()
+transport: Optional[httpx.AsyncBaseTransport] = None
+
+
+def _client() -> httpx.AsyncClient:
+ loop = asyncio.get_running_loop()
+ with _clients_lock:
+ http = _clients.get(loop)
+ if http is None:
+ # httpx drops Authorization when a redirect leaves the endpoint's origin (LFS CDNs).
+ http = _clients[loop] = httpx.AsyncClient(
+ timeout = httpx.Timeout(30.0, connect = 10.0),
+ follow_redirects = True,
+ cookies = CookieJar(DefaultCookiePolicy(allowed_domains = [])),
+ transport = transport,
+ headers = {"User-Agent": "unsloth-studio"},
+ )
+ return http
+
+
+# Keyed per process: /api/health is unauthenticated, and a bare hash of a private host:port is brute-forceable.
+_TAG_KEY = secrets.token_bytes(32)
+
+
+def _tag(upstream: str) -> str:
+ return hmac.new(_TAG_KEY, upstream.encode(), hashlib.sha256).hexdigest()[:12]
+
+
+def relay_path(prefix: str, upstream: str, default: str) -> Optional[str]:
+ """The relay's path for ``upstream``, or None when the browser can use the default directly."""
+ return None if upstream == default else f"{prefix}/{_tag(upstream)}"
+
+
+def _refuse(status: int, message: str) -> JSONResponse:
+ return JSONResponse(
+ {"error": message}, status_code = status, headers = {"X-Error-Message": message}
+ )
+
+
+def _rebase_link(link: str, upstream: str, base: str) -> str:
+ """Point pagination links at the proxy; a mirror may name itself or huggingface.co."""
+ prefix = urlsplit(upstream).path.rstrip("/")
+ parts = []
+ for part in link.split(","):
+ target, sep, params = part.strip().partition(">")
+ if not target.startswith("<"):
+ parts.append(part.strip())
+ continue
+ url = urlsplit(target[1:])
+ path = url.path[len(prefix) :] if prefix and url.path.startswith(prefix + "/") else url.path
+ parts.append(f"<{base}{path}{'?' + url.query if url.query else ''}{sep}{params}")
+ return ", ".join(parts)
+
+
+ANONYMOUS_ASSET_LIMIT = 20 * 1024 * 1024
+_ASSET_PATH = re.compile(r"^/(?:(?:datasets|spaces)/)?[^/]+/[^/]+/(?:resolve|raw)/[^/]+/.")
+# Served under Studio's origin, so only inert raster images, never markup or scripts.
+_ASSET_HEADERS = {
+ "X-Content-Type-Options": "nosniff",
+ "Content-Security-Policy": "default-src 'none'; sandbox",
+}
+
+
+async def _capped(chunks, limit: int):
+ """Bound what a signed-out README asset load can pull through the server."""
+ sent = 0
+ async for chunk in chunks:
+ sent += len(chunk)
+ if sent > limit:
+ raise RuntimeError("anonymous relay limit exceeded")
+ yield chunk
+
+
+def _saved_only(endpoint: str) -> bool:
+ """A redirect's Location would name an endpoint the owner-only settings route keeps private."""
+ from utils.hub_settings import saved_only_endpoints
+ return endpoint in saved_only_endpoints()
+
+
+def build_router(prefix: str, upstream: Callable[[], str], *, anonymous_pages: bool) -> APIRouter:
+ """``anonymous_pages`` redirects credential-less loads outside ``/api`` (README images,
+ repository links) to the endpoint; a settings-saved endpoint seen from off the host is
+ relayed without credentials instead, so its URL stays private."""
+ router = APIRouter()
+
+ @router.api_route("/{path:path}", methods = ["GET", "HEAD"])
+ async def relay(path: str, request: Request) -> Response:
+ raw = (request.scope.get("raw_path") or request.url.path.encode()).decode("latin-1")
+ tag, _, rest = raw.partition(prefix)[2].removeprefix("/").partition("/")
+ endpoint = upstream()
+ if tag != _tag(endpoint):
+ return _refuse(409, "The Hub endpoint changed. Reload to browse it.")
+ rest = f"/{rest}"
+ segments = [unquote(segment) for segment in rest.split("/")]
+ # Re-split: an encoded slash can hide a dot segment from this check but not from the endpoint.
+ if any(part in (".", "..") or "\\" in part for s in segments for part in s.split("/")):
+ return _refuse(400, "Invalid path.")
+ query = request.scope.get("query_string", b"").decode("latin-1")
+ target = f"{endpoint}{rest}{'?' + query if query else ''}"
+
+ anonymous = not await signed_in(request)
+ if anonymous:
+ if (
+ "authorization" in request.headers
+ or not anonymous_pages
+ or segments[1:2] == ["api"]
+ or not endpoint_is_reachable_by(endpoint, client_ip(request))
+ ):
+ return _refuse(401, "Sign in again to browse the Hub.")
+ # A Location would name an endpoint only the owner may read: relay the asset instead.
+ if not (_saved_only(endpoint) and not is_loopback_host(client_ip(request))):
+ return RedirectResponse(target, status_code = 302)
+ if not _ASSET_PATH.match(rest):
+ return _refuse(401, "Sign in again to browse the Hub.")
+
+ headers = {"Accept": request.headers.get("accept", "*/*")}
+ if not anonymous and (token := request.headers.get(TOKEN_HEADER)):
+ headers["Authorization"] = token
+ http = _client()
+ try:
+ answer = await http.send(
+ http.build_request(request.method, target, headers = headers), stream = True
+ )
+ except httpx.HTTPError as exc:
+ return _refuse(502, f"The Hub endpoint could not be reached ({type(exc).__name__}).")
+ body = answer.aiter_bytes()
+ if anonymous:
+ kind = answer.headers.get("content-type", "").split(";")[0].strip().lower()
+ if answer.is_success and (not kind.startswith("image/") or kind == "image/svg+xml"):
+ await answer.aclose()
+ return _refuse(401, "Sign in again to browse the Hub.")
+ if int(answer.headers.get("content-length") or 0) > ANONYMOUS_ASSET_LIMIT:
+ await answer.aclose()
+ return _refuse(413, "Sign in to download files this large.")
+ body = _capped(body, ANONYMOUS_ASSET_LIMIT)
+ passed = {name: answer.headers[name] for name in _PASSED_HEADERS if name in answer.headers}
+ if link := answer.headers.get("link"):
+ path = f"{request.scope.get('root_path', '')}{prefix}/{tag}"
+ passed["Link"] = _rebase_link(
+ link, endpoint, str(request.url.replace(path = path, query = ""))
+ )
+ passed[UPSTREAM_HEADER] = "1"
+ if anonymous:
+ passed.update(_ASSET_HEADERS)
+ return StreamingResponse(
+ body,
+ status_code = answer.status_code,
+ headers = passed,
+ background = BackgroundTask(answer.aclose),
+ )
+
+ return router
diff --git a/studio/backend/hub/modelscope/__init__.py b/studio/backend/hub/modelscope/__init__.py
new file mode 100644
index 00000000000..8f44e8d9e86
--- /dev/null
+++ b/studio/backend/hub/modelscope/__init__.py
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""ModelScope as a model source, served as the Hugging Face Hub API."""
diff --git a/studio/backend/hub/modelscope/router.py b/studio/backend/hub/modelscope/router.py
new file mode 100644
index 00000000000..f6b7341e82f
--- /dev/null
+++ b/studio/backend/hub/modelscope/router.py
@@ -0,0 +1,333 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The Hugging Face Hub API, read-only, over ModelScope: ``HF_ENDPOINT`` points here.
+
+A loopback server on its own thread serves this process and its workers, since a
+synchronous Hub call made on the main event loop could never be answered by it;
+the browser uses a mount in the main app, whose catalog routes need Studio auth.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import re
+import socket
+import threading
+import time
+from typing import Awaitable, Callable, Optional
+from urllib.parse import urlencode
+
+from fastapi import APIRouter, Depends, FastAPI, HTTPException, Request, Response
+from fastapi.responses import JSONResponse, RedirectResponse
+
+from hub.browser_session import signed_in
+from hub.modelscope import upstream as ms
+from loggers import get_logger
+
+logger = get_logger(__name__)
+
+BROWSER_PREFIX = "/api/hub/modelscope"
+
+_KINDS = {"models": "model", "datasets": "dataset"}
+_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$")
+_FILTER_SCAN_PAGES = 4
+# The browser cannot follow a redirect to ModelScope (no CORS), so small files are relayed.
+_RELAY_LIMIT = 16 * 1024 * 1024
+_UPLOAD_REFUSAL = (
+ "Uploading is unavailable while ModelScope is the model source. "
+ "Switch to Hugging Face in Settings to upload."
+)
+
+
+def _error(status: int, code: Optional[str], message: str) -> JSONResponse:
+ headers = {"X-Error-Message": message}
+ if code:
+ headers["X-Error-Code"] = code
+ return JSONResponse({"error": message}, status_code = status, headers = headers)
+
+
+async def _answer(call: Callable[[], Awaitable[Response]]) -> Response:
+ try:
+ return await call()
+ except ms.NotFound as exc:
+ return _error(404, exc.code, str(exc))
+ except ms.UpstreamError as exc:
+ logger.info("ModelScope adapter: %s", exc)
+ return _error(502, None, str(exc))
+
+
+def _repo(owner: str, name: str) -> str:
+ if not (_NAME.fullmatch(owner) and _NAME.fullmatch(name)) or ".." in name:
+ raise HTTPException(status_code = 400, detail = "Invalid repository id.")
+ return f"{owner}/{name}"
+
+
+def _file_path(path: str) -> str:
+ parts = path.split("/")
+ if (
+ len(path) > 1024
+ or "\\" in path
+ or any(part in ("", ".", "..") for part in parts)
+ or any(ord(char) < 0x20 for char in path)
+ ):
+ raise HTTPException(status_code = 400, detail = "Invalid file path.")
+ return path
+
+
+def _kind(kinds: str) -> str:
+ kind = _KINDS.get(kinds)
+ if kind is None:
+ raise HTTPException(status_code = 404, detail = "Not found.")
+ return kind
+
+
+async def _listing(kind: str, repo: str, revision: str) -> tuple[str, dict[str, dict]]:
+ sha = await ms.resolve_revision(kind, repo, revision)
+ return sha, await ms.files(kind, repo, sha)
+
+
+def build_router(*, browser: bool) -> APIRouter:
+ """``browser`` puts the catalog behind Studio auth and relays small files to signed-in callers."""
+ dependencies = []
+ if browser:
+ from auth.authentication import get_current_subject
+ dependencies = [Depends(get_current_subject)]
+ router = APIRouter()
+ api = APIRouter(dependencies = dependencies)
+
+ @api.get("/api/{kinds}")
+ async def search(kinds: str, request: Request) -> Response:
+ kind = _kind(kinds)
+ query = request.query_params
+ filters = [value for value in query.getlist("filter") if value]
+ pipeline_tag = query.get("pipeline_tag") or ""
+ sort = ms.SORTS.get(query.get("sort") or "", "default")
+ try:
+ page = max(1, int(query.get("p") or 1))
+ except ValueError:
+ page = 1
+
+ async def run() -> Response:
+ found, current, more = [], page, False
+ for _ in range(_FILTER_SCAN_PAGES if filters or pipeline_tag else 1):
+ items, total = await ms.search(
+ kind, query.get("search") or "", query.get("author") or "", sort, current
+ )
+ entries = [ms.hub_entry(kind, item) for item in items]
+ found.extend(entry for entry in entries if ms.matches(entry, filters, pipeline_tag))
+ more = bool(items) and current * ms.PAGE_SIZE < total
+ current += 1
+ if not more or found:
+ break
+ response = JSONResponse(found)
+ if more:
+ pairs = [(k, v) for k, v in query.multi_items() if k != "p"] + [("p", str(current))]
+ response.headers["Link"] = (
+ f'<{request.url.replace(query = urlencode(pairs))}>; rel="next"'
+ )
+ return response
+
+ return await _answer(run)
+
+ async def info(kind: str, repo: str, revision: str, request: Request) -> Response:
+ expand = set(request.query_params.getlist("expand"))
+ full = not expand or bool(expand & {"sha", "siblings"})
+
+ async def run() -> Response:
+ found = await ms.detail(kind, repo)
+ if found is None:
+ raise ms.repo_missing(repo)
+ body = ms.hub_entry(kind, {**found, "id": repo})
+ if full:
+ sha, files = await _listing(kind, repo, revision)
+ body["sha"] = ms.answered_commit(revision, sha)
+ body["siblings"] = [ms.sibling(path, meta) for path, meta in files.items()]
+ return JSONResponse(body)
+
+ return await _answer(run)
+
+ @api.get("/api/{kinds}/{owner}/{name}")
+ async def repo_info(kinds: str, owner: str, name: str, request: Request) -> Response:
+ return await info(_kind(kinds), _repo(owner, name), "main", request)
+
+ @api.get("/api/{kinds}/{owner}/{name}/revision/{revision:path}")
+ async def repo_info_at(
+ kinds: str, owner: str, name: str, revision: str, request: Request
+ ) -> Response:
+ return await info(_kind(kinds), _repo(owner, name), revision, request)
+
+ @api.get("/api/{kinds}/{owner}/{name}/auth-check")
+ async def auth_check(kinds: str, owner: str, name: str) -> Response:
+ kind, repo = _kind(kinds), _repo(owner, name)
+
+ async def run() -> Response:
+ await ms.repo_exists(kind, repo)
+ return JSONResponse({})
+
+ return await _answer(run)
+
+ @api.get("/api/{kinds}/{owner}/{name}/tree/{revision}")
+ @api.get("/api/{kinds}/{owner}/{name}/tree/{revision}/{path:path}")
+ async def tree(
+ kinds: str,
+ owner: str,
+ name: str,
+ revision: str,
+ request: Request,
+ path: str = "",
+ ) -> Response:
+ kind, repo = _kind(kinds), _repo(owner, name)
+ prefix = path.strip("/")
+ recursive = request.query_params.get("recursive", "").lower() in ("1", "true")
+
+ async def run() -> Response:
+ _, files = await _listing(kind, repo, revision)
+ dirs = ms.directories(files)
+ if prefix and prefix not in dirs:
+ raise ms.NotFound("EntryNotFound", f"{prefix} not found in {repo}.")
+ base = f"{prefix}/" if prefix else ""
+ entries = [
+ ms.tree_entry(candidate, files.get(candidate))
+ for candidate in sorted(dirs | set(files))
+ if candidate.startswith(base)
+ and candidate != prefix
+ and (recursive or "/" not in candidate[len(base) :])
+ ]
+ return JSONResponse(entries)
+
+ return await _answer(run)
+
+ @api.post("/api/{kinds}/{owner}/{name}/paths-info/{revision}")
+ async def paths_info(
+ kinds: str, owner: str, name: str, revision: str, request: Request
+ ) -> Response:
+ kind, repo = _kind(kinds), _repo(owner, name)
+ wanted = [path for path in (await request.form()).getlist("paths") if isinstance(path, str)]
+
+ async def run() -> Response:
+ _, files = await _listing(kind, repo, revision)
+ dirs = ms.directories(files)
+ return JSONResponse(
+ [
+ ms.tree_entry(path, files.get(path))
+ for path in dict.fromkeys(item.strip("/") for item in wanted)
+ if path in files or path in dirs
+ ]
+ )
+
+ return await _answer(run)
+
+ router.include_router(api)
+
+ async def resolve(kind: str, repo: str, revision: str, path: str, request: Request) -> Response:
+ path = _file_path(path)
+ session = browser and await signed_in(request)
+ if browser and not session:
+ if "authorization" in request.headers:
+ # A stale session: the page refreshes it on a 401, but cannot read a cross-origin redirect.
+ return _error(401, None, "Sign in again to browse ModelScope.")
+ return RedirectResponse(ms.branch_url(kind, repo, revision, path), status_code = 302)
+
+ async def run() -> Response:
+ sha, files = await _listing(kind, repo, revision)
+ meta = files.get(path)
+ if meta is None:
+ raise ms.NotFound("EntryNotFound", f"{path} not found in {repo}.")
+ etag = f'"{meta["sha256"]}"'
+ headers = {
+ "X-Repo-Commit": ms.answered_commit(revision, sha),
+ "ETag": etag,
+ "X-Linked-Etag": etag,
+ "X-Linked-Size": str(meta["size"]),
+ "Accept-Ranges": "bytes",
+ }
+ upstream = ms.file_url(kind, repo, sha, path)
+ if request.method == "GET" and session and meta["size"] <= _RELAY_LIMIT:
+ return await _relay(upstream, headers)
+ return RedirectResponse(upstream, status_code = 302, headers = headers)
+
+ return await _answer(run)
+
+ @router.api_route("/{owner}/{name}/resolve/{revision}/{path:path}", methods = ["GET", "HEAD"])
+ async def resolve_model(
+ owner: str, name: str, revision: str, path: str, request: Request
+ ) -> Response:
+ return await resolve("model", _repo(owner, name), revision, path, request)
+
+ @router.api_route(
+ "/datasets/{owner}/{name}/resolve/{revision}/{path:path}", methods = ["GET", "HEAD"]
+ )
+ async def resolve_dataset(
+ owner: str, name: str, revision: str, path: str, request: Request
+ ) -> Response:
+ return await resolve("dataset", _repo(owner, name), revision, path, request)
+
+ @router.api_route("/", methods = ["GET", "HEAD"])
+ async def root() -> Response:
+ return Response(status_code = 200 if await ms.reachable() else 503)
+
+ @router.get("/datasets/{owner}/{name}")
+ async def dataset_page(owner: str, name: str) -> Response:
+ return RedirectResponse(ms.page_url("dataset", _repo(owner, name)))
+
+ @router.get("/{owner}/{name}")
+ async def model_page(owner: str, name: str) -> Response:
+ return RedirectResponse(ms.page_url("model", _repo(owner, name)))
+
+ @router.api_route("/{rest:path}", methods = ["POST", "PUT", "PATCH", "DELETE"])
+ async def refuse_writes(rest: str) -> Response:
+ return _error(403, None, _UPLOAD_REFUSAL)
+
+ return router
+
+
+async def _relay(url: str, headers: dict) -> Response:
+ http, limiter = ms.client()
+ async with limiter:
+ try:
+ response = await http.get(url, follow_redirects = True)
+ except Exception as exc: # noqa: BLE001 - any transport failure is an upstream one
+ raise ms.UpstreamError(f"ModelScope request failed: {type(exc).__name__}") from exc
+ if response.status_code != 200:
+ raise ms.UpstreamError(f"ModelScope answered {response.status_code}")
+ return Response(
+ content = response.content,
+ media_type = response.headers.get("content-type", "application/octet-stream"),
+ headers = headers,
+ )
+
+
+_listener_lock = threading.Lock()
+_listener_url: Optional[str] = None
+
+
+def internal_endpoint() -> str:
+ """The loopback listener's base URL. Started on first use and kept for the process, so
+ workers spawned under it keep working after the source changes."""
+ global _listener_url
+ with _listener_lock:
+ if _listener_url is not None:
+ return _listener_url
+ import uvicorn
+
+ app = FastAPI(docs_url = None, redoc_url = None, openapi_url = None)
+ app.include_router(build_router(browser = False))
+ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+ sock.bind(("127.0.0.1", 0))
+ server = uvicorn.Server(
+ uvicorn.Config(app, log_level = "warning", access_log = False, lifespan = "off")
+ )
+ threading.Thread(
+ target = lambda: asyncio.run(server.serve(sockets = [sock])),
+ name = "modelscope-hub",
+ daemon = True,
+ ).start()
+ deadline = time.monotonic() + 10
+ while not server.started:
+ if time.monotonic() > deadline:
+ raise RuntimeError("The ModelScope adapter did not start.")
+ time.sleep(0.02)
+ _listener_url = f"http://127.0.0.1:{sock.getsockname()[1]}"
+ return _listener_url
diff --git a/studio/backend/hub/modelscope/upstream.py b/studio/backend/hub/modelscope/upstream.py
new file mode 100644
index 00000000000..30813b86983
--- /dev/null
+++ b/studio/backend/hub/modelscope/upstream.py
@@ -0,0 +1,431 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""ModelScope's public API, answered in Hugging Face Hub shapes."""
+
+from __future__ import annotations
+
+import asyncio
+import re
+import threading
+import time
+import weakref
+from typing import Any, Awaitable, Callable, Optional
+
+import httpx
+
+from hub.utils.hf_errors import not_on_modelscope
+
+MODELSCOPE = "https://www.modelscope.cn"
+PAGE_SIZE = 50
+
+_SHA = re.compile(r"^[0-9a-f]{40}$")
+_DEFAULT_REVISIONS = {"", "main", "master", "HEAD", "refs/heads/main", "refs/heads/master"}
+_HIDDEN_DATASET_FILES = {"dataset_infos.json"}
+_TREE_PAGE = 500
+_TREE_MAX_PAGES = 200
+
+# HF ids ModelScope hosts under another org; only copies whose files all match (LFS sha256, else size).
+_ALIASES = {
+ "model": {
+ "unslothai/Qwen3-ASR-0.6B-GGUF": "ggml-org/Qwen3-ASR-0.6B-GGUF",
+ "unslothai/Qwen3-ASR-1.7B-GGUF": "ggml-org/Qwen3-ASR-1.7B-GGUF",
+ "MiniMaxAI/MiniMax-H3": "MiniMax/MiniMax-H3",
+ "MiniMaxAI/MiniMax-Music3": "MiniMax/MiniMax-Music3",
+ "OpenMOSS-Team/MOSS-TTS-Local-Transformer-v1.5": "openmoss/MOSS-TTS-Local-Transformer-v1.5",
+ "OpenMOSS-Team/MOSS-TTS-Nano-100M": "openmoss/MOSS-TTS-Nano-100M",
+ "OpenMOSS-Team/MOSS-Audio-Tokenizer-v2": "openmoss/MOSS-Audio-Tokenizer-v2",
+ "OpenMOSS-Team/MOSS-Audio-Tokenizer-Nano": "openmoss/MOSS-Audio-Tokenizer-Nano",
+ "Shakker-Labs/FLUX.1-dev-ControlNet-Union-Pro": "AI-ModelScope/FLUX.1-dev-ControlNet-Union-Pro",
+ },
+ "dataset": {
+ "openai/gsm8k": "AI-ModelScope/gsm8k",
+ "unsloth/alpaca-cleaned": "AI-ModelScope/alpaca-cleaned",
+ "garage-bAInd/Open-Platypus": "swift/Open-Platypus",
+ "AI4Math/MathVista": "evalscope/MathVista",
+ "AI4Math/MathVerse": "evalscope/MathVerse",
+ "ylecun/mnist": "modelscope/mnist",
+ "MrDragonFox/Elise": "AI-ModelScope/Elise",
+ "tatsu-lab/alpaca": "OmniData/alpaca",
+ "philschmid/guanaco-sharegpt-style": "OmniData/guanaco-sharegpt-style",
+ },
+}
+_ALIAS_INDEX = {
+ kind: {repo.casefold(): target for repo, target in table.items()}
+ for kind, table in _ALIASES.items()
+}
+
+
+def upstream_id(kind: str, repo: str) -> str:
+ return _ALIAS_INDEX[kind].get(repo.casefold(), repo)
+
+
+# HF pins missing from ModelScope history -> matching ModelScope commit; unlisted pins miss (a pin guards remote code).
+_PINNED_COMMITS = {
+ (
+ "model",
+ "ibm-research/dac.speech.v1.0",
+ "1ea7f64cd0678415e2d8c32d67b190722cb9b149",
+ ): "3268c083add6c07ae3c4a36650863eb2c3bb6717",
+}
+
+
+def upstream_commit(kind: str, repo: str, sha: str) -> str:
+ return _PINNED_COMMITS.get((kind, repo.casefold(), sha.lower()), sha.lower())
+
+
+def answered_commit(revision: str, sha: str) -> str:
+ """A full-sha request is answered under that commit, so a mapped pin caches as the pin."""
+ return revision.lower() if _SHA.fullmatch(revision.lower()) else sha
+
+
+class UpstreamError(Exception):
+ """ModelScope could not answer. Never read as absence."""
+
+
+class NotFound(Exception):
+ def __init__(self, code: str, message: str):
+ super().__init__(message)
+ self.code = code
+
+
+def repo_missing(repo: str) -> NotFound:
+ return NotFound("RepoNotFound", not_on_modelscope(repo))
+
+
+class _TTLCache:
+ def __init__(self, max_entries: int = 512):
+ self._data: dict[Any, tuple[float, Any]] = {}
+ self._lock = threading.Lock()
+ self._max = max_entries
+
+ def get(self, key):
+ with self._lock:
+ hit = self._data.get(key)
+ return hit[1] if hit is not None and hit[0] > time.monotonic() else None
+
+ def put(self, key, value, ttl: float) -> None:
+ with self._lock:
+ if len(self._data) >= self._max:
+ self._data.clear()
+ self._data[key] = (time.monotonic() + ttl, value)
+
+ def clear(self) -> None:
+ with self._lock:
+ self._data.clear()
+
+
+_cache = _TTLCache()
+_clients: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, tuple[httpx.AsyncClient, asyncio.Semaphore]]" = weakref.WeakKeyDictionary()
+_clients_lock = threading.Lock()
+transport: Optional[httpx.AsyncBaseTransport] = None
+
+
+def client() -> tuple[httpx.AsyncClient, asyncio.Semaphore]:
+ loop = asyncio.get_running_loop()
+ with _clients_lock:
+ entry = _clients.get(loop)
+ if entry is None:
+ entry = _clients[loop] = (
+ httpx.AsyncClient(
+ base_url = MODELSCOPE,
+ timeout = httpx.Timeout(20.0, connect = 10.0),
+ transport = transport,
+ headers = {"User-Agent": "unsloth-studio"},
+ ),
+ asyncio.Semaphore(8),
+ )
+ return entry
+
+
+async def _get(
+ path: str,
+ *,
+ params = None,
+ headers = None,
+) -> httpx.Response:
+ http, limiter = client()
+ async with limiter:
+ try:
+ return await http.get(path, params = params, headers = headers)
+ except httpx.HTTPError as exc:
+ raise UpstreamError(f"ModelScope request failed: {type(exc).__name__}") from exc
+
+
+async def _json(path: str, params = None) -> Any:
+ response = await _get(path, params = params)
+ if response.status_code == 404:
+ return None
+ if response.status_code != 200:
+ raise UpstreamError(f"ModelScope answered {response.status_code}")
+ try:
+ return response.json()
+ except ValueError as exc:
+ raise UpstreamError("ModelScope answered with invalid JSON") from exc
+
+
+def _cached(ttl: float):
+ def wrap(fn: Callable[..., Awaitable[Any]]):
+ async def inner(*args):
+ key = (fn.__name__, *args)
+ hit = _cache.get(key)
+ if hit is not None:
+ return hit
+ value = await fn(*args)
+ _cache.put(key, value, ttl)
+ return value
+
+ return inner
+
+ return wrap
+
+
+def parse_ref_advertisement(body: bytes) -> dict[str, str]:
+ refs: dict[str, str] = {}
+ pos = 0
+ while pos + 4 <= len(body):
+ try:
+ size = int(body[pos : pos + 4], 16)
+ except ValueError:
+ break
+ if size < 4:
+ pos += 4
+ continue
+ line = body[pos + 4 : pos + size].split(b"\0", 1)[0].decode("ascii", "replace").strip()
+ pos += size
+ sha, _, ref = line.partition(" ")
+ if _SHA.fullmatch(sha) and ref:
+ refs[ref] = sha
+ return refs
+
+
+@_cached(60)
+async def _refs(kind: str, repo: str) -> dict[str, str]:
+ # The REST API names no commit for a branch; git does.
+ prefix = "/datasets" if kind == "dataset" else ""
+ response = await _get(
+ f"{prefix}/{upstream_id(kind, repo)}.git/info/refs",
+ params = {"service": "git-upload-pack"},
+ headers = {"User-Agent": "git/2.45.0"},
+ )
+ if response.status_code in (401, 403, 404):
+ raise repo_missing(repo)
+ if response.status_code != 200:
+ raise UpstreamError(f"ModelScope refs answered {response.status_code}")
+ refs = parse_ref_advertisement(response.content)
+ if not refs:
+ raise UpstreamError("ModelScope advertised no refs")
+ return refs
+
+
+async def repo_exists(kind: str, repo: str) -> None:
+ await _refs(kind, repo)
+
+
+async def resolve_revision(kind: str, repo: str, revision: str) -> str:
+ if _SHA.fullmatch(revision.lower()):
+ return upstream_commit(kind, repo, revision)
+ refs = await _refs(kind, repo)
+ if revision in _DEFAULT_REVISIONS:
+ sha = refs.get("HEAD") or refs.get("refs/heads/master")
+ else:
+ name = revision.removeprefix("refs/heads/").removeprefix("refs/tags/")
+ sha = refs.get(f"refs/heads/{name}") or refs.get(f"refs/tags/{name}")
+ if not sha:
+ raise NotFound("RevisionNotFound", f"Revision {revision} not found on ModelScope.")
+ return sha
+
+
+async def _file_entries(kind: str, repo: str, sha: str) -> list[dict]:
+ source = upstream_id(kind, repo)
+ if kind == "model":
+ data = await _json(
+ f"/api/v1/models/{source}/repo/files", {"Revision": sha, "Recursive": "true"}
+ )
+ if data is None:
+ raise repo_missing(repo)
+ return (data.get("Data") or {}).get("Files") or []
+ entries: list[dict] = []
+ for page in range(1, _TREE_MAX_PAGES + 1):
+ data = await _json(
+ f"/api/v1/datasets/{source}/repo/tree",
+ {
+ "Revision": sha,
+ "Root": "/",
+ "Recursive": "True",
+ "PageNumber": page,
+ "PageSize": _TREE_PAGE,
+ },
+ )
+ if data is None:
+ raise repo_missing(repo)
+ batch = (data.get("Data") or {}).get("Files") or []
+ entries.extend(batch)
+ if len(batch) < _TREE_PAGE:
+ return entries
+ raise UpstreamError(f"ModelScope lists more than {len(entries)} files for {repo}")
+
+
+@_cached(300)
+async def files(kind: str, repo: str, sha: str) -> dict[str, dict]:
+ """Every file at ``sha`` by path. The one place existence is decided: ModelScope
+ answers a missing file with a 500, not a 404."""
+ found = {}
+ for entry in await _file_entries(kind, repo, sha):
+ path = entry.get("Path")
+ if entry.get("Type") == "tree" or not isinstance(path, str):
+ continue
+ if kind == "dataset" and path in _HIDDEN_DATASET_FILES:
+ continue
+ found[path] = {
+ "size": int(entry.get("Size") or 0),
+ "sha256": str(entry.get("Sha256") or ""),
+ "lfs": bool(entry.get("IsLFS")),
+ }
+ return found
+
+
+@_cached(300)
+async def detail(kind: str, repo: str) -> Optional[dict]:
+ data = await _json(f"/openapi/v1/{kind}s/{upstream_id(kind, repo)}")
+ return (data or {}).get("data") or None
+
+
+@_cached(60)
+async def search(kind: str, query: str, author: str, sort: str, page: int) -> tuple[list, int]:
+ params: dict[str, Any] = {"page_number": page, "page_size": PAGE_SIZE}
+ if query:
+ params["search"] = query
+ if author:
+ params["author"] = author
+ if sort != "default":
+ params["sort"] = sort
+ body = ((await _json(f"/openapi/v1/{kind}s", params)) or {}).get("data") or {}
+ return body.get(f"{kind}s") or [], int(body.get("total_count") or 0)
+
+
+@_cached(30)
+async def reachable() -> bool:
+ try:
+ response = await _get("/openapi/v1/models", params = {"page_size": 1})
+ except UpstreamError:
+ return False
+ return response.status_code < 500
+
+
+def page_url(kind: str, repo: str) -> str:
+ return f"{MODELSCOPE}/{kind}s/{upstream_id(kind, repo)}"
+
+
+def file_url(kind: str, repo: str, sha: str, path: str) -> str:
+ from urllib.parse import quote
+ return f"{page_url(kind, repo)}/resolve/{sha}/{quote(path)}"
+
+
+def branch_url(kind: str, repo: str, revision: str, path: str) -> str:
+ """``path`` on a named revision, without asking ModelScope; its default branch is ``master``."""
+ from urllib.parse import quote
+
+ name = revision.removeprefix("refs/heads/").removeprefix("refs/tags/")
+ if revision in _DEFAULT_REVISIONS:
+ ref = "master"
+ elif _SHA.fullmatch(name.lower()):
+ ref = upstream_commit(kind, repo, name)
+ else:
+ ref = quote(name, safe = "")
+ return file_url(kind, repo, ref, path)
+
+
+SORTS = {"downloads": "downloads", "likes": "likes", "lastModified": "last_modified"}
+_EPOCH = "1970-01-01T00:00:00Z"
+
+
+def _tag_parts(tags) -> tuple[list[str], list[str], Optional[str], list[str]]:
+ libraries, hub_tags, model_type, tasks = [], [], None, []
+ for tag in tags or []:
+ if not isinstance(tag, str):
+ continue
+ prefix, _, value = tag.partition(":")
+ if not value:
+ continue
+ if prefix == "library":
+ libraries.append("transformers" if value == "transformer" else value)
+ elif prefix == "model_type":
+ model_type = value
+ elif prefix == "task":
+ tasks.append(value)
+ elif prefix == "license":
+ hub_tags.append(tag)
+ elif prefix == "custom_tag":
+ hub_tags.append(value)
+ return libraries, hub_tags, model_type, tasks
+
+
+def hub_entry(kind: str, item: dict) -> dict:
+ repo = str(item.get("id") or "")
+ libraries, tags, model_type, tag_tasks = _tag_parts(item.get("tags"))
+ tasks = [task for task in (item.get("tasks") or []) if isinstance(task, str)] or tag_tasks
+ downloads = int(item.get("downloads") or 0)
+ entry = {
+ "_id": repo,
+ "id": repo,
+ "author": repo.split("/", 1)[0],
+ "private": bool(item.get("private")),
+ "gated": "manual" if item.get("gated") else False,
+ "downloads": downloads,
+ "downloadsAllTime": downloads,
+ "likes": int(item.get("likes") or 0),
+ "lastModified": item.get("last_modified") or item.get("created_at") or _EPOCH,
+ "createdAt": item.get("created_at") or _EPOCH,
+ "tags": list(dict.fromkeys(libraries + tags + tasks)),
+ }
+ if kind == "dataset":
+ entry["cardData"] = {"pretty_name": item.get("display_name")}
+ return entry
+ params = int(item.get("params") or 0)
+ entry["modelId"] = repo
+ entry["pipeline_tag"] = tasks[0] if tasks else None
+ entry["library_name"] = (
+ "transformers" if "transformers" in libraries else next(iter(libraries), None)
+ )
+ if model_type:
+ entry["config"] = {"model_type": model_type}
+ if params and "gguf" in libraries:
+ entry["gguf"] = {"total": params, **({"architecture": model_type} if model_type else {})}
+ elif params and "safetensors" in libraries:
+ entry["safetensors"] = {"total": params, "parameters": {"BF16": params}}
+ return entry
+
+
+def matches(entry: dict, filters: list[str], pipeline_tag: str) -> bool:
+ tags = {tag.lower() for tag in entry.get("tags", [])}
+ pipeline_tag = pipeline_tag.lower()
+ if pipeline_tag and pipeline_tag not in tags and entry.get("pipeline_tag") != pipeline_tag:
+ return False
+ library = str(entry.get("library_name")).lower()
+ return all(f.lower() in tags or f.lower() == library for f in filters)
+
+
+def sibling(path: str, meta: dict) -> dict:
+ entry = {"rfilename": path, "size": meta["size"], "blobId": meta["sha256"]}
+ if meta["lfs"]:
+ entry["lfs"] = {"sha256": meta["sha256"], "size": meta["size"], "pointerSize": 134}
+ return entry
+
+
+def tree_entry(path: str, meta: Optional[dict]) -> dict:
+ if meta is None:
+ return {"type": "directory", "oid": "", "size": 0, "path": path}
+ entry = {"type": "file", "oid": meta["sha256"], "size": meta["size"], "path": path}
+ if meta["lfs"]:
+ entry["lfs"] = {"oid": meta["sha256"], "size": meta["size"], "pointerSize": 134}
+ return entry
+
+
+def directories(paths) -> set[str]:
+ found = set()
+ for path in paths:
+ parts = path.split("/")[:-1]
+ for index in range(1, len(parts) + 1):
+ found.add("/".join(parts[:index]))
+ return found
diff --git a/studio/backend/hub/routes/datasets.py b/studio/backend/hub/routes/datasets.py
index 361ab0f7fce..1b632fb0fd4 100644
--- a/studio/backend/hub/routes/datasets.py
+++ b/studio/backend/hub/routes/datasets.py
@@ -18,6 +18,8 @@ from hub.schemas.datasets import (
CheckFormatRequest,
CheckFormatResponse,
DeleteCachedDatasetResponse,
+ HubDatasetOptionsRequest,
+ HubDatasetOptionsResponse,
LocalDatasetOptionsRequest,
LocalDatasetOptionsResponse,
LocalDatasetsResponse,
@@ -71,6 +73,15 @@ def get_local_dataset_options(
return local_options.local_dataset_options(request)
[email protected]("/hub-options", response_model = HubDatasetOptionsResponse)
+def get_hub_dataset_options(
+ request: HubDatasetOptionsRequest,
+ hf_token: HfTokenArg = Depends(get_request_hf_token),
+ current_subject: str = Depends(get_current_subject),
+) -> HubDatasetOptionsResponse:
+ return local_options.hub_dataset_options(request, hf_token)
+
+
@router.delete("/cached", response_model = DeleteCachedDatasetResponse)
async def delete_cached_dataset(
repo_id: str = Body(..., embed = True),
diff --git a/studio/backend/hub/schemas/datasets.py b/studio/backend/hub/schemas/datasets.py
index 6f17cc7a590..d90265dc8e3 100644
--- a/studio/backend/hub/schemas/datasets.py
+++ b/studio/backend/hub/schemas/datasets.py
@@ -124,6 +124,14 @@ class LocalDatasetOptionsResponse(BaseModel):
splits: List[DatasetSplitOption] = Field(default_factory = list)
+class HubDatasetOptionsRequest(BaseModel):
+ dataset_name: str
+
+
+class HubDatasetOptionsResponse(BaseModel):
+ splits: List[DatasetSplitOption] = Field(default_factory = list)
+
+
class DeleteCachedDatasetResponse(BaseModel):
status: str
repo_id: str
diff --git a/studio/backend/hub/services/datasets/formatting.py b/studio/backend/hub/services/datasets/formatting.py
index e76abd6931b..7e7d4ad2ed6 100644
--- a/studio/backend/hub/services/datasets/formatting.py
+++ b/studio/backend/hub/services/datasets/formatting.py
@@ -43,7 +43,7 @@ from hub.utils.dataset_cache import (
from hub.utils.dataset_cache import refuse_unauthorized_dataset_preview
from hub.utils import download_registry
from hub.utils.dataset_format import check_dataset_format, format_dataset_preview
-from hub.utils.hf_errors import hf_error_status
+from hub.utils.hf_errors import hf_error_status, modelscope_missing
from hub.utils.paths import (
is_valid_repo_id as _is_valid_repo_id,
normalize_path,
@@ -589,7 +589,9 @@ def check_format_response(
except HTTPException:
raise
except Exception as e:
- scrubbed = download_registry.scrub_secrets(str(e), hf_token = hf_token)
+ scrubbed = modelscope_missing(e, request.dataset_name) or download_registry.scrub_secrets(
+ str(e), hf_token = hf_token
+ )
# Missing/gated/bad-token and malformed names are client errors, not 500s.
status = hf_error_status(e)
if (
diff --git a/studio/backend/hub/services/datasets/local_options.py b/studio/backend/hub/services/datasets/local_options.py
index c48e58e8f67..f48709bb472 100644
--- a/studio/backend/hub/services/datasets/local_options.py
+++ b/studio/backend/hub/services/datasets/local_options.py
@@ -12,6 +12,8 @@ from typing import Any, Iterable, Optional
from hub.schemas.datasets import (
DatasetSplitOption,
+ HubDatasetOptionsRequest,
+ HubDatasetOptionsResponse,
LocalDatasetOptionsRequest,
LocalDatasetOptionsResponse,
)
@@ -36,6 +38,7 @@ _MAX_PROCESSED_METADATA_FILES = 256
_MAX_PROCESSED_WALK_DEPTH = 4
_MAX_OPTIONS = 2048
_MAX_OPTION_LENGTH = 128
+_HUB_INFO_TIMEOUT_SECONDS = 20.0
_CONFIG_RE = re.compile(r"[^<>:/\\|?*\x00-\x1f\x7f]+")
_SPLIT_RE = HF_DATASET_SPLIT_NAME_PATTERN
@@ -745,3 +748,52 @@ def local_dataset_options(request: LocalDatasetOptionsRequest) -> LocalDatasetOp
splits = _sorted_options(options, repo_id)
return LocalDatasetOptionsResponse(cache_available = True, splits = splits)
+
+
+def hub_dataset_options(
+ request: HubDatasetOptionsRequest, hf_token = None
+) -> HubDatasetOptionsResponse:
+ """Configs and splits resolved from the repo's files through ``HF_ENDPOINT``, as training
+ resolves them, for when no datasets-server answers (a mirror, ModelScope). Reads no rows."""
+ from datasets import DownloadConfig
+ from datasets.load import dataset_module_factory
+ from fastapi import HTTPException
+ from huggingface_hub import HfApi
+
+ from hub.services.models.account_access import account_hf_token
+ from hub.utils import download_registry
+ from hub.utils.dataset_cache import refuse_unauthorized_dataset_preview
+ from hub.utils.hf_errors import hf_error_status, modelscope_missing
+ from hub.utils.hf_tokens import recording_a_request_token_fetch
+
+ hf_token = account_hf_token(hf_token)
+ repo_id = request.dataset_name.strip()
+ if not is_valid_repo_id(repo_id):
+ raise HTTPException(status_code = 400, detail = "Invalid dataset name")
+ refuse_unauthorized_dataset_preview(hf_token, repo_id)
+ try:
+ with recording_a_request_token_fetch(hf_token, repo_id, "dataset"):
+ # Pinned: on "main", datasets asks a datasets-server for exported infos (100 s timeout).
+ sha = (
+ HfApi().dataset_info(repo_id, token = hf_token, timeout = _HUB_INFO_TIMEOUT_SECONDS).sha
+ )
+ module = dataset_module_factory(
+ repo_id, revision = sha, download_config = DownloadConfig(token = hf_token)
+ )
+ except Exception as exc:
+ detail = modelscope_missing(exc) or download_registry.scrub_secrets(
+ str(exc), hf_token = hf_token
+ )
+ status = hf_error_status(exc)
+ if status is None:
+ status = 404 if isinstance(exc, FileNotFoundError) else 400
+ raise HTTPException(status_code = status, detail = detail) from exc
+
+ # Builder configs only: card metadata can name configs with no files, which training rejects.
+ options: set[tuple[str, str]] = set()
+ for config in module.builder_configs_parameters.builder_configs or []:
+ _add_info_options(
+ options,
+ {"config_name": config.name, "splits": [str(s) for s in config.data_files or {}]},
+ )
+ return HubDatasetOptionsResponse(splits = _sorted_options(options, repo_id))
diff --git a/studio/backend/hub/services/models/account_access.py b/studio/backend/hub/services/models/account_access.py
index 2e7bd278376..98f3089b3f4 100644
--- a/studio/backend/hub/services/models/account_access.py
+++ b/studio/backend/hub/services/models/account_access.py
@@ -21,7 +21,7 @@ from pathlib import Path
from fastapi import HTTPException
from fastapi.responses import JSONResponse
-from huggingface_hub import HfApi
+from huggingface_hub import HfApi, constants as hf_constants
from auth import policy
from core.inference.gpu_arbiter import GpuBusyForAnotherAccountError
@@ -450,9 +450,9 @@ def require_installation_owner() -> None:
_PUBLIC_TTL = 300.0
_PRIVATE_TTL = 30.0
-_public_repos: dict[tuple[str, str], tuple[float, bool]] = {}
+_public_repos: dict[tuple[str, str, str], tuple[float, bool]] = {}
_public_lock = threading.Lock()
-_public_flights: dict[tuple[str, str], Future] = {}
+_public_flights: dict[tuple[str, str, str], Future] = {}
_PROBE_FANOUT = 8
@@ -489,6 +489,22 @@ def _remember_public_verdict(name: str, public: bool) -> None:
del verdicts[name]
else:
return
+ _write_public_verdicts(verdicts)
+
+
+def adopt_unnamed_public_proofs(endpoint: str) -> None:
+ """Name proofs recorded before proofs carried their endpoint after the one that recorded them."""
+ with _public_lock:
+ verdicts = _load_public_verdicts()
+ unnamed = [name for name in verdicts if "|" not in name]
+ if not unnamed:
+ return
+ for name in unnamed:
+ verdicts.setdefault(f"{endpoint.rstrip('/')}|{name}", verdicts.pop(name))
+ _write_public_verdicts(verdicts)
+
+
+def _write_public_verdicts(verdicts: dict[str, float]) -> None:
path = _public_verdicts_path()
try:
path.parent.mkdir(parents = True, exist_ok = True)
@@ -499,10 +515,17 @@ def _remember_public_verdict(name: str, public: bool) -> None:
pass
-def _hub_public_answer(repo_id: str, repo_type: str) -> bool | None:
+def _hub_public_answer(
+ repo_id: str,
+ repo_type: str,
+ *,
+ endpoint: str | None = None,
+) -> bool | None:
"""True when the Hub says public, False for private/gated/missing, None when unaskable."""
try:
- info = HfApi().repo_info(repo_id, repo_type = repo_type, token = False, timeout = 5.0)
+ info = HfApi(endpoint = endpoint).repo_info(
+ repo_id, repo_type = repo_type, token = False, timeout = 5.0
+ )
except Exception as exc: # noqa: BLE001 - classified below, never trusted as public
status = getattr(getattr(exc, "response", None), "status_code", None)
if status in _DEFINITIVE_HUB_STATUSES:
@@ -511,17 +534,29 @@ def _hub_public_answer(repo_id: str, repo_type: str) -> bool | None:
return getattr(info, "private", None) is False and not getattr(info, "gated", False)
+def _public_key(repo_id: str, repo_type: str) -> tuple[str, str, str]:
+ # A repo public on one Hub may be private, or someone else's, on another.
+ return (hf_constants.ENDPOINT.rstrip("/"), repo_type, repo_id.lower())
+
+
def _public_verdict(repo_id: str, repo_type: str) -> bool | None:
- key = (repo_type, repo_id.lower())
+ key = _public_key(repo_id, repo_type)
with _public_lock:
cached = _public_repos.get(key)
return cached[1] if cached is not None and cached[0] > time.monotonic() else None
+def _source_speaks_for_the_cache() -> bool:
+ from utils.hub_settings import MODELSCOPE, active_source
+ return active_source() != MODELSCOPE
+
+
def repo_is_public(repo_id: str, repo_type: str = "model") -> bool:
"""Only an anonymous Hub answer proves a shared-cache repo public."""
- key = (repo_type, repo_id.lower())
- name = f"{repo_type}:{repo_id.lower()}"
+ if not _source_speaks_for_the_cache():
+ return False
+ key = _public_key(repo_id, repo_type)
+ name = f"{key[0]}|{repo_type}:{repo_id.lower()}"
with _public_lock:
cached = _public_repos.get(key)
if cached is not None and cached[0] > time.monotonic():
@@ -533,7 +568,7 @@ def repo_is_public(repo_id: str, repo_type: str = "model") -> bool:
if not leading:
return flight.result()
try:
- answer = _hub_public_answer(repo_id, repo_type)
+ answer = _hub_public_answer(repo_id, repo_type, endpoint = key[0])
with _public_lock:
if answer is None:
public = name in _load_public_verdicts()
@@ -584,7 +619,8 @@ def _hub_probe_targets(references, repo_type: str, grants: set[str]) -> set[str]
unknown = {
repo_id: reference
for repo_id, reference in candidates.items()
- if (entry := _public_repos.get((repo_type, repo_id.lower()))) is None or entry[0] <= now
+ if (entry := _public_repos.get(_public_key(repo_id, repo_type))) is None
+ or entry[0] <= now
}
# A local path that happens to spell a repo id resolves against the cache, not the Hub.
return {
@@ -796,6 +832,11 @@ def authorize_download(repo_id: str, repo_type: str, hf_token) -> None:
"""A cache hit is not proof the requester may read private Hub content."""
if not managed_account():
return
+ if not _source_speaks_for_the_cache():
+ raise HTTPException(
+ status_code = 403,
+ detail = "Only the installation owner can download while ModelScope is the model source",
+ )
try:
api = HfApi()
token = account_hf_token(hf_token)
diff --git a/studio/backend/hub/services/models/gguf_variants.py b/studio/backend/hub/services/models/gguf_variants.py
index c5f5cbffaff..60fd0fc0a4c 100644
--- a/studio/backend/hub/services/models/gguf_variants.py
+++ b/studio/backend/hub/services/models/gguf_variants.py
@@ -24,7 +24,7 @@ from hub.schemas.inventory import GgufVariantDetail, GgufVariantsResponse
from hub.utils import download_manifest
from hub.utils import download_registry
from hub.utils import inventory_scan as hf_cache_scan
-from hub.utils.hf_errors import hf_error_status
+from hub.utils.hf_errors import hf_error_status, modelscope_missing
from hub.utils.hf_tokens import cached_read_refused as hub_cached_read_refused
from hub.utils.hf_cache_state import (
incomplete_blob_hash,
@@ -2096,7 +2096,9 @@ async def get_gguf_variants_answer(
except HTTPException:
raise
except Exception as e:
- scrubbed = download_registry.scrub_secrets(str(e), hf_token = hf_token)
+ scrubbed = modelscope_missing(e) or download_registry.scrub_secrets(
+ str(e), hf_token = hf_token
+ )
# Client-side HF error (missing repo, gated, bad token): pass the status through.
status = hf_error_status(e)
if status is not None:
diff --git a/studio/backend/hub/utils/download_registry.py b/studio/backend/hub/utils/download_registry.py
index 5489d474d37..a61a5275aad 100644
--- a/studio/backend/hub/utils/download_registry.py
+++ b/studio/backend/hub/utils/download_registry.py
@@ -32,6 +32,7 @@ from loggers import get_logger
from utils.process_lifetime import is_signalable_pid
from hub.utils import state_dir
+from hub.utils.hf_errors import modelscope_missing
from hub.utils.state_dir import RepoType
logger = get_logger(__name__)
@@ -97,7 +98,11 @@ def http_size_ceiling_reason(largest_file_bytes: Optional[int]) -> Optional[str]
def humanize_worker_error(text: str, *, largest_file_bytes: Optional[int] = None) -> str:
- """Replace the hub's misleading >50GB dependency error with the transport limit."""
+ """Name a repo missing on ModelScope, and replace the hub's misleading >50GB dependency
+ error with the transport limit."""
+ missing = modelscope_missing(text)
+ if missing:
+ return missing
if not text or _HTTP_SIZE_CEILING_MARKER not in text:
return text
reason = http_size_ceiling_reason(largest_file_bytes)
diff --git a/studio/backend/hub/utils/hf_errors.py b/studio/backend/hub/utils/hf_errors.py
index 69b4215ffa2..d11ce48ee68 100644
--- a/studio/backend/hub/utils/hf_errors.py
+++ b/studio/backend/hub/utils/hf_errors.py
@@ -1,12 +1,45 @@
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-"""Map Hugging Face Hub client-side errors to HTTP status codes."""
+"""Map Hugging Face Hub client-side errors to HTTP status codes and user-facing text."""
from __future__ import annotations
+import re
from typing import Optional
+_NOT_ON_MODELSCOPE = re.compile(
+ r"[\w.\-]+/[\w.\-]+ is not on ModelScope\. Switch the model source to Hugging Face in Settings to use it\."
+)
+
+
+def not_on_modelscope(repo: str) -> str:
+ return f"{repo} is not on ModelScope. Switch the model source to Hugging Face in Settings to use it."
+
+
+def modelscope_missing(error: object, repo: Optional[str] = None) -> Optional[str]:
+ """The ModelScope adapter's missing-repo sentence carried by an error chain or text, else None.
+
+ `datasets` drops the Hub's message for a missing dataset, so when no link carries the
+ sentence and `repo` is given, a DatasetNotFoundError under the ModelScope source answers for it.
+ """
+ links: dict[int, object] = {}
+ pending = [error]
+ while pending:
+ link = pending.pop()
+ if link is not None and id(link) not in links:
+ links[id(link)] = link
+ pending += [getattr(link, "__cause__", None), getattr(link, "__context__", None)]
+ for link in links.values():
+ found = _NOT_ON_MODELSCOPE.search(str(link))
+ if found:
+ return found.group(0)
+ if repo and any(type(link).__name__ == "DatasetNotFoundError" for link in links.values()):
+ from utils.hub_settings import MODELSCOPE, active_source
+ if active_source() == MODELSCOPE:
+ return not_on_modelscope(repo)
+ return None
+
def hf_error_status(exc: Exception) -> Optional[int]:
# Client-side HF errors should surface as the status they mean, not a generic 500.
diff --git a/studio/backend/hub/utils/hf_tokens.py b/studio/backend/hub/utils/hf_tokens.py
index baf17190987..79ea9a1d69b 100644
--- a/studio/backend/hub/utils/hf_tokens.py
+++ b/studio/backend/hub/utils/hf_tokens.py
@@ -115,21 +115,21 @@ _REPO_ACCESS_TTL_S = 60.0
_REPO_ACCESS_UNREACHABLE_TTL_S = 30.0
_REPO_ACCESS_CACHE_MAX = 1024
# ``None`` is a THIRD value, not a miss: "the Hub could not be asked", never a denial.
-_repo_access_cache: dict[tuple[str, str, str], tuple[float, Optional[bool]]] = {}
+_repo_access_cache: dict[tuple[str, ...], tuple[float, Optional[bool]]] = {}
_CACHE_MISS = object()
_repo_access_lock = threading.Lock()
# One probe per key: the probe runs outside _repo_access_lock, so a cold key would
# otherwise open a connection per caller.
-_repo_access_inflight: dict[tuple[str, str, str], threading.Lock] = {}
+_repo_access_inflight: dict[tuple[str, ...], threading.Lock] = {}
# An answered NO never expires and its eviction is remembered, since 429/5xx are "unaskable"
# yet reachable: a refused caller is otherwise one outage from access.
_DENIAL_MEMORY_MAX = 8192
-_denied_repo_access: dict[tuple[str, str, str], float] = {}
+_denied_repo_access: dict[tuple[str, ...], float] = {}
_denial_memory_is_complete = True
-def _remember_denial(key: tuple[str, str, str], now: float) -> None:
+def _remember_denial(key: tuple[str, ...], now: float) -> None:
global _denial_memory_is_complete
with _repo_access_lock:
_denied_repo_access.pop(key, None)
@@ -144,17 +144,17 @@ def _denial_memory_lost_an_entry() -> bool:
return not _denial_memory_is_complete
-def _forget_denial(key: tuple[str, str, str]) -> None:
+def _forget_denial(key: tuple[str, ...]) -> None:
with _repo_access_lock:
_denied_repo_access.pop(key, None)
-def _denial_is_remembered(key: tuple[str, str, str]) -> bool:
+def _denial_is_remembered(key: tuple[str, ...]) -> bool:
with _repo_access_lock:
return key in _denied_repo_access
-def _with_remembered_denial(key: tuple[str, str, str], verdict: Optional[bool]) -> Optional[bool]:
+def _with_remembered_denial(key: tuple[str, ...], verdict: Optional[bool]) -> Optional[bool]:
""" "Could not ask" reads as the last answer if that was no; once any refusal is evicted, no
key may claim it was never refused."""
if verdict is None and (_denial_is_remembered(key) or _denial_memory_lost_an_entry()):
@@ -807,7 +807,7 @@ def _hub_offline() -> bool:
return False
-def _cached_repo_access(key: tuple[str, str, str], now: float):
+def _cached_repo_access(key: tuple[str, ...], now: float):
"""``None`` is a verdict of its own ("could not be asked"), so a miss needs its own sentinel."""
cached = _repo_access_cache.get(key)
if cached is not None and cached[0] > now:
@@ -822,11 +822,13 @@ def _explicit_token_reaches_repo(
offline: bool = False,
) -> Optional[bool]:
# None asks the public question, under its own key: a public repo answers 200 for every
- # token, so a shared key would let any string claim that verdict.
+ # token, so a shared key would let any string claim that verdict. The endpoint too: the
+ # same repo id on another Hub is another repo.
key = (
repo_id.casefold(),
repo_type,
hashlib.sha256(token.encode()).hexdigest()[:16] if token else "anonymous",
+ _probe_endpoint(),
)
cached = _cached_repo_access(key, time.monotonic())
if cached is not _CACHE_MISS:
@@ -840,7 +842,7 @@ def _explicit_token_reaches_repo(
if cached is not _CACHE_MISS:
return _with_remembered_denial(key, cached) # type: ignore[arg-type]
try:
- allowed = _probe_repo_access(repo_id, token, repo_type)
+ allowed = _probe_repo_access(repo_id, token, repo_type, endpoint = key[3])
except _ProbeTimedOut:
allowed = None
except Exception:
@@ -877,7 +879,7 @@ def _evict_repo_access_locked() -> None:
_repo_access_cache.clear()
-def _inflight_lock(key: tuple[str, str, str]) -> threading.Lock:
+def _inflight_lock(key: tuple[str, ...]) -> threading.Lock:
with _repo_access_lock:
lock = _repo_access_inflight.get(key)
if lock is None:
@@ -895,6 +897,16 @@ def _probe_endpoint() -> str:
URL both clients reject and every probe on that machine is denied. ``hf_endpoint_url``
is where the backend already normalises it; falls back since this module sits beneath.
"""
+ try:
+ from utils.hub_settings import MODELSCOPE, active_source, hugging_face_endpoint
+ except Exception:
+ active_source = None
+ # The cache holds Hugging Face snapshots: ModelScope's same-named repo cannot vouch for them.
+ if active_source is not None and active_source() == MODELSCOPE:
+ try:
+ return hugging_face_endpoint()
+ except Exception:
+ return "https://huggingface.co"
try:
from utils.utils import hf_endpoint_url
return hf_endpoint_url().rstrip("/")
@@ -960,7 +972,13 @@ def _has_hf_error_code(response) -> bool:
return False
-def _probe_repo_access(repo_id: str, token: Optional[str], repo_type: str) -> Optional[bool]:
+def _probe_repo_access(
+ repo_id: str,
+ token: Optional[str],
+ repo_type: str,
+ *,
+ endpoint: Optional[str] = None,
+) -> Optional[bool]:
response = None
try:
from huggingface_hub import constants
@@ -976,7 +994,7 @@ def _probe_repo_access(repo_id: str, token: Optional[str], repo_type: str) -> Op
# than the one memoized.
if any(segment in {".", ".."} for segment in repo_id.split("/")):
return False
- path = f"{_probe_endpoint()}/api/{repo_type}s/{quote(repo_id, safe = '/')}/auth-check"
+ path = f"{endpoint or _probe_endpoint()}/api/{repo_type}s/{quote(repo_id, safe = '/')}/auth-check"
response = get_session().get(
path,
# False, not None: None falls back to the ambient login, asking the public
diff --git a/studio/backend/main.py b/studio/backend/main.py
index 6c087b5284a..c1e5b4adcf6 100644
--- a/studio/backend/main.py
+++ b/studio/backend/main.py
@@ -251,11 +251,10 @@ if _STUDIO_ROOT_RESOLVED != _LEGACY_STUDIO_ROOT or _MASTER_ROOT is not None:
mark_managed_llama_cpp_path(_MANAGED_LLAMA_CPP_PATH)
# huggingface_hub reads HF_ENDPOINT itself, at import, unnormalised and unvalidated.
-# Rewrite it first, before anything imports the library.
-from utils.hf_endpoint import normalize_hf_endpoint_env as _normalize_hf_endpoint_env
+from utils.hub_settings import apply_hub_settings as _apply_hub_settings
-_normalize_hf_endpoint_env()
-del _normalize_hf_endpoint_env
+_apply_hub_settings()
+del _apply_hub_settings
# The studio bundles unsloth_zoo; declare unsloth present (as `import unsloth` does) so its
# lazy submodule imports and the DiffusionGemma runner don't trip the install guard.
@@ -399,11 +398,17 @@ from utils.client_ip import client_ip
from utils.hf_endpoint import (
DEFAULTS_BY_HEALTH_KEY as _HF_ENDPOINT_DEFAULTS,
endpoint_is_reachable_by as _endpoint_is_reachable_by,
+ browser_hf_endpoint,
csp_asset_sources,
csp_connect_sources,
- get_hf_endpoint,
get_hf_datasets_server,
)
+from hub import endpoint_proxy as _hub_endpoint_proxy
+from hub.modelscope.router import (
+ BROWSER_PREFIX as _MODELSCOPE_BROWSER_PREFIX,
+ build_router as _build_modelscope_router,
+)
+from utils.hub_settings import active_source as _active_hub_source
from utils.update_status import (
get_studio_install_source_status,
get_studio_update_status,
@@ -750,6 +755,13 @@ async def lifespan(app: FastAPI):
except Exception: # noqa: BLE001
pass
+ try:
+ from hub.services.models.account_access import adopt_unnamed_public_proofs
+ from utils.hub_settings import operator_hf_endpoint
+ adopt_unnamed_public_proofs(operator_hf_endpoint())
+ except Exception: # noqa: BLE001 -- unnamed proofs are then only ignored
+ _lifespan_log.warning("could not name recorded public-repo proofs", exc_info = True)
+
# Remove stale .venv_overlay from old versions; switching now uses .venv_t5/.
overlay_dir = Path(__file__).resolve().parent.parent.parent / ".venv_overlay"
if overlay_dir.is_dir():
@@ -1057,12 +1069,16 @@ def _reportable_hf_endpoints(request) -> dict:
Cloudflare tunnel the peer IS loopback, being the local cloudflared process
rather than the visitor, and an address it cannot determine reads as remote.
"""
+ from utils.hub_settings import saved_only_endpoints
+
+ # The owner-only settings route guards a saved endpoint; the browser reaches it through the relay.
+ hidden = saved_only_endpoints()
reported = {}
for key, value in (
- ("hf_endpoint", get_hf_endpoint()),
+ ("hf_endpoint", browser_hf_endpoint()),
("hf_datasets_server", get_hf_datasets_server()),
):
- if _endpoint_is_reachable_by(value, client_ip(request)):
+ if value not in hidden and _endpoint_is_reachable_by(value, client_ip(request)):
reported[key] = value
else:
reported[key] = _HF_ENDPOINT_DEFAULTS[key]
@@ -1582,7 +1598,11 @@ app.add_middleware(
allow_headers = ["*"],
# allow_headers is the REQUEST side; a response header is unreadable to JS unless
# exposed, and Studio is cross-origin from tauri://localhost and tunnels.
- expose_headers = ["X-Unsloth-Conflict-Kind", "X-Unsloth-Refusal"],
+ expose_headers = [
+ "X-Unsloth-Conflict-Kind",
+ "X-Unsloth-Refusal",
+ *_hub_endpoint_proxy.EXPOSED_HEADERS,
+ ],
# is_allowed_origin closes the moment the tunnel URL clears, but a preflight already cached by the browser
# does not. Measured in WebKit: with Starlette's 600s default, a state-changing request still REACHED the
# server after remote access was stopped. Keep the stale window short.
@@ -1650,6 +1670,20 @@ app.include_router(hub_inventory_router, prefix = "/api/hub", tags = ["hub"])
app.include_router(hub_datasets_router, prefix = "/api/hub/datasets", tags = ["hub"])
app.include_router(picker_templates_router, prefix = "/api/picker", tags = ["picker"])
app.include_router(hub_token_router, prefix = "/api/hub", tags = ["hub"])
+app.include_router(
+ _build_modelscope_router(browser = True),
+ prefix = _MODELSCOPE_BROWSER_PREFIX,
+ include_in_schema = False,
+)
+for _prefix, _upstream, _pages in (
+ (_hub_endpoint_proxy.HUB_PREFIX, browser_hf_endpoint, True),
+ (_hub_endpoint_proxy.DATASETS_SERVER_PREFIX, get_hf_datasets_server, False),
+):
+ app.include_router(
+ _hub_endpoint_proxy.build_router(_prefix, _upstream, anonymous_pages = _pages),
+ prefix = _prefix,
+ tags = ["hub"],
+ )
app.include_router(youtube_router, prefix = "/api/youtube", tags = ["youtube"])
# Re-wrap /v1/* client errors into OpenAI/Anthropic envelopes; non-/v1 keeps {"detail": ...}.
@@ -1927,6 +1961,17 @@ async def health_check(request: Request):
# Unauthenticated on purpose: an endpoint URL is not a host fingerprint,
# and the frontend needs it before a token exists.
**_reportable_hf_endpoints(request),
+ "hub_source": _active_hub_source(),
+ "hub_proxy": _hub_endpoint_proxy.relay_path(
+ _hub_endpoint_proxy.HUB_PREFIX,
+ browser_hf_endpoint(),
+ _HF_ENDPOINT_DEFAULTS["hf_endpoint"],
+ ),
+ "datasets_server_proxy": _hub_endpoint_proxy.relay_path(
+ _hub_endpoint_proxy.DATASETS_SERVER_PREFIX,
+ get_hf_datasets_server(),
+ _HF_ENDPOINT_DEFAULTS["hf_datasets_server"],
+ ),
**({"desktop_owner": owner} if (owner := _desktop_owner()) else {}),
}
# Lockstep with /api/liveness: the launcher falls back to this route on a backend too old
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 423e0ca445a..10861871368 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -80,6 +80,7 @@ from utils.api_errors import openai_error_body, anthropic_error_body, error_body
from utils.audio_tokens import GGUF_TTS_AUDIO_TYPES as _GGUF_TTS_AUDIO_TYPES
from utils.upload_limits import STT_AUDIO_B64_MAX_CHARS, STT_AUDIO_RAW_MAX_BYTES
from hub.dependencies import get_hf_token, get_request_hf_token
+from hub.utils.hf_errors import modelscope_missing
from hub.utils.hf_tokens import HfTokenArg
from hub.services.models.ollama import (
acquire_ollama_model_ref,
@@ -10329,11 +10330,12 @@ async def _maybe_auto_switch_model(
speech_codec_path = speech_preflight_result.codec_path
elif isinstance(speech_preflight_result, dict):
speech_cache_environment = speech_preflight_result
- except Exception:
+ except Exception as exc:
raise HTTPException(
status_code = 503,
detail = openai_error_body(
- "The requested model's codec assets are unavailable. Connect this "
+ modelscope_missing(exc)
+ or "The requested model's codec assets are unavailable. Connect this "
"server to the network once to download them, or install them in the "
"active cache before retrying.",
status = 503,
@@ -15971,13 +15973,21 @@ def _gguf_load_cancelled(llama_backend, load_cancel_event: Optional[threading.Ev
)
-async def _run_gguf_load_attempt(llama_backend, intent, load_cancel_event) -> bool:
+async def _run_gguf_load_attempt(
+ llama_backend,
+ intent,
+ load_cancel_event,
+ codec_failures: Optional[list] = None,
+) -> bool:
+ def load() -> bool:
+ loaded = llama_backend.load_model(intent = intent, load_cancel_event = load_cancel_event)
+ failure = getattr(llama_backend, "codec_failure", lambda: None)()
+ if isinstance(failure, str) and codec_failures is not None:
+ codec_failures.append(failure)
+ return loaded
+
try:
- return await asyncio.to_thread(
- llama_backend.load_model,
- intent = intent,
- load_cancel_event = load_cancel_event,
- )
+ return await asyncio.to_thread(load)
except GgufDownloadCancelled:
return False
@@ -16906,6 +16916,7 @@ async def _load_model_impl(
if gguf_intent is None:
raise RuntimeError("GGUF load intent was not resolved")
load_intent = gguf_intent
+ codec_failures: list[str] = []
# Run a single load attempt with the given tensor flag + extras.
async def _attempt_gguf_load(
@@ -16926,6 +16937,7 @@ async def _load_model_impl(
llama_backend,
attempt,
load_cancel_event,
+ codec_failures,
)
# Tensor parallelism is arch-gated in llama.cpp and crashes some loads
@@ -16958,7 +16970,9 @@ async def _load_model_impl(
raise HTTPException(status_code = 409, detail = "Model load cancelled")
raise HTTPException(
status_code = 500,
- detail = f"Failed to load GGUF model: {model_log_label if native_grant_backed else config.display_name}",
+ detail = codec_failures[-1]
+ if codec_failures
+ else f"Failed to load GGUF model: {model_log_label if native_grant_backed else config.display_name}",
)
# An Images/Video acquire can land in the gap between the acquire above and load_model clearing the cancel event, so
diff --git a/studio/backend/routes/settings.py b/studio/backend/routes/settings.py
index 0a76460fbbd..72540c966b3 100644
--- a/studio/backend/routes/settings.py
+++ b/studio/backend/routes/settings.py
@@ -80,6 +80,13 @@ from utils.download_transport_settings import (
get_download_transport_mode,
set_download_transport_mode,
)
+from utils.hub_settings import (
+ HubSettings,
+ active_source,
+ get_hub_settings,
+ set_hub_settings,
+ set_hub_source,
+)
from picker.schemas import MAX_CHAT_TEMPLATE_BYTES, chat_template_byte_length
from utils.reasoning_budget import validate_reasoning_budget_message
from utils.coding_agents import CODING_AGENTS, detect_installed_coding_agents
@@ -675,6 +682,22 @@ class DownloadTransportResponse(BaseModel):
auto_reason: Optional[str] = None
+class HubSettingsPayload(BaseModel):
+ hf_endpoint: str = Field(max_length = 2048)
+ datasets_server_follows_endpoint: StrictBool
+
+
+class HubSourcePayload(BaseModel):
+ source: Literal["huggingface", "modelscope"]
+
+
+class HubSettingsResponse(BaseModel):
+ hf_endpoint: str
+ datasets_server_follows_endpoint: bool
+ source: Literal["huggingface", "modelscope"]
+ active_source: Literal["huggingface", "modelscope"]
+
+
class XetNoticeReservePayload(BaseModel):
# A legacy localStorage count from a client that has not reported one before. Can only raise the
# stored count (see reserve_xet_notice), so a client cannot talk its own way back under the limit.
@@ -1441,6 +1464,52 @@ def update_download_transport(
return _download_transport_response(mode)
+def _hub_settings_response(settings: HubSettings) -> HubSettingsResponse:
+ return HubSettingsResponse(
+ hf_endpoint = settings.hf_endpoint,
+ datasets_server_follows_endpoint = settings.datasets_server_follows_endpoint,
+ source = settings.source,
+ active_source = active_source(),
+ )
+
+
+# Owner only: the endpoint can name a private address that other accounts' clients must not learn.
+@_owner_settings_router.get("/hub", response_model = HubSettingsResponse)
+def get_hub(current_subject: str = Depends(get_current_subject)) -> HubSettingsResponse:
+ return _hub_settings_response(get_hub_settings())
+
+
+@_owner_settings_router.put("/hub", response_model = HubSettingsResponse)
+def update_hub(
+ payload: HubSettingsPayload,
+ current_subject: str = Depends(get_current_subject),
+ via_api_key: bool = Depends(authenticated_via_api_key),
+) -> HubSettingsResponse:
+ # The endpoint receives the installation's Hugging Face token, like the token routes above.
+ require_ui_session(via_api_key)
+ try:
+ settings = set_hub_settings(payload.hf_endpoint, payload.datasets_server_follows_endpoint)
+ except ValueError as exc:
+ raise log_and_http_error(
+ exc,
+ 400,
+ safe_error_detail(exc, fallback = "Invalid Hugging Face endpoint."),
+ event = "settings.update_hub_failed",
+ log = logger,
+ ) from exc
+ return _hub_settings_response(settings)
+
+
+@_owner_settings_router.put("/hub/source", response_model = HubSettingsResponse)
+def update_hub_source(
+ payload: HubSourcePayload,
+ current_subject: str = Depends(get_current_subject),
+ via_api_key: bool = Depends(authenticated_via_api_key),
+) -> HubSettingsResponse:
+ require_ui_session(via_api_key)
+ return _hub_settings_response(set_hub_source(payload.source))
+
+
@_owner_settings_router.post("/xet-notice/reserve", response_model = XetNoticeResponse)
def post_xet_notice_reserve(
payload: XetNoticeReservePayload, current_subject: str = Depends(get_current_subject)
diff --git a/studio/backend/tests/test_account_auto_download_busy_message.py b/studio/backend/tests/test_account_auto_download_busy_message.py
index 542482e3ac6..5c0fb17e6e7 100644
--- a/studio/backend/tests/test_account_auto_download_busy_message.py
+++ b/studio/backend/tests/test_account_auto_download_busy_message.py
@@ -30,7 +30,7 @@ def alice_downloading(monkeypatch):
return True
monkeypatch.setattr(auto, "_is_downloadable_model", downloadable)
- monkeypatch.setattr(auto, "_is_not_servable", lambda repo_id, hf_token: False)
+ monkeypatch.setattr(auto, "_is_not_servable", lambda repo_id, hf_token, *_: False)
def _refusal(account):
diff --git a/studio/backend/tests/test_account_cached_listing_offload.py b/studio/backend/tests/test_account_cached_listing_offload.py
index 3b6175e8e85..ed3d2bc54c7 100644
--- a/studio/backend/tests/test_account_cached_listing_offload.py
+++ b/studio/backend/tests/test_account_cached_listing_offload.py
@@ -69,7 +69,7 @@ def slow_hub(monkeypatch, tmp_path):
probes: list[str] = []
- def answer(repo_id, repo_type):
+ def answer(repo_id, repo_type, **_k):
probes.append(repo_id)
time.sleep(PROBE_SECONDS)
return True
diff --git a/studio/backend/tests/test_account_model_access.py b/studio/backend/tests/test_account_model_access.py
index 297e685a9c9..f4b8b66c137 100644
--- a/studio/backend/tests/test_account_model_access.py
+++ b/studio/backend/tests/test_account_model_access.py
@@ -38,7 +38,7 @@ def isolated(monkeypatch, tmp_path):
monkeypatch.setattr(
access,
"HfApi",
- lambda: SimpleNamespace(
+ lambda **_k: SimpleNamespace(
repo_info = lambda *a, **k: (_ for _ in ()).throw(OSError("offline"))
),
)
@@ -67,7 +67,7 @@ def test_public_proof_is_anonymous_cached_and_fail_closed(monkeypatch, answer, v
gated = answer == "gated",
)
- monkeypatch.setattr(access, "HfApi", lambda: SimpleNamespace(repo_info = info))
+ monkeypatch.setattr(access, "HfApi", lambda **_k: SimpleNamespace(repo_info = info))
assert run_as(ALICE, access.repo_visible, "Org/Secret", repo_type) is visible
assert run_as(BOB, access.repo_visible, "Org/Secret", repo_type) is visible
assert calls == [("Org/Secret", {"repo_type": repo_type, "token": False, "timeout": 5.0})]
@@ -93,9 +93,11 @@ def test_a_proven_public_repo_stays_visible_when_the_hub_cannot_be_asked(
)
raise OSError("Hub unavailable")
- monkeypatch.setattr(access, "HfApi", lambda: SimpleNamespace(repo_info = info))
+ monkeypatch.setattr(access, "HfApi", lambda **_k: SimpleNamespace(repo_info = info))
assert run_as(ALICE, access.repo_visible, "Org/Public")
- assert json.loads(access._public_verdicts_path().read_text()).keys() == {"model:org/public"}
+ assert json.loads(access._public_verdicts_path().read_text()).keys() == {
+ "https://huggingface.co|model:org/public"
+ }
assert access._public_verdicts_path().is_relative_to(tmp_path / "cache")
access._public_repos.clear()
@@ -111,7 +113,7 @@ def test_a_proven_public_repo_stays_visible_when_the_hub_cannot_be_asked(
def definitive(repo, **kwargs):
raise error
- monkeypatch.setattr(access, "HfApi", lambda: SimpleNamespace(repo_info = definitive))
+ monkeypatch.setattr(access, "HfApi", lambda **_k: SimpleNamespace(repo_info = definitive))
assert not run_as(ALICE, access.repo_visible, "org/public")
assert json.loads(access._public_verdicts_path().read_text()) == {}
@@ -193,7 +195,7 @@ def test_concurrent_misses_ask_the_hub_once_per_repo(monkeypatch):
calls = []
barrier = threading.Barrier(8)
- def answer(repo_id, repo_type):
+ def answer(repo_id, repo_type, **_k):
calls.append(repo_id)
barrier.wait(timeout = 30)
time.sleep(0.02)
@@ -227,7 +229,7 @@ def test_a_listing_probes_its_unknown_repos_together(monkeypatch):
assert together > 1, f"_PROBE_FANOUT is {access._PROBE_FANOUT}; the listing is serial"
barrier = threading.Barrier(together)
- def answer(*_args):
+ def answer(*_args, **_k):
barrier.wait(timeout = 30)
return True
@@ -237,7 +239,7 @@ def test_a_listing_probes_its_unknown_repos_together(monkeypatch):
def test_a_warm_listing_asks_the_hub_nothing(monkeypatch):
rows = [{"repo_id": f"org/warm-{index}"} for index in range(4)]
- monkeypatch.setattr(access, "_hub_public_answer", lambda *a: True)
+ monkeypatch.setattr(access, "_hub_public_answer", lambda *a, **_k: True)
assert len(run_as(ALICE, access.filter_model_rows, rows)) == 4
def unexpected(*args):
@@ -392,7 +394,7 @@ def test_download_ownership_lands_before_the_hub_authorization(monkeypatch):
seen["alice"] = run_as(ALICE, download_lifecycle.download_belongs_to_account, registry, key)
return SimpleNamespace(gated = False)
- monkeypatch.setattr(access, "HfApi", lambda: SimpleNamespace(repo_info = repo_info))
+ monkeypatch.setattr(access, "HfApi", lambda **_k: SimpleNamespace(repo_info = repo_info))
def spawn():
raise OSError("no worker in this test")
@@ -500,7 +502,7 @@ def test_gated_metadata_alone_is_not_download_authorization(monkeypatch, authori
monkeypatch.setattr(
access,
"HfApi",
- lambda: SimpleNamespace(
+ lambda **_k: SimpleNamespace(
repo_info = lambda *a, **k: SimpleNamespace(gated = True), auth_check = check
),
)
@@ -517,27 +519,29 @@ def test_a_persisted_public_proof_expires_rather_than_outliving_a_privacy_change
monkeypatch.setattr(
access,
"HfApi",
- lambda: SimpleNamespace(
+ lambda **_k: SimpleNamespace(
repo_info = lambda *a, **k: SimpleNamespace(private = False, gated = False)
),
)
assert run_as(ALICE, access.repo_visible, "Org/Public")
path = access._public_verdicts_path()
- assert json.loads(path.read_text()).keys() == {"model:org/public"}
+ assert json.loads(path.read_text()).keys() == {"https://huggingface.co|model:org/public"}
monkeypatch.setattr(
access,
"HfApi",
- lambda: SimpleNamespace(
+ lambda **_k: SimpleNamespace(
repo_info = lambda *a, **k: (_ for _ in ()).throw(OSError("Hub unavailable"))
),
)
access._public_repos.clear()
- path.write_text(json.dumps({"model:org/public": time.time() - 3600}))
+ path.write_text(json.dumps({"https://huggingface.co|model:org/public": time.time() - 3600}))
assert run_as(BOB, access.repo_visible, "org/public"), "a proof inside the bound still carries"
access._public_repos.clear()
- path.write_text(json.dumps({"model:org/public": time.time() - 30 * 24 * 3600}))
+ path.write_text(
+ json.dumps({"https://huggingface.co|model:org/public": time.time() - 30 * 24 * 3600})
+ )
assert not run_as(BOB, access.repo_visible, "org/public")
assert not run_as(ALICE, access.repo_visible, "org/public")
diff --git a/studio/backend/tests/test_account_storage.py b/studio/backend/tests/test_account_storage.py
index aecd8412a34..4b33d15b5c3 100644
--- a/studio/backend/tests/test_account_storage.py
+++ b/studio/backend/tests/test_account_storage.py
@@ -262,7 +262,7 @@ def test_hf_validation_cache_and_budget_are_private(monkeypatch):
monkeypatch.setattr(
validation,
"_check_remote",
- lambda token: (
+ lambda token, **_k: (
calls.append(current_account_id()) or validation.TokenValidationResult(status = "valid")
),
)
@@ -409,7 +409,7 @@ def test_hf_inflight_checks_do_not_join_another_account(monkeypatch):
entered = threading.Barrier(2)
results = []
- def remote(token):
+ def remote(token, **_k):
entered.wait(timeout = 5)
return validation.TokenValidationResult(status = "valid")
diff --git a/studio/backend/tests/test_file_security.py b/studio/backend/tests/test_file_security.py
index a36e205e560..51ddeb65621 100644
--- a/studio/backend/tests/test_file_security.py
+++ b/studio/backend/tests/test_file_security.py
@@ -668,3 +668,49 @@ def test_security_load_subdirs_yaml_fallback(monkeypatch):
# A non-bicodec default contributes no subdir.
monkeypatch.setattr(mc, "load_model_defaults", lambda *_a, **_k: {"audio_type": None})
assert security_load_subdirs("unsloth/Llama-3.2-1B") == ()
+
+
+ "files, blocked",
+ [
+ (["config.json", "pytorch_model.bin"], ["pytorch_model.bin"]),
+ (["config.json", "pytorch_model.bin", "model.safetensors"], []),
+ (
+ [
+ "model-00001-of-00002.safetensors",
+ "model.safetensors.index.json",
+ "pytorch_model.bin",
+ ],
+ [],
+ ),
+ (["pytorch_model.bin.index.json", "shards/payload"], ["pytorch_model.bin.index.json"]),
+ (["model.safetensors", "adapter_model.bin"], ["adapter_model.bin"]),
+ (["LLM/pytorch_model.bin", "model.gguf"], ["LLM/pytorch_model.bin"]),
+ (["model.safetensors", "nemo/pytorch_model.bin"], []),
+ (["model.gguf", "README.md"], []),
+ ],
+)
+def test_modelscope_has_no_scan_so_pickle_weights_fail_closed(monkeypatch, files, blocked):
+ monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
+ with _patch_status(None), patch("huggingface_hub.HfApi.list_repo_files", return_value = files):
+ d = evaluate_file_security("org/repo", load_subdirs = ("LLM",))
+ assert d.blocked is bool(blocked)
+ assert [f["path"] for f in d.unsafe_files] == blocked
+ if blocked:
+ assert "Switch the model source to Hugging Face" in d.reason
+
+
+def test_modelscope_listing_failure_blocks(monkeypatch):
+ monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
+ with (
+ _patch_status(None),
+ patch("huggingface_hub.HfApi.list_repo_files", side_effect = OSError("down")),
+ ):
+ assert evaluate_file_security("org/repo").blocked is True
+
+
+def test_hugging_face_scan_unavailable_stays_fail_open(monkeypatch):
+ monkeypatch.delenv("UNSLOTH_STUDIO_HUB_SOURCE", raising = False)
+ with _patch_status(None), patch("huggingface_hub.HfApi.list_repo_files") as listing:
+ assert evaluate_file_security("org/repo").blocked is False
+ listing.assert_not_called()
diff --git a/studio/backend/tests/test_hf_token_validation.py b/studio/backend/tests/test_hf_token_validation.py
index 31b30fc37da..498ca570a94 100644
--- a/studio/backend/tests/test_hf_token_validation.py
+++ b/studio/backend/tests/test_hf_token_validation.py
@@ -29,7 +29,7 @@ def _reset_validation_state():
def test_cached_token_does_not_spend_another_attempt(monkeypatch):
calls = []
- def _check(token):
+ def _check(token, **_k):
calls.append(token)
return validation.TokenValidationResult(status = "valid")
@@ -41,11 +41,21 @@ def test_cached_token_does_not_spend_another_attempt(monkeypatch):
assert calls == ["hf_valid"]
+def test_modelscope_checks_a_token_against_hugging_face(monkeypatch):
+ seen = []
+ result = validation.TokenValidationResult(status = "valid")
+ monkeypatch.setattr(validation, "_check_remote", lambda _t, **k: seen.append(k) or result)
+ monkeypatch.setattr(validation, "active_source", lambda: validation.MODELSCOPE)
+ monkeypatch.setattr(validation, "hugging_face_endpoint", lambda: "https://hf-mirror.com")
+ assert validation.validate_hf_token("hf_ok", rate_key = "user:ip") is result
+ assert seen == [{"endpoint": "https://hf-mirror.com"}]
+
+
def test_three_uncached_attempts_per_hour(monkeypatch):
monkeypatch.setattr(
validation,
"_check_remote",
- lambda _token: validation.TokenValidationResult(status = "invalid"),
+ lambda _token, **_k: validation.TokenValidationResult(status = "invalid"),
)
for index in range(3):
@@ -69,7 +79,7 @@ def test_window_rolls_forward(monkeypatch):
monkeypatch.setattr(
validation,
"_check_remote",
- lambda _token: validation.TokenValidationResult(status = "invalid"),
+ lambda _token, **_k: validation.TokenValidationResult(status = "invalid"),
)
assert validation.validate_hf_token("hf_a", rate_key = "user:ip").status == "invalid"
@@ -97,7 +107,7 @@ def test_remote_status_classification(monkeypatch, status_code, expected):
return response
monkeypatch.setattr(validation, "get_session", lambda: _Session())
- result = validation._check_remote("hf_test")
+ result = validation._check_remote("hf_test", endpoint = "https://huggingface.co")
assert result.status == expected
if status_code == 429:
assert result.retry_after_seconds == 42
@@ -116,7 +126,9 @@ def test_wrapped_http_401_is_invalid(monkeypatch):
raise error
monkeypatch.setattr(validation, "get_session", lambda: _Session())
- assert validation._check_remote("hf_test").status == "invalid"
+ assert (
+ validation._check_remote("hf_test", endpoint = "https://huggingface.co").status == "invalid"
+ )
def test_remote_timeout_is_bounded_and_unavailable(monkeypatch):
@@ -127,14 +139,17 @@ def test_remote_timeout_is_bounded_and_unavailable(monkeypatch):
raise TimeoutError("timed out")
monkeypatch.setattr(validation, "get_session", lambda: _Session())
- assert validation._check_remote("hf_test").status == "unavailable"
+ assert (
+ validation._check_remote("hf_test", endpoint = "https://huggingface.co").status
+ == "unavailable"
+ )
def test_raw_token_is_not_retained(monkeypatch):
monkeypatch.setattr(
validation,
"_check_remote",
- lambda _token: validation.TokenValidationResult(status = "valid"),
+ lambda _token, **_k: validation.TokenValidationResult(status = "valid"),
)
token = "hf_do_not_store_this_value"
validation.validate_hf_token(token, rate_key = "user:ip")
@@ -147,7 +162,7 @@ def test_unexpected_remote_exception_releases_singleflight(monkeypatch):
calls = 0
monkeypatch.setattr(validation, "_INFLIGHT_WAIT_SECONDS", 0.0)
- def _check(_token):
+ def _check(_token, **_k):
nonlocal calls
calls += 1
if calls == 1:
diff --git a/studio/backend/tests/test_hub_endpoint_proxy.py b/studio/backend/tests/test_hub_endpoint_proxy.py
new file mode 100644
index 00000000000..6c879466419
--- /dev/null
+++ b/studio/backend/tests/test_hub_endpoint_proxy.py
@@ -0,0 +1,179 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The same-origin relay to a custom Hub endpoint and datasets server."""
+
+from __future__ import annotations
+
+from pathlib import Path
+import sys
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+import httpx
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+from hub import endpoint_proxy
+
+MIRROR = "https://mirror.test/hf"
+HUB = endpoint_proxy.relay_path(endpoint_proxy.HUB_PREFIX, MIRROR, "https://huggingface.co")
+DATASETS = endpoint_proxy.relay_path(
+ endpoint_proxy.DATASETS_SERVER_PREFIX, MIRROR, "https://huggingface.co"
+)
+
+
+def _mirror(request: httpx.Request) -> httpx.Response:
+ if request.url.host == "down.test":
+ raise httpx.ConnectError("refused", request = request)
+ headers = {"Set-Cookie": "s=1", "Link": f'<{MIRROR}/api/models?cursor=2>; rel="next"'}
+ kinds = {".png": "image/png", ".svg": "image/svg+xml", ".html": "text/html"}
+ for suffix, kind in kinds.items():
+ if request.url.path.endswith(suffix):
+ return httpx.Response(200, content = b"<x>", headers = {**headers, "Content-Type": kind})
+ return httpx.Response(401 if "gated" in request.url.path else 200, json = [], headers = headers)
+
+
+def proxy(monkeypatch):
+ seen, state = [], {"session": True, "upstream": MIRROR}
+ monkeypatch.setattr(
+ endpoint_proxy, "transport", httpx.MockTransport(lambda r: seen.append(r) or _mirror(r))
+ )
+ monkeypatch.setattr(endpoint_proxy, "_clients", endpoint_proxy.weakref.WeakKeyDictionary())
+
+ async def signed_in(_request) -> bool:
+ return state["session"]
+
+ monkeypatch.setattr(endpoint_proxy, "signed_in", signed_in)
+ app = FastAPI()
+ for prefix, pages in (
+ (endpoint_proxy.HUB_PREFIX, True),
+ (endpoint_proxy.DATASETS_SERVER_PREFIX, False),
+ ):
+ router = endpoint_proxy.build_router(
+ prefix, lambda: state["upstream"], anonymous_pages = pages
+ )
+ app.include_router(router, prefix = prefix)
+ with TestClient(app, follow_redirects = False) as client:
+ yield client, seen, state
+
+
+def test_a_session_is_relayed_with_only_the_hub_token(proxy):
+ client, seen, state = proxy
+ headers = {"Authorization": "Bearer session", "X-HF-Authorization": "Bearer hf_x"}
+ response = client.get(f"{HUB}/api/models?search=qwen", headers = headers)
+ assert (response.status_code, response.headers["x-hub-upstream"]) == (200, "1")
+ assert "set-cookie" not in response.headers
+ assert response.headers["link"] == f'<http://testserver{HUB}/api/models?cursor=2>; rel="next"'
+ assert (str(seen[0].url), seen[0].headers["authorization"]) == (
+ f"{MIRROR}/api/models?search=qwen",
+ "Bearer hf_x",
+ )
+
+ gated = client.get(f"{HUB}/api/models/org/gated/revision/refs%2Fpr%2F1")
+ assert (gated.status_code, gated.headers["x-hub-upstream"]) == (401, "1")
+ assert seen[1].url.raw_path == b"/hf/api/models/org/gated/revision/refs%2Fpr%2F1"
+ assert "authorization" not in seen[1].headers and "cookie" not in seen[1].headers
+
+ state["upstream"] = "https://down.test"
+ assert client.get(f"{DATASETS}/splits").status_code == 409
+ down = client.get(
+ f"{endpoint_proxy.relay_path(endpoint_proxy.DATASETS_SERVER_PREFIX, 'https://down.test', '')}/splits"
+ )
+ assert (down.status_code, "x-hub-upstream" in down.headers) == (502, False)
+ assert len(seen) == 3
+
+
+def test_without_a_session_nothing_reaches_the_endpoint(proxy):
+ client, seen, state = proxy
+ state["session"] = False
+ image = client.get(f"{HUB}/org/m/resolve/main/a.png?download=1")
+ assert image.headers["location"] == f"{MIRROR}/org/m/resolve/main/a.png?download=1"
+ for path, headers in (
+ (f"{HUB}/api/models", {}),
+ (f"{HUB}/org/m/resolve/main/README.md", {"Authorization": "Bearer stale"}),
+ (f"{DATASETS}/splits?dataset=a/b", {}),
+ ):
+ refused = client.get(path, headers = headers)
+ assert (refused.status_code, "x-hub-upstream" in refused.headers) == (401, False)
+ bad = ("%2e%2e", "a%5Cb", "a%2F..%2Fb")
+ assert {client.get(f"{HUB}/org/{b}/admin").status_code for b in bad} == {400}
+ state["upstream"] = "http://10.0.0.5:8080"
+ lan = endpoint_proxy.relay_path(endpoint_proxy.HUB_PREFIX, state["upstream"], "")
+ hidden = client.get(f"{lan}/org/m/resolve/main/a.png")
+ assert (hidden.status_code, "location" in hidden.headers) == (401, False)
+ assert seen == []
+
+
+def test_the_relay_tag_is_not_a_bare_hash_of_the_endpoint():
+ import hashlib
+ from hub import endpoint_proxy
+
+ endpoint = "https://10.0.0.5:8443"
+ tag = endpoint_proxy._tag(endpoint)
+ assert tag == endpoint_proxy._tag(endpoint) and len(tag) == 12
+ assert tag != hashlib.sha256(endpoint.encode()).hexdigest()[:12]
+
+
[email protected]("peer", ["127.0.0.1", "203.0.113.9"])
+def test_a_signed_out_asset_names_a_saved_endpoint_only_to_a_loopback_browser(
+ proxy, monkeypatch, peer
+):
+ import utils.hub_settings as hub_settings
+
+ client, seen, state = proxy
+ state["session"] = False
+ monkeypatch.setattr(endpoint_proxy, "client_ip", lambda _request: peer)
+ monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset({MIRROR}), raising = False)
+ page = client.get(
+ f"{HUB}/org/m/resolve/main/a.png",
+ headers = {endpoint_proxy.TOKEN_HEADER: "Bearer hf_x"},
+ follow_redirects = False,
+ )
+ if peer == "127.0.0.1":
+ assert page.status_code == 302 and page.headers["location"].startswith(MIRROR)
+ assert seen == []
+ else:
+ assert page.status_code == 200 and "location" not in page.headers
+ assert page.headers["x-content-type-options"] == "nosniff"
+ assert "sandbox" in page.headers["content-security-policy"]
+ assert [("authorization" in r.headers) for r in seen] == [False]
+ assert client.get(f"{HUB}/api/models").status_code == 401
+
+
+ "path, contacted",
+ [
+ ("/org/m", False),
+ ("/org/m/blob/main/README.md", False),
+ ("/org/m/resolve/main/page.html", True),
+ ("/org/m/resolve/main/logo.svg", True),
+ ],
+)
+def test_a_signed_out_relay_serves_only_raster_repository_files(
+ proxy, monkeypatch, path, contacted
+):
+ import utils.hub_settings as hub_settings
+
+ client, seen, state = proxy
+ state["session"] = False
+ monkeypatch.setattr(endpoint_proxy, "client_ip", lambda _request: "203.0.113.9")
+ monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset({MIRROR}), raising = False)
+ page = client.get(f"{HUB}{path}", follow_redirects = False)
+ assert page.status_code == 401 and b"<x>" not in page.content
+ assert bool(seen) == contacted
+
+
+def test_a_signed_out_relay_refuses_large_files(proxy, monkeypatch):
+ import utils.hub_settings as hub_settings
+
+ client, seen, state = proxy
+ state["session"] = False
+ monkeypatch.setattr(endpoint_proxy, "client_ip", lambda _request: "203.0.113.9")
+ monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset({MIRROR}), raising = False)
+ monkeypatch.setattr(endpoint_proxy, "ANONYMOUS_ASSET_LIMIT", 1)
+ assert client.get(f"{HUB}/org/m/resolve/main/big.png").status_code == 413
diff --git a/studio/backend/tests/test_hub_settings.py b/studio/backend/tests/test_hub_settings.py
new file mode 100644
index 00000000000..e44271736d6
--- /dev/null
+++ b/studio/backend/tests/test_hub_settings.py
@@ -0,0 +1,531 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The saved Hugging Face endpoint drives HF_ENDPOINT / HF_DATASETS_SERVER."""
+
+from __future__ import annotations
+
+from pathlib import Path
+import sys
+import types as _types
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+_loggers_stub = _types.ModuleType("loggers")
+_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
+sys.modules.setdefault("loggers", _loggers_stub)
+
+import json
+import os
+import time
+
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+import routes.settings as settings
+import storage.studio_db as studio_db
+import utils.hub_settings as hub_settings
+
+MIRROR = "https://hf-mirror.com"
+OPERATOR_DS = "https://ds.example.com"
+
+
+def store(monkeypatch):
+ import huggingface_hub.constants as constants
+
+ values: dict = {}
+ monkeypatch.setattr(constants, "HF_URL_HOSTS", constants.HF_URL_HOSTS)
+ monkeypatch.setattr(
+ studio_db, "get_app_settings", lambda keys: {k: values[k] for k in keys if k in values}
+ )
+ monkeypatch.setattr(
+ studio_db, "upsert_app_settings", lambda updates, **_: values.update(updates) or values
+ )
+ monkeypatch.setattr(hub_settings, "_operator_env", None)
+ monkeypatch.setattr(hub_settings, "_operator_endpoints", None, raising = False)
+ monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset(), raising = False)
+ monkeypatch.setenv("HF_ENDPOINT", "hf-mirror.com")
+ monkeypatch.setenv("HF_DATASETS_SERVER", OPERATOR_DS)
+ monkeypatch.delenv(hub_settings.SOURCE_ENV, raising = False)
+ yield values
+ monkeypatch.undo()
+ hub_settings._refresh_imported_hub_libraries()
+
+
+def test_operator_environment_stands_until_the_owner_saves(store):
+ hub_settings.apply_hub_settings()
+ assert os.environ["HF_ENDPOINT"] == MIRROR
+ assert os.environ["HF_DATASETS_SERVER"] == OPERATOR_DS
+ assert hub_settings.get_hub_settings().hf_endpoint == MIRROR
+ assert store == {}
+
+
+def test_saved_endpoint_reaches_env_and_imported_libraries(store):
+ import datasets.config as datasets_config
+ import huggingface_hub.constants as constants
+ import huggingface_hub.hf_api as hf_api
+ from huggingface_hub import HfFileSystem
+
+ before = HfFileSystem()
+ saved = hub_settings.set_hub_settings("http://127.0.0.1:9000/", True)
+ assert saved.hf_endpoint == "http://127.0.0.1:9000"
+ assert os.environ["HF_ENDPOINT"] == "http://127.0.0.1:9000"
+ assert os.environ["HF_DATASETS_SERVER"] == "http://127.0.0.1:9000"
+ assert constants.ENDPOINT == "http://127.0.0.1:9000"
+ assert constants.HUGGINGFACE_CO_URL_TEMPLATE.startswith("http://127.0.0.1:9000/")
+ assert hf_api.api.endpoint == "http://127.0.0.1:9000"
+ assert datasets_config.HF_ENDPOINT == "http://127.0.0.1:9000"
+ assert datasets_config.HUB_DATASETS_URL.startswith("http://127.0.0.1:9000/datasets/")
+ assert "127.0.0.1" in constants.HF_URL_HOSTS
+ assert before not in HfFileSystem._cache.values()
+
+ hub_settings.set_hub_settings("", True)
+ assert "HF_ENDPOINT" not in os.environ
+ assert os.environ["HF_DATASETS_SERVER"] == OPERATOR_DS
+ assert constants.ENDPOINT == "https://huggingface.co"
+ assert hf_api.api.endpoint == "https://huggingface.co"
+
+
+def test_datasets_server_follows_only_when_asked(store):
+ hub_settings.set_hub_settings(MIRROR, False)
+ assert os.environ["HF_ENDPOINT"] == MIRROR
+ assert os.environ["HF_DATASETS_SERVER"] == OPERATOR_DS
+ hub_settings.set_hub_settings(MIRROR, True)
+ assert os.environ["HF_DATASETS_SERVER"] == MIRROR
+
+
+def test_the_startup_read_leaves_studio_db_as_it_found_it(tmp_path, monkeypatch):
+ import sqlite3
+
+ db = tmp_path / "studio.db"
+ with sqlite3.connect(db) as conn:
+ conn.execute("CREATE TABLE app_settings (key TEXT PRIMARY KEY, value_json TEXT)")
+ conn.execute(
+ "INSERT INTO app_settings VALUES (?, ?)", (hub_settings.SOURCE_KEY, '"modelscope"')
+ )
+ conn.close()
+ before = db.read_bytes()
+ monkeypatch.delitem(sys.modules, "storage.studio_db")
+ monkeypatch.setattr("utils.paths.storage_roots.studio_db_path", lambda: db)
+ assert hub_settings.get_hub_settings().source == hub_settings.MODELSCOPE
+ assert db.read_bytes() == before
+ db.unlink()
+ assert hub_settings.get_hub_settings().source == hub_settings.HUGGINGFACE
+ assert not db.exists()
+
+
+def client(store):
+ app = FastAPI()
+ app.include_router(settings.router)
+ app.dependency_overrides[settings.get_current_subject] = lambda: "admin"
+ app.dependency_overrides[settings.authenticated_via_api_key] = lambda: False
+ return TestClient(app, raise_server_exceptions = False)
+
+
+def test_route_saves_and_reports(client, store):
+ body = client.put(
+ "/hub",
+ json = {"hf_endpoint": "HTTPS://hf-mirror.com/", "datasets_server_follows_endpoint": True},
+ ).json()
+ assert body == {
+ "hf_endpoint": MIRROR,
+ "datasets_server_follows_endpoint": True,
+ "source": "huggingface",
+ "active_source": "huggingface",
+ }
+ assert client.get("/hub").json() == body
+ assert os.environ["HF_DATASETS_SERVER"] == MIRROR
+
+
+def test_only_the_owner_reads_the_endpoint(client, monkeypatch):
+ from fastapi import HTTPException
+
+ async def refuse():
+ raise HTTPException(status_code = 403)
+
+ monkeypatch.setattr(settings.policy, "require_owner", refuse)
+ assert client.get("/hub").status_code == 403
+
+
+def test_an_api_key_cannot_move_the_endpoint_or_source(client, store):
+ before = client.get("/hub").json()
+ client.app.dependency_overrides[settings.authenticated_via_api_key] = lambda: True
+ assert (
+ client.put(
+ "/hub",
+ json = {
+ "hf_endpoint": "https://collector.example",
+ "datasets_server_follows_endpoint": True,
+ },
+ ).status_code
+ == 403
+ )
+ assert client.put("/hub/source", json = {"source": "modelscope"}).status_code == 403
+ client.app.dependency_overrides[settings.authenticated_via_api_key] = lambda: False
+ assert client.get("/hub").json() == before
+
+
+ "raw, canonical",
+ [("", ""), (" hf-mirror.com/ ", MIRROR), ("http://localhost:8080", "http://localhost:8080")],
+)
+def test_validate_hub_endpoint_canonicalises(raw, canonical):
+ assert hub_settings.validate_hub_endpoint(raw) == canonical
+
+
+ "bad",
+ ["http://example.com", "ftp://example.com", "https://u:[email protected]", "https://x.com?a=1"],
+)
+def test_route_rejects_unusable_endpoints(client, store, bad):
+ response = client.put(
+ "/hub", json = {"hf_endpoint": bad, "datasets_server_follows_endpoint": False}
+ )
+ assert response.status_code == 400
+ assert store == {}
+
+
+def test_access_verdicts_do_not_cross_endpoints(store, tmp_path, monkeypatch):
+ from hub.services.models import account_access
+ from hub.utils import hf_tokens
+
+ monkeypatch.setattr(account_access, "_public_repos", {})
+ monkeypatch.setattr(account_access, "_public_verdicts_path", lambda: tmp_path / "proofs.json")
+ monkeypatch.setattr(hf_tokens, "_hub_offline", lambda: False)
+ monkeypatch.setattr(hf_tokens, "_repo_access_cache", {})
+ hub_settings.set_hub_settings("https://a.example.com", False)
+
+ asked = []
+
+ def yes_then_switch(*_, endpoint):
+ asked.append(endpoint)
+ hub_settings.set_hub_settings("https://b.example.com", False)
+ return True
+
+ monkeypatch.setattr(account_access, "_hub_public_answer", yes_then_switch)
+ monkeypatch.setattr(hf_tokens, "_probe_repo_access", yes_then_switch)
+ assert account_access.repo_is_public("org/repo") is True
+ hub_settings.set_hub_settings("https://a.example.com", False)
+ assert hf_tokens._explicit_token_reaches_repo("org/repo", None, "model") is True
+ assert asked == ["https://a.example.com"] * 2
+
+ monkeypatch.setattr(account_access, "_hub_public_answer", lambda *_, **__: None)
+ monkeypatch.setattr(hf_tokens, "_probe_repo_access", lambda *_, **__: None)
+ assert account_access.repo_is_public("org/repo") is False
+ assert hf_tokens._explicit_token_reaches_repo("org/repo", None, "model") is None
+ hub_settings.set_hub_settings("https://a.example.com", False)
+ monkeypatch.setattr(account_access, "_public_repos", {})
+ assert account_access.repo_is_public("org/repo") is True
+
+ proofs = tmp_path / "proofs.json"
+ proofs.write_text(json.dumps({"model:org/old": time.time()}))
+ assert account_access.repo_is_public("org/old") is False
+ account_access.adopt_unnamed_public_proofs(hub_settings.operator_hf_endpoint())
+ assert list(json.loads(proofs.read_text())) == [f"{MIRROR}|model:org/old"]
+ assert account_access.repo_is_public("org/old") is False
+ hub_settings.set_hub_settings(MIRROR, False)
+ assert account_access.repo_is_public("org/old") is True
+
+
+def test_probes_ask_the_endpoint_they_are_given(store, monkeypatch):
+ from hub.services.models import account_access
+ from hub.utils import hf_tokens
+
+ asked = []
+
+ class _Api:
+ def __init__(self, endpoint = None):
+ asked.append(endpoint)
+
+ def repo_info(self, *_a, **_k):
+ raise OSError
+
+ class _Session:
+ def get(self, url, **_k):
+ asked.append(url)
+ raise OSError
+
+ monkeypatch.setattr(account_access, "HfApi", _Api)
+ monkeypatch.setattr("huggingface_hub.utils.get_session", lambda: _Session())
+ account_access._hub_public_answer("org/repo", "model", endpoint = "https://a.example.com")
+ hf_tokens._probe_repo_access("org/repo", None, "model", endpoint = "https://a.example.com")
+ assert asked == [
+ "https://a.example.com",
+ "https://a.example.com/api/models/org/repo/auth-check",
+ ]
+
+
+def test_hub_decisions_are_remembered_per_endpoint(store, monkeypatch):
+ from unittest.mock import MagicMock
+ from types import SimpleNamespace
+
+ import utils.hf_token_validation as validation
+ import asyncio
+
+ import utils.utils as utils_module
+ from core.inference import openai_auto_download as auto_download
+ from utils.security import trusted_org
+
+ checks, apis = [], MagicMock()
+ apis.return_value.model_info.return_value = SimpleNamespace(id = "unsloth/x", author = "unsloth")
+ monkeypatch.setattr(validation, "_cache", {})
+ monkeypatch.setattr(
+ validation,
+ "_check_remote",
+ lambda token, endpoint: checks.append(endpoint)
+ or validation.TokenValidationResult(status = "invalid"),
+ )
+ monkeypatch.setattr(trusted_org, "_verdict_cache", {})
+ monkeypatch.setattr("huggingface_hub.HfApi", apis)
+ monkeypatch.setattr(auto_download, "_not_servable", {})
+ monkeypatch.setattr(utils_module, "_hf_reachability", None)
+
+ for endpoint in ("https://a.example.com", "https://b.example.com"):
+ hub_settings.set_hub_settings(endpoint, False)
+ validation.validate_hf_token("hf_x", rate_key = "k")
+ assert trusted_org.is_trusted_org_repo("unsloth/x")
+ assert not auto_download._is_not_servable("org/repo", None)
+ auto_download._mark_not_servable("org/repo", None, endpoint)
+ assert checks == ["https://a.example.com", "https://b.example.com"]
+ assert [call.kwargs["endpoint"] for call in apis.call_args_list] == [
+ "https://a.example.com",
+ "https://b.example.com",
+ ]
+
+ hub_settings.set_hub_settings("https://a.example.com", False)
+
+ def no_gguf_then_switch(*_a, **_k):
+ hub_settings.set_hub_settings("https://c.example.com", False)
+ return SimpleNamespace(siblings = [])
+
+ apis.return_value.model_info.side_effect = no_gguf_then_switch
+ assert asyncio.run(auto_download._is_downloadable_model("org/other", None)) is False
+ assert apis.call_args.kwargs["endpoint"] == "https://a.example.com"
+ assert not auto_download._is_not_servable("org/other", None)
+ assert auto_download._is_not_servable("org/other", None, "https://a.example.com")
+
+ utils_module._hf_reachability = (time.monotonic(), True)
+ hub_settings.set_hub_settings("https://b.example.com", False)
+ assert utils_module._hf_reachability is None
+
+
+def test_modelscope_points_hub_clients_at_the_adapter_and_back(store, monkeypatch):
+ import huggingface_hub.constants as constants
+ import hub.modelscope.router as modelscope
+ from utils.hf_endpoint import browser_hf_endpoint
+
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: "http://127.0.0.1:1234")
+ settings = hub_settings.set_hub_source("modelscope")
+ assert (settings.source, hub_settings.active_source()) == ("modelscope", "modelscope")
+ assert os.environ["HF_ENDPOINT"] == constants.ENDPOINT == "http://127.0.0.1:1234"
+ assert hub_settings.hugging_face_endpoint() == MIRROR
+ assert os.environ[hub_settings.SOURCE_ENV] == "modelscope"
+ assert browser_hf_endpoint() == "https://huggingface.co"
+
+ hub_settings.set_hub_source("huggingface")
+ assert os.environ["HF_ENDPOINT"] == MIRROR == browser_hf_endpoint()
+
+ def no_adapter():
+ raise RuntimeError("port exhausted")
+
+ monkeypatch.setattr(modelscope, "internal_endpoint", no_adapter)
+ assert hub_settings.set_hub_source("modelscope").source == "modelscope"
+ assert hub_settings.active_source() == "huggingface" and os.environ["HF_ENDPOINT"] == MIRROR
+
+
+def test_modelscope_answers_never_open_the_shared_cache_or_trust_code(store, monkeypatch):
+ from fastapi import HTTPException
+
+ from hub.services.models import account_access
+ from utils.security import trusted_org
+
+ monkeypatch.setenv(hub_settings.SOURCE_ENV, hub_settings.MODELSCOPE)
+ monkeypatch.setattr(account_access, "_public_repos", {})
+ monkeypatch.setattr(account_access, "_hub_public_answer", lambda *_, **__: True)
+ monkeypatch.setattr(account_access, "managed_account", lambda: True)
+ monkeypatch.setattr(trusted_org, "_verdict_cache", {})
+ assert account_access.repo_is_public("org/repo") is False
+ with pytest.raises(HTTPException) as refused:
+ account_access.authorize_download("org/repo", "model", None)
+ assert refused.value.status_code == 403
+ assert trusted_org.is_trusted_org_repo("unsloth/Qwen3-8B", verify_remote = False) is False
+
+ monkeypatch.setenv(hub_settings.SOURCE_ENV, hub_settings.HUGGINGFACE)
+ assert account_access.repo_is_public("org/repo") is True
+ assert trusted_org.is_trusted_org_repo("unsloth/Qwen3-8B", verify_remote = False) is True
+
+
+def test_route_switches_the_source(client, store, monkeypatch):
+ import hub.modelscope.router as modelscope
+
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: "http://127.0.0.1:1234")
+ body = client.put("/hub/source", json = {"source": "modelscope"}).json()
+ assert (body["source"], body["active_source"]) == ("modelscope", "modelscope")
+ assert client.get("/hub").json()["source"] == "modelscope"
+ assert client.put("/hub/source", json = {"source": "gitee"}).status_code == 422
+ client.put("/hub/source", json = {"source": "huggingface"})
+
+
+def test_modelscope_adapter_is_reached_past_a_configured_proxy(store, monkeypatch):
+ import http.server
+ import socket
+ import threading
+ import hub.modelscope.router as modelscope
+ from huggingface_hub.utils import _http
+
+ class Ok(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ self.send_response(200)
+ self.end_headers()
+
+ def log_message(self, *_a):
+ pass
+
+ adapter = http.server.HTTPServer(("127.0.0.1", 0), Ok)
+ threading.Thread(target = adapter.serve_forever, daemon = True).start()
+ closed = socket.socket()
+ closed.bind(("127.0.0.1", 0))
+ dead_proxy = f"http://127.0.0.1:{closed.getsockname()[1]}"
+ for name in ("HTTP_PROXY", "http_proxy"):
+ monkeypatch.setenv(name, dead_proxy)
+ for name in ("NO_PROXY", "no_proxy"):
+ monkeypatch.setenv(name, "example.com")
+ url = f"http://127.0.0.1:{adapter.server_port}"
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: url)
+ _http.close_session()
+ _http.get_session()
+ try:
+ hub_settings.set_hub_source("modelscope")
+ assert _http.get_session().get(url + "/").status_code == 200
+ assert os.environ["NO_PROXY"] == "example.com,127.0.0.1"
+ finally:
+ hub_settings.set_hub_source("huggingface")
+ _http.close_session()
+ adapter.shutdown()
+ closed.close()
+
+
+def test_modelscope_keeps_a_system_proxy(store, monkeypatch):
+ # macOS / Windows: getproxies() = environment proxies OR the system (SCDynamicStore / registry) ones.
+ import urllib.request
+ import hub.modelscope.router as modelscope
+
+ system = {"http": "http://10.0.0.1:7890", "https": "http://10.0.0.1:7890"}
+ for name in (
+ "HTTP_PROXY",
+ "http_proxy",
+ "HTTPS_PROXY",
+ "https_proxy",
+ "ALL_PROXY",
+ "all_proxy",
+ "NO_PROXY",
+ "no_proxy",
+ ):
+ monkeypatch.delenv(name, raising = False)
+ monkeypatch.setattr(
+ urllib.request,
+ "getproxies",
+ lambda: urllib.request.getproxies_environment() or dict(system),
+ )
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: "http://127.0.0.1:1234")
+ try:
+ hub_settings.set_hub_source("modelscope")
+ hub_settings.set_hub_source("huggingface")
+ assert urllib.request.getproxies().get("https") == system["https"]
+ finally:
+ hub_settings.set_hub_source("huggingface")
+
+
+def test_live_switch_does_not_abort_a_streaming_download(store, monkeypatch):
+ import http.server
+ import threading
+ import hub.modelscope.router as modelscope
+ from huggingface_hub.utils import _http
+
+ started, release = threading.Event(), threading.Event()
+
+ class Slow(http.server.BaseHTTPRequestHandler):
+ def do_GET(self):
+ self.send_response(200)
+ self.send_header("Content-Length", str(6 * 65536))
+ self.end_headers()
+ self.wfile.write(b"x" * 65536)
+ self.wfile.flush()
+ started.set()
+ release.wait(10)
+ for _ in range(5):
+ time.sleep(0.2)
+ self.wfile.write(b"x" * 65536)
+ self.wfile.flush()
+
+ def log_message(self, *_a):
+ pass
+
+ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Slow)
+ threading.Thread(target = server.serve_forever, daemon = True).start()
+ monkeypatch.setenv("HTTP_PROXY", "http://127.0.0.1:9")
+ for name in ("NO_PROXY", "no_proxy"):
+ monkeypatch.setenv(name, "127.0.0.1") # the test server itself is reachable
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: "http://localhost:1234")
+ out = {}
+
+ def stream():
+ try:
+ with _http.get_session().stream("GET", f"http://127.0.0.1:{server.server_port}/") as r:
+ out["n"] = sum(len(c) for c in r.iter_bytes())
+ except Exception as exc: # noqa: BLE001
+ out["error"] = repr(exc)
+
+ _http.close_session()
+ t = threading.Thread(target = stream)
+ t.start()
+ try:
+ assert started.wait(10)
+ hub_settings.set_hub_source("modelscope")
+ release.set()
+ t.join(10)
+ assert out == {"n": 6 * 65536}
+ finally:
+ release.set()
+ hub_settings.set_hub_source("huggingface")
+ server.shutdown()
+
+
+def test_modelscope_switch_survives_huggingface_hub_0x(store, monkeypatch):
+ # 0.36.x (Python 3.9 installs) has requests sessions and no close_session / _CLIENT_LOCK.
+ import types
+ import hub.modelscope.router as modelscope
+
+ monkeypatch.setitem(
+ sys.modules,
+ "huggingface_hub.utils._http",
+ types.SimpleNamespace(reset_sessions = lambda: None),
+ )
+ monkeypatch.setenv("HTTP_PROXY", "http://127.0.0.1:9")
+ for name in ("NO_PROXY", "no_proxy"):
+ monkeypatch.setenv(name, "")
+ monkeypatch.setattr(modelscope, "internal_endpoint", lambda: "http://127.0.0.1:1234")
+ try:
+ assert hub_settings.set_hub_source("modelscope").source == "modelscope"
+ assert "127.0.0.1" in os.environ["NO_PROXY"].split(",")
+ finally:
+ hub_settings.set_hub_source("huggingface")
+
+
+def test_a_saved_endpoint_stays_out_of_the_csp_the_operators_does_not(store):
+ from utils.hf_endpoint import csp_asset_sources, csp_connect_sources
+
+ hub_settings.apply_hub_settings()
+ assert csp_connect_sources() == (MIRROR, OPERATOR_DS)
+ hub_settings.set_hub_settings("https://10.0.0.5:9000", True)
+ assert os.environ["HF_ENDPOINT"] == "https://10.0.0.5:9000"
+ assert csp_connect_sources() == ()
+ hub_settings.set_hub_settings("http://127.0.0.1:9700", False)
+ assert csp_connect_sources() == (OPERATOR_DS,) and csp_asset_sources() == ()
+ hub_settings.set_hub_settings("", False)
+ assert csp_connect_sources() == ("https://huggingface.co", OPERATOR_DS)
diff --git a/studio/backend/tests/test_hub_token_caller_identity.py b/studio/backend/tests/test_hub_token_caller_identity.py
index 38051b6af91..3ccad4f97f7 100644
--- a/studio/backend/tests/test_hub_token_caller_identity.py
+++ b/studio/backend/tests/test_hub_token_caller_identity.py
@@ -2009,7 +2009,7 @@ def test_an_anonymous_caller_keeps_a_public_cached_dataset_and_loses_a_private_o
monkeypatch.setattr(
hf_tokens,
"_probe_repo_access",
- lambda _repo, _token, repo_type: public and repo_type == "dataset",
+ lambda _repo, _token, repo_type, **_k: public and repo_type == "dataset",
)
_hub_reachable(monkeypatch)
monkeypatch.setattr(dataset_cache, "dataset_cache_can_answer", lambda *_a, **_k: True)
diff --git a/studio/backend/tests/test_local_options_match_start_validation.py b/studio/backend/tests/test_local_options_match_start_validation.py
index 56e74b1430d..574b72e8cdb 100644
--- a/studio/backend/tests/test_local_options_match_start_validation.py
+++ b/studio/backend/tests/test_local_options_match_start_validation.py
@@ -114,3 +114,77 @@ def test_the_two_grammars_agree_over_a_generated_alphabet():
if offered_subset is not None and not _subset_accepted(offered_subset):
mismatches.append(("subset offered, start rejects", value, offered_subset))
assert not mismatches, mismatches
+
+
+def _hub_options(
+ monkeypatch,
+ factory,
+ refuse = lambda *a, **k: None,
+ info = lambda self, repo, token, timeout: type("I", (), {"sha": "c0ffee" if timeout else None}),
+):
+ import datasets.load
+ from hub.schemas.datasets import HubDatasetOptionsRequest
+ from hub.services.datasets.local_options import hub_dataset_options
+ from hub.utils import dataset_cache
+
+ monkeypatch.setattr(dataset_cache, "refuse_unauthorized_dataset_preview", refuse)
+ monkeypatch.setattr(datasets.load, "dataset_module_factory", factory)
+ monkeypatch.setattr("huggingface_hub.HfApi.dataset_info", info)
+ return hub_dataset_options(HubDatasetOptionsRequest(dataset_name = " org/data "), "hf_x")
+
+
+def test_hub_options_come_from_the_repo_files_not_card_only_configs(monkeypatch):
+ from types import SimpleNamespace as NS
+ from datasets import NamedSplit
+
+ configs = [
+ NS(name = "cola", data_files = {"validation": 0, NamedSplit("train"): 0}),
+ NS(name = "../x", data_files = {"train": 0}),
+ ]
+ module = NS(
+ builder_configs_parameters = NS(builder_configs = configs),
+ dataset_infos = {"ax": NS(splits = {"test": 0})},
+ )
+ tokens = []
+ response = _hub_options(
+ monkeypatch,
+ lambda repo, revision, download_config: tokens.append(
+ (repo, revision, download_config.token)
+ )
+ or module,
+ )
+ assert ([(o.config, o.split) for o in response.splits], tokens) == (
+ [("cola", "train"), ("cola", "validation")],
+ [("org/data", "c0ffee", "hf_x")],
+ )
+
+
+def _raises(error):
+ def raise_(*_a, **_k):
+ raise error
+
+ return raise_
+
+
+ "error, status",
+ [
+ (RuntimeError("Dataset scripts are no longer supported"), 400),
+ (FileNotFoundError("gone"), 404),
+ ],
+)
+def test_an_unresolvable_hub_dataset_is_a_client_error(monkeypatch, error, status):
+ from fastapi import HTTPException
+ with pytest.raises(HTTPException) as caught:
+ _hub_options(monkeypatch, _raises(error))
+ assert (caught.value.status_code, str(error) in caught.value.detail) == (status, True)
+
+
+def test_the_hub_options_preview_gate_runs_before_any_fetch(monkeypatch):
+ with pytest.raises(LookupError):
+ _hub_options(
+ monkeypatch,
+ _raises(AssertionError()),
+ refuse = _raises(LookupError()),
+ info = _raises(AssertionError()),
+ )
diff --git a/studio/backend/tests/test_memory_estimate_side_effects.py b/studio/backend/tests/test_memory_estimate_side_effects.py
index 7c21a480e70..55d9dcf8107 100644
--- a/studio/backend/tests/test_memory_estimate_side_effects.py
+++ b/studio/backend/tests/test_memory_estimate_side_effects.py
@@ -489,6 +489,7 @@ class TestInertProbeFallback:
"threading.Lock()",
"threading.RLock()",
"threading.Event()",
+ "threading.local()",
}, (
"the inert branch of LlamaCppBackend.__init__ grew a call that could "
f"raise: {sorted(set(unguarded_calls))}"
diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.py
index 15c543167f9..1db76b2e8b0 100644
--- a/studio/backend/tests/test_middleware.py
+++ b/studio/backend/tests/test_middleware.py
@@ -1470,3 +1470,24 @@ class TestRemoteAccessCORS:
for published in (None, self.TUNNEL):
app.state.cloudflare_url = published
assert self._allowed(client, "https://evil.example") == "https://evil.example"
+
+
+def test_health_reports_the_default_for_a_settings_saved_endpoint(main_module, monkeypatch):
+ from starlette.requests import Request
+ import utils.hub_settings as hub_settings
+
+ local = Request({"type": "http", "headers": [], "client": ("127.0.0.1", 1)})
+ monkeypatch.setenv("HF_ENDPOINT", "https://hub.internal")
+ monkeypatch.setenv("HF_DATASETS_SERVER", "https://hub.internal")
+ monkeypatch.setattr(hub_settings, "_saved_only_endpoints", frozenset(), raising = False)
+ assert main_module._reportable_hf_endpoints(local) == {
+ "hf_endpoint": "https://hub.internal",
+ "hf_datasets_server": "https://hub.internal",
+ }
+ monkeypatch.setattr(
+ hub_settings, "_saved_only_endpoints", frozenset({"https://hub.internal"}), raising = False
+ )
+ assert main_module._reportable_hf_endpoints(local) == {
+ "hf_endpoint": "https://huggingface.co",
+ "hf_datasets_server": "https://datasets-server.huggingface.co",
+ }
diff --git a/studio/backend/tests/test_modelscope_hub.py b/studio/backend/tests/test_modelscope_hub.py
new file mode 100644
index 00000000000..4be9373ba6e
--- /dev/null
+++ b/studio/backend/tests/test_modelscope_hub.py
@@ -0,0 +1,313 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The ModelScope adapter answers the Hub routes huggingface_hub and the browser use."""
+
+from __future__ import annotations
+
+from pathlib import Path
+import sys
+import types as _types
+
+_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)
+if _BACKEND_DIR not in sys.path:
+ sys.path.insert(0, _BACKEND_DIR)
+
+_loggers_stub = _types.ModuleType("loggers")
+_loggers_stub.get_logger = lambda name: __import__("logging").getLogger(name)
+sys.modules.setdefault("loggers", _loggers_stub)
+
+import httpx
+import pytest
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+from hub.modelscope import upstream
+from hub.modelscope.router import build_router
+
+SHA = "a" * 40
+TAG = "b" * 40
+
+
+def _pkt(line: str) -> bytes:
+ return f"{len(line) + 4:04x}".encode() + line.encode()
+
+
+REFS = (
+ _pkt("# service=git-upload-pack\n")
+ + b"0000"
+ + _pkt(f"{SHA} HEAD\0multi_ack\n")
+ + _pkt(f"{SHA} refs/heads/master\n")
+ + _pkt(f"{TAG} refs/tags/v1\n")
+ + b"0000"
+)
+MODEL_FILES = [
+ {"Path": "config.json", "Size": 659, "Sha256": "c" * 64, "IsLFS": False},
+ {"Path": "model.safetensors", "Size": 1000, "Sha256": "d" * 64, "IsLFS": True},
+ {"Path": "sub", "Type": "tree"},
+ {"Path": "sub/tokenizer.json", "Size": 5, "Sha256": "e" * 64, "IsLFS": False},
+]
+DATASET_FILES = [
+ {"Path": "dataset_infos.json", "Size": 9, "Sha256": "f" * 64, "IsLFS": False},
+ {"Path": "data.jsonl", "Size": 90, "Sha256": "9" * 64, "IsLFS": False},
+]
+ITEM = {
+ "id": "Qwen/Tiny",
+ "downloads": 7,
+ "likes": 2,
+ "params": 494032768,
+ "last_modified": "2025-02-26T18:24:53Z",
+ "tags": [
+ "model_type:qwen2",
+ "library:transformer",
+ "library:safetensors",
+ "task:text-generation",
+ ],
+ "tasks": ["text-generation"],
+}
+
+
+def _upstream(request: httpx.Request) -> httpx.Response:
+ path = request.url.path
+ if path in ("/Qwen/Tiny.git/info/refs", "/datasets/org/ds.git/info/refs"):
+ return httpx.Response(200, content = REFS)
+ if path.endswith(".git/info/refs"):
+ return httpx.Response(500 if "boom" in path else 404)
+ if path == "/api/v1/models/Qwen/Tiny/repo/files":
+ return httpx.Response(200, json = {"Data": {"Files": MODEL_FILES}})
+ if path == "/api/v1/datasets/org/ds/repo/tree":
+ return httpx.Response(200, json = {"Data": {"Files": DATASET_FILES}})
+ if path == "/openapi/v1/models/Qwen/Tiny":
+ return httpx.Response(200, json = {"data": ITEM})
+ if path == f"/models/Qwen/Tiny/resolve/{SHA}/config.json":
+ return httpx.Response(200, content = b'{"model_type": "qwen2"}')
+ if path == "/openapi/v1/models":
+ return httpx.Response(200, json = {"data": {"models": [ITEM], "total_count": 120}})
+ return httpx.Response(404)
+
+
+def hub(monkeypatch):
+ monkeypatch.setattr(upstream, "transport", httpx.MockTransport(_upstream))
+ monkeypatch.setattr(upstream, "_clients", upstream.weakref.WeakKeyDictionary())
+ upstream._cache.clear()
+ app = FastAPI()
+ app.include_router(build_router(browser = False))
+ return TestClient(app, follow_redirects = False)
+
+
+def test_search_answers_in_hub_shape_with_absolute_paging(hub):
+ response = hub.get("/api/models", params = {"search": "tiny", "sort": "downloads"})
+ [entry] = response.json()
+ assert entry["id"] == "Qwen/Tiny" and entry["lastModified"] == "2025-02-26T18:24:53Z"
+ assert entry["library_name"] == "transformers" and entry["pipeline_tag"] == "text-generation"
+ assert entry["safetensors"]["total"] == 494032768 and entry["config"] == {"model_type": "qwen2"}
+ assert response.headers["link"] == (
+ '<http://testserver/api/models?search=tiny&sort=downloads&p=2>; rel="next"'
+ )
+ assert hub.get("/api/models", params = {"filter": "gguf"}).json() == []
+ assert len(hub.get("/api/models", params = {"pipeline_tag": "Text-Generation"}).json()) == 1
+ assert hub.get("/api/models", params = {"pipeline_tag": "image-classification"}).json() == []
+
+
+def test_info_names_the_commit_and_files(hub):
+ body = hub.get("/api/models/Qwen/Tiny").json()
+ assert body["sha"] == SHA
+ assert {
+ "rfilename": "model.safetensors",
+ "size": 1000,
+ "blobId": "d" * 64,
+ "lfs": {"sha256": "d" * 64, "size": 1000, "pointerSize": 134},
+ } in body["siblings"]
+ assert hub.get("/api/models/Qwen/Tiny/revision/v1").json()["sha"] == TAG
+ for path, code in [
+ ("/api/models/Qwen/Tiny/revision/nope", "RevisionNotFound"),
+ ("/api/models/no/one", "RepoNotFound"),
+ ("/api/models/no/one/auth-check", "RepoNotFound"),
+ ]:
+ response = hub.get(path)
+ assert (response.status_code, response.headers.get("x-error-code")) == (404, code)
+
+
+def test_resolve_describes_then_redirects_to_the_pinned_file(hub):
+ head = hub.head("/Qwen/Tiny/resolve/main/model.safetensors")
+ assert head.headers["x-repo-commit"] == SHA
+ assert (
+ head.headers["location"]
+ == f"https://www.modelscope.cn/models/Qwen/Tiny/resolve/{SHA}/model.safetensors"
+ )
+ assert (
+ head.headers["x-linked-etag"] == f'"{"d" * 64}"' and head.headers["x-linked-size"] == "1000"
+ )
+ get = hub.get("/Qwen/Tiny/resolve/main/sub/tokenizer.json")
+ assert get.status_code == 302
+ assert (
+ get.headers["location"]
+ == f"https://www.modelscope.cn/models/Qwen/Tiny/resolve/{SHA}/sub/tokenizer.json"
+ )
+ missing = hub.head("/Qwen/Tiny/resolve/main/nope.json")
+ assert (missing.status_code, missing.headers["x-error-code"]) == (404, "EntryNotFound")
+
+
+def test_browser_resolve_relays_to_a_session_and_redirects_anyone_else(hub, monkeypatch):
+ import hub.modelscope.router as router
+
+ seen = []
+ monkeypatch.setattr(
+ upstream,
+ "transport",
+ httpx.MockTransport(lambda request: seen.append(request) or _upstream(request)),
+ )
+ signed_in = False
+
+ async def session(_request) -> bool:
+ return signed_in
+
+ monkeypatch.setattr(router, "signed_in", session)
+ app = FastAPI()
+ app.include_router(build_router(browser = True))
+ browser = TestClient(app, follow_redirects = False)
+ response = browser.get("/datasets/org/ds/resolve/main/img/a.png")
+ assert (
+ response.headers["location"]
+ == "https://www.modelscope.cn/datasets/org/ds/resolve/master/img/a.png"
+ )
+ assert seen == []
+ assert (
+ browser.get(
+ "/Qwen/Tiny/resolve/main/config.json", headers = {"Authorization": "Bearer stale"}
+ ).status_code
+ == 401
+ )
+ signed_in = True
+ relayed = browser.get("/Qwen/Tiny/resolve/main/config.json")
+ assert (relayed.status_code, relayed.json()) == (200, {"model_type": "qwen2"})
+ assert relayed.headers["x-repo-commit"] == SHA
+
+
+def test_tree_and_paths_info_walk_the_listing(hub):
+ top = hub.get(f"/api/models/Qwen/Tiny/tree/{SHA}").json()
+ assert [(e["path"], e["type"]) for e in top] == [
+ ("config.json", "file"),
+ ("model.safetensors", "file"),
+ ("sub", "directory"),
+ ]
+ assert "sub/tokenizer.json" in [
+ e["path"] for e in hub.get("/api/models/Qwen/Tiny/tree/main?recursive=1").json()
+ ]
+ found = hub.post(
+ "/api/models/Qwen/Tiny/paths-info/main", data = {"paths": ["config.json", "sub", "nope"]}
+ )
+ assert [e["path"] for e in found.json()] == ["config.json", "sub"]
+
+
+def test_modelscope_dataset_metadata_is_hidden_from_datasets(hub):
+ assert [e["path"] for e in hub.get("/api/datasets/org/ds/tree/main").json()] == ["data.jsonl"]
+ assert hub.head("/datasets/org/ds/resolve/main/dataset_infos.json").status_code == 404
+ assert hub.head("/datasets/org/ds/resolve/main/data.jsonl").status_code == 302
+
+
+def test_an_aliased_repo_is_served_under_its_hugging_face_id(hub, monkeypatch):
+ assert upstream.upstream_id("model", "minimaxai/minimax-music3") == "MiniMax/MiniMax-Music3"
+ monkeypatch.setattr(
+ upstream,
+ "_ALIAS_INDEX",
+ {"model": {"hf/tiny": "Qwen/Tiny"}, "dataset": {"hf/ds": "org/ds"}},
+ )
+ body = hub.get("/api/models/HF/Tiny").json()
+ assert (body["id"], body["sha"], len(body["siblings"])) == ("HF/Tiny", SHA, 3)
+ location = hub.head("/HF/Tiny/resolve/main/config.json").headers["location"]
+ assert location == f"https://www.modelscope.cn/models/Qwen/Tiny/resolve/{SHA}/config.json"
+ assert hub.get("/HF/Tiny").headers["location"] == "https://www.modelscope.cn/models/Qwen/Tiny"
+ assert [e["path"] for e in hub.get("/api/datasets/hf/ds/tree/main").json()] == ["data.jsonl"]
+ assert (
+ hub.get("/datasets/hf/ds").headers["location"]
+ == "https://www.modelscope.cn/datasets/org/ds"
+ )
+
+
+def test_a_listed_hugging_face_pin_is_served_from_the_matching_modelscope_commit(hub, monkeypatch):
+ dac = ("model", "ibm-research/DAC.speech.v1.0", "1ea7f64cd0678415e2d8c32d67b190722cb9b149")
+ assert upstream.upstream_commit(*dac) == "3268c083add6c07ae3c4a36650863eb2c3bb6717"
+ monkeypatch.setattr(upstream, "_PINNED_COMMITS", {("model", "qwen/tiny", "c" * 40): SHA})
+ head = hub.head(f"/Qwen/Tiny/resolve/{'C' * 40}/config.json")
+ assert head.headers["x-repo-commit"] == "c" * 40
+ assert head.headers["location"].endswith(f"/Qwen/Tiny/resolve/{SHA}/config.json")
+ assert hub.get(f"/api/models/Qwen/Tiny/revision/{'c' * 40}").json()["sha"] == "c" * 40
+ assert upstream.branch_url("model", "Qwen/Tiny", "c" * 40, "a").endswith(f"/{SHA}/a")
+ assert upstream.upstream_commit("model", "Qwen/Tiny", "D" * 40) == "d" * 40
+
+
+def test_every_error_reader_names_a_repo_missing_on_modelscope(hub, monkeypatch):
+ from datasets.exceptions import DatasetNotFoundError
+ from huggingface_hub.errors import RepositoryNotFoundError
+ from huggingface_hub.utils import _http as hf_http, hf_raise_for_status
+ from hub.utils import download_registry
+ from hub.utils.hf_errors import modelscope_missing, not_on_modelscope
+ from utils.utils import format_error_message
+
+ sentence = not_on_modelscope("o/tiny.en-GGUF")
+ answer = hub.get("/api/models/o/tiny.en-GGUF")
+ response = hf_http.httpx.Response(
+ answer.status_code,
+ headers = answer.headers,
+ content = answer.content,
+ request = hf_http.httpx.Request("GET", str(answer.url)),
+ )
+ missing = pytest.raises(RepositoryNotFoundError, hf_raise_for_status, response)
+ wrapped = RuntimeError("load failed")
+ wrapped.__cause__, wrapped.__context__ = Exception("unrelated"), missing.value
+ assert modelscope_missing(wrapped) == sentence
+ wrapped.__cause__, wrapped.__context__ = missing.value, None
+ assert format_error_message(wrapped, "unsloth/orpheus-3b-0.1-ft") == sentence
+ assert download_registry.humanize_worker_error(f"Error: {missing.value}") == sentence
+ lost = DatasetNotFoundError("Dataset 'org/ds' doesn't exist on the Hub")
+ monkeypatch.delenv("UNSLOTH_STUDIO_HUB_SOURCE", raising = False)
+ assert modelscope_missing(lost, "org/ds") is None
+ monkeypatch.setenv("UNSLOTH_STUDIO_HUB_SOURCE", "modelscope")
+ assert modelscope_missing(lost, "org/ds") == not_on_modelscope("org/ds")
+
+
+def test_writes_bad_ids_and_upstream_failures(hub):
+ assert hub.post("/api/repos/create", json = {}).status_code == 403
+ assert hub.get("/api/models/a/b..c").status_code == 400
+ assert hub.get("/Qwen/Tiny/resolve/main/sub/%2E%2E/config.json").status_code == 400
+ assert hub.get("/api/models/boom/repo/auth-check").status_code == 502
+
+
+def test_cache_gate_asks_hugging_face_while_modelscope_serves(monkeypatch):
+ import huggingface_hub.utils as hf_utils
+ from hub.modelscope.router import internal_endpoint
+ from hub.utils import hf_tokens
+ from utils import hub_settings
+
+ asked = []
+
+ class Session:
+ def get(self, url, **_k):
+ asked.append(url)
+ return hf_http_response(401, url)
+
+ def hf_http_response(status, url):
+ return httpx.Response(status, request = httpx.Request("GET", url))
+
+ monkeypatch.setattr(hf_utils, "get_session", lambda: Session())
+ monkeypatch.setattr(hub_settings, "hugging_face_endpoint", lambda: "https://hf.example")
+ monkeypatch.setenv("HF_ENDPOINT", internal_endpoint())
+ monkeypatch.setenv(hub_settings.SOURCE_ENV, hub_settings.MODELSCOPE)
+ refused = hf_tokens.cached_read_refused(
+ "hf_junk", repo_id = "victim/secret-model", is_cached = lambda: True
+ )
+ assert refused is True
+ assert asked and all(url.startswith("https://hf.example/api/") for url in asked)
+
+
+def test_a_dataset_tree_past_the_page_cap_is_an_error_not_a_truncated_listing(hub, monkeypatch):
+ import asyncio
+
+ monkeypatch.setattr(upstream, "_TREE_MAX_PAGES", 2)
+ monkeypatch.setattr(upstream, "_TREE_PAGE", len(DATASET_FILES))
+ with pytest.raises(upstream.UpstreamError):
+ asyncio.run(upstream._file_entries("dataset", "org/ds", SHA))
+ monkeypatch.setattr(upstream, "_TREE_PAGE", len(DATASET_FILES) + 1)
+ assert asyncio.run(upstream._file_entries("dataset", "org/ds", SHA)) == DATASET_FILES
diff --git a/studio/backend/tests/test_openai_auto_download.py b/studio/backend/tests/test_openai_auto_download.py
index 003af402826..186368f5a6d 100644
--- a/studio/backend/tests/test_openai_auto_download.py
+++ b/studio/backend/tests/test_openai_auto_download.py
@@ -159,7 +159,11 @@ def hub(monkeypatch):
}
class _FakeApi:
- def __init__(self, token = None):
+ def __init__(
+ self,
+ token = None,
+ **_k,
+ ):
state["token"] = token
def model_info(self, repo_id, **kwargs):
@@ -381,6 +385,16 @@ def test_missing_repo_is_404_without_confirming_existence(hub):
assert hub["started"] == []
+def test_a_repo_missing_on_modelscope_keeps_the_switch_back_advice(hub):
+ from hub.utils.hf_errors import not_on_modelscope
+
+ sentence = not_on_modelscope("unsloth/not-real")
+ hub["raise"] = _hub_error(_repo_not_found_error(), 404, sentence)
+ refusal = _run("unsloth/not-real:UD-Q4_K_XL")
+ assert refusal.status == 404 and refusal.code == "model_not_found"
+ assert refusal.message == sentence
+
+
def test_an_id_the_hub_does_not_know_falls_through(hub):
hub["raise"] = _hub_error(_repo_not_found_error(), 404, "nope")
# "vendor/model" is how LiteLLM names providers, so an unknown id stays a foreign label.
diff --git a/studio/backend/tests/test_orchestrator_unload_cancel.py b/studio/backend/tests/test_orchestrator_unload_cancel.py
index 9e8f63f227f..491c998a45d 100644
--- a/studio/backend/tests/test_orchestrator_unload_cancel.py
+++ b/studio/backend/tests/test_orchestrator_unload_cancel.py
@@ -1446,6 +1446,25 @@ def test_gguf_load_attempt_does_not_hide_a_real_resolver_failure():
)
+def test_a_codec_missing_on_modelscope_stays_with_the_load_that_hit_it():
+ import asyncio
+ import routes.inference as inference_route
+ from unittest import mock
+ from core.inference.llama_cpp import GgufLoadIntent, LlamaCppBackend
+ from hub.utils.hf_errors import not_on_modelscope
+
+ llama = LlamaCppBackend()
+ llama._codec_failure.message, llama._llama_update_in_progress = "x", True # refused after reset
+ pytest.raises(RuntimeError, llama.load_model, intent = GgufLoadIntent(model_identifier = "o/m"))
+ assert llama.codec_failure() is None
+ llama._healthy, failures = True, []
+ llama.init_audio_codec = mock.Mock(side_effect = OSError(not_on_modelscope("o/c")))
+ llama.load_model = lambda **_: llama._apply_detected_audio("snac")
+ run = inference_route._run_gguf_load_attempt(llama, object(), threading.Event(), failures)
+ assert asyncio.run(run) is False and failures == [not_on_modelscope("o/c")]
+ assert llama.codec_failure() is None # set on the loading thread only
+
+
def test_stale_unload_does_not_hide_an_update_refusal():
import asyncio
import routes.inference as inference_route
diff --git a/studio/backend/tests/test_trainer_modelscope_gate.py b/studio/backend/tests/test_trainer_modelscope_gate.py
new file mode 100644
index 00000000000..0eaa2d0afe5
--- /dev/null
+++ b/studio/backend/tests/test_trainer_modelscope_gate.py
@@ -0,0 +1,103 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""Training's gate check names a repo ModelScope does not host instead of asking for a token."""
+
+from __future__ import annotations
+
+import importlib
+import sys
+import types
+from unittest.mock import MagicMock
+
+import pytest
+
+_STUBBED: list[str] = []
+
+
+def _stub_if_missing(name, attrs):
+ """Register a stub module for a dep the backend pytest job does not install.
+
+ Same helper and reason as test_trainer_stdout_quiet.py: core.training.trainer imports
+ unsloth (and through it unsloth_zoo) and trl at module scope, while the pytest matrix in
+ studio-backend-ci.yml installs studio.txt plus torch and transformers and deliberately
+ stops there, because the repo-cpu-tests job beside it is the one that installs
+ unsloth_zoo, for the REPO-ROOT tests/ tree. Unstubbed, this module fails COLLECTION and
+ takes the whole job down. A real install is left alone. __spec__ = None keeps the
+ trainer's own _ensure_real_packages namespace-shadow guard a no-op on the stub.
+ """
+ if name in sys.modules:
+ return
+ try:
+ importlib.import_module(name)
+ return
+ except Exception: # noqa: BLE001 - unusable here either way, so stub it
+ pass
+ _STUBBED.append(name)
+ mod = types.ModuleType(name)
+ mod.__spec__ = None
+ for attr in attrs:
+ setattr(mod, attr, MagicMock())
+ sys.modules[name] = mod
+ parent, _, child = name.rpartition(".")
+ if parent and parent in sys.modules:
+ setattr(sys.modules[parent], child, mod)
+
+
+pytest.importorskip("torch")
+pytest.importorskip("transformers")
+
+_stub_if_missing("unsloth", ("FastLanguageModel", "FastVisionModel", "is_bfloat16_supported"))
+_stub_if_missing("unsloth.chat_templates", ("get_chat_template",))
+_stub_if_missing("trl", ("SFTTrainer", "SFTConfig"))
+
+from core.training import trainer as trainer_module # noqa: E402
+from core.training.trainer import UnslothTrainer # noqa: E402
+
+for _name in reversed(_STUBBED):
+ sys.modules.pop(_name, None)
+
+
+def _missing_on_modelscope(repo):
+ import httpx
+ from huggingface_hub.errors import RepositoryNotFoundError
+ from hub.utils.hf_errors import not_on_modelscope
+
+ return RepositoryNotFoundError(
+ not_on_modelscope(repo),
+ response = httpx.Response(
+ 404, request = httpx.Request("GET", "http://127.0.0.1:1/api/models/" + repo)
+ ),
+ )
+
+
+ "gone, expected", [(True, "not on ModelScope"), (False, "gated or private")]
+)
+def test_gate_check_reports_a_repo_missing_on_modelscope(monkeypatch, gone, expected):
+ import huggingface_hub
+ import utils.cache_cleanup
+ from huggingface_hub.errors import RepositoryNotFoundError
+ import httpx
+
+ def model_info(*_a, **_k):
+ if gone:
+ raise _missing_on_modelscope("org/model")
+ raise RepositoryNotFoundError(
+ "Repository Not Found",
+ response = httpx.Response(404, request = httpx.Request("GET", "https://huggingface.co")),
+ )
+
+ monkeypatch.setattr(huggingface_hub, "model_info", model_info)
+ monkeypatch.setattr(trainer_module, "clear_gpu_cache", lambda *a, **k: None)
+ monkeypatch.setattr(utils.cache_cleanup, "clear_unsloth_compiled_cache", lambda *a, **k: None)
+ monkeypatch.setattr(trainer_module, "detect_audio_type_checked", lambda *a, **k: (None, True))
+ monkeypatch.setattr(trainer_module, "is_vision_model", lambda *a, **k: False)
+ monkeypatch.setattr(trainer_module, "_env_offline", lambda: False)
+ trainer = object.__new__(UnslothTrainer)
+ trainer.model = trainer.tokenizer = trainer.trainer = None
+ errors = []
+ trainer._update_progress = lambda **kw: errors.append(kw["error"]) if kw.get("error") else None
+ trainer._cleanup_audio_artifacts = lambda: None
+ assert trainer.load_model("org/model", hf_token = None) is False
+ assert expected in errors[-1]
diff --git a/studio/backend/utils/hf_endpoint.py b/studio/backend/utils/hf_endpoint.py
index 6272c8d0146..b6c0fb30f34 100644
--- a/studio/backend/utils/hf_endpoint.py
+++ b/studio/backend/utils/hf_endpoint.py
@@ -88,6 +88,23 @@ def _sanitize(candidate: str, default: str, var_name: str) -> str:
"""
if not candidate:
return default
+ canonical, reason = _check(candidate)
+ if canonical is not None:
+ return canonical
+ if candidate not in _rejected_warned:
+ _rejected_warned.add(candidate)
+ logger.warning(
+ "%s=%r %s; ignoring it and using %s instead.",
+ var_name,
+ candidate,
+ reason,
+ default,
+ )
+ return default
+
+
+def _check(candidate: str) -> tuple[str | None, str | None]:
+ """``(canonical, None)`` for a usable endpoint, else ``(None, reason)``."""
if any(ch in _FORBIDDEN_CHARS for ch in candidate) or any(
ord(ch) < 0x20 or ord(ch) == 0x7F for ch in candidate
):
@@ -122,17 +139,25 @@ def _sanitize(candidate: str, default: str, var_name: str) -> str:
)
else:
# Folded: RFC 3986 3.1, and the frontend keys its cache on this string.
- return _canonical(parts, parts.scheme + candidate[len(parts.scheme) :])
- if candidate not in _rejected_warned:
- _rejected_warned.add(candidate)
- logger.warning(
- "%s=%r %s; ignoring it and using %s instead.",
- var_name,
- candidate,
- reason,
- default,
- )
- return default
+ return _canonical(parts, parts.scheme + candidate[len(parts.scheme) :]), None
+ return None, reason
+
+
+def validate_hub_endpoint(raw: str) -> str:
+ """A user-entered endpoint in the form the env vars carry; ``""`` means the official Hub.
+
+ Same rules the environment values are held to, but a rejected value raises
+ ``ValueError`` with the reason instead of silently falling back.
+ """
+ value = raw.strip().rstrip("/")
+ if not value:
+ return ""
+ if "://" not in value:
+ value = "https://" + value
+ canonical, reason = _check(value)
+ if canonical is None:
+ raise ValueError(f"The endpoint {reason}.")
+ return canonical
def is_private_host(hostname: str | None) -> bool:
@@ -253,7 +278,7 @@ def normalize_hf_endpoint_env() -> None:
os.environ["HF_ENDPOINT"] = endpoint
-def csp_connect_sources() -> tuple[str, str]:
+def csp_connect_sources() -> tuple[str, ...]:
"""The two endpoints as CSP ``connect-src`` sources, i.e. origins only.
A CSP host-source carrying a path is matched *exactly* unless the path ends
@@ -262,8 +287,18 @@ def csp_connect_sources() -> tuple[str, str]:
``/hf/api/models`` request under it, in Chrome, Edge, Firefox and Safari
alike. The path belongs in the request URL, not in the policy, so the source
is reduced to scheme://host[:port].
+
+ A settings-saved endpoint stays out: the browser reaches it through the backend
+ relay, and this policy goes to every client, where a private address must not show.
"""
- return (_origin_of(get_hf_endpoint()), _origin_of(get_hf_datasets_server()))
+ from utils.hub_settings import saved_only_endpoints
+
+ hidden = saved_only_endpoints()
+ return tuple(
+ _origin_of(endpoint)
+ for endpoint in (browser_hf_endpoint(), get_hf_datasets_server())
+ if endpoint not in hidden
+ )
def csp_asset_sources() -> tuple[str, ...]:
@@ -296,6 +331,13 @@ def get_hf_endpoint() -> str:
return _sanitize(hf_endpoint_url().rstrip("/"), _DEFAULT_HF_ENDPOINT, "HF_ENDPOINT")
+def browser_hf_endpoint() -> str:
+ """The endpoint the browser uses. The ModelScope adapter's loopback listener is for
+ this process only; the browser reaches ModelScope through its authenticated mount."""
+ from utils.hub_settings import MODELSCOPE, active_source
+ return _DEFAULT_HF_ENDPOINT if active_source() == MODELSCOPE else get_hf_endpoint()
+
+
def get_hf_datasets_server() -> str:
"""Return the datasets-server base URL (no trailing slash).
@@ -309,7 +351,7 @@ def get_hf_datasets_server() -> str:
endpoint = raw if "://" in raw else "https://" + raw
return _sanitize(endpoint.rstrip("/"), _DEFAULT_DATASETS_SERVER, "HF_DATASETS_SERVER")
global _ds_mirror_warned
- if not _ds_mirror_warned and get_hf_endpoint() != _DEFAULT_HF_ENDPOINT:
+ if not _ds_mirror_warned and browser_hf_endpoint() != _DEFAULT_HF_ENDPOINT:
_ds_mirror_warned = True
logger.warning(
"HF_ENDPOINT is set to %s but HF_DATASETS_SERVER is unset; "
diff --git a/studio/backend/utils/hf_token_validation.py b/studio/backend/utils/hf_token_validation.py
index 894c3d95096..b0ef4ad28be 100644
--- a/studio/backend/utils/hf_token_validation.py
+++ b/studio/backend/utils/hf_token_validation.py
@@ -13,6 +13,7 @@ from dataclasses import dataclass
from typing import Literal
from utils.account_context import current_account_id
+from utils.hub_settings import MODELSCOPE, active_source, hugging_face_endpoint
from huggingface_hub import HfApi
from huggingface_hub.utils import build_hf_headers, get_session
@@ -37,8 +38,8 @@ _INFLIGHT_WAIT_SECONDS = 30.0
_REMOTE_TIMEOUT_SECONDS = 10.0
_attempts: dict[tuple[str, str], deque[float]] = {}
-_cache: dict[tuple[str, str], tuple[float, TokenValidationResult]] = {}
-_inflight: dict[tuple[str, str], threading.Event] = {}
+_cache: dict[tuple[str, str, str], tuple[float, TokenValidationResult]] = {}
+_inflight: dict[tuple[str, str, str], threading.Event] = {}
_lock = threading.Lock()
@@ -62,7 +63,7 @@ def _prune_locked(now: float) -> None:
del _cache[key]
-def _cached_locked(fingerprint: tuple[str, str], now: float) -> TokenValidationResult | None:
+def _cached_locked(fingerprint: tuple[str, str, str], now: float) -> TokenValidationResult | None:
cached = _cache.get(fingerprint)
if cached is None:
return None
@@ -131,13 +132,12 @@ def _classify_response(response: object | None) -> TokenValidationResult:
return TokenValidationResult(status = "unavailable")
-def _check_remote(token: str) -> TokenValidationResult:
- api = HfApi()
+def _check_remote(token: str, *, endpoint: str) -> TokenValidationResult:
try:
# HfApi.whoami has no timeout parameter in the pinned Hub client.
# Use its session and headers against the same whoami endpoint.
response = get_session().get(
- f"{api.endpoint}/api/whoami-v2",
+ f"{endpoint}/api/whoami-v2",
headers = build_hf_headers(token = token),
timeout = _REMOTE_TIMEOUT_SECONDS,
)
@@ -153,7 +153,9 @@ def validate_hf_token(token: str, *, rate_key: str) -> TokenValidationResult:
if not normalized:
return TokenValidationResult(status = "invalid")
account_id = current_account_id()
- token_fingerprint = (account_id, _fingerprint(normalized))
+ # Per endpoint: a token one Hub rejects may be another's. The ModelScope adapter has no whoami.
+ endpoint = hugging_face_endpoint() if active_source() == MODELSCOPE else HfApi().endpoint
+ token_fingerprint = (account_id, _fingerprint(normalized), endpoint.rstrip("/"))
account_rate_key = (account_id, rate_key)
owner_event: threading.Event | None = None
@@ -175,7 +177,7 @@ def validate_hf_token(token: str, *, rate_key: str) -> TokenValidationResult:
if not waiting.wait(_INFLIGHT_WAIT_SECONDS):
return TokenValidationResult(status = "unavailable")
- result = _check_remote(normalized)
+ result = _check_remote(normalized, endpoint = token_fingerprint[2])
now = time.monotonic()
ttl = (
_CACHE_TTL_SECONDS
diff --git a/studio/backend/utils/hub_settings.py b/studio/backend/utils/hub_settings.py
new file mode 100644
index 00000000000..166309043cd
--- /dev/null
+++ b/studio/backend/utils/hub_settings.py
@@ -0,0 +1,259 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""The install's model source and Hugging Face endpoint, saved in settings.
+
+The saved values are applied as ``HF_ENDPOINT`` / ``HF_DATASETS_SERVER``, which
+everything in Unsloth -- huggingface_hub, datasets, the browser via /api/health,
+and every worker process spawned afterwards -- already follows. Until the owner
+saves, whatever the operator exported stays in effect. ModelScope as the source
+points ``HF_ENDPOINT`` at the loopback adapter in ``hub.modelscope``.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import sqlite3
+import sys
+import threading
+from contextlib import closing
+from dataclasses import dataclass
+from urllib.parse import urlsplit
+
+from loggers import get_logger
+from utils.hf_endpoint import (
+ DEFAULTS_BY_HEALTH_KEY,
+ get_hf_datasets_server,
+ get_hf_endpoint,
+ normalize_hf_endpoint_env,
+ validate_hub_endpoint,
+)
+
+logger = get_logger(__name__)
+
+HF_ENDPOINT_KEY = "hub_hf_endpoint"
+DATASETS_SERVER_FOLLOWS_KEY = "hub_datasets_server_follows_endpoint"
+SOURCE_KEY = "hub_source"
+HUGGINGFACE = "huggingface"
+MODELSCOPE = "modelscope"
+SOURCES = (HUGGINGFACE, MODELSCOPE)
+
+_ENV_VARS = ("HF_ENDPOINT", "HF_DATASETS_SERVER")
+SOURCE_ENV = "UNSLOTH_STUDIO_HUB_SOURCE"
+_operator_env: dict[str, str | None] | None = None
+_operator_endpoints: tuple[str, ...] | None = None
+_saved_only_endpoints: frozenset[str] = frozenset()
+_apply_lock = threading.Lock()
+
+
+@dataclass(frozen = True)
+class HubSettings:
+ hf_endpoint: str
+ datasets_server_follows_endpoint: bool
+ saved: bool
+ source: str = HUGGINGFACE
+
+
+def _capture_operator_env() -> dict[str, str | None]:
+ global _operator_env
+ if _operator_env is None:
+ _operator_env = {name: os.environ.get(name) for name in _ENV_VARS}
+ return _operator_env
+
+
+def _operator_endpoint() -> str:
+ raw = _capture_operator_env()["HF_ENDPOINT"] or ""
+ try:
+ return validate_hub_endpoint(raw)
+ except ValueError:
+ return ""
+
+
+def operator_hf_endpoint() -> str:
+ """The endpoint the environment configured: the one state recorded before saved settings refers to."""
+ return _operator_endpoint() or DEFAULTS_BY_HEALTH_KEY["hf_endpoint"]
+
+
+def _read_stored() -> dict:
+ keys = [HF_ENDPOINT_KEY, DATASETS_SERVER_FOLLOWS_KEY, SOURCE_KEY]
+ try:
+ from utils.account_context import OWNER, run_as
+
+ # get_app_settings creates and migrates studio.db; the startup read must leave it untouched.
+ if "storage.studio_db" not in sys.modules:
+ from utils.paths.storage_roots import studio_db_path
+
+ path = run_as(OWNER, studio_db_path)
+ with closing(sqlite3.connect(f"{path.resolve().as_uri()}?mode=ro", uri = True)) as conn:
+ rows = conn.execute(
+ "SELECT key, value_json FROM app_settings WHERE key IN (?, ?, ?)", keys
+ ).fetchall()
+ return {key: json.loads(value) for key, value in rows}
+ from storage.studio_db import get_app_settings
+
+ return run_as(OWNER, get_app_settings, keys)
+ except Exception as exc: # noqa: BLE001 - a missing or unreadable db keeps the environment's values
+ logger.debug("hub settings read failed (%s)", exc)
+ return {}
+
+
+def get_hub_settings() -> HubSettings:
+ stored = _read_stored()
+ source = stored.get(SOURCE_KEY) if stored.get(SOURCE_KEY) in SOURCES else HUGGINGFACE
+ endpoint = stored.get(HF_ENDPOINT_KEY)
+ if not isinstance(endpoint, str):
+ return HubSettings(_operator_endpoint(), False, saved = False, source = source)
+ try:
+ endpoint = validate_hub_endpoint(endpoint)
+ except ValueError:
+ endpoint = ""
+ return HubSettings(
+ endpoint, stored.get(DATASETS_SERVER_FOLLOWS_KEY) is True, saved = True, source = source
+ )
+
+
+def active_source() -> str:
+ return MODELSCOPE if os.environ.get(SOURCE_ENV) == MODELSCOPE else HUGGINGFACE
+
+
+def hugging_face_endpoint() -> str:
+ """The Hugging Face endpoint the settings select, also while ModelScope serves."""
+ return (get_hub_settings().hf_endpoint or DEFAULTS_BY_HEALTH_KEY["hf_endpoint"]).rstrip("/")
+
+
+def set_hub_source(source: str) -> HubSettings:
+ """Persist and apply the model source. Raises ValueError on an unknown one."""
+ if source not in SOURCES:
+ raise ValueError(f"Unknown model source {source!r}.")
+ from storage.studio_db import upsert_app_settings
+
+ upsert_app_settings({SOURCE_KEY: source}, read_back = False)
+ apply_hub_settings()
+ return get_hub_settings()
+
+
+def set_hub_settings(hf_endpoint: str, datasets_server_follows_endpoint: bool) -> HubSettings:
+ """Validate, persist and apply. Raises ValueError on an unusable endpoint."""
+ endpoint = validate_hub_endpoint(hf_endpoint)
+ from storage.studio_db import upsert_app_settings
+
+ upsert_app_settings(
+ {
+ HF_ENDPOINT_KEY: endpoint,
+ DATASETS_SERVER_FOLLOWS_KEY: bool(datasets_server_follows_endpoint),
+ },
+ read_back = False,
+ )
+ apply_hub_settings()
+ return get_hub_settings()
+
+
+def _effective_env(settings: HubSettings) -> tuple[dict[str, str | None], str]:
+ """The environment for ``settings``, and the source it actually selects."""
+ operator = _capture_operator_env()
+ if settings.source == MODELSCOPE:
+ try:
+ from hub.modelscope.router import internal_endpoint
+ env = {
+ "HF_ENDPOINT": internal_endpoint(),
+ "HF_DATASETS_SERVER": operator["HF_DATASETS_SERVER"],
+ }
+ return env, MODELSCOPE
+ except Exception: # noqa: BLE001 - a dead adapter must not take the backend down
+ logger.exception("ModelScope adapter failed to start; using Hugging Face")
+ if not settings.saved:
+ return dict(operator), HUGGINGFACE
+ datasets_server = operator["HF_DATASETS_SERVER"]
+ if settings.datasets_server_follows_endpoint and settings.hf_endpoint:
+ datasets_server = settings.hf_endpoint
+ env = {"HF_ENDPOINT": settings.hf_endpoint or None, "HF_DATASETS_SERVER": datasets_server}
+ return env, HUGGINGFACE
+
+
+def apply_hub_settings() -> None:
+ """Point this process, and every worker it spawns from now on, at the saved endpoints.
+
+ Safe before huggingface_hub is imported (startup) and after (a save): the
+ library reads the endpoint once at import, so its copies are refreshed too.
+ Workers already running keep the endpoint they started with.
+ """
+ global _operator_endpoints, _saved_only_endpoints
+ with _apply_lock:
+ if _operator_endpoints is None:
+ _operator_endpoints = (get_hf_endpoint(), get_hf_datasets_server())
+ env, source = _effective_env(get_hub_settings())
+ for name, value in env.items():
+ if value:
+ os.environ[name] = value
+ else:
+ os.environ.pop(name, None)
+ os.environ[SOURCE_ENV] = source
+ if source == MODELSCOPE:
+ _bypass_proxy_for(env["HF_ENDPOINT"])
+ normalize_hf_endpoint_env()
+ _saved_only_endpoints = frozenset(
+ endpoint
+ for endpoint in (get_hf_endpoint(), get_hf_datasets_server())
+ if endpoint not in _operator_endpoints
+ and endpoint not in DEFAULTS_BY_HEALTH_KEY.values()
+ )
+ _refresh_imported_hub_libraries()
+ utils_module = sys.modules.get("utils.utils")
+ if utils_module is not None:
+ utils_module.reset_hf_reachability_cache()
+
+
+def saved_only_endpoints() -> frozenset[str]:
+ """Endpoints only saved settings name, which the browser reaches through the backend relay."""
+ return _saved_only_endpoints
+
+
+def _bypass_proxy_for(url: str) -> None:
+ """Keep the loopback adapter off a configured proxy: httpx proxies 127.0.0.1 too unless NO_PROXY lists it."""
+ # Environment proxies only: on macOS / Windows an env no_proxy makes getproxies() skip the system proxy.
+ from urllib.request import getproxies_environment
+
+ host = urlsplit(url).hostname
+ proxies = getproxies_environment()
+ if not host or not any(proxies.get(k) for k in ("http", "all")):
+ return
+ names = [n for n in ("no_proxy", "NO_PROXY") if n in os.environ] or ["no_proxy", "NO_PROXY"]
+ changed = False
+ for name in names:
+ entries = [e.strip() for e in os.environ.get(name, "").split(",") if e.strip()]
+ if host not in entries:
+ os.environ[name] = ",".join([*entries, host])
+ changed = True
+ http = sys.modules.get("huggingface_hub.utils._http")
+ lock = getattr(http, "_CLIENT_LOCK", None)
+ if changed and lock is not None and hasattr(http, "_GLOBAL_CLIENT"):
+ # The 1.x shared client read proxies once: drop it, never close it (aborts live downloads).
+ with lock:
+ http._GLOBAL_CLIENT = None
+
+
+def _refresh_imported_hub_libraries() -> None:
+ constants = sys.modules.get("huggingface_hub.constants")
+ if constants is None or getattr(constants, "_staging_mode", False):
+ return
+ endpoint = os.environ.get("HF_ENDPOINT", constants._HF_DEFAULT_ENDPOINT).rstrip("/")
+ constants.ENDPOINT = endpoint
+ constants.HUGGINGFACE_CO_URL_TEMPLATE = endpoint + "/{repo_id}/resolve/{revision}/{filename}"
+ host = urlsplit(endpoint).hostname
+ hosts = getattr(constants, "HF_URL_HOSTS", None)
+ if host and isinstance(hosts, frozenset):
+ constants.HF_URL_HOSTS = hosts | {host.lower()}
+ hf_api = sys.modules.get("huggingface_hub.hf_api")
+ if hf_api is not None and getattr(hf_api, "api", None) is not None:
+ hf_api.api.endpoint = endpoint
+ hf_file_system = sys.modules.get("huggingface_hub.hf_file_system")
+ if hf_file_system is not None:
+ # fsspec hands back the cached instance, which kept the old endpoint.
+ hf_file_system.HfFileSystem.clear_instance_cache()
+ datasets_config = sys.modules.get("datasets.config")
+ if datasets_config is not None:
+ datasets_config.HF_ENDPOINT = endpoint
+ datasets_config.HUB_DATASETS_URL = (
+ endpoint + "/datasets/{repo_id}/resolve/{revision}/{path}"
+ )
diff --git a/studio/backend/utils/security/file_security.py b/studio/backend/utils/security/file_security.py
index ade8ceea913..a6035d04eda 100644
--- a/studio/backend/utils/security/file_security.py
+++ b/studio/backend/utils/security/file_security.py
@@ -479,6 +479,70 @@ def _evaluate_local_snapshot(
)
+def _modelscope_serves() -> bool:
+ try:
+ from utils.hub_settings import MODELSCOPE, active_source
+ except Exception:
+ return False
+ return active_source() == MODELSCOPE
+
+
+def _evaluate_unscanned_listing(
+ model_name: str,
+ hf_token: Optional[str],
+ load_subdirs = (),
+) -> FileSecurityDecision:
+ """ModelScope publishes no malware scan, so its "unavailable" is permanent, not transient:
+ fail closed on pickle weights, as the offline gate does for a cached snapshot."""
+ context = "ModelScope has no malware scan"
+ try:
+ from huggingface_hub import HfApi
+ files = HfApi().list_repo_files(model_name, token = hf_token or False)
+ except Exception:
+ logger.warning(
+ "Blocking load of '%s': %s and its files could not be listed.", model_name, context
+ )
+ return FileSecurityDecision(
+ model_name, True, reason = f"{context}; could not list the repository"
+ )
+ by_dir: dict[str, set] = {}
+ for path in files:
+ folder, _, name = _normalize_repo_path(str(path)).rpartition("/")
+ by_dir.setdefault(folder, set()).add(name)
+ blocked = []
+ roots = {"", *(_normalize_repo_path(str(d)).strip("/") for d in load_subdirs or ())}
+ for folder, names in sorted(by_dir.items()):
+ if folder not in roots:
+ continue
+ prefix = f"{folder}/" if folder else ""
+ has_base = bool(names & {"model.safetensors", "model.safetensors.index.json"})
+ has_adapter = "adapter_model.safetensors" in names
+ for name in sorted(names):
+ if _PICKLE_WEIGHT_RE.match(name):
+ if not (has_adapter if name.lower().startswith("adapter_model") else has_base):
+ blocked.append(prefix + name)
+ # Its shards are named freely and torch.loaded unless they end in .safetensors.
+ if "pytorch_model.bin.index.json" in names and not has_base:
+ blocked.append(prefix + "pytorch_model.bin.index.json")
+ if not blocked:
+ return FileSecurityDecision(
+ model_name, False, reason = f"{context}; no pickle weights to load"
+ )
+ listed = ", ".join(blocked)
+ logger.warning(
+ "Blocking load of '%s': %s and it ships pickle weights (%s).", model_name, context, listed
+ )
+ return FileSecurityDecision(
+ model_name,
+ True,
+ unsafe_files = [{"path": path, "level": "unscanned"} for path in blocked],
+ reason = (
+ f"{context}; unscanned pickle weights with no safetensors alternative: {listed}. "
+ "Switch the model source to Hugging Face in Settings to load it."
+ ),
+ )
+
+
def evaluate_file_security(
model_name: str,
hf_token: Optional[str] = None,
@@ -522,6 +586,8 @@ def evaluate_file_security(
context = "Hub scan unavailable",
load_subdirs = load_subdirs,
)
+ if _modelscope_serves():
+ return _evaluate_unscanned_listing(model_name, hf_token, load_subdirs)
return FileSecurityDecision(
model_name, False, reason = "scan unavailable; allowed (fail-open)"
)
diff --git a/studio/backend/utils/security/trusted_org.py b/studio/backend/utils/security/trusted_org.py
index 2d4b81de4f0..4e28eae74b4 100644
--- a/studio/backend/utils/security/trusted_org.py
+++ b/studio/backend/utils/security/trusted_org.py
@@ -24,8 +24,9 @@ logger = get_logger(__name__)
TRUSTED_ORGS: frozenset[str] = frozenset({"unsloth", "nvidia"})
# Keyed on (name, verify_remote, token) so an unauthenticated failure cannot poison a later
-# authenticated lookup; the token is hashed, never stored raw.
-_verdict_cache: dict[tuple[str, bool, str], bool] = {}
+# authenticated lookup; the token is hashed, never stored raw. And on the endpoint, which
+# decides who owns the name.
+_verdict_cache: dict[tuple[str, bool, str, str], bool] = {}
def _token_key(hf_token: Optional[str]) -> str:
@@ -53,12 +54,19 @@ def is_trusted_org_repo(
"""
if not name or not isinstance(name, str):
return False
+ from utils.hub_settings import MODELSCOPE, active_source
- cache_key = (name, verify_remote, _token_key(hf_token))
+ if active_source() == MODELSCOPE:
+ return False
+
+ from huggingface_hub import constants
+
+ endpoint = constants.ENDPOINT.rstrip("/")
+ cache_key = (name, verify_remote, _token_key(hf_token), endpoint)
if cache_key in _verdict_cache:
return _verdict_cache[cache_key]
- verdict = _evaluate(name, hf_token, verify_remote)
+ verdict = _evaluate(name, hf_token, verify_remote, endpoint)
_verdict_cache[cache_key] = verdict
return verdict
@@ -71,7 +79,7 @@ def _namespace(name: str) -> Optional[str]:
return parts[0].lower()
-def _evaluate(name: str, hf_token: Optional[str], verify_remote: bool) -> bool:
+def _evaluate(name: str, hf_token: Optional[str], verify_remote: bool, endpoint: str) -> bool:
# Local paths are never a trusted remote repo (the spoof this guards against).
try:
if is_local_path(name):
@@ -91,7 +99,7 @@ def _evaluate(name: str, hf_token: Optional[str], verify_remote: bool) -> bool:
try:
from huggingface_hub import HfApi
- info = HfApi().model_info(name, token = hf_token)
+ info = HfApi(endpoint = endpoint).model_info(name, token = hf_token)
resolved_id = getattr(info, "id", None) or name
resolved_ns = _namespace(resolved_id)
author = getattr(info, "author", None)
diff --git a/studio/backend/utils/utils.py b/studio/backend/utils/utils.py
index 12394295620..3eb872b7f47 100644
--- a/studio/backend/utils/utils.py
+++ b/studio/backend/utils/utils.py
@@ -15,6 +15,7 @@ from typing import Optional, Union
import shutil
import tempfile
from utils.paths.path_utils import is_appledouble_metadata
+from hub.utils.hf_errors import modelscope_missing
from . import auth_safe
AuthSafeRedirectHandler = auth_safe.AuthSafeRedirectHandler
@@ -909,6 +910,10 @@ def format_error_message(error: Exception, model_name: str) -> str:
error: The exception that occurred
model_name: Name of the model being loaded
"""
+ missing = modelscope_missing(error)
+ if missing:
+ return missing
+
error_str = str(error).lower()
model_short = model_name.split("/")[-1] if "/" in model_name else model_name
diff --git a/studio/frontend/src/components/resource-picker/picker-shell.tsx b/studio/frontend/src/components/resource-picker/picker-shell.tsx
index 952dd235571..1f96678c792 100644
--- a/studio/frontend/src/components/resource-picker/picker-shell.tsx
+++ b/studio/frontend/src/components/resource-picker/picker-shell.tsx
@@ -9,6 +9,7 @@ import {
} from "@/components/ui/popover";
import { Spinner } from "@/components/ui/spinner";
import { useT } from "@/i18n";
+import { useHubSource } from "@/lib/hf-endpoint";
import { cn } from "@/lib/utils";
import {
FolderSearchIcon,
@@ -191,6 +192,7 @@ export function PickerShell({
useThisLabel: string;
}) {
const t = useT();
+ const hubSource = useHubSource();
const idBase = useId();
const panelId = `${idBase}-panel`;
const activeTabId = pickerTabId(idBase, tab);
@@ -199,7 +201,10 @@ export function PickerShell({
const [queryStatus, setQueryStatus] = useState("");
const tabs = [
{ value: PICKER_TAB.device, label: t("picker.onDevice") },
- { value: PICKER_TAB.hub, label: t("picker.huggingFace") },
+ {
+ value: PICKER_TAB.hub,
+ label: t(hubSource === "modelscope" ? "picker.modelScope" : "picker.huggingFace"),
+ },
] as const;
const canCommitQuery = tab !== PICKER_TAB.hub || online;
const canUseThis = showUseThis && canCommitQuery;
diff --git a/studio/frontend/src/config/env.ts b/studio/frontend/src/config/env.ts
index b506144661e..ed05d22e599 100644
--- a/studio/frontend/src/config/env.ts
+++ b/studio/frontend/src/config/env.ts
@@ -10,6 +10,13 @@ import {
} from "@/config/hardware-verdict";
import { create } from "zustand";
+// Backend routes, so same origin: the page CSP already allows them whatever endpoint is saved later.
+const MODELSCOPE_HUB_PATH = "/api/hub/modelscope";
+
+function backendUrl(path: string): string {
+ return new URL(apiUrl(path), window.location.href).href;
+}
+
export const env = {
MODE: import.meta.env.MODE,
DEV: import.meta.env.DEV,
@@ -173,6 +180,9 @@ export async function fetchDeviceType(options?: {
secure?: boolean;
hf_endpoint?: string;
hf_datasets_server?: string;
+ hub_source?: string;
+ hub_proxy?: string | null;
+ datasets_server_proxy?: string | null;
};
// Once the store holds an authoritative (server-reported) platform, a non-forced response
// must not overwrite it. It may be an unauthenticated fallback, or an earlier authenticated
@@ -182,7 +192,19 @@ export async function fetchDeviceType(options?: {
// Before the authoritative-platform guard below: unauthenticated and
// idempotent, and a mirror whose first authoritative reply already landed
// would otherwise never route its Hub calls.
- setHfEndpoints(data.hf_endpoint, data.hf_datasets_server);
+ const hubProxy = typeof data.hub_proxy === "string" ? data.hub_proxy : null;
+ const datasetsProxy =
+ typeof data.datasets_server_proxy === "string" ? data.datasets_server_proxy : null;
+ setHfEndpoints(
+ data.hub_source === "modelscope"
+ ? backendUrl(MODELSCOPE_HUB_PATH)
+ : hubProxy
+ ? backendUrl(hubProxy)
+ : data.hf_endpoint,
+ datasetsProxy ? backendUrl(datasetsProxy) : data.hf_datasets_server,
+ data.hub_source,
+ { endpoint: hubProxy !== null, datasetsServer: datasetsProxy !== null },
+ );
if (shouldKeepAuthoritativePlatform(options?.force)) {
return usePlatformStore.getState().deviceType;
}
diff --git a/studio/frontend/src/features/hub/catalog/catalog-states.tsx b/studio/frontend/src/features/hub/catalog/catalog-states.tsx
index 5dbda488b82..a4ff1af1c6d 100644
--- a/studio/frontend/src/features/hub/catalog/catalog-states.tsx
+++ b/studio/frontend/src/features/hub/catalog/catalog-states.tsx
@@ -13,7 +13,13 @@ import { HugeiconsIcon } from "@hugeicons/react";
import type { ReactNode } from "react";
import { useUiSpaceScale } from "@/hooks/use-ui-space-scale";
import { useLayoutEffect, useRef, useState } from "react";
-import type { HubFailure } from "@/features/hub/lib/network";
+import { Button } from "@/components/ui/button";
+import { useHubAvailability } from "../hooks/use-online-status";
+import { clearRemoteBackoff, type HubFailure } from "../lib/network";
+import { useIsAccountOwner } from "@/features/auth";
+import { updateHubSource } from "@/features/settings";
+import { useT } from "@/i18n";
+import { useHubName, useHubSource } from "@/lib/hf-endpoint";
// Only a browser reporting itself offline earns "You're offline". Calling a DNS
// filter or extension block "offline" is what made these bugs undiagnosable.
@@ -21,24 +27,25 @@ function describeFailure(
failure: HubFailure | null | undefined,
online: boolean,
resourceLabel: "models" | "datasets",
+ hub: string,
): { title: string; body: string; offlineLike: boolean } {
switch (failure?.kind) {
case "browser-offline":
return {
title: "You're offline",
- body: `Reconnect to the internet to browse ${resourceLabel} from Hugging Face.`,
+ body: `Reconnect to the internet to browse ${resourceLabel} from ${hub}.`,
offlineLike: true,
};
case "timeout":
return {
- title: "Hugging Face timed out",
+ title: `${hub} timed out`,
body: failure.message,
offlineLike: false,
};
case "network-opaque":
case "unknown":
return {
- title: "Can't reach Hugging Face",
+ title: `Can't reach ${hub}`,
body: failure.message,
offlineLike: false,
};
@@ -47,17 +54,50 @@ function describeFailure(
}
return online
? {
- title: "Couldn't reach Hugging Face",
+ title: `Couldn't reach ${hub}`,
body: "The discovery feed couldn't load. Check your connection or try again.",
offlineLike: false,
}
: {
- title: "Can't reach Hugging Face",
- body: `Unsloth couldn't load ${resourceLabel} from Hugging Face.`,
+ title: `Can't reach ${hub}`,
+ body: `Unsloth couldn't load ${resourceLabel} from ${hub}.`,
offlineLike: false,
};
}
+function UseModelScopeButton() {
+ const t = useT();
+ const isOwner = useIsAccountOwner();
+ const source = useHubSource();
+ const [switching, setSwitching] = useState(false);
+ const [failed, setFailed] = useState(false);
+ if (!isOwner || source !== "huggingface") return null;
+ return (
+ <span className="inline-flex items-center gap-2">
+ <Button
+ size="sm"
+ disabled={switching}
+ title={t("picker.useModelScopeHint")}
+ onClick={() => {
+ setSwitching(true);
+ setFailed(false);
+ updateHubSource("modelscope")
+ .catch(() => setFailed(true))
+ .finally(() => setSwitching(false));
+ }}
+ className="h-8 rounded-full"
+ >
+ {t("picker.useModelScope")}
+ </Button>
+ {failed ? (
+ <span className="text-ui-11 text-destructive">
+ {t("picker.useModelScopeFailed")}
+ </span>
+ ) : null}
+ </span>
+ );
+}
+
export function NetworkErrorState({
online,
message,
@@ -77,6 +117,7 @@ export function NetworkErrorState({
failure,
online,
resourceLabel,
+ useHubName(),
);
const icon = offlineLike ? WifiDisconnected02Icon : CloudOffIcon;
@@ -95,6 +136,8 @@ export function NetworkErrorState({
<p className="text-ui-11 text-muted-foreground/70">{message}</p>
</div>
<div className="flex flex-wrap items-center justify-center gap-2">
+ {/* A reachable hub answering an HTTP error is no reason to switch hubs. */}
+ {failure && !offlineLike ? <UseModelScopeButton /> : null}
{onSwitchDevice ? (
<button
type="button"
@@ -121,6 +164,53 @@ export function NetworkErrorState({
);
}
+export function HubFailureHint({
+ message,
+ onRetry,
+}: {
+ message: string | null;
+ onRetry: () => void;
+}) {
+ const { phase, failure } = useHubAvailability();
+ const { title, body, offlineLike } = describeFailure(
+ failure,
+ phase === "available",
+ "models",
+ useHubName(),
+ );
+ return (
+ <div className="flex flex-col gap-2 px-2.5 py-2">
+ <div className="space-y-0.5">
+ <p className="text-xs font-medium text-foreground">{title}</p>
+ <p className="text-xs leading-relaxed text-muted-foreground">{body}</p>
+ {/* A classified failure already names the cause; an HTTP error only has its message. */}
+ {failure || !message ? null : (
+ <p className="text-xs text-muted-foreground/70">{message}</p>
+ )}
+ </div>
+ <div className="flex flex-wrap items-center gap-2">
+ {failure && !offlineLike ? <UseModelScopeButton /> : null}
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={() => {
+ clearRemoteBackoff();
+ onRetry();
+ }}
+ className="h-8 rounded-full"
+ >
+ <HugeiconsIcon
+ icon={Refresh01Icon}
+ strokeWidth={1.75}
+ className="size-3.5"
+ />
+ Try again
+ </Button>
+ </div>
+ </div>
+ );
+}
+
export function DiscoverFetchMoreState({
scannedCount,
hasActiveFilters,
@@ -134,6 +224,7 @@ export function DiscoverFetchMoreState({
onFetchMore: () => void;
onClearFilters: () => void;
}) {
+ const hubName = useHubName();
return (
<div className="flex min-h-[calc(260px*var(--ui-space-scale,1))] flex-col items-center justify-center gap-3 px-6 text-center">
<div className="inline-flex size-11 items-center justify-center rounded-[12px] bg-muted text-muted-foreground">
@@ -145,7 +236,7 @@ export function DiscoverFetchMoreState({
</p>
<p className="max-w-md text-ui-12p5 leading-5 text-muted-foreground">
Scanned {scannedCount.toLocaleString()} results. Load another page to
- keep searching Hugging Face.
+ keep searching {hubName}.
</p>
</div>
<div className="flex flex-wrap items-center justify-center gap-2 pt-1">
diff --git a/studio/frontend/src/features/hub/catalog/model-inspector.tsx b/studio/frontend/src/features/hub/catalog/model-inspector.tsx
index d9965861456..5dddb7e3ee2 100644
--- a/studio/frontend/src/features/hub/catalog/model-inspector.tsx
+++ b/studio/frontend/src/features/hub/catalog/model-inspector.tsx
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { useHfEndpoint } from "@/lib/hf-endpoint";
+import { useHfEndpoint, useHubName } from "@/lib/hf-endpoint";
import {
Tooltip,
TooltipContent,
@@ -76,6 +76,7 @@ function ViewRepositoryButton({
}) {
const online = useOnlineStatus();
const hfEndpoint = useHfEndpoint();
+ const hubName = useHubName();
const url = `${hfEndpoint}/${isDataset ? "datasets/" : ""}${repoId}`;
const baseClass =
"inline-flex size-6 shrink-0 items-center justify-center rounded-full text-muted-foreground transition-colors";
@@ -116,7 +117,7 @@ function ViewRepositoryButton({
)}
</TooltipTrigger>
<TooltipContent side="bottom" className="tooltip-compact">
- {online ? "Open on Hugging Face" : "Unavailable offline"}
+ {online ? `Open on ${hubName}` : "Unavailable offline"}
</TooltipContent>
</Tooltip>
);
diff --git a/studio/frontend/src/features/hub/catalog/models-table.tsx b/studio/frontend/src/features/hub/catalog/models-table.tsx
index 25989f69545..d2135f27f15 100644
--- a/studio/frontend/src/features/hub/catalog/models-table.tsx
+++ b/studio/frontend/src/features/hub/catalog/models-table.tsx
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { useHfEndpoint } from "@/lib/hf-endpoint";
+import { useHfEndpoint, useHubName } from "@/lib/hf-endpoint";
import {
DropdownMenu,
DropdownMenuContent,
@@ -491,6 +491,7 @@ function RowActions({
onSelect: (id: string) => void;
}) {
const hfEndpoint = useHfEndpoint();
+ const hubName = useHubName();
const hfUrl = `${hfEndpoint}/${isDataset ? "datasets/" : ""}${row.result.id}`;
const actionClass =
"pointer-events-auto inline-flex size-7 shrink-0 cursor-pointer items-center justify-center rounded-full text-muted-foreground/70 transition-colors hover:bg-[color-mix(in_oklab,var(--foreground)_calc(7%*var(--contrast-wash-gain,1)),transparent)] hover:text-foreground focus-visible:text-foreground data-[state=open]:bg-[color-mix(in_oklab,var(--foreground)_calc(7%*var(--contrast-wash-gain,1)),transparent)] data-[state=open]:text-foreground";
@@ -560,7 +561,7 @@ function RowActions({
strokeWidth={1.75}
className="size-4"
/>
- Open on Hugging Face
+ Open on {hubName}
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
diff --git a/studio/frontend/src/features/hub/catalog/models-toolbar.tsx b/studio/frontend/src/features/hub/catalog/models-toolbar.tsx
index 51bbc09a9f2..36d90393e08 100644
--- a/studio/frontend/src/features/hub/catalog/models-toolbar.tsx
+++ b/studio/frontend/src/features/hub/catalog/models-toolbar.tsx
@@ -10,6 +10,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import type { HfSortKey } from "@/features/hub/hooks/use-hub-model-search";
+import { useHubSource } from "@/lib/hf-endpoint";
import { cn } from "@/lib/utils";
import {
AiChipIcon,
@@ -187,13 +188,16 @@ export const ModelsToolbar = memo(function ModelsToolbar({
})),
[],
);
+ const hubSource = useHubSource();
const sortOptions = useMemo<HubOption<HfSortKey>[]>(
() =>
- SORT_OPTIONS.map((option) => ({
+ SORT_OPTIONS.filter(
+ (option) => hubSource !== "modelscope" || option.value !== "createdAt",
+ ).map((option) => ({
value: option.value,
label: option.label,
})),
- [],
+ [hubSource],
);
const triggerBase = cn(
"field-trigger hub-menu-trigger field-soft transition-colors",
diff --git a/studio/frontend/src/features/hub/hooks/use-dataset-size.ts b/studio/frontend/src/features/hub/hooks/use-dataset-size.ts
index 4d3fb78aaac..7f9d790cbf2 100644
--- a/studio/frontend/src/features/hub/hooks/use-dataset-size.ts
+++ b/studio/frontend/src/features/hub/hooks/use-dataset-size.ts
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { useHfDatasetsServer } from "@/lib/hf-endpoint";
+import { useHfDatasetsServer, useHubSource } from "@/lib/hf-endpoint";
import { useOnlineStatus } from "@/features/hub/hooks/use-online-status";
import { fingerprintToken } from "@/features/hub/lib/token-fingerprint";
import { useEffect, useState } from "react";
@@ -19,8 +19,9 @@ export function useDatasetSize(
const token = options.token || undefined;
const repoKey = repoId ?? "";
const hfDatasetsServer = useHfDatasetsServer();
+ const hubSource = useHubSource();
const key = repoKey
- ? `${hfDatasetsServer}::${repoKey}::${fingerprintToken(token)}`
+ ? `${hubSource}::${hfDatasetsServer}::${repoKey}::${fingerprintToken(token)}`
: "";
const [state, setState] = useState<{
key: string;
diff --git a/studio/frontend/src/features/hub/hooks/use-discover-search.ts b/studio/frontend/src/features/hub/hooks/use-discover-search.ts
index 7ebbab4860b..8c5762cb796 100644
--- a/studio/frontend/src/features/hub/hooks/use-discover-search.ts
+++ b/studio/frontend/src/features/hub/hooks/use-discover-search.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { useCallback, useEffect, useRef } from "react";
+import { useHubName } from "@/lib/hf-endpoint";
import { toast } from "@/lib/toast";
import { clearRemoteBackoff, type HubFailure } from "@/features/hub/lib/network";
import { useHubAvailability } from "./use-online-status";
@@ -75,16 +76,16 @@ function classifyDiscoverError(
return "unknown";
}
-function discoverErrorTitle(kind: DiscoverErrorKind): string {
+function discoverErrorTitle(kind: DiscoverErrorKind, hub: string): string {
switch (kind) {
case "offline":
- return "Can't reach Hugging Face";
+ return `Can't reach ${hub}`;
case "auth":
- return "Hugging Face auth failed";
+ return `${hub} auth failed`;
case "rate-limited":
- return "Hugging Face rate limit";
+ return `${hub} rate limit`;
default:
- return "Couldn't reach Hugging Face";
+ return `Couldn't reach ${hub}`;
}
}
@@ -108,6 +109,7 @@ export function useDiscoverSearch({
ownerScope: "unsloth" | "all";
}): DiscoverSearch {
const { phase, failure } = useHubAvailability();
+ const hub = useHubName();
// "probing" counts: a lapsed backoff is exactly when the next request should
// be allowed to test the network. Only a live backoff ("unavailable") holds it.
const canProbe = phase !== "unavailable";
@@ -194,13 +196,13 @@ export function useDiscoverSearch({
const errorKind = classifyDiscoverError(searchError, online);
if (lastErrorRef.current === errorKind) return;
lastErrorRef.current = errorKind;
- toast.error(discoverErrorTitle(errorKind), {
+ toast.error(discoverErrorTitle(errorKind, hub), {
// The classified failure names the cause; the raw message covers HTTP
// errors that never reach the network layer.
description: searchFailure?.message ?? searchError,
action: { label: "Retry", onClick: handleRetrySearch },
});
- }, [isDiscoverTab, searchError, searchFailure, online, handleRetrySearch]);
+ }, [isDiscoverTab, searchError, searchFailure, online, handleRetrySearch, hub]);
// Driven by a successful request, never a lapsed timer. Announcing recovery
// on TTL expiry produced a permanent offline/back-online loop.
diff --git a/studio/frontend/src/features/hub/hooks/use-selected-model-view.ts b/studio/frontend/src/features/hub/hooks/use-selected-model-view.ts
index 68b88a29edc..32f59753af4 100644
--- a/studio/frontend/src/features/hub/hooks/use-selected-model-view.ts
+++ b/studio/frontend/src/features/hub/hooks/use-selected-model-view.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { useMemo } from "react";
+import { useHubName } from "@/lib/hf-endpoint";
import { detectCapabilities, detectLicense } from "../lib/model-capabilities";
import {
buildSummary,
@@ -47,6 +48,7 @@ export function useSelectedModelView({
selectedHfResult: HfResult;
isDatasetMode: boolean;
}): SelectedModelView | null {
+ const hubName = useHubName();
return useMemo<SelectedModelView | null>(() => {
if (selectedDiscoverRow) {
if (
@@ -137,7 +139,7 @@ export function useSelectedModelView({
? "On device"
: isResolvedPartial
? "Partial on device"
- : "Hugging Face",
+ : hubName,
path: onDevicePath,
isLocal: false,
isGguf:
@@ -392,6 +394,7 @@ export function useSelectedModelView({
return null;
}, [
+ hubName,
isDatasetMode,
selectedCachedRow,
selectedDiscoverRow,
diff --git a/studio/frontend/src/features/hub/hub-page.tsx b/studio/frontend/src/features/hub/hub-page.tsx
index 4ef39449f59..f909f4e8091 100644
--- a/studio/frontend/src/features/hub/hub-page.tsx
+++ b/studio/frontend/src/features/hub/hub-page.tsx
@@ -2,7 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { useAppShellReadySignal } from "@/components/app-readiness";
-import { useHfEndpoint } from "@/lib/hf-endpoint";
+import { useHfEndpoint, useHubSource } from "@/lib/hf-endpoint";
import { usePlatformStore } from "@/config/env";
import {
applyActiveModelStatusToStore,
@@ -822,8 +822,14 @@ export function ModelsPage() {
return null;
}, [isChannelListMode, isFeedMode, activeChannel]);
- const effectiveSort: HfSortKey =
+ const hubSource = useHubSource();
+ const requestedSort: HfSortKey =
isFeedMode && liveListChannel ? liveListChannel.sort : sortBy;
+ // ModelScope has no creation-date sort; its closest is last modified.
+ const effectiveSort: HfSortKey =
+ hubSource === "modelscope" && requestedSort === "createdAt"
+ ? "lastModified"
+ : requestedSort;
const effectiveDirection: HfSortDirection = isFeedMode ? "desc" : direction;
// The format dropdown always filters the visible list, including the feed's "Latest" list, so
// the default (GGUF) hides fp8/safetensors and picking a format actually changes the rows.
diff --git a/studio/frontend/src/features/hub/index.ts b/studio/frontend/src/features/hub/index.ts
index 4a6830dfcff..1a96fd7d97a 100644
--- a/studio/frontend/src/features/hub/index.ts
+++ b/studio/frontend/src/features/hub/index.ts
@@ -29,7 +29,8 @@ export {
export { useHubInfiniteScroll } from "./hooks/use-hub-infinite-scroll";
export type { CapabilityKey } from "./lib/model-capabilities";
export { useLatestRef } from "./hooks/use-latest-ref";
-export { useOnlineStatus } from "./hooks/use-online-status";
+export { useHubAvailability, useOnlineStatus } from "./hooks/use-online-status";
+export { HubFailureHint } from "./catalog/catalog-states";
export {
INVENTORY_HINT_KIND,
INVENTORY_HINT_KINDS,
diff --git a/studio/frontend/src/features/hub/lib/dataset-size.ts b/studio/frontend/src/features/hub/lib/dataset-size.ts
index 87621559698..8e2c6b550da 100644
--- a/studio/frontend/src/features/hub/lib/dataset-size.ts
+++ b/studio/frontend/src/features/hub/lib/dataset-size.ts
@@ -1,7 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
-import { getHfDatasetsServerBase, getHfEndpoint } from "@/lib/hf-endpoint";
+import {
+ getHfDatasetsServerBase,
+ getHfEndpoint,
+ hasDatasetsServer,
+} from "@/lib/hf-endpoint";
import { LruMap } from "./lru-map";
import { fetchWithTimeout } from "./network";
import { fingerprintToken } from "./token-fingerprint";
@@ -209,6 +213,7 @@ export function fetchDatasetSize(
tokenOrSignal?: string | AbortSignal,
signal?: AbortSignal,
): Promise<DatasetSizeInfo | null> {
+ if (!hasDatasetsServer()) return Promise.resolve(null);
const resolvedToken =
typeof tokenOrSignal === "string" ? tokenOrSignal : undefined;
const resolvedSignal =
diff --git a/studio/frontend/src/features/hub/lib/hf-cache.ts b/studio/frontend/src/features/hub/lib/hf-cache.ts
index 58b091d780b..0eeb125bede 100644
--- a/studio/frontend/src/features/hub/lib/hf-cache.ts
+++ b/studio/frontend/src/features/hub/lib/hf-cache.ts
@@ -4,6 +4,7 @@
import { type ModelEntry, modelInfo } from "@huggingface/hub";
import { getHfEndpoint } from "@/lib/hf-endpoint";
+import { hubFetch } from "@/lib/hub-fetch";
import { LruMap } from "./lru-map";
import { fingerprintToken } from "./token-fingerprint";
@@ -120,6 +121,7 @@ export async function cachedModelInfo(
]),
);
const result = await modelInfo({
+ fetch: hubFetch,
...params,
additionalFields,
});
diff --git a/studio/frontend/src/features/hub/lib/network.ts b/studio/frontend/src/features/hub/lib/network.ts
index c299fdebe15..42fd507529e 100644
--- a/studio/frontend/src/features/hub/lib/network.ts
+++ b/studio/frontend/src/features/hub/lib/network.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { getHfEndpoint } from "@/lib/hf-endpoint";
+import { fetchHub } from "@/lib/hub-fetch";
const NETWORK_STATUS_EVENT = "unsloth-network-status";
const REMOTE_OFFLINE_TTL_MS = 30_000;
@@ -358,7 +359,10 @@ export async function fetchWithTimeout(
const origin = originFromFetchInput(input);
try {
- const response = await fetch(input, { ...init, signal: controller.signal });
+ const response = await fetchHub(input, {
+ ...init,
+ signal: controller.signal,
+ });
if (origin) {
markRemoteNetworkOnline(origin);
}
diff --git a/studio/frontend/src/features/images/train/example-dataset-cards.tsx b/studio/frontend/src/features/images/train/example-dataset-cards.tsx
index 554e1ab5c79..344c8ba94d2 100644
--- a/studio/frontend/src/features/images/train/example-dataset-cards.tsx
+++ b/studio/frontend/src/features/images/train/example-dataset-cards.tsx
@@ -9,7 +9,13 @@ import {
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";
-import { getHfDatasetsServerBase, useHfDatasetsServer } from "@/lib/hf-endpoint";
+import {
+ getHfDatasetsServerBase,
+ hasDatasetsServer,
+ useHfDatasetsServer,
+ useHubSource,
+} from "@/lib/hf-endpoint";
+import { hubFetch } from "@/lib/hub-fetch";
import { toast } from "@/lib/toast";
import { cn } from "@/lib/utils";
@@ -25,6 +31,7 @@ import {
const _previewCache = new Map<string, Promise<string[]>>();
async function fetchPreviews(repo: string): Promise<string[]> {
+ if (!hasDatasetsServer()) return [];
// Keyed by server too: an empty result cached against the default would
// otherwise never be retried against a mirror that arrives later.
const base = getHfDatasetsServerBase();
@@ -33,7 +40,7 @@ async function fetchPreviews(repo: string): Promise<string[]> {
if (cached) return cached;
const p = (async () => {
try {
- const res = await fetch(
+ const res = await hubFetch(
`${base}/first-rows?dataset=${encodeURIComponent(
repo,
)}&config=default&split=train`,
@@ -68,6 +75,7 @@ export function shortExampleLabel(label: string): string {
function ExamplePreviews({ repo }: { repo: string }) {
const [urls, setUrls] = useState<string[] | null>(null);
const hfDatasetsServer = useHfDatasetsServer();
+ const hubSource = useHubSource();
useEffect(() => {
let cancelled = false;
void fetchPreviews(repo).then((u) => {
@@ -76,7 +84,7 @@ function ExamplePreviews({ repo }: { repo: string }) {
return () => {
cancelled = true;
};
- }, [repo, hfDatasetsServer]);
+ }, [repo, hfDatasetsServer, hubSource]);
if (!urls || urls.length === 0) return null;
return (
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
index 08a85039075..b660e1bf822 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
@@ -71,6 +71,8 @@ import {
scanFolderStatusCopy,
useDownloadManagerStore,
useHfTokenStore,
+ HubFailureHint,
+ useHubAvailability,
useOnlineStatus,
pendingDrafterPresentation,
} from "@/features/hub";
@@ -217,6 +219,7 @@ import {
matchesFormatFilter,
orderRecommendedRows,
paramsFromId,
+ recommendedEmptyState,
searchRowFitsDevice,
searchableRecommendedIds,
type CuratedBudget,
@@ -2956,6 +2959,7 @@ export function HubModelPicker({
const debouncedQuery = useDebouncedValue(query);
// Shared Hub search stack so the picker and Hub run one implementation. Scoped to unsloth like the old listing.
const online = useOnlineStatus();
+ const { phase: hubPhase } = useHubAvailability();
// Sanitize to anonymous on a malformed token, matching the Hub page.
const accessToken = hfApiToken(hfToken);
// Recommended section: a live unsloth listing sorted by the dropdown, the same sort that drives search results.
@@ -2968,6 +2972,8 @@ export function HubModelPicker({
fetchMore,
scannedCount,
hasMore,
+ error: searchError,
+ retry: retrySearch,
} = useHubModelSearch(debouncedQuery, {
ownerScope: "unsloth",
sortBy: recommendedSort,
@@ -5592,6 +5598,16 @@ export function HubModelPicker({
const showDownloaded = section === "downloaded";
const showCustom = section === "downloaded";
const showRecommendedSection = !showHfSection && section === "recommended";
+ const recommendedEmpty = recommendedEmptyState({
+ isLoading: recommendedSearch.isLoading,
+ error: recommendedSearch.error,
+ hubPhase,
+ });
+ const searchEmpty = recommendedEmptyState({
+ isLoading,
+ error: searchError,
+ hubPhase,
+ });
const downloadedEmpty =
pinnedRows.length === 0 &&
visibleCachedGguf.length === 0 &&
@@ -7652,14 +7668,20 @@ export function HubModelPicker({
{showNpuBrowse ? (
<ListLabel divider={!npuBrowseFolded}>Unsloth</ListLabel>
) : null}
- {recommendedSearch.isLoading &&
- recommendedRows.length === 0 ? (
+ {recommendedRows.length === 0 &&
+ recommendedEmpty === "loading" ? (
<div className="flex items-center gap-2 px-5 py-3">
<Spinner className="size-3 text-muted-foreground" />
<span className="text-xs text-muted-foreground">
Loading models…
</span>
</div>
+ ) : recommendedRows.length === 0 &&
+ recommendedEmpty === "failed" ? (
+ <HubFailureHint
+ message={recommendedSearch.error}
+ onRetry={recommendedSearch.retry}
+ />
) : recommendedRows.length === 0 ? (
<div className="px-2.5 py-2 text-xs text-muted-foreground">
No models found.
@@ -7899,13 +7921,15 @@ export function HubModelPicker({
formatFilter !== "npu" ? (
<>
{searchRowIds.length === 0 && !isLoading ? (
- filteredRecommendedIds.length === 0 ? (
+ filteredRecommendedIds.length > 0 ? null : searchEmpty === "failed" ? (
+ <HubFailureHint message={searchError} onRetry={retrySearch} />
+ ) : (
<div className="px-2.5 py-2 text-xs text-muted-foreground">
{communityDiscoveryEnabled
? "No matching models."
: "No matching Unsloth models."}
</div>
- ) : null
+ )
) : (
searchRowIds.map((id) => {
const vram = vramMap.get(id);
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts b/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
index e771d89c951..4e1f8ed6fc6 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
+++ b/studio/frontend/src/features/model-picker/components/model-selector/recommended-fit.ts
@@ -450,3 +450,16 @@ export function curatedBudgetText(est: number, gpuGb: number, budget: CuratedBud
: `a ${gpuGb}GB GPU`;
return `Needs ~${needGb}GB for weights (budget: ~${budgetGb}GB, 70% of ${of})`;
}
+
+export function recommendedEmptyState({
+ isLoading,
+ error,
+ hubPhase,
+}: {
+ isLoading: boolean;
+ error: string | null;
+ hubPhase: "available" | "probing" | "unavailable";
+}): "loading" | "failed" | "empty" {
+ if (isLoading) return "loading";
+ return error || hubPhase !== "available" ? "failed" : "empty";
+}
diff --git a/studio/frontend/src/features/settings/api/hub-settings.ts b/studio/frontend/src/features/settings/api/hub-settings.ts
new file mode 100644
index 00000000000..d7985877022
--- /dev/null
+++ b/studio/frontend/src/features/settings/api/hub-settings.ts
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { fetchDeviceType } from "@/config/env";
+import { authFetch } from "@/features/auth";
+import { markRemoteNetworkOnline } from "@/features/hub/lib/network";
+import { readFastApiError } from "@/lib/format-fastapi-error";
+import type { HubSource } from "@/lib/hf-endpoint";
+
+export type HubSettings = {
+ hfEndpoint: string;
+ datasetsServerFollowsEndpoint: boolean;
+ source: HubSource;
+ activeSource: HubSource;
+};
+
+type ApiHubSettings = {
+ // biome-ignore lint/style/useNamingConvention: API schema
+ hf_endpoint: string;
+ // biome-ignore lint/style/useNamingConvention: API schema
+ datasets_server_follows_endpoint: boolean;
+ source: HubSource;
+ // biome-ignore lint/style/useNamingConvention: API schema
+ active_source: HubSource;
+};
+
+function fromApi(settings: ApiHubSettings): HubSettings {
+ return {
+ hfEndpoint: settings.hf_endpoint,
+ datasetsServerFollowsEndpoint: settings.datasets_server_follows_endpoint,
+ source: settings.source,
+ activeSource: settings.active_source,
+ };
+}
+
+export async function loadHubSettings(): Promise<HubSettings> {
+ const res = await authFetch("/api/settings/hub");
+ if (!res.ok) {
+ throw new Error(await readFastApiError(res, "Failed to load hub settings"));
+ }
+ return fromApi(await res.json());
+}
+
+async function followSavedEndpoints(): Promise<void> {
+ await fetchDeviceType({ force: true }).catch(() => undefined);
+ // Backoffs were recorded against the old endpoints; the relay and ModelScope share one origin.
+ markRemoteNetworkOnline();
+}
+
+export class InvalidHubEndpointError extends Error {}
+
+export async function updateHubSource(source: HubSource): Promise<HubSettings> {
+ const res = await authFetch("/api/settings/hub/source", {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ source }),
+ });
+ if (!res.ok) {
+ throw new Error(await readFastApiError(res, "Failed to switch the model source"));
+ }
+ const saved = fromApi(await res.json());
+ await followSavedEndpoints();
+ return saved;
+}
+
+export type HubEndpointSettings = Pick<
+ HubSettings,
+ "hfEndpoint" | "datasetsServerFollowsEndpoint"
+>;
+
+export async function updateHubSettings(
+ settings: HubEndpointSettings,
+): Promise<HubSettings> {
+ const res = await authFetch("/api/settings/hub", {
+ method: "PUT",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({
+ // biome-ignore lint/style/useNamingConvention: API schema
+ hf_endpoint: settings.hfEndpoint,
+ // biome-ignore lint/style/useNamingConvention: API schema
+ datasets_server_follows_endpoint: settings.datasetsServerFollowsEndpoint,
+ }),
+ });
+ if (!res.ok) {
+ const message = await readFastApiError(res, "Failed to save hub settings");
+ throw res.status === 400 || res.status === 422
+ ? new InvalidHubEndpointError(message)
+ : new Error(message);
+ }
+ const saved = fromApi(await res.json());
+ await followSavedEndpoints();
+ return saved;
+}
diff --git a/studio/frontend/src/features/settings/components/hub-settings-section.tsx b/studio/frontend/src/features/settings/components/hub-settings-section.tsx
new file mode 100644
index 00000000000..8780f43774e
--- /dev/null
+++ b/studio/frontend/src/features/settings/components/hub-settings-section.tsx
@@ -0,0 +1,207 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import { Switch } from "@/components/ui/switch";
+import { useIsAccountOwner } from "@/features/auth";
+import { useT } from "@/i18n";
+import { DEFAULT_HF_ENDPOINT, type HubSource } from "@/lib/hf-endpoint";
+import { cn } from "@/lib/utils";
+import { useEffect, useState } from "react";
+import {
+ type HubEndpointSettings,
+ type HubSettings,
+ InvalidHubEndpointError,
+ loadHubSettings,
+ updateHubSettings,
+ updateHubSource,
+} from "../api/hub-settings";
+import { SettingsRow } from "./settings-row";
+import { SettingsSection } from "./settings-section";
+
+const SOURCES: { value: HubSource; label: string }[] = [
+ { value: "huggingface", label: "Hugging Face" },
+ { value: "modelscope", label: "ModelScope" },
+];
+
+export function HubSettingsSection() {
+ const t = useT();
+ const isOwner = useIsAccountOwner();
+ const [settings, setSettings] = useState<HubSettings | null>(null);
+ const [draftEndpoint, setDraftEndpoint] = useState("");
+ const [saving, setSaving] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+
+ useEffect(() => {
+ let cancelled = false;
+ loadHubSettings()
+ .then((loaded) => {
+ if (cancelled) {
+ return;
+ }
+ setSettings(loaded);
+ setDraftEndpoint(loaded.hfEndpoint);
+ })
+ .catch(() => {
+ if (!cancelled) {
+ setError(t("settings.general.hub.loadFailed"));
+ }
+ });
+ return () => {
+ cancelled = true;
+ };
+ }, [t]);
+
+ const save = async (next: HubEndpointSettings) => {
+ setSaving(true);
+ setError(null);
+ try {
+ const saved = await updateHubSettings(next);
+ setSettings(saved);
+ if (next.hfEndpoint === draftEndpoint) {
+ setDraftEndpoint(saved.hfEndpoint);
+ }
+ } catch (err) {
+ setError(
+ err instanceof InvalidHubEndpointError
+ ? t("settings.general.hub.invalidEndpoint")
+ : t("settings.general.hub.saveFailed"),
+ );
+ } finally {
+ setSaving(false);
+ }
+ };
+
+ const locked = !settings || saving || !isOwner;
+ const endpointChanged =
+ settings !== null && draftEndpoint.trim() !== settings.hfEndpoint;
+ const saveSource = async (source: HubSource) => {
+ setSaving(true);
+ setError(null);
+ try {
+ setSettings(await updateHubSource(source));
+ } catch {
+ setError(t("settings.general.hub.saveFailed"));
+ } finally {
+ setSaving(false);
+ }
+ };
+
+ const showEndpointRows = settings?.activeSource !== "modelscope";
+ const errorNote = error ? (
+ <span className="max-w-[calc(300px*var(--ui-space-scale,1))] text-right text-xs text-destructive">
+ {error}
+ </span>
+ ) : null;
+
+ const saveEndpoint = () =>
+ settings &&
+ void save({
+ hfEndpoint: draftEndpoint,
+ datasetsServerFollowsEndpoint:
+ settings.datasetsServerFollowsEndpoint && draftEndpoint.trim() !== "",
+ });
+
+ return (
+ <SettingsSection title={t("settings.general.hub.sectionTitle")}>
+ <SettingsRow
+ alignTop={true}
+ label={t("settings.general.hub.source")}
+ description={t("settings.general.hub.sourceDescription")}
+ >
+ <div className="flex flex-col items-end gap-1">
+ <div
+ role="radiogroup"
+ aria-label={t("settings.general.hub.source")}
+ className="hub-tab-toggle inline-flex h-8 items-center rounded-full"
+ >
+ {SOURCES.map((option) => {
+ const active = settings?.source === option.value;
+ return (
+ <button
+ key={option.value}
+ type="button"
+ role="radio"
+ data-settings-label={option.label}
+ aria-checked={active}
+ disabled={locked}
+ onClick={() => {
+ if (!active) void saveSource(option.value);
+ }}
+ className={cn(
+ "relative flex h-8 items-center rounded-full px-3 text-xs font-medium transition-colors disabled:cursor-not-allowed",
+ active
+ ? "hub-tab-toggle-pill text-foreground"
+ : "text-muted-foreground hover:text-foreground",
+ )}
+ >
+ <span className="relative z-10">{option.label}</span>
+ </button>
+ );
+ })}
+ </div>
+ {settings && settings.source !== settings.activeSource ? (
+ <span className="max-w-[calc(300px*var(--ui-space-scale,1))] text-right text-xs text-destructive">
+ {t("settings.general.hub.sourceFallback")}
+ </span>
+ ) : null}
+ {showEndpointRows ? null : errorNote}
+ </div>
+ </SettingsRow>
+ {showEndpointRows ? (
+ <>
+ <SettingsRow
+ alignTop={true}
+ label={t("settings.general.hub.endpoint")}
+ description={t("settings.general.hub.endpointDescription")}
+ >
+ <div className="flex flex-col items-end gap-1">
+ <div className="flex items-center gap-2">
+ <Input
+ type="url"
+ value={draftEndpoint}
+ placeholder={DEFAULT_HF_ENDPOINT}
+ disabled={locked}
+ aria-label={t("settings.general.hub.endpoint")}
+ onChange={(event) => setDraftEndpoint(event.target.value)}
+ onKeyDown={(event) => {
+ if (event.key === "Enter" && endpointChanged) {
+ saveEndpoint();
+ }
+ }}
+ className="h-8 w-60"
+ />
+ <Button
+ variant="outline"
+ size="sm"
+ disabled={locked || !endpointChanged}
+ onClick={saveEndpoint}
+ >
+ {saving ? t("common.saving") : t("common.save")}
+ </Button>
+ </div>
+ {errorNote}
+ </div>
+ </SettingsRow>
+ <SettingsRow
+ label={t("settings.general.hub.datasetsServer")}
+ description={t("settings.general.hub.datasetsServerDescription")}
+ >
+ <Switch
+ checked={settings?.datasetsServerFollowsEndpoint ?? false}
+ disabled={locked || !settings?.hfEndpoint}
+ onCheckedChange={(checked) =>
+ settings &&
+ void save({
+ ...settings,
+ datasetsServerFollowsEndpoint: checked,
+ })
+ }
+ />
+ </SettingsRow>
+ </>
+ ) : null}
+ </SettingsSection>
+ );
+}
diff --git a/studio/frontend/src/features/settings/index.ts b/studio/frontend/src/features/settings/index.ts
index 370eba407de..1c26c353aa2 100644
--- a/studio/frontend/src/features/settings/index.ts
+++ b/studio/frontend/src/features/settings/index.ts
@@ -10,6 +10,7 @@ export {
updateDownloadTransportSettings,
} from "./api/download-transport";
export { loadEmbeddingModelSettings } from "./api/embedding-model";
+export { updateHubSource } from "./api/hub-settings";
export { loadOpenAIAutoSwitchSettings } from "./api/openai-auto-switch";
export {
loadHuggingFaceCacheSettings,
diff --git a/studio/frontend/src/features/settings/settings-dialog.tsx b/studio/frontend/src/features/settings/settings-dialog.tsx
index ae6a523e0d2..05098ed2e5a 100644
--- a/studio/frontend/src/features/settings/settings-dialog.tsx
+++ b/studio/frontend/src/features/settings/settings-dialog.tsx
@@ -13,6 +13,7 @@ import {
} from "@/components/ui/dialog";
import { type TranslationKey, useT } from "@/i18n";
import { isTauri } from "@/lib/api-base";
+import { useHubSource } from "@/lib/hf-endpoint";
import { MicIcon } from "@/lib/mic-icon";
import { cn } from "@/lib/utils";
import { useScrollFades } from "@/hooks/use-scroll-fades";
@@ -55,6 +56,7 @@ import {
import {
SETTINGS_SEARCH_KEYWORDS,
createSettingsSearchIndex,
+ renderedSearchEntries,
} from "./settings-search";
import {
type SettingsTab,
@@ -297,6 +299,7 @@ export function SettingsDialog() {
const t = useT();
const isOwner = useIsAccountOwner();
const stacked = useStackedLayout();
+ const hubSource = useHubSource();
const { attach: attachRail, onScroll: onRailScroll, className: railFadeClass } = useScrollFades();
const visibleTabs = useMemo(() => TABS.filter((tab) => settingsTabVisible(tab.id, isOwner)), [isOwner]);
const open = useSettingsDialogStore((s) => s.open);
@@ -336,7 +339,7 @@ export function SettingsDialog() {
}
return visibleTabs.map((tab) => {
const tabLabel = t(tab.labelKey);
- const entries = SETTINGS_SEARCH_INDEX[tab.id]
+ const entries = renderedSearchEntries(SETTINGS_SEARCH_INDEX, tab.id, hubSource)
.filter((key) => {
if (t(key).toLowerCase().includes(q)) {
return true;
@@ -353,7 +356,7 @@ export function SettingsDialog() {
tabMatches: tabLabel.toLowerCase().includes(q),
};
}).filter((r) => r.tabMatches || r.entries.length > 0);
- }, [query, t, visibleTabs]);
+ }, [query, t, visibleTabs, hubSource]);
const [pendingScroll, setPendingScroll] = useState<{
tab: SettingsTab;
diff --git a/studio/frontend/src/features/settings/settings-search.ts b/studio/frontend/src/features/settings/settings-search.ts
index f04ae3ae68b..aee7e7057a3 100644
--- a/studio/frontend/src/features/settings/settings-search.ts
+++ b/studio/frontend/src/features/settings/settings-search.ts
@@ -2,6 +2,7 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import type { TranslationKey } from "@/i18n";
+import type { HubSource } from "@/lib/hf-endpoint";
import type { SettingsTab } from "./stores/settings-dialog-store";
/**
@@ -29,6 +30,10 @@ export const SETTINGS_SEARCH_INDEX: Record<SettingsTab, TranslationKey[]> = {
"settings.general.rag.embeddingModel",
"settings.general.helperLlm.sectionTitle",
"settings.general.helperLlm.preloadOnStartup",
+ "settings.general.hub.sectionTitle",
+ "settings.general.hub.source",
+ "settings.general.hub.endpoint",
+ "settings.general.hub.datasetsServer",
"settings.general.downloads.sectionTitle",
"settings.general.downloads.transport",
"settings.general.downloads.https",
@@ -316,6 +321,21 @@ export function createSettingsSearchIndex({
};
}
+const HUGGING_FACE_ONLY_ENTRIES: ReadonlySet<TranslationKey> = new Set([
+ "settings.general.hub.endpoint",
+ "settings.general.hub.datasetsServer",
+]);
+
+export function renderedSearchEntries(
+ index: Record<SettingsTab, TranslationKey[]>,
+ tab: SettingsTab,
+ hubSource: HubSource,
+): TranslationKey[] {
+ return hubSource === "modelscope"
+ ? index[tab].filter((key) => !HUGGING_FACE_ONLY_ENTRIES.has(key))
+ : index[tab];
+}
+
/**
* Extra terms a row matches on, beyond its own label. The value is a
* translation key holding space-separated synonyms; it is never rendered.
diff --git a/studio/frontend/src/features/settings/tabs/general-tab.tsx b/studio/frontend/src/features/settings/tabs/general-tab.tsx
index c4fdae9d562..994820755c3 100644
--- a/studio/frontend/src/features/settings/tabs/general-tab.tsx
+++ b/studio/frontend/src/features/settings/tabs/general-tab.tsx
@@ -81,6 +81,7 @@ import { DocumentsRagSection } from "../components/documents-rag-section";
import { LanguageSelect } from "../components/language-select";
import { TRANSPORT_MODE_STORAGE_KEY } from "@/features/hub";
import { DownloadTransportRow } from "../components/download-transport-row";
+import { HubSettingsSection } from "../components/hub-settings-section";
import { SettingsRow } from "../components/settings-row";
import { SettingsSection } from "../components/settings-section";
import { StudioVersionSection } from "../components/studio-version-section";
@@ -770,6 +771,8 @@ export function GeneralTab() {
<DocumentsRagSection />
+ <HubSettingsSection />
+
<SettingsSection title={t("settings.general.downloads.sectionTitle")}>
<DownloadTransportRow />
</SettingsSection>
diff --git a/studio/frontend/src/hooks/hf-dataset-split-sources.ts b/studio/frontend/src/hooks/hf-dataset-split-sources.ts
index f8f2a10b013..0d4b40afdfe 100644
--- a/studio/frontend/src/hooks/hf-dataset-split-sources.ts
+++ b/studio/frontend/src/hooks/hf-dataset-split-sources.ts
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+import { hasDatasetsServer } from "@/lib/hf-endpoint";
+
export interface HfSplitEntry {
dataset: string;
config: string;
@@ -19,16 +21,20 @@ export type LoadHfDatasetSplitsArgs = {
export type DatasetSplitLoadResult = {
entries: HfSplitEntry[];
error: string | null;
- source: "local" | "remote" | "manual";
+ source: "local" | "remote" | "hub" | "manual";
};
export type DatasetSplitFetchers = {
local: (args: LoadHfDatasetSplitsArgs) => Promise<HfSplitEntry[]>;
remote: (args: LoadHfDatasetSplitsArgs) => Promise<HfSplitEntry[]>;
+ hub: (args: LoadHfDatasetSplitsArgs) => Promise<HfSplitEntry[]>;
};
export function normalizeDatasetSplitsError(message: string): string {
const normalized = message.toLowerCase();
+ if (normalized.includes("is not on modelscope")) {
+ return message;
+ }
if (
normalized.includes("dataset scripts are no longer supported") ||
normalized.includes("runs arbitrary python code")
@@ -37,6 +43,7 @@ export function normalizeDatasetSplitsError(message: string): string {
}
if (
normalized.includes("unauthorized") ||
+ normalized.includes("authorization") ||
normalized.includes("forbidden") ||
normalized.includes("access token") ||
normalized.includes("private") ||
@@ -75,17 +82,24 @@ export async function loadHfDatasetSplits(
}
if (args.online) {
- try {
- const entries = await fetchers.remote(args);
- throwIfAborted(args.signal);
- if (entries.length > 0) {
- return { entries, error: null, source: "remote" };
+ let failure: unknown = null;
+ const sources = hasDatasetsServer() ? (["remote", "hub"] as const) : (["hub"] as const);
+ for (const source of sources) {
+ try {
+ const entries = await fetchers[source](args);
+ throwIfAborted(args.signal);
+ if (entries.length > 0) {
+ return { entries, error: null, source };
+ }
+ } catch (error) {
+ throwIfAborted(args.signal);
+ failure = error;
}
- } catch (error) {
- throwIfAborted(args.signal);
+ }
+ if (failure !== null) {
const message =
- error instanceof Error
- ? error.message
+ failure instanceof Error
+ ? failure.message
: "Failed to fetch dataset splits";
return {
entries: [],
diff --git a/studio/frontend/src/hooks/use-hf-dataset-splits.ts b/studio/frontend/src/hooks/use-hf-dataset-splits.ts
index 9128321559a..1503e617d20 100644
--- a/studio/frontend/src/hooks/use-hf-dataset-splits.ts
+++ b/studio/frontend/src/hooks/use-hf-dataset-splits.ts
@@ -2,7 +2,14 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import { authFetch } from "@/features/auth";
-import { getHfDatasetsServerBase, useHfDatasetsServer } from "@/lib/hf-endpoint";
+import { hubTokenHeader } from "@/features/hub";
+import {
+ getHfDatasetsServerBase,
+ useHfDatasetsServer,
+ useHfEndpoint,
+ useHubSource,
+} from "@/lib/hf-endpoint";
+import { hubFetch } from "@/lib/hub-fetch";
import { useEffect, useState } from "react";
import {
type DatasetSplitFetchers,
@@ -110,7 +117,7 @@ async function fetchRemoteSplits({
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`;
}
- const response = await fetch(url, { headers, signal });
+ const response = await hubFetch(url, { headers, signal });
if (!response.ok) {
const body = await response.json().catch(() => null);
throw new Error(
@@ -121,9 +128,36 @@ async function fetchRemoteSplits({
return validatedEntries(payload.splits);
}
+async function fetchHubSplits({
+ accessToken,
+ datasetName,
+ signal,
+}: LoadHfDatasetSplitsArgs): Promise<HfSplitEntry[]> {
+ const response = await authFetch("/api/hub/datasets/hub-options", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ ...hubTokenHeader(accessToken),
+ },
+ body: JSON.stringify({ dataset_name: datasetName }),
+ signal,
+ });
+ if (!response.ok) {
+ const body = await response.json().catch(() => null);
+ throw new Error(
+ typeof body?.detail === "string"
+ ? body.detail
+ : `Failed to read dataset options (${response.status})`,
+ );
+ }
+ const payload = (await response.json()) as { splits?: unknown };
+ return validatedEntries(payload.splits);
+}
+
const DEFAULT_FETCHERS: DatasetSplitFetchers = {
local: fetchLocalSplits,
remote: fetchRemoteSplits,
+ hub: fetchHubSplits,
};
// ---------------------------------------------------------------------------
@@ -146,12 +180,16 @@ export function useHfDatasetSplits(
// In the request identity, or a server arriving later leaves the selector on
// the official one, or on the failure it reached there.
const hfDatasetsServer = useHfDatasetsServer();
+ const hubSource = useHubSource();
+ const hfEndpoint = useHfEndpoint();
const requestKey = JSON.stringify([
datasetName,
options?.preferLocalCache === true,
options?.localPath ?? null,
options?.online !== false,
hfDatasetsServer,
+ hubSource,
+ hfEndpoint,
]);
const [previousRequestKey, setPreviousRequestKey] = useState(requestKey);
if (requestKey !== previousRequestKey) {
@@ -219,7 +257,7 @@ export function useHfDatasetSplits(
});
return () => controller.abort();
- }, [accessToken, datasetName, localPath, online, preferLocalCache, hfDatasetsServer]);
+ }, [accessToken, datasetName, localPath, online, preferLocalCache, hfDatasetsServer, hubSource, hfEndpoint]);
// Derive unique subsets
const subsets = Array.from(new Set(entries.map((e) => e.config)));
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 35067ffe072..a49e94c4b8f 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -91,6 +91,10 @@ export const ar = {
"توجد عدة نتائج مطابقة ضمن {noun}. اختر نتيجة من القائمة.",
rateLimitedTitle: "تم بلوغ حد طلبات Hugging Face",
rateLimitedBody: "انتظر قليلًا، ثم أعد البحث في {noun}.",
+ modelScope: "ModelScope",
+ useModelScope: "استخدام ModelScope",
+ useModelScopeHint: "ابحث ونزّل من ModelScope بدلًا من ذلك. يمكنك العودة من الإعدادات.",
+ useModelScopeFailed: "تعذّر التبديل إلى ModelScope.",
hfToken: {
label: "توكن HF",
saved: "محفوظ",
@@ -1045,6 +1049,19 @@ export const ar = {
loadError: "تعذر تحميل إعداد التشغيل عند تسجيل الدخول.",
saveError: "تعذر تحديث إعداد التشغيل عند تسجيل الدخول.",
},
+ hub: {
+ source: "مصدر النماذج",
+ sourceDescription: "المكان الذي يبحث فيه مركز النماذج ويُنزّل منه. اختر ModelScope إذا كان Hugging Face محظورًا أو بطيئًا على شبكتك.",
+ sourceFallback: "تعذّر تشغيل ModelScope، لذلك يُستخدم Hugging Face. راجع سجلات Unsloth.",
+ sectionTitle: "مركز النماذج",
+ endpoint: "نقطة نهاية Hugging Face",
+ endpointDescription: "المصدر الذي تُنزَّل منه النماذج ومجموعات البيانات. اتركه فارغًا لاستخدام huggingface.co، أو أدخل مرآة مثل https://hf-mirror.com.",
+ datasetsServer: "استخدامه أيضًا لمعاينات مجموعات البيانات",
+ datasetsServerDescription: "إرسال طلبات معاينة مجموعات البيانات (HF_DATASETS_SERVER) إلى نقطة النهاية هذه أيضًا. فعّله فقط إذا كانت المرآة تدعمها.",
+ invalidEndpoint: "أدخل عنوان http(s) بلا بيانات دخول أو استعلام. لا يعمل http العادي إلا مع عنوان محلي.",
+ saveFailed: "تعذّر حفظ إعدادات المركز.",
+ loadFailed: "تعذّر تحميل إعدادات المركز.",
+ },
downloads: {
sectionTitle: "التنزيلات",
transport: "طريقة نقل التنزيل",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index a07ec7d877c..33671c9af27 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -92,6 +92,10 @@ export const de = {
rateLimitedTitle: "Hugging Face-Ratenlimit erreicht",
rateLimitedBody:
"Warten Sie einen Moment und wiederholen Sie dann die Suche ({noun}).",
+ modelScope: "ModelScope",
+ useModelScope: "ModelScope verwenden",
+ useModelScopeHint: "Stattdessen über ModelScope suchen und herunterladen. Sie können in den Einstellungen zurückwechseln.",
+ useModelScopeFailed: "Wechsel zu ModelScope fehlgeschlagen.",
hfToken: {
label: "HF-Token",
saved: "Gespeichert",
@@ -1068,6 +1072,19 @@ export const de = {
saveError:
"Die Autostart-Einstellung konnte nicht aktualisiert werden.",
},
+ hub: {
+ source: "Modellquelle",
+ sourceDescription: "Wo der Model-Hub sucht und herunterlädt. Wählen Sie ModelScope, wenn Hugging Face in Ihrem Netzwerk blockiert oder langsam ist.",
+ sourceFallback: "ModelScope konnte nicht gestartet werden, daher wird Hugging Face verwendet. Prüfen Sie die Unsloth-Logs.",
+ sectionTitle: "Modell-Hub",
+ endpoint: "Hugging-Face-Endpunkt",
+ endpointDescription: "Von hier werden Modelle und Datensätze geladen. Leer lassen für huggingface.co oder einen Mirror wie https://hf-mirror.com eintragen.",
+ datasetsServer: "Auch für Datensatz-Vorschauen verwenden",
+ datasetsServerDescription: "Datensatz-Vorschauen (HF_DATASETS_SERVER) ebenfalls an diesen Endpunkt senden. Nur einschalten, wenn der Mirror sie bereitstellt.",
+ invalidEndpoint: "Geben Sie eine http(s)-URL ohne Anmeldedaten oder Query ein. Reines http funktioniert nur für eine lokale Adresse.",
+ saveFailed: "Die Hub-Einstellungen konnten nicht gespeichert werden.",
+ loadFailed: "Die Hub-Einstellungen konnten nicht geladen werden.",
+ },
downloads: {
sectionTitle: "Downloads",
transport: "Download-Transport",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index b4959d08111..8b0cfaeedd6 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -92,6 +92,10 @@ export const en = {
multipleMatches: "Multiple matching {noun}. Choose one from the list.",
rateLimitedTitle: "Hugging Face rate limit reached",
rateLimitedBody: "Wait a moment, then retry searching {noun}.",
+ modelScope: "ModelScope",
+ useModelScope: "Use ModelScope",
+ useModelScopeHint: "Search and download from ModelScope instead. You can switch back in Settings.",
+ useModelScopeFailed: "Couldn't switch to ModelScope.",
hfToken: {
label: "HF token",
saved: "Saved",
@@ -1054,6 +1058,19 @@ export const en = {
loadError: "Failed to load the launch at login setting.",
saveError: "Failed to update the launch at login setting.",
},
+ hub: {
+ source: "Model source",
+ sourceDescription: "Where the model hub searches and downloads. Choose ModelScope if Hugging Face is blocked or slow on your network.",
+ sourceFallback: "ModelScope couldn't be started, so Hugging Face is in use. Check the Unsloth logs.",
+ sectionTitle: "Model hub",
+ endpoint: "Hugging Face endpoint",
+ endpointDescription: "Where models and datasets are downloaded from. Leave empty for huggingface.co, or enter a mirror such as https://hf-mirror.com.",
+ datasetsServer: "Use it for dataset previews",
+ datasetsServerDescription: "Also send dataset preview requests (HF_DATASETS_SERVER) to this endpoint. Turn on only if the mirror serves them.",
+ invalidEndpoint: "Enter an http(s) URL without a login or query. Plain http works only for a local address.",
+ saveFailed: "Couldn't save the hub settings.",
+ loadFailed: "Couldn't load the hub settings.",
+ },
downloads: {
sectionTitle: "Downloads",
transport: "Download transport",
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index 10de1fe6108..83178bb8f7c 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -92,6 +92,10 @@ export const es = {
rateLimitedTitle: "Se alcanzó el límite de solicitudes de Hugging Face",
rateLimitedBody:
"Espera un momento y vuelve a intentar la búsqueda de {noun}.",
+ modelScope: "ModelScope",
+ useModelScope: "Usar ModelScope",
+ useModelScopeHint: "Busca y descarga desde ModelScope en su lugar. Puedes volver a cambiarlo en Ajustes.",
+ useModelScopeFailed: "No se pudo cambiar a ModelScope.",
hfToken: {
label: "Token de HF",
saved: "Guardado",
@@ -1063,6 +1067,19 @@ export const es = {
loadError: "No se pudo cargar el ajuste de inicio automático.",
saveError: "No se pudo actualizar el ajuste de inicio automático.",
},
+ hub: {
+ source: "Origen de los modelos",
+ sourceDescription: "Dónde busca y descarga el hub de modelos. Elige ModelScope si Hugging Face está bloqueado o es lento en tu red.",
+ sourceFallback: "No se pudo iniciar ModelScope, así que se usa Hugging Face. Revisa los registros de Unsloth.",
+ sectionTitle: "Hub de modelos",
+ endpoint: "Endpoint de Hugging Face",
+ endpointDescription: "De dónde se descargan los modelos y conjuntos de datos. Déjalo vacío para usar huggingface.co o escribe un espejo como https://hf-mirror.com.",
+ datasetsServer: "Usarlo también para las vistas previas de datos",
+ datasetsServerDescription: "Enviar también las vistas previas de conjuntos de datos (HF_DATASETS_SERVER) a este endpoint. Actívalo solo si el espejo las ofrece.",
+ invalidEndpoint: "Escribe una URL http(s) sin credenciales ni parámetros. http sin cifrar solo funciona con una dirección local.",
+ saveFailed: "No se pudo guardar la configuración del hub.",
+ loadFailed: "No se pudo cargar la configuración del hub.",
+ },
downloads: {
sectionTitle: "Descargas",
transport: "Transporte de descarga",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index 630283ae3ea..f3248f6957a 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -92,6 +92,10 @@ export const fr = {
rateLimitedTitle: "Limite de requêtes Hugging Face atteinte",
rateLimitedBody:
"Patientez un instant, puis relancez la recherche de {noun}.",
+ modelScope: "ModelScope",
+ useModelScope: "Utiliser ModelScope",
+ useModelScopeHint: "Rechercher et télécharger depuis ModelScope à la place. Vous pouvez revenir en arrière dans les Paramètres.",
+ useModelScopeFailed: "Impossible de passer à ModelScope.",
hfToken: {
label: "Token HF",
saved: "Enregistré",
@@ -1067,6 +1071,19 @@ export const fr = {
saveError:
"Impossible de mettre à jour le réglage de lancement à la connexion.",
},
+ hub: {
+ source: "Source des modèles",
+ sourceDescription: "Où le hub de modèles recherche et télécharge. Choisissez ModelScope si Hugging Face est bloqué ou lent sur votre réseau.",
+ sourceFallback: "ModelScope n'a pas pu démarrer, Hugging Face est donc utilisé. Consultez les journaux d'Unsloth.",
+ sectionTitle: "Hub de modèles",
+ endpoint: "Point de terminaison Hugging Face",
+ endpointDescription: "Source de téléchargement des modèles et jeux de données. Laissez vide pour huggingface.co, ou saisissez un miroir comme https://hf-mirror.com.",
+ datasetsServer: "L'utiliser aussi pour les aperçus de jeux de données",
+ datasetsServerDescription: "Envoyer aussi les aperçus de jeux de données (HF_DATASETS_SERVER) à ce point de terminaison. À activer seulement si le miroir les fournit.",
+ invalidEndpoint: "Saisissez une URL http(s) sans identifiants ni paramètres. Le http simple ne fonctionne que pour une adresse locale.",
+ saveFailed: "Impossible d'enregistrer les réglages du hub.",
+ loadFailed: "Impossible de charger les réglages du hub.",
+ },
downloads: {
sectionTitle: "Téléchargements",
transport: "Transport de téléchargement",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index cf2113a6d6d..085ced94585 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -92,6 +92,10 @@ export const hi = {
rateLimitedTitle: "Hugging Face की अनुरोध सीमा पूरी हो गई",
rateLimitedBody:
"कुछ देर प्रतीक्षा करें, फिर {noun} की खोज दोबारा करें।",
+ modelScope: "ModelScope",
+ useModelScope: "ModelScope इस्तेमाल करें",
+ useModelScopeHint: "इसके बजाय ModelScope से खोजें और डाउनलोड करें। आप सेटिंग्स में वापस बदल सकते हैं।",
+ useModelScopeFailed: "ModelScope पर स्विच नहीं हो सका।",
hfToken: {
label: "HF टोकन",
saved: "सहेजा गया",
@@ -1048,6 +1052,19 @@ export const hi = {
loadError: "लॉगिन पर चलाने की सेटिंग लोड नहीं हो सकी।",
saveError: "लॉगिन पर चलाने की सेटिंग अपडेट नहीं हो सकी।",
},
+ hub: {
+ source: "मॉडल स्रोत",
+ sourceDescription: "मॉडल हब कहाँ खोजता और डाउनलोड करता है। अगर आपके नेटवर्क पर Hugging Face ब्लॉक या धीमा है तो ModelScope चुनें।",
+ sourceFallback: "ModelScope शुरू नहीं हो सका, इसलिए Hugging Face इस्तेमाल हो रहा है। Unsloth के लॉग देखें।",
+ sectionTitle: "मॉडल हब",
+ endpoint: "Hugging Face एंडपॉइंट",
+ endpointDescription: "मॉडल और डेटासेट यहीं से डाउनलोड होते हैं। huggingface.co के लिए खाली छोड़ें, या https://hf-mirror.com जैसा कोई मिरर डालें।",
+ datasetsServer: "डेटासेट प्रीव्यू के लिए भी इस्तेमाल करें",
+ datasetsServerDescription: "डेटासेट प्रीव्यू अनुरोध (HF_DATASETS_SERVER) भी इसी एंडपॉइंट पर भेजें। इसे तभी चालू करें जब मिरर इन्हें सपोर्ट करता हो।",
+ invalidEndpoint: "लॉगिन जानकारी या क्वेरी के बिना http(s) URL डालें। सादा http केवल लोकल पते के लिए काम करता है।",
+ saveFailed: "हब सेटिंग्स सेव नहीं हो सकीं।",
+ loadFailed: "हब सेटिंग्स लोड नहीं हो सकीं।",
+ },
downloads: {
sectionTitle: "डाउनलोड",
transport: "डाउनलोड ट्रांसपोर्ट",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index 42d23916874..0654ac0969b 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -1039,6 +1039,19 @@ export const it = {
saveError:
"Impossibile aggiornare l'impostazione di avvio all'accesso.",
},
+ hub: {
+ source: "Origine dei modelli",
+ sourceDescription: "Dove l'hub dei modelli cerca e scarica. Scegli ModelScope se Hugging Face è bloccato o lento sulla tua rete.",
+ sourceFallback: "Non è stato possibile avviare ModelScope, quindi viene usato Hugging Face. Controlla i log di Unsloth.",
+ sectionTitle: "Hub dei modelli",
+ endpoint: "Endpoint Hugging Face",
+ endpointDescription: "Da dove vengono scaricati modelli e dataset. Lascia vuoto per huggingface.co oppure inserisci un mirror come https://hf-mirror.com.",
+ datasetsServer: "Usalo anche per le anteprime dei dataset",
+ datasetsServerDescription: "Invia anche le anteprime dei dataset (HF_DATASETS_SERVER) a questo endpoint. Attivalo solo se il mirror le fornisce.",
+ invalidEndpoint: "Inserisci un URL http(s) senza credenziali né parametri. http semplice funziona solo con un indirizzo locale.",
+ saveFailed: "Impossibile salvare le impostazioni dell'hub.",
+ loadFailed: "Impossibile caricare le impostazioni dell'hub.",
+ },
downloads: {
sectionTitle: "Download",
transport: "Trasporto di download",
@@ -2261,6 +2274,10 @@ export const it = {
"Sono stati trovati più {noun} corrispondenti. Scegline uno dall'elenco.",
rateLimitedTitle: "Limite di richieste di Hugging Face raggiunto",
rateLimitedBody: "Attendi un momento, poi riprova a cercare {noun}.",
+ modelScope: "ModelScope",
+ useModelScope: "Usa ModelScope",
+ useModelScopeHint: "Cerca e scarica da ModelScope. Puoi tornare indietro nelle Impostazioni.",
+ useModelScopeFailed: "Impossibile passare a ModelScope.",
hfToken: {
label: "Token HF",
saved: "Salvato",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index 69863a690b5..5bdb78cfd73 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -92,6 +92,10 @@ export const ja = {
"一致する{noun}が複数あります。リストから1つ選択してください。",
rateLimitedTitle: "Hugging Face のレート制限に達しました",
rateLimitedBody: "しばらく待ってから、{noun}の検索を再試行してください。",
+ modelScope: "ModelScope",
+ useModelScope: "ModelScope を使う",
+ useModelScopeHint: "代わりに ModelScope で検索・ダウンロードします。設定からいつでも戻せます。",
+ useModelScopeFailed: "ModelScope に切り替えられませんでした。",
hfToken: {
label: "HF トークン",
saved: "保存済み",
@@ -1034,6 +1038,19 @@ export const ja = {
loadError: "ログイン時起動の設定を読み込めませんでした。",
saveError: "ログイン時起動の設定を更新できませんでした。",
},
+ hub: {
+ source: "モデルの取得元",
+ sourceDescription: "モデルハブの検索とダウンロード先です。ネットワークで Hugging Face がブロックされている、または遅い場合は ModelScope を選んでください。",
+ sourceFallback: "ModelScope を起動できなかったため、Hugging Face を使用しています。Unsloth のログを確認してください。",
+ sectionTitle: "モデルハブ",
+ endpoint: "Hugging Face エンドポイント",
+ endpointDescription: "モデルとデータセットのダウンロード元です。空欄なら huggingface.co、ミラーを使う場合は https://hf-mirror.com などを入力します。",
+ datasetsServer: "データセットのプレビューにも使う",
+ datasetsServerDescription: "データセットのプレビュー要求(HF_DATASETS_SERVER)もこのエンドポイントに送ります。ミラーが対応している場合のみオンにしてください。",
+ invalidEndpoint: "ログイン情報やクエリを含まない http(s) の URL を入力してください。http はローカルアドレスでのみ使えます。",
+ saveFailed: "ハブ設定を保存できませんでした。",
+ loadFailed: "ハブ設定を読み込めませんでした。",
+ },
downloads: {
sectionTitle: "ダウンロード",
transport: "ダウンロード方式",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index c401847281a..438221281e0 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -90,6 +90,10 @@ export const ko = {
"일치하는 {noun} 항목이 여러 개 있습니다. 목록에서 하나를 선택하세요.",
rateLimitedTitle: "Hugging Face 요청 한도에 도달했습니다",
rateLimitedBody: "잠시 기다린 후 {noun} 검색을 다시 시도하세요.",
+ modelScope: "ModelScope",
+ useModelScope: "ModelScope 사용",
+ useModelScopeHint: "대신 ModelScope에서 검색하고 다운로드합니다. 설정에서 다시 바꿀 수 있습니다.",
+ useModelScopeFailed: "ModelScope로 전환하지 못했습니다.",
hfToken: {
label: "HF 토큰",
saved: "저장됨",
@@ -1042,6 +1046,19 @@ export const ko = {
loadError: "로그인 시 실행 설정을 불러오지 못했습니다.",
saveError: "로그인 시 실행 설정을 업데이트하지 못했습니다.",
},
+ hub: {
+ source: "모델 소스",
+ sourceDescription: "모델 허브가 검색하고 다운로드하는 곳입니다. 네트워크에서 Hugging Face가 차단되었거나 느리면 ModelScope를 선택하세요.",
+ sourceFallback: "ModelScope를 시작할 수 없어 Hugging Face를 사용하고 있습니다. Unsloth 로그를 확인하세요.",
+ sectionTitle: "모델 허브",
+ endpoint: "Hugging Face 엔드포인트",
+ endpointDescription: "모델과 데이터셋을 내려받는 곳입니다. 비워 두면 huggingface.co를 사용하고, 미러를 쓰려면 https://hf-mirror.com 같은 주소를 입력하세요.",
+ datasetsServer: "데이터셋 미리보기에도 사용",
+ datasetsServerDescription: "데이터셋 미리보기 요청(HF_DATASETS_SERVER)도 이 엔드포인트로 보냅니다. 미러가 지원할 때만 켜세요.",
+ invalidEndpoint: "로그인 정보나 쿼리가 없는 http(s) URL을 입력하세요. http는 로컬 주소에서만 쓸 수 있습니다.",
+ saveFailed: "허브 설정을 저장하지 못했습니다.",
+ loadFailed: "허브 설정을 불러오지 못했습니다.",
+ },
downloads: {
sectionTitle: "다운로드",
transport: "다운로드 전송 방식",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index 8cdcfad5dbb..731a1876c0e 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -92,6 +92,10 @@ export const ptBR = {
rateLimitedTitle: "Limite de requisições do Hugging Face atingido",
rateLimitedBody:
"Aguarde um momento e tente buscar {noun} novamente.",
+ modelScope: "ModelScope",
+ useModelScope: "Usar ModelScope",
+ useModelScopeHint: "Pesquise e baixe pelo ModelScope. Você pode voltar nas Configurações.",
+ useModelScopeFailed: "Não foi possível mudar para o ModelScope.",
hfToken: {
label: "Token do HF",
saved: "Salvo",
@@ -1056,6 +1060,19 @@ export const ptBR = {
saveError:
"Não foi possível atualizar a configuração de inicialização no login.",
},
+ hub: {
+ source: "Origem dos modelos",
+ sourceDescription: "Onde o hub de modelos pesquisa e baixa. Escolha ModelScope se o Hugging Face estiver bloqueado ou lento na sua rede.",
+ sourceFallback: "Não foi possível iniciar o ModelScope, então o Hugging Face está em uso. Verifique os logs do Unsloth.",
+ sectionTitle: "Hub de modelos",
+ endpoint: "Endpoint do Hugging Face",
+ endpointDescription: "De onde os modelos e conjuntos de dados são baixados. Deixe vazio para usar huggingface.co ou informe um espelho como https://hf-mirror.com.",
+ datasetsServer: "Usar também nas prévias de conjuntos de dados",
+ datasetsServerDescription: "Enviar também as prévias de conjuntos de dados (HF_DATASETS_SERVER) para este endpoint. Ative só se o espelho oferecer esse serviço.",
+ invalidEndpoint: "Informe uma URL http(s) sem credenciais nem parâmetros. http simples só funciona com um endereço local.",
+ saveFailed: "Não foi possível salvar as configurações do hub.",
+ loadFailed: "Não foi possível carregar as configurações do hub.",
+ },
downloads: {
sectionTitle: "Downloads",
transport: "Transporte de download",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index d795be570af..7294ba2fd9c 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -92,6 +92,10 @@ export const ru = {
rateLimitedTitle: "Достигнут лимит запросов Hugging Face",
rateLimitedBody:
"Подождите немного, затем повторите поиск в категории «{noun}».",
+ modelScope: "ModelScope",
+ useModelScope: "Использовать ModelScope",
+ useModelScopeHint: "Искать и скачивать через ModelScope. Вернуть можно в настройках.",
+ useModelScopeFailed: "Не удалось переключиться на ModelScope.",
hfToken: {
label: "Токен HF",
saved: "Сохранён",
@@ -1050,6 +1054,19 @@ export const ru = {
loadError: "Не удалось загрузить настройку автозапуска.",
saveError: "Не удалось обновить настройку автозапуска.",
},
+ hub: {
+ source: "Источник моделей",
+ sourceDescription: "Где хаб моделей ищет и откуда скачивает. Выберите ModelScope, если Hugging Face заблокирован или медленно работает в вашей сети.",
+ sourceFallback: "Не удалось запустить ModelScope, поэтому используется Hugging Face. Проверьте журналы Unsloth.",
+ sectionTitle: "Хаб моделей",
+ endpoint: "Эндпоинт Hugging Face",
+ endpointDescription: "Откуда загружаются модели и датасеты. Оставьте пустым для huggingface.co или укажите зеркало, например https://hf-mirror.com.",
+ datasetsServer: "Использовать и для предпросмотра датасетов",
+ datasetsServerDescription: "Отправлять запросы предпросмотра датасетов (HF_DATASETS_SERVER) на этот же эндпоинт. Включайте, только если зеркало их поддерживает.",
+ invalidEndpoint: "Введите адрес http(s) без учётных данных и параметров. Обычный http работает только для локального адреса.",
+ saveFailed: "Не удалось сохранить настройки хаба.",
+ loadFailed: "Не удалось загрузить настройки хаба.",
+ },
downloads: {
sectionTitle: "Загрузки",
transport: "Транспорт загрузки",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index f496f03ad69..82b56de911c 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -89,6 +89,10 @@ export const zhCN = {
multipleMatches: "找到多个匹配的{noun}。请从列表中选择一个。",
rateLimitedTitle: "已达到 Hugging Face 速率限制",
rateLimitedBody: "请稍候,然后重试搜索{noun}。",
+ modelScope: "ModelScope",
+ useModelScope: "使用 ModelScope",
+ useModelScopeHint: "改为从 ModelScope 搜索和下载。可随时在设置中切换回来。",
+ useModelScopeFailed: "无法切换到 ModelScope。",
hfToken: {
label: "HF token",
saved: "已保存",
@@ -1025,6 +1029,19 @@ export const zhCN = {
loadError: "无法加载登录时启动设置。",
saveError: "无法更新登录时启动设置。",
},
+ hub: {
+ source: "模型来源",
+ sourceDescription: "模型中心搜索和下载的来源。如果你的网络无法访问 Hugging Face 或速度很慢,请选择 ModelScope。",
+ sourceFallback: "无法启动 ModelScope,当前使用 Hugging Face。请查看 Unsloth 日志。",
+ sectionTitle: "模型中心",
+ endpoint: "Hugging Face 端点",
+ endpointDescription: "模型和数据集的下载来源。留空则使用 huggingface.co,或填写镜像地址,例如 https://hf-mirror.com。",
+ datasetsServer: "同时用于数据集预览",
+ datasetsServerDescription: "数据集预览请求(HF_DATASETS_SERVER)也发送到此端点。仅当镜像提供该服务时开启。",
+ invalidEndpoint: "请输入不含登录信息和查询参数的 http(s) 地址。http 仅适用于本机地址。",
+ saveFailed: "无法保存模型中心设置。",
+ loadFailed: "无法加载模型中心设置。",
+ },
downloads: {
sectionTitle: "下载",
transport: "下载传输方式",
diff --git a/studio/frontend/src/lib/hf-endpoint.ts b/studio/frontend/src/lib/hf-endpoint.ts
index 38336f394e4..2585ed0bcf0 100644
--- a/studio/frontend/src/lib/hf-endpoint.ts
+++ b/studio/frontend/src/lib/hf-endpoint.ts
@@ -16,9 +16,21 @@ import { createStore } from "zustand/vanilla";
export const DEFAULT_HF_ENDPOINT = "https://huggingface.co";
export const DEFAULT_DATASETS_SERVER = "https://datasets-server.huggingface.co";
-const store = createStore<{ endpoint: string; datasetsServer: string }>(() => ({
+export type HubSource = "huggingface" | "modelscope";
+
+const store = createStore<{
+ endpoint: string;
+ datasetsServer: string;
+ source: HubSource;
+ /** Kept after a switch back: requests already queued for the adapter still go there. */
+ modelScopeBase: string | null;
+ proxyBases: readonly string[];
+}>(() => ({
endpoint: DEFAULT_HF_ENDPOINT,
datasetsServer: DEFAULT_DATASETS_SERVER,
+ source: "huggingface",
+ modelScopeBase: null,
+ proxyBases: [],
}));
/**
@@ -43,26 +55,45 @@ function usableEndpoint(raw: string | null | undefined): string | null {
/**
* Apply the endpoints reported by `/api/health`. A blank, absent or unparseable
* value leaves the current one alone: older backends report neither field, and
- * resetting would strand a mirror-only deployment on huggingface.co.
+ * resetting would strand a mirror-only deployment on huggingface.co. `proxied`
+ * marks a value that is the backend's relay rather than the endpoint itself.
*/
export function setHfEndpoints(
endpoint?: string | null,
datasetsServer?: string | null,
+ source?: string | null,
+ proxied: { endpoint?: boolean; datasetsServer?: boolean } = {},
): void {
const next = {
endpoint: usableEndpoint(endpoint),
datasetsServer: usableEndpoint(datasetsServer),
};
- store.setState((prev) => ({
- endpoint: next.endpoint ?? prev.endpoint,
- datasetsServer: next.datasetsServer ?? prev.datasetsServer,
- }));
+ store.setState((prev) => {
+ const nextSource =
+ source === "huggingface" || source === "modelscope" ? source : prev.source;
+ const applied = next.endpoint ? nextSource : prev.source;
+ const relays = [
+ proxied.endpoint ? next.endpoint : null,
+ proxied.datasetsServer ? next.datasetsServer : null,
+ ].filter((base): base is string => base !== null && !prev.proxyBases.includes(base));
+ return {
+ endpoint: next.endpoint ?? prev.endpoint,
+ datasetsServer: next.datasetsServer ?? prev.datasetsServer,
+ source: applied,
+ modelScopeBase:
+ next.endpoint && applied === "modelscope" ? next.endpoint : prev.modelScopeBase,
+ proxyBases: relays.length ? [...prev.proxyBases, ...relays] : prev.proxyBases,
+ };
+ });
}
export function resetHfEndpoints(): void {
store.setState({
endpoint: DEFAULT_HF_ENDPOINT,
datasetsServer: DEFAULT_DATASETS_SERVER,
+ source: "huggingface",
+ modelScopeBase: null,
+ proxyBases: [],
});
}
@@ -70,6 +101,42 @@ export function getHfEndpoint(): string {
return store.getState().endpoint;
}
+export function getHubSource(): HubSource {
+ return store.getState().source;
+}
+
+export function isModelScopeHubUrl(url: string): boolean {
+ const base = store.getState().modelScopeBase;
+ return base !== null && url.startsWith(`${base}/`);
+}
+
+export function isProxiedHubUrl(url: string): boolean {
+ return store.getState().proxyBases.some((base) => url.startsWith(`${base}/`));
+}
+
+let sessionRefresh: (() => Promise<boolean>) | null = null;
+
+export function setHubSessionRefresh(refresh: () => Promise<boolean>): void {
+ sessionRefresh = refresh;
+}
+
+export function refreshHubSession(): Promise<boolean> {
+ return sessionRefresh ? sessionRefresh() : Promise.resolve(false);
+}
+
+export function useHubSource(): HubSource {
+ return useStore(store, (s) => s.source);
+}
+
+export function useHubName(): string {
+ return useHubSource() === "modelscope" ? "ModelScope" : "Hugging Face";
+}
+
+/** The datasets-server knows Hugging Face repos only: a ModelScope id would get another dataset's data. */
+export function hasDatasetsServer(): boolean {
+ return store.getState().source !== "modelscope";
+}
+
export function getHfDatasetsServerBase(): string {
return store.getState().datasetsServer;
}
diff --git a/studio/frontend/src/lib/hub-fetch.ts b/studio/frontend/src/lib/hub-fetch.ts
new file mode 100644
index 00000000000..b01034aef1b
--- /dev/null
+++ b/studio/frontend/src/lib/hub-fetch.ts
@@ -0,0 +1,69 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+/** Hub fetch: the adapter and relays take the Unsloth session; a relay forwards the HF token in `X-HF-Authorization`. */
+
+import {
+ isModelScopeHubUrl,
+ isProxiedHubUrl,
+ refreshHubSession,
+} from "@/lib/hf-endpoint";
+
+// Set by the relay on the endpoint's own answers, whose 401 is about the Hugging Face token.
+const UPSTREAM_HEADER = "X-Hub-Upstream";
+
+function requestUrl(input: Parameters<typeof fetch>[0]): string {
+ return typeof input === "string"
+ ? input
+ : input instanceof URL
+ ? input.href
+ : input.url;
+}
+
+function takesSession(url: string): boolean {
+ return isModelScopeHubUrl(url) || isProxiedHubUrl(url);
+}
+
+function withHubAuth(
+ input: Parameters<typeof fetch>[0],
+ init: RequestInit,
+): RequestInit {
+ const url = requestUrl(input);
+ if (!takesSession(url)) return init;
+ const headers = new Headers(init.headers);
+ const hfToken = headers.get("Authorization");
+ if (hfToken && isProxiedHubUrl(url)) headers.set("X-HF-Authorization", hfToken);
+ let token: string | null = null;
+ try {
+ token = localStorage.getItem("unsloth_auth_token");
+ } catch {
+ token = null;
+ }
+ if (token) headers.set("Authorization", `Bearer ${token}`);
+ else headers.delete("Authorization");
+ return { ...init, headers };
+}
+
+export async function fetchHub(
+ input: Parameters<typeof fetch>[0],
+ init: RequestInit = {},
+): Promise<Response> {
+ const url = requestUrl(input);
+ let response = await fetch(input, withHubAuth(input, init));
+ if (
+ response.status === 401 &&
+ !response.headers.has(UPSTREAM_HEADER) &&
+ takesSession(url) &&
+ (await refreshHubSession())
+ ) {
+ response = await fetch(input, withHubAuth(input, init));
+ }
+ // The relay's own 502 means it could not reach the endpoint: fail as a direct fetch would.
+ if (response.status === 502 && !response.headers.has(UPSTREAM_HEADER) && isProxiedHubUrl(url)) {
+ throw new TypeError("The Hub endpoint could not be reached.");
+ }
+ return response;
+}
+
+export const hubFetch: typeof fetch = (input, init) =>
+ fetchHub(input, init ?? {});
diff --git a/studio/frontend/src/main.tsx b/studio/frontend/src/main.tsx
index db8fefcc5c6..1fdfa7ba6b6 100644
--- a/studio/frontend/src/main.tsx
+++ b/studio/frontend/src/main.tsx
@@ -11,14 +11,18 @@ import {
watchMathBlockContainmentOverride,
} from "./components/assistant-ui/math-block-containment";
import { fetchDeviceType } from "./config/env";
+import { refreshSession } from "./features/auth/api";
import {
applyInterfaceScaleBeforeFirstPaint,
useInterfaceScaleStore,
} from "./features/settings/stores/interface-scale-store";
import { initializeLocale } from "./i18n";
import { isTauri } from "./lib/api-base";
+import { setHubSessionRefresh } from "./lib/hf-endpoint";
import { watchOverlayScrollbarGutter } from "./lib/overlay-scrollbar";
+setHubSessionRefresh(refreshSession);
+
const rootElement = document.getElementById("root");
if (!rootElement) {
throw new Error("Root element not found");
diff --git a/studio/frontend/tests/helpers/store-stubs/env.ts b/studio/frontend/tests/helpers/store-stubs/env.ts
index aedb8739bf8..76c16bf84c0 100644
--- a/studio/frontend/tests/helpers/store-stubs/env.ts
+++ b/studio/frontend/tests/helpers/store-stubs/env.ts
@@ -10,3 +10,5 @@ export const usePlatformStore = {
deviceType = next.deviceType;
},
};
+
+export async function fetchDeviceType(_options?: { force?: boolean }): Promise<void> {}
diff --git a/studio/frontend/tests/hf-dataset-split-sources.test.ts b/studio/frontend/tests/hf-dataset-split-sources.test.ts
index 739c37cdaf0..4d1673df378 100644
--- a/studio/frontend/tests/hf-dataset-split-sources.test.ts
+++ b/studio/frontend/tests/hf-dataset-split-sources.test.ts
@@ -2,11 +2,15 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
import assert from "node:assert/strict";
+import { register } from "node:module";
import test from "node:test";
-const { loadHfDatasetSplits } = await import(
+register("./bundler-resolver.mjs", import.meta.url);
+const { loadHfDatasetSplits, normalizeDatasetSplitsError } = await import(
"../src/hooks/hf-dataset-split-sources.ts"
);
+const { resetHfEndpoints, setHfEndpoints } = await import("../src/lib/hf-endpoint.ts");
+const hub = async (): Promise<{ dataset: string; config: string; split: string }[]> => [];
function args(overrides: Record<string, unknown> = {}) {
return {
@@ -38,6 +42,7 @@ test("cached dataset split resolution uses local metadata without a remote reque
remoteCalls += 1;
return [remoteEntry];
},
+ hub,
});
assert.equal(result.source, "local");
@@ -49,6 +54,7 @@ test("online resolution falls back to datasets-server when local metadata is abs
const result = await loadHfDatasetSplits(args(), {
local: async () => [],
remote: async () => [remoteEntry],
+ hub,
});
assert.equal(result.source, "remote");
@@ -63,6 +69,7 @@ test("offline resolution exposes manual entry instead of attempting the network"
remoteCalls += 1;
return [remoteEntry];
},
+ hub,
});
assert.equal(result.source, "manual");
@@ -80,8 +87,39 @@ test("an aborted local lookup cannot publish stale dataset options", async () =>
return [localEntry];
},
remote: async () => [remoteEntry],
+ hub,
}),
(error: unknown) =>
error instanceof DOMException && error.name === "AbortError",
);
});
+
+test("a datasets-server that fails or knows nothing falls back to the repo files", async (t) => {
+ const load = (remote: typeof hub, hubFetch: typeof hub) =>
+ loadHfDatasetSplits(args({ preferLocalCache: false }), { local: hub, remote, hub: hubFetch });
+ const fail = (message: string) => () => Promise.reject(new Error(message));
+ for (const remote of [fail("Unexpected token '<'"), hub]) {
+ const result = await load(remote, async () => [remoteEntry]);
+ assert.deepEqual([result.source, result.entries], ["hub", [remoteEntry]]);
+ }
+ const failed = await load(fail("Failed to fetch splits (502)"), fail("Dataset scripts are no longer supported"));
+ assert.deepEqual([failed.source, /legacy custom script/.test(failed.error ?? "")], ["manual", true]);
+ setHfEndpoints("http://127.0.0.1:8888/api/hub/modelscope", undefined, "modelscope");
+ t.after(resetHfEndpoints);
+ assert.equal((await load(async () => [remoteEntry], async () => [remoteEntry])).source, "hub");
+});
+
+test("a dataset missing on ModelScope keeps the backend's reason", () => {
+ const missing = "o/d is not on ModelScope. Switch the model source to Hugging Face in Settings to use it.";
+ assert.equal(normalizeDatasetSplitsError(missing), missing);
+});
+
+test("a cached gated dataset the backend refuses keeps the private-or-gated guidance", async () => {
+ const fail = (message: string) => () => Promise.reject(new Error(message));
+ const result = await loadHfDatasetSplits(args({ preferLocalCache: false }), {
+ local: fail("unused"),
+ remote: fail("Failed to fetch splits (401): gated dataset"),
+ hub: fail("Dataset preview is not available without Hub authorization."),
+ });
+ assert.match(result.error ?? "", /private or gated/);
+});
diff --git a/studio/frontend/tests/hf-endpoint.test.ts b/studio/frontend/tests/hf-endpoint.test.ts
index 765bef27922..de3dc70652d 100644
--- a/studio/frontend/tests/hf-endpoint.test.ts
+++ b/studio/frontend/tests/hf-endpoint.test.ts
@@ -20,12 +20,17 @@ const {
getHfEndpoint,
resetHfEndpoints,
setHfEndpoints,
+ setHubSessionRefresh,
useHfDatasetsServer,
useHfEndpoint,
} = await import("../src/lib/hf-endpoint.ts");
-const { isHuggingFaceOffline, markRemoteNetworkOffline, clearRemoteBackoff } =
- await import("../src/features/hub/lib/network.ts");
+const {
+ isHuggingFaceOffline,
+ markRemoteNetworkOffline,
+ clearRemoteBackoff,
+ fetchWithTimeout,
+} = await import("../src/features/hub/lib/network.ts");
test("before /api/health answers, both endpoints are the official ones", () => {
resetHfEndpoints();
@@ -149,3 +154,86 @@ test("the Hub offline backoff keys on the configured mirror, not huggingface.co"
assert.equal(isHuggingFaceOffline(), false);
resetHfEndpoints();
});
+
+test("the ModelScope adapter gets the Unsloth session, never the Hugging Face token", async () => {
+ const adapter = "http://127.0.0.1:8888/api/hub/modelscope";
+ const sent: (string | null)[] = [];
+ let session: string | null = "expired";
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = (async (_input: unknown, init?: RequestInit) => {
+ const auth = new Headers(init?.headers).get("authorization");
+ sent.push(auth);
+ return new Response("[]", { status: auth === "Bearer expired" ? 401 : 200 });
+ }) as typeof fetch;
+ Object.defineProperty(globalThis, "localStorage", {
+ configurable: true,
+ value: { getItem: (key: string) => (key === "unsloth_auth_token" ? session : null) },
+ });
+ setHubSessionRefresh(async () => {
+ session = "fresh";
+ return true;
+ });
+ const hfToken = { headers: { Authorization: "Bearer hf_x" } };
+ try {
+ setHfEndpoints(adapter, undefined, "modelscope");
+ const answer = await fetchWithTimeout(`${adapter}/api/models`, hfToken);
+ await fetchWithTimeout("https://cdn.example.com/a.png", hfToken);
+ setHfEndpoints("https://huggingface.co", undefined, "huggingface");
+ await fetchWithTimeout("https://huggingface.co/api/models", hfToken);
+ await fetchWithTimeout(`${adapter}/api/models/a/b`, hfToken);
+ session = null;
+ await fetchWithTimeout(`${adapter}/api/models/a/c`, hfToken);
+ assert.equal(answer.status, 200);
+ } finally {
+ globalThis.fetch = realFetch;
+ Reflect.deleteProperty(globalThis, "localStorage");
+ setHubSessionRefresh(async () => false);
+ resetHfEndpoints();
+ }
+ assert.deepEqual(sent, [
+ "Bearer expired",
+ "Bearer fresh",
+ "Bearer hf_x",
+ "Bearer hf_x",
+ "Bearer fresh",
+ null,
+ ]);
+});
+
+test("a relay to a custom endpoint gets the session, and the Hugging Face token beside it", async () => {
+ const relay = "http://127.0.0.1:8888/api/hub/proxy";
+ const datasets = "http://127.0.0.1:8888/api/hub/datasets-server-proxy";
+ const sent: (string | null)[][] = [];
+ let refreshes = 0;
+ const realFetch = globalThis.fetch;
+ globalThis.fetch = (async (input: string, init?: RequestInit) => {
+ const headers = new Headers(init?.headers);
+ sent.push([headers.get("authorization"), headers.get("x-hf-authorization")]);
+ const gated = input.includes("gated");
+ const stale = input.includes("stale") && headers.get("authorization") === "Bearer session";
+ return new Response("[]", { status: gated || stale ? 401 : 200, headers: gated ? { "X-Hub-Upstream": "1" } : {} });
+ }) as typeof fetch;
+ let session = "session";
+ Object.defineProperty(globalThis, "localStorage", { configurable: true, value: { getItem: () => session } });
+ setHubSessionRefresh(async () => Boolean((session = `fresh${++refreshes}`)));
+ const hfToken = { headers: { Authorization: "Bearer hf_x" } };
+ try {
+ setHfEndpoints(relay, datasets, "huggingface", { endpoint: true, datasetsServer: true });
+ await fetchWithTimeout(`${relay}/api/models`, hfToken);
+ await fetchWithTimeout(`${datasets}/splits?dataset=a/b`, {});
+ assert.equal((await fetchWithTimeout(`${relay}/api/models/org/gated`, hfToken)).status, 401);
+ assert.equal((await fetchWithTimeout(`${relay}/api/models/stale`, hfToken)).status, 200);
+ setHfEndpoints("https://huggingface.co", "https://datasets-server.huggingface.co");
+ await fetchWithTimeout(`${datasets}/splits?dataset=a/c`, hfToken);
+ await fetchWithTimeout("https://huggingface.co/api/models", hfToken);
+ } finally {
+ globalThis.fetch = realFetch;
+ Reflect.deleteProperty(globalThis, "localStorage");
+ setHubSessionRefresh(async () => false);
+ resetHfEndpoints();
+ }
+ assert.equal(refreshes, 1);
+ const both = ["Bearer session", "Bearer hf_x"];
+ const fresh = ["Bearer fresh1", "Bearer hf_x"];
+ assert.deepEqual(sent, [both, ["Bearer session", null], both, both, fresh, fresh, ["Bearer hf_x", null]]);
+});
diff --git a/studio/frontend/tests/hub-network-availability.test.ts b/studio/frontend/tests/hub-network-availability.test.ts
index e81efd43f51..4fa93e7de87 100644
--- a/studio/frontend/tests/hub-network-availability.test.ts
+++ b/studio/frontend/tests/hub-network-availability.test.ts
@@ -8,7 +8,7 @@ import test from "node:test";
// network.ts resolves the Hub origin through "@/lib/hf-endpoint", the way vite
// resolves the alias. Bare node does not, so the import has to go through the
// resolver, which in turn means a dynamic import after register().
-register("./bundler-resolver.mjs", import.meta.url);
+register("./store-stub-resolver.mjs", import.meta.url);
const {
classifyFetchFailure,
clearRemoteBackoff,
@@ -23,6 +23,9 @@ const {
markRemoteNetworkOnline,
sanitizeHubErrorMessage,
} = await import("../src/features/hub/lib/network.ts");
+const { resetHfEndpoints, setHfEndpoints } = await import("../src/lib/hf-endpoint.ts");
+const { updateHubSource } = await import("../src/features/settings/api/hub-settings.ts");
+const { setAuthFetchHandler } = await import("./helpers/store-stubs/auth.ts");
import { readSrcAsync } from "./helpers/kit.ts";
@@ -255,6 +258,36 @@ test("a connectivity failure still does", async () => {
}
});
+test("a relay that cannot reach its endpoint counts as the endpoint unreachable", async () => {
+ reset();
+ const relay = "http://127.0.0.1:8888/api/hub/proxy/t";
+ setHfEndpoints(relay, null, "huggingface", { endpoint: true });
+ const original = globalThis.fetch;
+ let upstream = false;
+ globalThis.fetch = (async () =>
+ new Response("{}", { status: 502, headers: upstream ? { "X-Hub-Upstream": "1" } : {} })) as typeof fetch;
+ try {
+ await assert.rejects(fetchWithTimeout(`${relay}/api/models`, {}, 1_000));
+ assert.equal(getLastHubFailure("http://127.0.0.1:8888")?.kind, "network-opaque");
+ setAuthFetchHandler(() =>
+ Response.json({
+ hf_endpoint: "",
+ datasets_server_follows_endpoint: false,
+ source: "modelscope",
+ active_source: "modelscope",
+ }),
+ );
+ await updateHubSource("modelscope");
+ assert.equal(isRemoteNetworkOffline("http://127.0.0.1:8888"), false);
+ upstream = true;
+ assert.equal((await fetchWithTimeout(`${relay}/api/models`, {}, 1_000)).status, 502);
+ } finally {
+ globalThis.fetch = original;
+ setAuthFetchHandler(null);
+ resetHfEndpoints();
+ }
+});
+
test("a successful response clears a prior failure", async () => {
reset();
markRemoteNetworkOffline(HF, 60_000, {
diff --git a/studio/frontend/tests/recommended-catalog-seeds.test.ts b/studio/frontend/tests/recommended-catalog-seeds.test.ts
index f34ff099039..1172ee60c1d 100644
--- a/studio/frontend/tests/recommended-catalog-seeds.test.ts
+++ b/studio/frontend/tests/recommended-catalog-seeds.test.ts
@@ -17,6 +17,7 @@ import {
hfModelFitsDevice,
loadScopedGpu,
orderRecommendedRows,
+ recommendedEmptyState,
searchRowFitsDevice,
} from "../src/features/model-picker/components/model-selector/recommended-fit.ts";
@@ -653,3 +654,12 @@ test("a pinToTop family leads Recommended whatever the listing sort, both artifa
assert.deepEqual(order(), [qwen2512, zImageTurbo, qwen21, qwen21Gguf]);
assert.deepEqual(order(pinnedFamilies), [qwen21, qwen21Gguf, qwen2512, zImageTurbo]);
});
+
+test("an empty Recommended list names a failed search or unreachable hub, not an empty catalog", () => {
+ const state = (isLoading: boolean, error: string | null, hubPhase: "available" | "probing" | "unavailable") =>
+ recommendedEmptyState({ isLoading, error, hubPhase });
+ assert.deepEqual(
+ [state(false, null, "available"), state(false, "Failed to fetch", "available"), state(false, null, "unavailable"), state(false, null, "probing"), state(true, "Failed to fetch", "unavailable")],
+ ["empty", "failed", "failed", "failed", "loading"],
+ );
+});
diff --git a/studio/frontend/tests/settings-search.test.ts b/studio/frontend/tests/settings-search.test.ts
index 0feb2892d68..8e31194d656 100644
--- a/studio/frontend/tests/settings-search.test.ts
+++ b/studio/frontend/tests/settings-search.test.ts
@@ -7,6 +7,7 @@ import test from "node:test";
import {
SETTINGS_SEARCH_KEYWORDS,
createSettingsSearchIndex,
+ renderedSearchEntries,
} from "../src/features/settings/settings-search.ts";
import { en } from "../src/i18n/locales/en.ts";
@@ -112,3 +113,12 @@ test("close to tray is searchable only on supported desktops", () => {
assert.ok(!mac.general.includes(CLOSE_TO_TRAY_ENTRY));
assert.ok(!browser.general.includes(CLOSE_TO_TRAY_ENTRY));
});
+
+test("the endpoint rows are searchable only while Hugging Face serves, as they render", () => {
+ const index = createSettingsSearchIndex({ desktop: false, closeToTray: false });
+ const endpoint = ["settings.general.hub.endpoint", "settings.general.hub.datasetsServer"] as const;
+ const modelScope = renderedSearchEntries(index, "general", "modelscope");
+ const huggingFace = renderedSearchEntries(index, "general", "huggingface");
+ assert.deepEqual(endpoint.map((key) => [huggingFace.includes(key), modelScope.includes(key)]), [[true, false], [true, false]]);
+ assert.ok(modelScope.includes("settings.general.hub.source"));
+});