Skip to content

Commit b584e39

Browse files
committed
support HF_TOKEN
1 parent 4f65762 commit b584e39

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

convert_hf_to_gguf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5498,7 +5498,7 @@ def parse_args() -> argparse.Namespace:
54985498
)
54995499
parser.add_argument(
55005500
"--remote", action="store_true",
5501-
help="(Experimental) Read safetensors file remotely without downloading to disk. Config and tokenizer files will still be downloaded. To use this feature, you need to specify Hugging Face model repo name instead of a local directory. For example: 'HuggingFaceTB/SmolLM2-1.7B-Instruct'",
5501+
help="(Experimental) Read safetensors file remotely without downloading to disk. Config and tokenizer files will still be downloaded. To use this feature, you need to specify Hugging Face model repo name instead of a local directory. For example: 'HuggingFaceTB/SmolLM2-1.7B-Instruct'. Note: To access gated repo, set HF_TOKEN environment variable to your Hugging Face token.",
55025502
)
55035503

55045504
args = parser.parse_args()

gguf-py/gguf/utility.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from dataclasses import dataclass
44
from typing import Literal
55

6+
import os
67
import json
78

89

@@ -94,7 +95,7 @@ class SafetensorRemote:
9495
Example (one model has single safetensor file, the other has multiple):
9596
for model_id in ["ngxson/TEST-Tiny-Llama4", "Qwen/Qwen2.5-7B-Instruct"]:
9697
tensors = SafetensorRemote.get_list_tensors_hf_model(model_id)
97-
print(json.dumps(tensors, indent=2))
98+
print(tensors)
9899
99100
Example reading tensor data:
100101
tensors = SafetensorRemote.get_list_tensors_hf_model(model_id)
@@ -223,8 +224,10 @@ def get_data_by_range(cls, url: str, start: int, size: int = -1) -> bytes:
223224
raise ValueError(f"Invalid URL: {url}")
224225

225226
headers = {}
227+
if os.environ.get("HF_TOKEN"):
228+
headers["Authorization"] = f"Bearer {os.environ['HF_TOKEN']}"
226229
if size > -1:
227-
headers = {"Range": f"bytes={start}-{start + size}"}
230+
headers["Range"] = f"bytes={start}-{start + size}"
228231
response = requests.get(url, allow_redirects=True, headers=headers)
229232
response.raise_for_status()
230233

@@ -246,6 +249,8 @@ def check_file_exist(cls, url: str) -> bool:
246249

247250
try:
248251
headers = {"Range": "bytes=0-0"}
252+
if os.environ.get("HF_TOKEN"):
253+
headers["Authorization"] = f"Bearer {os.environ['HF_TOKEN']}"
249254
response = requests.head(url, allow_redirects=True, headers=headers)
250255
# Success (2xx) or redirect (3xx)
251256
return 200 <= response.status_code < 400

0 commit comments

Comments
 (0)