Skip to content

Commit 0417fd5

Browse files
Align signature of create/delete_repo with latest hfh (#5064)
* Align create_repo signature with hfh * Align delete_repo signature with hfh * Fix bug in organization as list * Swap name and org
1 parent 3c19812 commit 0417fd5

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

src/datasets/arrow_dataset.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4323,17 +4323,14 @@ def _push_parquet_shards_to_hub(
43234323
f"The identifier should be in the format <repo_id> or <namespace>/<repo_id>. It is {identifier}, "
43244324
"which doesn't conform to either format."
43254325
)
4326-
elif len(identifier) == 2:
4327-
organization_or_username, dataset_name = identifier
43284326
elif len(identifier) == 1:
43294327
dataset_name = identifier[0]
43304328
organization_or_username = api.whoami(token)["name"]
43314329
repo_id = f"{organization_or_username}/{dataset_name}"
43324330

43334331
create_repo(
4334-
hf_api=api,
4335-
name=dataset_name,
4336-
organization=organization_or_username,
4332+
api,
4333+
repo_id,
43374334
token=token,
43384335
repo_type="dataset",
43394336
private=private,

src/datasets/utils/_hf_hub_fixes.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88

99
def create_repo(
1010
hf_api: HfApi,
11-
name: str,
11+
repo_id: str,
1212
token: Optional[str] = None,
13-
organization: Optional[str] = None,
1413
private: Optional[bool] = None,
1514
repo_type: Optional[str] = None,
1615
exist_ok: Optional[bool] = False,
@@ -22,10 +21,8 @@ def create_repo(
2221
2322
Args:
2423
hf_api (`huggingface_hub.HfApi`): Hub client
25-
name (`str`): name of the repository (without the namespace)
24+
repo_id (`str`): A namespace (user or an organization) and a repo name separated by a `/`.
2625
token (`str`, *optional*): user or organization token. Defaults to None.
27-
organization (`str`, *optional*): namespace for the repository: the username or organization name.
28-
By default it uses the namespace associated to the token used.
2926
private (`bool`, *optional*):
3027
Whether the model repo should be private.
3128
repo_type (`str`, *optional*):
@@ -42,6 +39,7 @@ def create_repo(
4239
`str`: URL to the newly created repo.
4340
"""
4441
if version.parse(huggingface_hub.__version__) < version.parse("0.5.0"):
42+
organization, name = repo_id.split("/")
4543
return hf_api.create_repo(
4644
name=name,
4745
organization=organization,
@@ -53,7 +51,7 @@ def create_repo(
5351
)
5452
else: # the `organization` parameter is deprecated in huggingface_hub>=0.5.0
5553
return hf_api.create_repo(
56-
repo_id=f"{organization}/{name}",
54+
repo_id=repo_id,
5755
token=token,
5856
private=private,
5957
repo_type=repo_type,
@@ -64,9 +62,8 @@ def create_repo(
6462

6563
def delete_repo(
6664
hf_api: HfApi,
67-
name: str,
65+
repo_id: str,
6866
token: Optional[str] = None,
69-
organization: Optional[str] = None,
7067
repo_type: Optional[str] = None,
7168
) -> str:
7269
"""
@@ -75,10 +72,8 @@ def delete_repo(
7572
7673
Args:
7774
hf_api (`huggingface_hub.HfApi`): Hub client
78-
name (`str`): name of the repository (without the namespace)
75+
repo_id (`str`): A namespace (user or an organization) and a repo name separated by a `/`.
7976
token (`str`, *optional*): user or organization token. Defaults to None.
80-
organization (`str`, *optional*): namespace for the repository: the username or organization name.
81-
By default it uses the namespace associated to the token used.
8277
repo_type (`str`, *optional*):
8378
Set to `"dataset"` or `"space"` if uploading to a dataset or
8479
space, `None` or `"model"` if uploading to a model. Default is
@@ -88,6 +83,7 @@ def delete_repo(
8883
`str`: URL to the newly created repo.
8984
"""
9085
if version.parse(huggingface_hub.__version__) < version.parse("0.5.0"):
86+
organization, name = repo_id.split("/")
9187
return hf_api.delete_repo(
9288
name=name,
9389
organization=organization,
@@ -96,7 +92,7 @@ def delete_repo(
9692
)
9793
else: # the `organization` parameter is deprecated in huggingface_hub>=0.5.0
9894
return hf_api.delete_repo(
99-
repo_id=f"{organization}/{name}",
95+
repo_id=repo_id,
10096
token=token,
10197
repo_type=repo_type,
10298
)

tests/fixtures/hub.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ def hf_token(hf_api: HfApi):
6060
@pytest.fixture
6161
def cleanup_repo(hf_api):
6262
def _cleanup_repo(repo_id):
63-
organization, name = repo_id.split("/")
64-
delete_repo(hf_api=hf_api, name=name, organization=organization, token=CI_HUB_USER_TOKEN, repo_type="dataset")
63+
delete_repo(hf_api, repo_id, token=CI_HUB_USER_TOKEN, repo_type="dataset")
6564

6665
return _cleanup_repo
6766

@@ -81,8 +80,8 @@ def _temporary_repo(repo_id):
8180
@pytest.fixture(scope="session")
8281
def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file):
8382
repo_name = f"repo_txt_data-{int(time.time() * 10e3)}"
84-
create_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset", private=True)
8583
repo_id = f"{CI_HUB_USER}/{repo_name}"
84+
create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True)
8685
hf_api.upload_file(
8786
token=hf_token,
8887
path_or_fileobj=str(text_file),
@@ -92,7 +91,7 @@ def hf_private_dataset_repo_txt_data_(hf_api: HfApi, hf_token, text_file):
9291
)
9392
yield repo_id
9493
try:
95-
delete_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset")
94+
delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset")
9695
except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error
9796
pass
9897

@@ -107,8 +106,8 @@ def hf_private_dataset_repo_txt_data(hf_private_dataset_repo_txt_data_):
107106
@pytest.fixture(scope="session")
108107
def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_with_dir_path):
109108
repo_name = f"repo_zipped_txt_data-{int(time.time() * 10e3)}"
110-
create_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset", private=True)
111109
repo_id = f"{CI_HUB_USER}/{repo_name}"
110+
create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True)
112111
hf_api.upload_file(
113112
token=hf_token,
114113
path_or_fileobj=str(zip_csv_with_dir_path),
@@ -118,7 +117,7 @@ def hf_private_dataset_repo_zipped_txt_data_(hf_api: HfApi, hf_token, zip_csv_wi
118117
)
119118
yield repo_id
120119
try:
121-
delete_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset")
120+
delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset")
122121
except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error
123122
pass
124123

@@ -133,8 +132,8 @@ def hf_private_dataset_repo_zipped_txt_data(hf_private_dataset_repo_zipped_txt_d
133132
@pytest.fixture(scope="session")
134133
def hf_private_dataset_repo_zipped_img_data_(hf_api: HfApi, hf_token, zip_image_path):
135134
repo_name = f"repo_zipped_img_data-{int(time.time() * 10e3)}"
136-
create_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset", private=True)
137135
repo_id = f"{CI_HUB_USER}/{repo_name}"
136+
create_repo(hf_api, repo_id, token=hf_token, repo_type="dataset", private=True)
138137
hf_api.upload_file(
139138
token=hf_token,
140139
path_or_fileobj=str(zip_image_path),
@@ -144,7 +143,7 @@ def hf_private_dataset_repo_zipped_img_data_(hf_api: HfApi, hf_token, zip_image_
144143
)
145144
yield repo_id
146145
try:
147-
delete_repo(hf_api, repo_name, token=hf_token, organization=CI_HUB_USER, repo_type="dataset")
146+
delete_repo(hf_api, repo_id, token=hf_token, repo_type="dataset")
148147
except (requests.exceptions.HTTPError, ValueError): # catch http error and token invalid error
149148
pass
150149

0 commit comments

Comments
 (0)