From f04a73f35b3ce208667738747c3bbc8a3f3e43a6 Mon Sep 17 00:00:00 2001 From: Sai-Suraj-27 Date: Mon, 22 Apr 2024 21:13:39 +0530 Subject: [PATCH] Fixed requests.get function call by adding timeout parameter. --- examples/instruct_pix2pix/train_instruct_pix2pix.py | 2 +- .../instructpix2pix_lora/train_instruct_pix2pix_lora.py | 2 +- .../convert_original_promptdiffusion_to_diffusers.py | 2 +- scripts/convert_dance_diffusion_to_diffusers.py | 2 +- scripts/convert_vae_pt_to_diffusers.py | 2 +- src/diffusers/loaders/single_file_utils.py | 4 ++-- .../pipelines/stable_diffusion/convert_from_ckpt.py | 2 +- src/diffusers/utils/loading_utils.py | 2 +- src/diffusers/utils/testing_utils.py | 6 +++--- utils/fetch_latest_release_branch.py | 2 +- utils/notify_slack_about_release.py | 4 ++-- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/examples/instruct_pix2pix/train_instruct_pix2pix.py b/examples/instruct_pix2pix/train_instruct_pix2pix.py index f1125a2919f0..db67ec3f8668 100644 --- a/examples/instruct_pix2pix/train_instruct_pix2pix.py +++ b/examples/instruct_pix2pix/train_instruct_pix2pix.py @@ -418,7 +418,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=10).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 997d448fa281..1d0d30b12c62 100644 --- a/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py +++ b/examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py @@ -378,7 +378,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=10).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 76b7b133ad70..d2a69e33df0c 100644 --- a/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py +++ b/examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py @@ -1435,7 +1435,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=10).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..e99b01a2576e 100755 --- a/scripts/convert_dance_diffusion_to_diffusers.py +++ b/scripts/convert_dance_diffusion_to_diffusers.py @@ -74,7 +74,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=10) 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..fb10a5da925d 100644 --- a/scripts/convert_vae_pt_to_diffusers.py +++ b/scripts/convert_vae_pt_to_diffusers.py @@ -122,7 +122,7 @@ 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=10 ) 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 5b01b8da2b1f..a8e67e0bff81 100644 --- a/src/diffusers/loaders/single_file_utils.py +++ b/src/diffusers/loaders/single_file_utils.py @@ -381,7 +381,7 @@ def infer_original_config_file(class_name, checkpoint): else: config_url = CONFIG_URLS["v1"] - original_config_file = BytesIO(requests.get(config_url).content) + original_config_file = BytesIO(requests.get(config_url, timeout=10).content) return original_config_file @@ -402,7 +402,7 @@ def is_valid_url(url): original_config_file = fp.read() elif is_valid_url(original_config_file): - original_config_file = BytesIO(requests.get(original_config_file).content) + original_config_file = BytesIO(requests.get(original_config_file, timeout=10).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 f04a21ef4857..3656732b602f 100644 --- a/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py +++ b/src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py @@ -1324,7 +1324,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=10).content) else: with open(original_config_file, "r") as f: original_config_file = f.read() diff --git a/src/diffusers/utils/loading_utils.py b/src/diffusers/utils/loading_utils.py index aa087e981731..5640d8be1493 100644 --- a/src/diffusers/utils/loading_utils.py +++ b/src/diffusers/utils/loading_utils.py @@ -25,7 +25,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=10).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 c1756d6590d1..34c72b265a62 100644 --- a/src/diffusers/utils/testing_utils.py +++ b/src/diffusers/utils/testing_utils.py @@ -396,7 +396,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=10) response.raise_for_status() arry = np.load(BytesIO(response.content)) elif os.path.isfile(arry): @@ -417,7 +417,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) - def load_pt(url: str): - response = requests.get(url) + response = requests.get(url, timeout=10) response.raise_for_status() arry = torch.load(BytesIO(response.content)) return arry @@ -436,7 +436,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=10).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..03d4db678e3e 100644 --- a/utils/fetch_latest_release_branch.py +++ b/utils/fetch_latest_release_branch.py @@ -27,7 +27,7 @@ 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=10) # 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 3e433281ff65..44a6452ee6e9 100644 --- a/utils/notify_slack_about_release.py +++ b/utils/notify_slack_about_release.py @@ -26,7 +26,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=10) if response.status_code == 200: data = response.json() return data["info"]["version"] @@ -38,7 +38,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=10) if response.status_code == 200: data = response.json()