From b584e3979dec9f54b9278bef4307d4abf44ac108 Mon Sep 17 00:00:00 2001 From: Xuan Son Nguyen Date: Tue, 8 Apr 2025 17:24:28 +0200 Subject: [PATCH] support HF_TOKEN --- convert_hf_to_gguf.py | 2 +- gguf-py/gguf/utility.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/convert_hf_to_gguf.py b/convert_hf_to_gguf.py index 008a41764a..72d2f7a815 100755 --- a/convert_hf_to_gguf.py +++ b/convert_hf_to_gguf.py @@ -5498,7 +5498,7 @@ def parse_args() -> argparse.Namespace: ) parser.add_argument( "--remote", action="store_true", - 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'", + 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.", ) args = parser.parse_args() diff --git a/gguf-py/gguf/utility.py b/gguf-py/gguf/utility.py index 5bae22e755..62ccc4f930 100644 --- a/gguf-py/gguf/utility.py +++ b/gguf-py/gguf/utility.py @@ -3,6 +3,7 @@ from __future__ import annotations from dataclasses import dataclass from typing import Literal +import os import json @@ -94,7 +95,7 @@ class SafetensorRemote: Example (one model has single safetensor file, the other has multiple): for model_id in ["ngxson/TEST-Tiny-Llama4", "Qwen/Qwen2.5-7B-Instruct"]: tensors = SafetensorRemote.get_list_tensors_hf_model(model_id) - print(json.dumps(tensors, indent=2)) + print(tensors) Example reading tensor data: tensors = SafetensorRemote.get_list_tensors_hf_model(model_id) @@ -223,8 +224,10 @@ class SafetensorRemote: raise ValueError(f"Invalid URL: {url}") headers = {} + if os.environ.get("HF_TOKEN"): + headers["Authorization"] = f"Bearer {os.environ['HF_TOKEN']}" if size > -1: - headers = {"Range": f"bytes={start}-{start + size}"} + headers["Range"] = f"bytes={start}-{start + size}" response = requests.get(url, allow_redirects=True, headers=headers) response.raise_for_status() @@ -246,6 +249,8 @@ class SafetensorRemote: try: headers = {"Range": "bytes=0-0"} + if os.environ.get("HF_TOKEN"): + headers["Authorization"] = f"Bearer {os.environ['HF_TOKEN']}" response = requests.head(url, allow_redirects=True, headers=headers) # Success (2xx) or redirect (3xx) return 200 <= response.status_code < 400