@@ -224,9 +224,7 @@ def get_data_by_range(cls, url: str, start: int, size: int = -1) -> bytes:
224
224
if not parsed_url .scheme or not parsed_url .netloc :
225
225
raise ValueError (f"Invalid URL: { url } " )
226
226
227
- headers = {}
228
- if os .environ .get ("HF_TOKEN" ):
229
- headers ["Authorization" ] = f"Bearer { os .environ ['HF_TOKEN' ]} "
227
+ headers = cls ._get_request_headers ()
230
228
if size > - 1 :
231
229
headers ["Range" ] = f"bytes={ start } -{ start + size } "
232
230
response = requests .get (url , allow_redirects = True , headers = headers )
@@ -249,11 +247,18 @@ def check_file_exist(cls, url: str) -> bool:
249
247
raise ValueError (f"Invalid URL: { url } " )
250
248
251
249
try :
252
- headers = {"Range" : "bytes=0-0" }
253
- if os .environ .get ("HF_TOKEN" ):
254
- headers ["Authorization" ] = f"Bearer { os .environ ['HF_TOKEN' ]} "
250
+ headers = cls ._get_request_headers ()
251
+ headers ["Range" ] = "bytes=0-0"
255
252
response = requests .head (url , allow_redirects = True , headers = headers )
256
253
# Success (2xx) or redirect (3xx)
257
254
return 200 <= response .status_code < 400
258
255
except requests .RequestException :
259
256
return False
257
+
258
+ @classmethod
259
+ def _get_request_headers (cls ) -> dict [str , str ]:
260
+ """Prepare common headers for requests."""
261
+ headers = {"User-Agent" : "convert_hf_to_gguf" }
262
+ if os .environ .get ("HF_TOKEN" ):
263
+ headers ["Authorization" ] = f"Bearer { os .environ ['HF_TOKEN' ]} "
264
+ return headers
0 commit comments