Skip to content

Commit f10775b

Browse files
kghamilton89Sai-Suraj-27github-actions[bot]
authored
Fixed requests.get function call by adding timeout parameter. (#11156)
* Fixed requests.get function call by adding timeout parameter. * declare DIFFUSERS_REQUEST_TIMEOUT in constants and import when needed * remove unneeded os import * Apply style fixes --------- Co-authored-by: Sai-Suraj-27 <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 6edb774 commit f10775b

12 files changed

+34
-14
lines changed

Diff for: examples/instruct_pix2pix/train_instruct_pix2pix.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from diffusers.optimization import get_scheduler
5050
from diffusers.training_utils import EMAModel
5151
from diffusers.utils import check_min_version, deprecate, is_wandb_available
52+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
5253
from diffusers.utils.import_utils import is_xformers_available
5354
from diffusers.utils.torch_utils import is_compiled_module
5455

@@ -418,7 +419,7 @@ def convert_to_np(image, resolution):
418419

419420

420421
def download_image(url):
421-
image = PIL.Image.open(requests.get(url, stream=True).raw)
422+
image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
422423
image = PIL.ImageOps.exif_transpose(image)
423424
image = image.convert("RGB")
424425
return image

Diff for: examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from diffusers.optimization import get_scheduler
5555
from diffusers.training_utils import EMAModel, cast_training_params
5656
from diffusers.utils import check_min_version, convert_state_dict_to_diffusers, deprecate, is_wandb_available
57+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
5758
from diffusers.utils.hub_utils import load_or_create_model_card, populate_model_card
5859
from diffusers.utils.import_utils import is_xformers_available
5960
from diffusers.utils.torch_utils import is_compiled_module
@@ -475,7 +476,7 @@ def convert_to_np(image, resolution):
475476

476477

477478
def download_image(url):
478-
image = PIL.Image.open(requests.get(url, stream=True).raw)
479+
image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
479480
image = PIL.ImageOps.exif_transpose(image)
480481
image = image.convert("RGB")
481482
return image

Diff for: examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
UnCLIPScheduler,
6060
)
6161
from diffusers.utils import is_accelerate_available, logging
62+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
6263

6364

6465
if is_accelerate_available():
@@ -1435,7 +1436,7 @@ def download_from_original_stable_diffusion_ckpt(
14351436
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml"
14361437

14371438
if config_url is not None:
1438-
original_config_file = BytesIO(requests.get(config_url).content)
1439+
original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
14391440
else:
14401441
with open(original_config_file, "r") as f:
14411442
original_config_file = f.read()

Diff for: scripts/convert_dance_diffusion_to_diffusers.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from torch import nn
1212

1313
from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel
14+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
1415

1516

1617
MODELS_MAP = {
@@ -74,7 +75,7 @@ def __init__(self, global_args):
7475

7576
def download(model_name):
7677
url = MODELS_MAP[model_name]["url"]
77-
r = requests.get(url, stream=True)
78+
r = requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT)
7879

7980
local_filename = f"./{model_name}.ckpt"
8081
with open(local_filename, "wb") as fp:

Diff for: scripts/convert_vae_pt_to_diffusers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
renew_vae_attention_paths,
1414
renew_vae_resnet_paths,
1515
)
16+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
1617

1718

1819
def custom_convert_ldm_vae_checkpoint(checkpoint, config):
@@ -122,7 +123,8 @@ def vae_pt_to_vae_diffuser(
122123
):
123124
# Only support V1
124125
r = requests.get(
125-
" https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml"
126+
" https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml",
127+
timeout=DIFFUSERS_REQUEST_TIMEOUT,
126128
)
127129
io_obj = io.BytesIO(r.content)
128130

Diff for: src/diffusers/loaders/single_file_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
is_transformers_available,
4545
logging,
4646
)
47+
from ..utils.constants import DIFFUSERS_REQUEST_TIMEOUT
4748
from ..utils.hub_utils import _get_model_file
4849

4950

@@ -443,7 +444,7 @@ def fetch_original_config(original_config_file, local_files_only=False):
443444
"Please provide a valid local file path."
444445
)
445446

446-
original_config_file = BytesIO(requests.get(original_config_file).content)
447+
original_config_file = BytesIO(requests.get(original_config_file, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
447448

448449
else:
449450
raise ValueError("Invalid `original_config_file` provided. Please set it to a valid file path or URL.")

Diff for: src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
UnCLIPScheduler,
5353
)
5454
from ...utils import is_accelerate_available, logging
55+
from ...utils.constants import DIFFUSERS_REQUEST_TIMEOUT
5556
from ..latent_diffusion.pipeline_latent_diffusion import LDMBertConfig, LDMBertModel
5657
from ..paint_by_example import PaintByExampleImageEncoder
5758
from ..pipeline_utils import DiffusionPipeline
@@ -1324,7 +1325,7 @@ def download_from_original_stable_diffusion_ckpt(
13241325
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml"
13251326

13261327
if config_url is not None:
1327-
original_config_file = BytesIO(requests.get(config_url).content)
1328+
original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
13281329
else:
13291330
with open(original_config_file, "r") as f:
13301331
original_config_file = f.read()

Diff for: src/diffusers/utils/constants.py

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
DIFFUSERS_DYNAMIC_MODULE_NAME = "diffusers_modules"
4141
HF_MODULES_CACHE = os.getenv("HF_MODULES_CACHE", os.path.join(HF_HOME, "modules"))
4242
DEPRECATED_REVISION_ARGS = ["fp16", "non-ema"]
43+
DIFFUSERS_REQUEST_TIMEOUT = 60
4344

4445
# Below should be `True` if the current version of `peft` and `transformers` are compatible with
4546
# PEFT backend. Will automatically fall back to PEFT backend if the correct versions of the libraries are

Diff for: src/diffusers/utils/loading_utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import PIL.ImageOps
88
import requests
99

10+
from .constants import DIFFUSERS_REQUEST_TIMEOUT
1011
from .import_utils import BACKENDS_MAPPING, is_imageio_available
1112

1213

@@ -29,7 +30,7 @@ def load_image(
2930
"""
3031
if isinstance(image, str):
3132
if image.startswith("http://") or image.startswith("https://"):
32-
image = PIL.Image.open(requests.get(image, stream=True).raw)
33+
image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
3334
elif os.path.isfile(image):
3435
image = PIL.Image.open(image)
3536
else:

Diff for: src/diffusers/utils/testing_utils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from numpy.linalg import norm
2727
from packaging import version
2828

29+
from .constants import DIFFUSERS_REQUEST_TIMEOUT
2930
from .import_utils import (
3031
BACKENDS_MAPPING,
3132
is_accelerate_available,
@@ -594,7 +595,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) -
594595
# local_path can be passed to correct images of tests
595596
return Path(local_path, arry.split("/")[-5], arry.split("/")[-2], arry.split("/")[-1]).as_posix()
596597
elif arry.startswith("http://") or arry.startswith("https://"):
597-
response = requests.get(arry)
598+
response = requests.get(arry, timeout=DIFFUSERS_REQUEST_TIMEOUT)
598599
response.raise_for_status()
599600
arry = np.load(BytesIO(response.content))
600601
elif os.path.isfile(arry):
@@ -615,7 +616,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) -
615616

616617

617618
def load_pt(url: str, map_location: str):
618-
response = requests.get(url)
619+
response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT)
619620
response.raise_for_status()
620621
arry = torch.load(BytesIO(response.content), map_location=map_location)
621622
return arry
@@ -634,7 +635,7 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
634635
"""
635636
if isinstance(image, str):
636637
if image.startswith("http://") or image.startswith("https://"):
637-
image = PIL.Image.open(requests.get(image, stream=True).raw)
638+
image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
638639
elif os.path.isfile(image):
639640
image = PIL.Image.open(image)
640641
else:

Diff for: utils/fetch_latest_release_branch.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16+
1617
import requests
1718
from packaging.version import parse
1819

20+
from ..src.diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
21+
1922

2023
# GitHub repository details
2124
USER = "huggingface"
@@ -27,7 +30,11 @@ def fetch_all_branches(user, repo):
2730
page = 1 # Start from first page
2831
while True:
2932
# Make a request to the GitHub API for the branches
30-
response = requests.get(f"https://api.github.com/repos/{user}/{repo}/branches", params={"page": page})
33+
response = requests.get(
34+
f"https://api.github.com/repos/{user}/{repo}/branches",
35+
params={"page": page},
36+
timeout=DIFFUSERS_REQUEST_TIMEOUT,
37+
)
3138

3239
# Check if the request was successful
3340
if response.status_code == 200:

Diff for: utils/notify_slack_about_release.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import requests
1919

20+
from ..src.diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
21+
2022

2123
# Configuration
2224
LIBRARY_NAME = "diffusers"
@@ -26,7 +28,7 @@
2628

2729
def check_pypi_for_latest_release(library_name):
2830
"""Check PyPI for the latest release of the library."""
29-
response = requests.get(f"https://pypi.org/pypi/{library_name}/json")
31+
response = requests.get(f"https://pypi.org/pypi/{library_name}/json", timeout=DIFFUSERS_REQUEST_TIMEOUT)
3032
if response.status_code == 200:
3133
data = response.json()
3234
return data["info"]["version"]
@@ -38,7 +40,7 @@ def check_pypi_for_latest_release(library_name):
3840
def get_github_release_info(github_repo):
3941
"""Fetch the latest release info from GitHub."""
4042
url = f"https://api.github.com/repos/{github_repo}/releases/latest"
41-
response = requests.get(url)
43+
response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT)
4244

4345
if response.status_code == 200:
4446
data = response.json()

0 commit comments

Comments
 (0)