diff --git a/examples/instruct_pix2pix/train_instruct_pix2pix.py b/examples/instruct_pix2pix/train_instruct_pix2pix.py index d1caf281a2c5..028257c98857 100644 --- a/examples/instruct_pix2pix/train_instruct_pix2pix.py +++ b/examples/instruct_pix2pix/train_instruct_pix2pix.py @@ -49,6 +49,7 @@ from diffusers.optimization import get_scheduler from diffusers.training_utils import EMAModel from diffusers.utils import check_min_version, deprecate, is_wandb_available +from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.torch_utils import is_compiled_module @@ -418,7 +419,7 @@ def convert_to_np(image, resolution): def download_image(url): - image = PIL.Image.open(requests.get(url, stream=True).raw) + image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw) image = PIL.ImageOps.exif_transpose(image) image = image.convert("RGB") return image diff --git a/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py b/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py index 070cdad15564..1d9203be7e01 100644 --- a/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py +++ b/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py @@ -54,6 +54,7 @@ from diffusers.optimization import get_scheduler from diffusers.training_utils import EMAModel, cast_training_params from diffusers.utils import check_min_version, convert_state_dict_to_diffusers, deprecate, is_wandb_available +from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card from diffusers.utils.import_utils import is_xformers_available from diffusers.utils.torch_utils import is_compiled_module @@ -475,7 +476,7 @@ def convert_to_np(image, resolution): def download_image(url): - image = PIL.Image.open(requests.get(url, stream=True).raw) + image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw) image = PIL.ImageOps.exif_transpose(image) image = image.convert("RGB") return image diff --git a/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py b/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py index 26b56a21e865..c9efcffa5bb8 100644 --- a/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py +++ b/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py @@ -59,6 +59,7 @@ UnCLIPScheduler, ) from diffusers.utils import is_accelerate_available, logging +from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT if is_accelerate_available(): @@ -1435,7 +1436,7 @@ def download_from_original_stable_diffusion_ckpt( config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml" if config_url is not None: - original_config_file = BytesIO(requests.get(config_url).content) + original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content) else: with open(original_config_file, "r") as f: original_config_file = f.read() diff --git a/scripts/convert_dance_diffusion_to_diffusers.py b/scripts/convert_dance_diffusion_to_diffusers.py index ce69bfe2bfc8..f9caa50dfc9b 100755 --- a/scripts/convert_dance_diffusion_to_diffusers.py +++ b/scripts/convert_dance_diffusion_to_diffusers.py @@ -11,6 +11,7 @@ from torch import nn from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel +from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT MODELS_MAP = { @@ -74,7 +75,7 @@ def __init__(self, global_args): def download(model_name): url = MODELS_MAP[model_name]["url"] - r = requests.get(url, stream=True) + r = requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT) local_filename = f"./{model_name}.ckpt" with open(local_filename, "wb") as fp: diff --git a/scripts/convert_vae_pt_to_diffusers.py b/scripts/convert_vae_pt_to_diffusers.py index a4f967c94fa6..13ceca40f34f 100644 --- a/scripts/convert_vae_pt_to_diffusers.py +++ b/scripts/convert_vae_pt_to_diffusers.py @@ -13,6 +13,7 @@ renew_vae_attention_paths, renew_vae_resnet_paths, ) +from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT def custom_convert_ldm_vae_checkpoint(checkpoint, config): @@ -122,7 +123,8 @@ def vae_pt_to_vae_diffuser( ): # Only support V1 r = requests.get( - " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml" + " https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml", + timeout=DIFFUSERS_REQUEST_TIMEOUT, ) io_obj = io.BytesIO(r.content) diff --git a/src/diffusers/loaders/single_file_utils.py b/src/diffusers/loaders/single_file_utils.py index 42aee4a84822..a93f2307bbf3 100644 --- a/src/diffusers/loaders/single_file_utils.py +++ b/src/diffusers/loaders/single_file_utils.py @@ -44,6 +44,7 @@ is_transformers_available, logging, ) +from ..utils.constants import DIFFUSERS_REQUEST_TIMEOUT from ..utils.hub_utils import _get_model_file @@ -443,7 +444,7 @@ def fetch_original_config(original_config_file, local_files_only=False): "Please provide a valid local file path." ) - original_config_file = BytesIO(requests.get(original_config_file).content) + original_config_file = BytesIO(requests.get(original_config_file, timeout=DIFFUSERS_REQUEST_TIMEOUT).content) else: raise ValueError("Invalid `original_config_file` provided. Please set it to a valid file path or URL.") diff --git a/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py b/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py index 4cc4eabd4a40..d337aba8e9d5 100644 --- a/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +++ b/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py @@ -52,6 +52,7 @@ UnCLIPScheduler, ) from ...utils import is_accelerate_available, logging +from ...utils.constants import DIFFUSERS_REQUEST_TIMEOUT from ..latent_diffusion.pipeline_latent_diffusion import LDMBertConfig, LDMBertModel from ..paint_by_example import PaintByExampleImageEncoder from ..pipeline_utils import DiffusionPipeline @@ -1324,7 +1325,7 @@ def download_from_original_stable_diffusion_ckpt( config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml" if config_url is not None: - original_config_file = BytesIO(requests.get(config_url).content) + original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content) else: with open(original_config_file, "r") as f: original_config_file = f.read() diff --git a/src/diffusers/utils/constants.py b/src/diffusers/utils/constants.py index fa12318f4714..7c04287d33ed 100644 --- a/src/diffusers/utils/constants.py +++ b/src/diffusers/utils/constants.py @@ -40,6 +40,7 @@ DIFFUSERS_DYNAMIC_MODULE_NAME = "diffusers_modules" HF_MODULES_CACHE = os.getenv("HF_MODULES_CACHE", os.path.join(HF_HOME, "modules")) DEPRECATED_REVISION_ARGS = ["fp16", "non-ema"] +DIFFUSERS_REQUEST_TIMEOUT = 60 # Below should be `True` if the current version of `peft` and `transformers` are compatible with # PEFT backend. Will automatically fall back to PEFT backend if the correct versions of the libraries are diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index fd66aaa4da6e..dd23ae73c861 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -7,6 +7,7 @@ import PIL.ImageOps import requests +from .constants import DIFFUSERS_REQUEST_TIMEOUT from .import_utils import BACKENDS_MAPPING, is_imageio_available @@ -29,7 +30,7 @@ def load_image( """ if isinstance(image, str): if image.startswith("http://") or image.startswith("https://"): - image = PIL.Image.open(requests.get(image, stream=True).raw) + image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw) elif os.path.isfile(image): image = PIL.Image.open(image) else: diff --git a/src/diffusers/utils/testing_utils.py b/src/diffusers/utils/testing_utils.py index 137420945340..6c16a08a4a99 100644 --- a/src/diffusers/utils/testing_utils.py +++ b/src/diffusers/utils/testing_utils.py @@ -26,6 +26,7 @@ from numpy.linalg import norm from packaging import version +from .constants import DIFFUSERS_REQUEST_TIMEOUT from .import_utils import ( BACKENDS_MAPPING, is_accelerate_available, @@ -594,7 +595,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) - # local_path can be passed to correct images of tests return Path(local_path, arry.split("/")[-5], arry.split("/")[-2], arry.split("/")[-1]).as_posix() elif arry.startswith("http://") or arry.startswith("https://"): - response = requests.get(arry) + response = requests.get(arry, timeout=DIFFUSERS_REQUEST_TIMEOUT) response.raise_for_status() arry = np.load(BytesIO(response.content)) elif os.path.isfile(arry): @@ -615,7 +616,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) - def load_pt(url: str, map_location: str): - response = requests.get(url) + response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT) response.raise_for_status() arry = torch.load(BytesIO(response.content), map_location=map_location) return arry @@ -634,7 +635,7 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image: """ if isinstance(image, str): if image.startswith("http://") or image.startswith("https://"): - image = PIL.Image.open(requests.get(image, stream=True).raw) + image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw) elif os.path.isfile(image): image = PIL.Image.open(image) else: diff --git a/utils/fetch_latest_release_branch.py b/utils/fetch_latest_release_branch.py index 9bf578a5f58e..f0602d5b29a8 100644 --- a/utils/fetch_latest_release_branch.py +++ b/utils/fetch_latest_release_branch.py @@ -13,9 +13,12 @@ # See the License for the specific language governing permissions and # limitations under the License. + import requests from packaging.version import parse +from ..src.diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT + # GitHub repository details USER = "huggingface" @@ -27,7 +30,11 @@ def fetch_all_branches(user, repo): page = 1 # Start from first page while True: # Make a request to the GitHub API for the branches - response = requests.get(f"https://api.github.com/repos/{user}/{repo}/branches", params={"page": page}) + response = requests.get( + f"https://api.github.com/repos/{user}/{repo}/branches", + params={"page": page}, + timeout=DIFFUSERS_REQUEST_TIMEOUT, + ) # Check if the request was successful if response.status_code == 200: diff --git a/utils/notify_slack_about_release.py b/utils/notify_slack_about_release.py index a67dd8bd0685..9d4d11f4508f 100644 --- a/utils/notify_slack_about_release.py +++ b/utils/notify_slack_about_release.py @@ -17,6 +17,8 @@ import requests +from ..src.diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT + # Configuration LIBRARY_NAME = "diffusers" @@ -26,7 +28,7 @@ def check_pypi_for_latest_release(library_name): """Check PyPI for the latest release of the library.""" - response = requests.get(f"https://pypi.org/pypi/{library_name}/json") + response = requests.get(f"https://pypi.org/pypi/{library_name}/json", timeout=DIFFUSERS_REQUEST_TIMEOUT) if response.status_code == 200: data = response.json() return data["info"]["version"] @@ -38,7 +40,7 @@ def check_pypi_for_latest_release(library_name): def get_github_release_info(github_repo): """Fetch the latest release info from GitHub.""" url = f"https://api.github.com/repos/{github_repo}/releases/latest" - response = requests.get(url) + response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT) if response.status_code == 200: data = response.json()