Spaces:
Running on Zero
Running on Zero
Read gated status from Hub metadata fallback
Browse files- backend_router.py +21 -0
backend_router.py
CHANGED
|
@@ -14,7 +14,9 @@ import re
|
|
| 14 |
from dataclasses import dataclass, field
|
| 15 |
from pathlib import Path
|
| 16 |
from typing import Any, Iterable
|
|
|
|
| 17 |
|
|
|
|
| 18 |
from huggingface_hub import HfApi, hf_hub_download
|
| 19 |
|
| 20 |
|
|
@@ -299,6 +301,25 @@ class BackendRouter:
|
|
| 299 |
# Keep compatibility with lightweight/fake API clients and older
|
| 300 |
# Hub clients that do not expose files_metadata.
|
| 301 |
files = list(self.api.list_repo_files(repo_id=model_id, repo_type="model"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
gguf_files = [name for name in files if _is_gguf(name)]
|
| 303 |
config: dict[str, Any] = {}
|
| 304 |
|
|
|
|
| 14 |
from dataclasses import dataclass, field
|
| 15 |
from pathlib import Path
|
| 16 |
from typing import Any, Iterable
|
| 17 |
+
from urllib.parse import quote
|
| 18 |
|
| 19 |
+
import requests
|
| 20 |
from huggingface_hub import HfApi, hf_hub_download
|
| 21 |
|
| 22 |
|
|
|
|
| 301 |
# Keep compatibility with lightweight/fake API clients and older
|
| 302 |
# Hub clients that do not expose files_metadata.
|
| 303 |
files = list(self.api.list_repo_files(repo_id=model_id, repo_type="model"))
|
| 304 |
+
|
| 305 |
+
# Older Hub clients may omit the `gated` field from ModelInfo even
|
| 306 |
+
# though the REST endpoint exposes it. This is a small metadata call;
|
| 307 |
+
# it never downloads weights and lets the UI explain a 401 in advance.
|
| 308 |
+
if not gated:
|
| 309 |
+
try:
|
| 310 |
+
headers = {}
|
| 311 |
+
token = os.getenv("HF_TOKEN") or ""
|
| 312 |
+
if token:
|
| 313 |
+
headers["Authorization"] = f"Bearer {token}"
|
| 314 |
+
response = requests.get(
|
| 315 |
+
f"https://huggingface.co/api/models/{quote(model_id, safe='/')}",
|
| 316 |
+
headers=headers,
|
| 317 |
+
timeout=15,
|
| 318 |
+
)
|
| 319 |
+
if response.ok:
|
| 320 |
+
gated = response.json().get("gated", False) or False
|
| 321 |
+
except Exception:
|
| 322 |
+
pass
|
| 323 |
gguf_files = [name for name in files if _is_gguf(name)]
|
| 324 |
config: dict[str, Any] = {}
|
| 325 |
|