@@ -224,9 +224,7 @@ def get_data_by_range(cls, url: str, start: int, size: int = -1) -> bytes:
224224 if not parsed_url .scheme or not parsed_url .netloc :
225225 raise ValueError (f"Invalid URL: { url } " )
226226
227- headers = {}
228- if os .environ .get ("HF_TOKEN" ):
229- headers ["Authorization" ] = f"Bearer { os .environ ['HF_TOKEN' ]} "
227+ headers = cls ._get_request_headers ()
230228 if size > - 1 :
231229 headers ["Range" ] = f"bytes={ start } -{ start + size } "
232230 response = requests .get (url , allow_redirects = True , headers = headers )
@@ -249,11 +247,18 @@ def check_file_exist(cls, url: str) -> bool:
249247 raise ValueError (f"Invalid URL: { url } " )
250248
251249 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"
255252 response = requests .head (url , allow_redirects = True , headers = headers )
256253 # Success (2xx) or redirect (3xx)
257254 return 200 <= response .status_code < 400
258255 except requests .RequestException :
259256 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