def patch_hf_hub_http_backoff()

in ultravox/utils/monkey_patches.py [0:0]


def patch_hf_hub_http_backoff():
    """
    Monkey patch the huggingface_hub http_backoff implementation to include the ChunkedEncodingError exception.
    """
    original_http_backoff = huggingface_hub.hf_file_system.http_backoff

    @tenacity.retry(
        stop=tenacity.stop_after_attempt(3),
        wait=tenacity.wait_exponential(multiplier=1, min=4, max=10),
        retry=tenacity.retry_if_exception_type(Exception),
        before_sleep=tenacity.before_sleep_log(logger, logging.INFO),
    )
    def http_backoff(
        method: huggingface_hub.utils._typing.HTTP_METHOD_T,
        url: str,
        *,
        max_retries: int = 10,
        retry_on_exceptions: tuple = (
            requests.Timeout,
            requests.ConnectionError,
            requests.exceptions.ChunkedEncodingError,
        ),
        **kwargs,
    ) -> requests.Response:
        return original_http_backoff(
            method=method,
            url=url,
            max_retries=max_retries,
            retry_on_exceptions=retry_on_exceptions,
            **kwargs,
        )

    huggingface_hub.hf_file_system.http_backoff = http_backoff
    logger.info(
        "Applied retry patch to huggingface_hub http_backoff with ChunkedEncodingError support"
    )