Skip to content

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

Studio: load downloaded models with fewer Hugging Face requests (#15844)

Nilay8c3add52bf27
diff --git a/studio/backend/core/inference/worker.py b/studio/backend/core/inference/worker.py
index 23aca7aada9..8e9d5f91c2b 100644
--- a/studio/backend/core/inference/worker.py
+++ b/studio/backend/core/inference/worker.py
@@ -284,13 +284,15 @@ def _apply_worker_hf_token_environment(config: dict) -> None:
def _build_model_config(config: dict):
from utils.models import ModelConfig
+ from utils.models.model_config import shared_hub_model_info
model_name = config["model_name"]
- mc = ModelConfig.from_identifier(
- model_id = model_name,
- hf_token = _config_hf_token(config),
- gguf_variant = config.get("gguf_variant"),
- )
+ with shared_hub_model_info():
+ mc = ModelConfig.from_identifier(
+ model_id = model_name,
+ hf_token = _config_hf_token(config),
+ gguf_variant = config.get("gguf_variant"),
+ )
if not mc:
raise ValueError(f"Invalid model identifier: {model_name}")
return mc
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index 1f23c8e7b83..8815771a5cf 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -1673,6 +1673,7 @@ try:
detect_dspark_file,
detect_mtp_file,
load_model_defaults,
+ shared_hub_model_info,
)
from utils.native_path_leases import (
NativePathLeaseError,
@@ -1737,6 +1738,7 @@ except ImportError:
detect_dspark_file,
detect_mtp_file,
load_model_defaults,
+ shared_hub_model_info,
)
from utils.native_path_leases import (
NativePathLeaseError,
@@ -18736,7 +18738,7 @@ async def _load_model_impl(
# Offline guard skips soft-failed network checks before the worker starts; off-loop, as its DNS/HEAD can take seconds.
def _resolve_config():
- with _hf_offline_if_unreachable_for(model_identifier):
+ with _hf_offline_if_unreachable_for(model_identifier), shared_hub_model_info():
return ModelConfig.from_identifier(
model_id = model_identifier,
hf_token = request.hf_token,
@@ -19892,7 +19894,7 @@ async def validate_model(
_caller_is_owner = _owner_session(fastapi_request)
def _resolve_config():
- with _hf_offline_if_unreachable_for(model_identifier):
+ with _hf_offline_if_unreachable_for(model_identifier), shared_hub_model_info():
return ModelConfig.from_identifier(
model_id = model_identifier,
hf_token = request.hf_token,
diff --git a/studio/backend/tests/test_offline_gguf_cache_fallback.py b/studio/backend/tests/test_offline_gguf_cache_fallback.py
index 19ecd491118..04e74875aa2 100644
--- a/studio/backend/tests/test_offline_gguf_cache_fallback.py
+++ b/studio/backend/tests/test_offline_gguf_cache_fallback.py
@@ -1112,6 +1112,57 @@ class TestSharedHubModelInfo:
assert len(hub.calls) == 1
assert pinned_inside == [True]
+ @staticmethod
+ def _two_probe_from_identifier(
+ cls,
+ model_id,
+ hf_token = None,
+ **_kwargs,
+ ):
+ _hub_model_info(model_id, hf_token)
+ _hub_model_info(model_id, hf_token, files_metadata = True)
+ return None
+
+ @pytest.mark.parametrize("route", ["validate", "load"])
+ def test_validate_and_load_read_the_hub_once(self, hub, monkeypatch, route):
+ import asyncio
+
+ import core.inference.llama_cpp as llama_cpp
+ import routes.inference as inf
+ from models.inference import LoadRequest, ValidateModelRequest
+
+ monkeypatch.setattr(
+ inf.ModelConfig, "from_identifier", classmethod(self._two_probe_from_identifier)
+ )
+ monkeypatch.setattr(inf, "_owner_session", lambda _request: True)
+ monkeypatch.setattr(
+ inf, "_hf_offline_if_unreachable_for", lambda *_a: contextlib.nullcontext()
+ )
+ monkeypatch.setattr(
+ llama_cpp, "_hf_offline_if_unreachable_for", lambda *_a: contextlib.nullcontext()
+ )
+
+ if route == "validate":
+ call = inf.validate_model(ValidateModelRequest(model_path = "org/repo"), None, "owner")
+ else:
+ call = inf._load_model_impl(LoadRequest(model_path = "org/repo"), None, "owner")
+ with pytest.raises(inf.HTTPException):
+ asyncio.run(call)
+
+ assert len(hub.calls) == 1
+
+ def test_the_inference_worker_reads_the_hub_once(self, hub, monkeypatch):
+ from core.inference import worker
+
+ monkeypatch.setattr(
+ mc.ModelConfig, "from_identifier", classmethod(self._two_probe_from_identifier)
+ )
+
+ with pytest.raises(ValueError):
+ worker._build_model_config({"model_name": "org/repo"})
+
+ assert len(hub.calls) == 1
+
class _DnsState:
"""Toggles resolver failure. The probe uses ``getaddrinfo`` (IPv4 + IPv6), so patch