@@ -254,6 +254,7 @@ def get_cached_module_file(
254254 revision : str | None = None ,
255255 local_files_only : bool = False ,
256256 local_dir : str | None = None ,
257+ trust_remote_code : bool = False ,
257258):
258259 """
259260 Prepares Downloads a module from a local folder or a distant repo and returns its path inside the cached
@@ -289,6 +290,10 @@ def get_cached_module_file(
289290 identifier allowed by git.
290291 local_files_only (`bool`, *optional*, defaults to `False`):
291292 If `True`, will only try to load the tokenizer configuration from local files.
293+ trust_remote_code (`bool`, *optional*, defaults to `False`):
294+ Whether or not to allow for custom pipelines and components defined on the Hub in their own files. This
295+ option should only be set to `True` for repositories you trust and in which you have read the code, as it
296+ will execute code present on the Hub on your local machine.
292297
293298 > [!TIP] > You may pass a token in `token` if you are not logged in (`hf auth login`) and want to use private or
294299 [gated > models](https://huggingface.co/docs/hub/models-gated#gated-models).
@@ -299,15 +304,29 @@ def get_cached_module_file(
299304 # Download and cache module_file from the repo `pretrained_model_name_or_path` of grab it if it's a local file.
300305 pretrained_model_name_or_path = str (pretrained_model_name_or_path )
301306
307+ if DIFFUSERS_DISABLE_REMOTE_CODE :
308+ raise ValueError (
309+ "Downloading remote code is disabled globally via the DIFFUSERS_DISABLE_REMOTE_CODE environment variable."
310+ )
311+
302312 if subfolder is not None :
303313 module_file_or_url = os .path .join (pretrained_model_name_or_path , subfolder , module_file )
304314 else :
305315 module_file_or_url = os .path .join (pretrained_model_name_or_path , module_file )
306316
307- if os .path .isfile (module_file_or_url ):
317+ is_local_file = os .path .isfile (module_file_or_url )
318+ is_community_pipeline = not is_local_file and pretrained_model_name_or_path .count ("/" ) == 0
319+
320+ if is_local_file :
308321 resolved_module_file = module_file_or_url
309322 submodule = "local"
310- elif pretrained_model_name_or_path .count ("/" ) == 0 :
323+ if not trust_remote_code :
324+ raise ValueError (
325+ f"The directory { pretrained_model_name_or_path } contains custom code in { module_file } which must be executed to correctly "
326+ f"load the model. You can inspect the file content at { module_file_or_url } .\n "
327+ f"Pass `trust_remote_code=True` to allow loading remote code modules."
328+ )
329+ elif is_community_pipeline :
311330 available_versions = get_diffusers_versions ()
312331 # cut ".dev0"
313332 latest_version = "v" + "." .join (__version__ .split ("." )[:3 ])
@@ -349,6 +368,12 @@ def get_cached_module_file(
349368 logger .error (f"Could not locate the { module_file } inside { pretrained_model_name_or_path } ." )
350369 raise
351370 else :
371+ if not trust_remote_code :
372+ raise ValueError (
373+ f"The repository for { pretrained_model_name_or_path } contains custom code in { module_file } which must be executed to correctly "
374+ f"load the model. You can inspect the repository content at https://hf.co/{ pretrained_model_name_or_path } /blob/main/{ module_file } .\n "
375+ f"Pass `trust_remote_code=True` to allow loading remote code modules."
376+ )
352377 try :
353378 # Load from URL or cache if already cached
354379 resolved_module_file = hf_hub_download (
@@ -426,6 +451,7 @@ def get_cached_module_file(
426451 revision = revision ,
427452 local_files_only = local_files_only ,
428453 local_dir = local_dir ,
454+ trust_remote_code = trust_remote_code ,
429455 )
430456 return os .path .join (full_submodule , module_file )
431457
@@ -443,6 +469,7 @@ def get_class_from_dynamic_module(
443469 revision : str | None = None ,
444470 local_files_only : bool = False ,
445471 local_dir : str | None = None ,
472+ trust_remote_code : bool = False ,
446473):
447474 """
448475 Extracts a class from a module file, present in the local folder or repository of a model.
@@ -482,6 +509,10 @@ def get_class_from_dynamic_module(
482509 identifier allowed by git.
483510 local_files_only (`bool`, *optional*, defaults to `False`):
484511 If `True`, will only try to load the tokenizer configuration from local files.
512+ trust_remote_code (`bool`, *optional*, defaults to `False`):
513+ Whether or not to allow for custom pipelines and components defined on the Hub in their own files. This
514+ option should only be set to `True` for repositories you trust and in which you have read the code, as it
515+ will execute code present on the Hub on your local machine.
485516
486517 > [!TIP] > You may pass a token in `token` if you are not logged in (`hf auth login`) and want to use private or
487518 [gated > models](https://huggingface.co/docs/hub/models-gated#gated-models).
@@ -508,5 +539,6 @@ def get_class_from_dynamic_module(
508539 revision = revision ,
509540 local_files_only = local_files_only ,
510541 local_dir = local_dir ,
542+ trust_remote_code = trust_remote_code ,
511543 )
512544 return get_class_in_module (class_name , final_module )
0 commit comments