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

Fix a bug in loading without trust remote code #1684

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/unitxt/error_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Documentation:
BENCHMARKS = "docs/benchmark.html"
DATA_CLASSIFICATION_POLICY = "docs/data_classification_policy.html"
CATALOG = "docs/saving_and_loading_from_catalog.html"
SETTINGS = "docs/settings.html"


def additional_info(path: str) -> str:
Expand Down
61 changes: 33 additions & 28 deletions src/unitxt/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
from tqdm import tqdm

from .dataclass import NonPositionalField
from .error_utils import UnitxtError, UnitxtWarning
from .error_utils import Documentation, UnitxtError, UnitxtWarning
from .fusion import FixedFusion
from .logging_utils import get_logger
from .operator import SourceOperator
Expand All @@ -80,19 +80,27 @@
logger = get_logger()
settings = get_settings()

class UnitxtUnverifiedCodeError(UnitxtError):
def __init__(self, path):
super().__init__(f"Loader cannot load and run remote code from {path} in huggingface without setting unitxt.settings.allow_unverified_code=True or by setting environment variable: UNITXT_ALLOW_UNVERIFIED_CODE.", Documentation.SETTINGS)

def hf_load_dataset(path: str, *args, **kwargs):
if settings.hf_offline_datasets_path is not None:
path = os.path.join(settings.hf_offline_datasets_path, path)
return _hf_load_dataset(
path,
*args, **kwargs,
download_config=DownloadConfig(
max_retries=settings.loaders_max_retries,
),
verification_mode="no_checks",
trust_remote_code=settings.allow_unverified_code,
download_mode= "force_redownload" if settings.disable_hf_datasets_cache else "reuse_dataset_if_exists"
)
try:
return _hf_load_dataset(
path,
*args, **kwargs,
download_config=DownloadConfig(
max_retries=settings.loaders_max_retries,
),
verification_mode="no_checks",
trust_remote_code=settings.allow_unverified_code,
download_mode= "force_redownload" if settings.disable_hf_datasets_cache else "reuse_dataset_if_exists"
)
except ValueError as e:
if "trust_remote_code" in str(e):
raise UnitxtUnverifiedCodeError(path) from e

class Loader(SourceOperator):
"""A base class for all loaders.
Expand Down Expand Up @@ -288,22 +296,17 @@ def load_dataset(
if dataset is None:
if streaming is None:
streaming = self.is_streaming()
try:
dataset = hf_load_dataset(
self.path,
name=self.name,
data_dir=self.data_dir,
data_files=self.data_files,
revision=self.revision,
streaming=streaming,
split=split,
num_proc=self.num_proc,
)
except ValueError as e:
if "trust_remote_code" in str(e):
raise ValueError(
f"{self.__class__.__name__} cannot run remote code from huggingface without setting unitxt.settings.allow_unverified_code=True or by setting environment variable: UNITXT_ALLOW_UNVERIFIED_CODE."
) from e

dataset = hf_load_dataset(
self.path,
name=self.name,
data_dir=self.data_dir,
data_files=self.data_files,
revision=self.revision,
streaming=streaming,
split=split,
num_proc=self.num_proc,
)
self.__class__._loader_cache.max_size = settings.loader_cache_size
if not disable_memory_caching:
self.__class__._loader_cache[dataset_id] = dataset
Expand Down Expand Up @@ -333,7 +336,9 @@ def get_splits(self):
extract_on_the_fly=True,
),
)
except:
except Exception as e:
if "trust_remote_code" in str(e):
raise UnitxtUnverifiedCodeError(self.path) from e
UnitxtWarning(
f'LoadHF(path="{self.path}", name="{self.name}") could not retrieve split names without loading the dataset. Consider defining "splits" in the LoadHF definition to improve loading time.'
)
Expand Down
Loading