Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed requests.get function call by adding timeout parameter. #11156

Merged
merged 8 commits into from
Apr 4, 2025
3 changes: 2 additions & 1 deletion examples/instruct_pix2pix/train_instruct_pix2pix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion scripts/convert_dance_diffusion_to_diffusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from torch import nn

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT


MODELS_MAP = {
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion scripts/convert_vae_pt_to_diffusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/diffusers/loaders/single_file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
is_transformers_available,
logging,
)
from ..utils.constants import DIFFUSERS_REQUEST_TIMEOUT
from ..utils.hub_utils import _get_model_file


Expand Down Expand Up @@ -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.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/diffusers/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/diffusers/utils/loading_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import PIL.ImageOps
import requests

from .constants import DIFFUSERS_REQUEST_TIMEOUT
from .import_utils import BACKENDS_MAPPING, is_imageio_available


Expand All @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions src/diffusers/utils/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion utils/fetch_latest_release_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions utils/notify_slack_about_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import requests

from ..src.diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT


# Configuration
LIBRARY_NAME = "diffusers"
Expand All @@ -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"]
Expand All @@ -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()
Expand Down
Loading