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

Warn if user tries to upload a parquet file to a model repo #2403

Merged
merged 1 commit into from
Jul 24, 2024
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
9 changes: 9 additions & 0 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3733,6 +3733,15 @@ def create_commit(
" new CommitOperationAdd object if you want to create a new commit."
)

if repo_type != "dataset":
for addition in additions:
if addition.path_in_repo.endswith((".arrow", ".parquet")):
warnings.warn(
f"It seems that you are about to commit a data file ({addition.path_in_repo}) to a {repo_type}"
" repository. You are sure this is intended? If you are trying to upload a dataset, please"
" set `repo_type='dataset'` or `--repo-type=dataset` in a CLI."
)

logger.debug(
f"About to commit to the hub: {len(additions)} addition(s), {len(copies)} copie(s) and"
f" {nb_deletions} deletion(s)."
Expand Down
27 changes: 27 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,33 @@ def test_upload_file_bytesio(self, repo_url: RepoUrl) -> None:
with open(hf_hub_download(repo_id=repo_id, filename="temp/new_file.md", cache_dir=cache_dir)) as f:
self.assertEqual(f.read(), content.getvalue().decode())

@use_tmp_repo()
def test_upload_data_files_to_model_repo(self, repo_url: RepoUrl) -> None:
# If a .parquet file is uploaded to a model repo, it should be uploaded correctly but a warning is raised.
with self.assertWarns(UserWarning) as cm:
self._api.upload_file(
path_or_fileobj=b"content",
path_in_repo="data.parquet",
repo_id=repo_url.repo_id,
)
assert (
cm.warnings[0].message.args[0]
== "It seems that you are about to commit a data file (data.parquet) to a model repository. You are sure this is intended? If you are trying to upload a dataset, please set `repo_type='dataset'` or `--repo-type=dataset` in a CLI."
)

# Same for arrow file
with self.assertWarns(UserWarning) as cm:
self._api.upload_file(
path_or_fileobj=b"content",
path_in_repo="data.arrow",
repo_id=repo_url.repo_id,
)

# Still correctly uploaded
files = self._api.list_repo_files(repo_url.repo_id)
assert "data.parquet" in files
assert "data.arrow" in files

def test_create_repo_return_value(self) -> None:
REPO_NAME = repo_name("org")
url = self._api.create_repo(repo_id=REPO_NAME)
Expand Down
Loading