Skip to content

Instantly share code, notes, and snippets.

@eric-czech
Created November 24, 2025 13:57
Show Gist options
  • Select an option

  • Save eric-czech/c95c0ebdd087e06314598ff48aa9c4e6 to your computer and use it in GitHub Desktop.

Select an option

Save eric-czech/c95c0ebdd087e06314598ff48aa9c4e6 to your computer and use it in GitHub Desktop.
Example for streaming HTTP requests with retries
import sys, os, tempfile
from huggingface_hub.utils import http_stream_backoff
url = "https://cdn.mos.cms.futurecdn.net/v2/t:0,l:160,cw:960,ch:720,q:80,w:960/FaWKMJQnr2PFcYCmEyfiTm.jpg"
ext = os.path.splitext(url)[1]
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as f:
with http_stream_backoff(
method="GET",
url=url,
max_retries=5,
max_wait_time=60,
retry_on_status_codes=(500, 502, 503, 504),
headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"},
) as response:
for chunk in response.iter_bytes(): f.write(chunk)
print(f"URL {url!r} downloaded to {f.name!r}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment