in src/huggingface_hub/hf_file_system.py [0:0]
def read(self, length: int = -1):
read_args = (length,) if length >= 0 else ()
if self.response is None:
url = hf_hub_url(
repo_id=self.resolved_path.repo_id,
revision=self.resolved_path.revision,
filename=self.resolved_path.path_in_repo,
repo_type=self.resolved_path.repo_type,
endpoint=self.fs.endpoint,
)
self.response = http_backoff(
"GET",
url,
headers=self.fs._api._build_hf_headers(),
retry_on_status_codes=(500, 502, 503, 504),
stream=True,
timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
)
hf_raise_for_status(self.response)
try:
out = self.response.raw.read(*read_args)
except Exception:
self.response.close()
# Retry by recreating the connection
url = hf_hub_url(
repo_id=self.resolved_path.repo_id,
revision=self.resolved_path.revision,
filename=self.resolved_path.path_in_repo,
repo_type=self.resolved_path.repo_type,
endpoint=self.fs.endpoint,
)
self.response = http_backoff(
"GET",
url,
headers={"Range": "bytes=%d-" % self.loc, **self.fs._api._build_hf_headers()},
retry_on_status_codes=(500, 502, 503, 504),
stream=True,
timeout=constants.HF_HUB_DOWNLOAD_TIMEOUT,
)
hf_raise_for_status(self.response)
try:
out = self.response.raw.read(*read_args)
except Exception:
self.response.close()
raise
self.loc += len(out)
return out