Skip to content

Commit b514069

Browse files
authored
Fix HfApi.create_repo when repo_type is 'space' (#394)
* 🐛 fix create spaces * 🐛 fix create spaces bug * ✨ add spaces sdk types as constant * 🧪 add a commit that unskips spaces test * ✅ skip spaces tests while in beta * 🚧 update cli * 🐛 fix missed merge confict * 💄 style * 🐛 add missing import * 🎨 only hardcode gated param for spaces repos * ✅ Update spaces tests to use prod * ✅ use staging endpoint for spaces tests * 🔥 remove hack
1 parent 0466022 commit b514069

File tree

4 files changed

+86
-24
lines changed

4 files changed

+86
-24
lines changed

src/huggingface_hub/commands/user.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
from typing import List, Union
1919

2020
from huggingface_hub.commands import BaseHuggingfaceCLICommand
21-
from huggingface_hub.constants import REPO_TYPES, REPO_TYPES_URL_PREFIXES
21+
from huggingface_hub.constants import (
22+
REPO_TYPES,
23+
REPO_TYPES_URL_PREFIXES,
24+
SPACES_SDK_TYPES,
25+
)
2226
from huggingface_hub.hf_api import HfApi, HfFolder
2327
from requests.exceptions import HTTPError
2428

@@ -68,6 +72,12 @@ def register_subcommand(parser: ArgumentParser):
6872
repo_create_parser.add_argument(
6973
"--organization", type=str, help="Optional: organization namespace."
7074
)
75+
repo_create_parser.add_argument(
76+
"--space_sdk",
77+
type=str,
78+
help='Optional: Hugging Face Spaces SDK type. Required when --type is set to "space".',
79+
choices=SPACES_SDK_TYPES,
80+
)
7181
repo_create_parser.add_argument(
7282
"-y",
7383
"--yes",
@@ -263,6 +273,7 @@ def run(self):
263273
token=token,
264274
organization=self.args.organization,
265275
repo_type=self.args.type,
276+
space_sdk=self.args.space_sdk,
266277
)
267278
except HTTPError as e:
268279
print(e)

src/huggingface_hub/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
REPO_TYPE_DATASET = "dataset"
3333
REPO_TYPE_SPACE = "space"
3434
REPO_TYPES = [None, REPO_TYPE_DATASET, REPO_TYPE_SPACE]
35+
SPACES_SDK_TYPES = ["gradio", "streamlit", "static"]
3536

3637
REPO_TYPES_URL_PREFIXES = {
3738
REPO_TYPE_DATASET: "datasets/",

src/huggingface_hub/hf_api.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@
2626
import requests
2727
from requests.exceptions import HTTPError
2828

29-
from .constants import ENDPOINT, REPO_TYPES, REPO_TYPES_MAPPING, REPO_TYPES_URL_PREFIXES
29+
from .constants import (
30+
ENDPOINT,
31+
REPO_TYPES,
32+
REPO_TYPES_MAPPING,
33+
REPO_TYPES_URL_PREFIXES,
34+
SPACES_SDK_TYPES,
35+
)
3036

3137

3238
if sys.version_info >= (3, 8):
@@ -664,6 +670,7 @@ def create_repo(
664670
repo_type: Optional[str] = None,
665671
exist_ok=False,
666672
lfsmultipartthresh: Optional[int] = None,
673+
space_sdk: Optional[str] = None,
667674
) -> str:
668675
"""
669676
HuggingFace git-based system, used for models, datasets, and spaces.
@@ -679,6 +686,8 @@ def create_repo(
679686
680687
lfsmultipartthresh: Optional: internal param for testing purposes.
681688
689+
space_sdk: Choice of SDK to use if repo_type is "space". Can be "streamlit", "gradio", or "static".
690+
682691
Returns:
683692
URL to the newly created repo.
684693
"""
@@ -707,6 +716,22 @@ def create_repo(
707716
json = {"name": name, "organization": organization, "private": private}
708717
if repo_type is not None:
709718
json["type"] = repo_type
719+
if repo_type == "space":
720+
if space_sdk is None:
721+
raise ValueError(
722+
"No space_sdk provided. `create_repo` expects space_sdk to be one of "
723+
f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
724+
)
725+
if space_sdk not in SPACES_SDK_TYPES:
726+
raise ValueError(
727+
f"Invalid space_sdk. Please choose one of {SPACES_SDK_TYPES}."
728+
)
729+
json["sdk"] = space_sdk
730+
if space_sdk is not None and repo_type != "space":
731+
warnings.warn(
732+
"Ignoring provided space_sdk because repo_type is not 'space'."
733+
)
734+
710735
if lfsmultipartthresh is not None:
711736
json["lfsmultipartthresh"] = lfsmultipartthresh
712737
r = requests.post(
@@ -821,6 +846,7 @@ def update_repo_visibility(
821846
path_prefix += REPO_TYPES_URL_PREFIXES[repo_type]
822847

823848
path = "{}{}/{}/settings".format(path_prefix, namespace, name)
849+
824850
json = {"private": private}
825851

826852
r = requests.put(

tests/test_hf_api.py

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
import unittest
2222
from io import BytesIO
2323

24+
import pytest
25+
2426
import requests
25-
from huggingface_hub.constants import REPO_TYPE_DATASET, REPO_TYPE_SPACE
27+
from huggingface_hub.constants import (
28+
REPO_TYPE_DATASET,
29+
REPO_TYPE_SPACE,
30+
SPACES_SDK_TYPES,
31+
)
2632
from huggingface_hub.file_download import cached_download, hf_hub_download
2733
from huggingface_hub.hf_api import (
2834
DatasetInfo,
@@ -153,28 +159,46 @@ def test_create_update_and_delete_dataset_repo(self):
153159
name=DATASET_REPO_NAME, token=self._token, repo_type=REPO_TYPE_DATASET
154160
)
155161

156-
@unittest.skip("skipped while spaces in beta")
157162
def test_create_update_and_delete_space_repo(self):
158-
self._api.create_repo(
159-
name=SPACE_REPO_NAME, token=self._token, repo_type=REPO_TYPE_SPACE
160-
)
161-
res = self._api.update_repo_visibility(
162-
name=SPACE_REPO_NAME,
163-
token=self._token,
164-
private=True,
165-
repo_type=REPO_TYPE_SPACE,
166-
)
167-
self.assertTrue(res["private"])
168-
res = self._api.update_repo_visibility(
169-
name=SPACE_REPO_NAME,
170-
token=self._token,
171-
private=False,
172-
repo_type=REPO_TYPE_SPACE,
173-
)
174-
self.assertFalse(res["private"])
175-
self._api.delete_repo(
176-
name=SPACE_REPO_NAME, token=self._token, repo_type=REPO_TYPE_SPACE
177-
)
163+
with pytest.raises(ValueError, match=r"No space_sdk provided.*"):
164+
self._api.create_repo(
165+
token=self._token,
166+
name=SPACE_REPO_NAME,
167+
repo_type=REPO_TYPE_SPACE,
168+
space_sdk=None,
169+
)
170+
with pytest.raises(ValueError, match=r"Invalid space_sdk.*"):
171+
self._api.create_repo(
172+
token=self._token,
173+
name=SPACE_REPO_NAME,
174+
repo_type=REPO_TYPE_SPACE,
175+
space_sdk="asdfasdf",
176+
)
177+
178+
for sdk in SPACES_SDK_TYPES:
179+
self._api.create_repo(
180+
name=SPACE_REPO_NAME,
181+
token=self._token,
182+
repo_type=REPO_TYPE_SPACE,
183+
space_sdk=sdk,
184+
)
185+
res = self._api.update_repo_visibility(
186+
name=SPACE_REPO_NAME,
187+
token=self._token,
188+
private=True,
189+
repo_type=REPO_TYPE_SPACE,
190+
)
191+
self.assertTrue(res["private"])
192+
res = self._api.update_repo_visibility(
193+
name=SPACE_REPO_NAME,
194+
token=self._token,
195+
private=False,
196+
repo_type=REPO_TYPE_SPACE,
197+
)
198+
self.assertFalse(res["private"])
199+
self._api.delete_repo(
200+
name=SPACE_REPO_NAME, token=self._token, repo_type=REPO_TYPE_SPACE
201+
)
178202

179203

180204
class HfApiUploadFileTest(HfApiCommonTestWithLogin):

0 commit comments

Comments
 (0)