Skip to content

Commit 44b5c60

Browse files
committed
declare DIFFUSERS_REQUEST_TIMEOUT in constants and import when needed
1 parent 4dd115c commit 44b5c60

File tree

12 files changed

+28
-56
lines changed

12 files changed

+28
-56
lines changed

examples/instruct_pix2pix/train_instruct_pix2pix.py

Lines changed: 2 additions & 5 deletions
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

@@ -67,10 +68,6 @@
6768
WANDB_TABLE_COL_NAMES = ["original_image", "edited_image", "edit_prompt"]
6869

6970

70-
# Set global timeout
71-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
72-
73-
7471
def log_validation(
7572
pipeline,
7673
args,
@@ -422,7 +419,7 @@ def convert_to_np(image, resolution):
422419

423420

424421
def download_image(url):
425-
image = PIL.Image.open(requests.get(url, stream=True, timeout=request_timeout).raw)
422+
image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
426423
image = PIL.ImageOps.exif_transpose(image)
427424
image = image.convert("RGB")
428425
return image

examples/research_projects/instructpix2pix_lora/train_instruct_pix2pix_lora.py

Lines changed: 2 additions & 5 deletions
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
@@ -74,10 +75,6 @@
7475
WANDB_TABLE_COL_NAMES = ["original_image", "edited_image", "edit_prompt"]
7576

7677

77-
# Set global timeout
78-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
79-
80-
8178
def save_model_card(
8279
repo_id: str,
8380
images: list = None,
@@ -479,7 +476,7 @@ def convert_to_np(image, resolution):
479476

480477

481478
def download_image(url):
482-
image = PIL.Image.open(requests.get(url, stream=True, timeout=request_timeout).raw)
479+
image = PIL.Image.open(requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
483480
image = PIL.ImageOps.exif_transpose(image)
484481
image = image.convert("RGB")
485482
return image

examples/research_projects/promptdiffusion/convert_original_promptdiffusion_to_diffusers.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
UnCLIPScheduler,
6161
)
6262
from diffusers.utils import is_accelerate_available, logging
63+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
6364

6465

6566
if is_accelerate_available():
@@ -69,10 +70,6 @@
6970
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
7071

7172

72-
# Set global timeout
73-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
74-
75-
7673
def shave_segments(path, n_shave_prefix_segments=1):
7774
"""
7875
Removes segments. Positive values shave the first segments, negative shave the last segments.
@@ -1440,7 +1437,7 @@ def download_from_original_stable_diffusion_ckpt(
14401437
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml"
14411438

14421439
if config_url is not None:
1443-
original_config_file = BytesIO(requests.get(config_url, timeout=request_timeout).content)
1440+
original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
14441441
else:
14451442
with open(original_config_file, "r") as f:
14461443
original_config_file = f.read()

scripts/convert_dance_diffusion_to_diffusers.py

Lines changed: 2 additions & 4 deletions
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 = {
@@ -46,9 +47,6 @@
4647
},
4748
}
4849

49-
# Set global timeout
50-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
51-
5250

5351
def alpha_sigma_to_t(alpha, sigma):
5452
"""Returns a timestep, given the scaling factors for the clean image and for
@@ -77,7 +75,7 @@ def __init__(self, global_args):
7775

7876
def download(model_name):
7977
url = MODELS_MAP[model_name]["url"]
80-
r = requests.get(url, stream=True, timeout=request_timeout)
78+
r = requests.get(url, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT)
8179

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

scripts/convert_vae_pt_to_diffusers.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import argparse
22
import io
3-
import os
43

54
import requests
65
import torch
@@ -14,10 +13,7 @@
1413
renew_vae_attention_paths,
1514
renew_vae_resnet_paths,
1615
)
17-
18-
19-
# Set global timeout
20-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
16+
from diffusers.utils.constants import DIFFUSERS_REQUEST_TIMEOUT
2117

2218

2319
def custom_convert_ldm_vae_checkpoint(checkpoint, config):
@@ -127,7 +123,7 @@ def vae_pt_to_vae_diffuser(
127123
):
128124
# Only support V1
129125
r = requests.get(
130-
" https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml", timeout=request_timeout
126+
" https://raw.githubusercontent.com/CompVis/stable-diffusion/main/configs/stable-diffusion/v1-inference.yaml", timeout=DIFFUSERS_REQUEST_TIMEOUT
131127
)
132128
io_obj = io.BytesIO(r.content)
133129

src/diffusers/loaders/single_file_utils.py

Lines changed: 2 additions & 4 deletions
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

@@ -57,9 +58,6 @@
5758

5859
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
5960

60-
# Set global timeout
61-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
62-
6361
CHECKPOINT_KEY_NAMES = {
6462
"v2": "model.diffusion_model.input_blocks.2.1.transformer_blocks.0.attn2.to_k.weight",
6563
"xl_base": "conditioner.embedders.1.model.transformer.resblocks.9.mlp.c_proj.bias",
@@ -446,7 +444,7 @@ def fetch_original_config(original_config_file, local_files_only=False):
446444
"Please provide a valid local file path."
447445
)
448446

449-
original_config_file = BytesIO(requests.get(original_config_file, timeout=request_timeout).content)
447+
original_config_file = BytesIO(requests.get(original_config_file, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
450448

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

src/diffusers/pipelines/stable_diffusion/convert_from_ckpt.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
UnCLIPScheduler,
5454
)
5555
from ...utils import is_accelerate_available, logging
56+
from ...utils.constants import DIFFUSERS_REQUEST_TIMEOUT
5657
from ..latent_diffusion.pipeline_latent_diffusion import LDMBertConfig, LDMBertModel
5758
from ..paint_by_example import PaintByExampleImageEncoder
5859
from ..pipeline_utils import DiffusionPipeline
@@ -67,10 +68,6 @@
6768
logger = logging.get_logger(__name__) # pylint: disable=invalid-name
6869

6970

70-
# Set global timeout
71-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
72-
73-
7471
def shave_segments(path, n_shave_prefix_segments=1):
7572
"""
7673
Removes segments. Positive values shave the first segments, negative shave the last segments.
@@ -1329,7 +1326,7 @@ def download_from_original_stable_diffusion_ckpt(
13291326
config_url = "https://raw.githubusercontent.com/Stability-AI/stablediffusion/main/configs/stable-diffusion/x4-upscaling.yaml"
13301327

13311328
if config_url is not None:
1332-
original_config_file = BytesIO(requests.get(config_url, timeout=request_timeout).content)
1329+
original_config_file = BytesIO(requests.get(config_url, timeout=DIFFUSERS_REQUEST_TIMEOUT).content)
13331330
else:
13341331
with open(original_config_file, "r") as f:
13351332
original_config_file = f.read()

src/diffusers/utils/constants.py

Lines changed: 1 addition & 0 deletions
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

src/diffusers/utils/loading_utils.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
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

13-
# Set global timeout
14-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
15-
16-
1714
def load_image(
1815
image: Union[str, PIL.Image.Image], convert_method: Optional[Callable[[PIL.Image.Image], PIL.Image.Image]] = None
1916
) -> PIL.Image.Image:
@@ -33,7 +30,7 @@ def load_image(
3330
"""
3431
if isinstance(image, str):
3532
if image.startswith("http://") or image.startswith("https://"):
36-
image = PIL.Image.open(requests.get(image, stream=True, timeout=request_timeout).raw)
33+
image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
3734
elif os.path.isfile(image):
3835
image = PIL.Image.open(image)
3936
else:

src/diffusers/utils/testing_utils.py

Lines changed: 4 additions & 6 deletions
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,
@@ -47,9 +48,6 @@
4748
from .logging import get_logger
4849

4950

50-
# Set global timeout
51-
request_timeout = int(os.environ.get("DIFFUSERS_REQUEST_TIMEOUT", 60))
52-
5351
global_rng = random.Random()
5452

5553
logger = get_logger(__name__)
@@ -597,7 +595,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) -
597595
# local_path can be passed to correct images of tests
598596
return Path(local_path, arry.split("/")[-5], arry.split("/")[-2], arry.split("/")[-1]).as_posix()
599597
elif arry.startswith("http://") or arry.startswith("https://"):
600-
response = requests.get(arry, timeout=request_timeout)
598+
response = requests.get(arry, timeout=DIFFUSERS_REQUEST_TIMEOUT)
601599
response.raise_for_status()
602600
arry = np.load(BytesIO(response.content))
603601
elif os.path.isfile(arry):
@@ -618,7 +616,7 @@ def load_numpy(arry: Union[str, np.ndarray], local_path: Optional[str] = None) -
618616

619617

620618
def load_pt(url: str, map_location: str):
621-
response = requests.get(url, timeout=request_timeout)
619+
response = requests.get(url, timeout=DIFFUSERS_REQUEST_TIMEOUT)
622620
response.raise_for_status()
623621
arry = torch.load(BytesIO(response.content), map_location=map_location)
624622
return arry
@@ -637,7 +635,7 @@ def load_image(image: Union[str, PIL.Image.Image]) -> PIL.Image.Image:
637635
"""
638636
if isinstance(image, str):
639637
if image.startswith("http://") or image.startswith("https://"):
640-
image = PIL.Image.open(requests.get(image, stream=True, timeout=request_timeout).raw)
638+
image = PIL.Image.open(requests.get(image, stream=True, timeout=DIFFUSERS_REQUEST_TIMEOUT).raw)
641639
elif os.path.isfile(image):
642640
image = PIL.Image.open(image)
643641
else:

0 commit comments

Comments
 (0)