@@ -404,6 +404,7 @@ def http_get(
404404 expected_size : Optional [int ] = None ,
405405 displayed_filename : Optional [str ] = None ,
406406 _nb_retries : int = 5 ,
407+ _tqdm_bar : Optional [tqdm ] = None ,
407408) -> None :
408409 """
409410 Download a remote file. Do not gobble up errors, and will return errors tailored to the Hugging Face Hub.
@@ -483,84 +484,90 @@ def http_get(
483484 )
484485
485486 # Stream file to buffer
486- with tqdm (
487- unit = "B" ,
488- unit_scale = True ,
489- total = total ,
490- initial = resume_size ,
491- desc = displayed_filename ,
492- disable = True if (logger .getEffectiveLevel () == logging .NOTSET ) else None ,
493- # ^ set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
494- # see https://github.com/huggingface/huggingface_hub/pull/2000
495- ) as progress :
496- if hf_transfer and total is not None and total > 5 * DOWNLOAD_CHUNK_SIZE :
497- supports_callback = "callback" in inspect .signature (hf_transfer .download ).parameters
498- if not supports_callback :
499- warnings .warn (
500- "You are using an outdated version of `hf_transfer`. "
501- "Consider upgrading to latest version to enable progress bars "
502- "using `pip install -U hf_transfer`."
503- )
504- try :
505- hf_transfer .download (
506- url = url ,
507- filename = temp_file .name ,
508- max_files = HF_TRANSFER_CONCURRENCY ,
509- chunk_size = DOWNLOAD_CHUNK_SIZE ,
510- headers = headers ,
511- parallel_failures = 3 ,
512- max_retries = 5 ,
513- ** ({"callback" : progress .update } if supports_callback else {}),
514- )
515- except Exception as e :
516- raise RuntimeError (
517- "An error occurred while downloading using `hf_transfer`. Consider"
518- " disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling."
519- ) from e
520- if not supports_callback :
521- progress .update (total )
522- if expected_size is not None and expected_size != os .path .getsize (temp_file .name ):
523- raise EnvironmentError (
524- consistency_error_message .format (
525- actual_size = os .path .getsize (temp_file .name ),
526- )
527- )
528- return
529- new_resume_size = resume_size
487+ progress = _tqdm_bar
488+ if progress is None :
489+ progress = tqdm (
490+ unit = "B" ,
491+ unit_scale = True ,
492+ total = total ,
493+ initial = resume_size ,
494+ desc = displayed_filename ,
495+ disable = True if (logger .getEffectiveLevel () == logging .NOTSET ) else None ,
496+ # ^ set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
497+ # see https://github.com/huggingface/huggingface_hub/pull/2000
498+ )
499+
500+ if hf_transfer and total is not None and total > 5 * DOWNLOAD_CHUNK_SIZE :
501+ supports_callback = "callback" in inspect .signature (hf_transfer .download ).parameters
502+ if not supports_callback :
503+ warnings .warn (
504+ "You are using an outdated version of `hf_transfer`. "
505+ "Consider upgrading to latest version to enable progress bars "
506+ "using `pip install -U hf_transfer`."
507+ )
530508 try :
531- for chunk in r .iter_content (chunk_size = DOWNLOAD_CHUNK_SIZE ):
532- if chunk : # filter out keep-alive new chunks
533- progress .update (len (chunk ))
534- temp_file .write (chunk )
535- new_resume_size += len (chunk )
536- # Some data has been downloaded from the server so we reset the number of retries.
537- _nb_retries = 5
538- except (requests .ConnectionError , requests .ReadTimeout ) as e :
539- # If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely
540- # a transient error (network outage?). We log a warning message and try to resume the download a few times
541- # before giving up. Tre retry mechanism is basic but should be enough in most cases.
542- if _nb_retries <= 0 :
543- logger .warning ("Error while downloading from %s: %s\n Max retries exceeded." , url , str (e ))
544- raise
545- logger .warning ("Error while downloading from %s: %s\n Trying to resume download..." , url , str (e ))
546- time .sleep (1 )
547- reset_sessions () # In case of SSLError it's best to reset the shared requests.Session objects
548- return http_get (
509+ hf_transfer .download (
549510 url = url ,
550- temp_file = temp_file ,
551- proxies = proxies ,
552- resume_size = new_resume_size ,
553- headers = initial_headers ,
554- expected_size = expected_size ,
555- _nb_retries = _nb_retries - 1 ,
511+ filename = temp_file .name ,
512+ max_files = HF_TRANSFER_CONCURRENCY ,
513+ chunk_size = DOWNLOAD_CHUNK_SIZE ,
514+ headers = headers ,
515+ parallel_failures = 3 ,
516+ max_retries = 5 ,
517+ ** ({"callback" : progress .update } if supports_callback else {}),
556518 )
557-
558- if expected_size is not None and expected_size != temp_file .tell ():
519+ except Exception as e :
520+ raise RuntimeError (
521+ "An error occurred while downloading using `hf_transfer`. Consider"
522+ " disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling."
523+ ) from e
524+ if not supports_callback :
525+ progress .update (total )
526+ if expected_size is not None and expected_size != os .path .getsize (temp_file .name ):
559527 raise EnvironmentError (
560528 consistency_error_message .format (
561- actual_size = temp_file . tell ( ),
529+ actual_size = os . path . getsize ( temp_file . name ),
562530 )
563531 )
532+ return
533+ new_resume_size = resume_size
534+ try :
535+ for chunk in r .iter_content (chunk_size = DOWNLOAD_CHUNK_SIZE ):
536+ if chunk : # filter out keep-alive new chunks
537+ progress .update (len (chunk ))
538+ temp_file .write (chunk )
539+ new_resume_size += len (chunk )
540+ # Some data has been downloaded from the server so we reset the number of retries.
541+ _nb_retries = 5
542+ except (requests .ConnectionError , requests .ReadTimeout ) as e :
543+ # If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely
544+ # a transient error (network outage?). We log a warning message and try to resume the download a few times
545+ # before giving up. Tre retry mechanism is basic but should be enough in most cases.
546+ if _nb_retries <= 0 :
547+ logger .warning ("Error while downloading from %s: %s\n Max retries exceeded." , url , str (e ))
548+ raise
549+ logger .warning ("Error while downloading from %s: %s\n Trying to resume download..." , url , str (e ))
550+ time .sleep (1 )
551+ reset_sessions () # In case of SSLError it's best to reset the shared requests.Session objects
552+ return http_get (
553+ url = url ,
554+ temp_file = temp_file ,
555+ proxies = proxies ,
556+ resume_size = new_resume_size ,
557+ headers = initial_headers ,
558+ expected_size = expected_size ,
559+ _nb_retries = _nb_retries - 1 ,
560+ _tqdm_bar = _tqdm_bar ,
561+ )
562+
563+ progress .close ()
564+
565+ if expected_size is not None and expected_size != temp_file .tell ():
566+ raise EnvironmentError (
567+ consistency_error_message .format (
568+ actual_size = temp_file .tell (),
569+ )
570+ )
564571
565572
566573@validate_hf_hub_args
0 commit comments