Repository: huggingface/huggingface_hub
Pull request #620 (2022-01-21 20:13:46)

Include "model" in repo_type to keep consistency 
Closes https://github.com/huggingface/huggingface_hub/issues/557


Patch:
diff --git a/src/huggingface_hub/constants.py b/src/huggingface_hub/constants.py
index 4c938633..8f40922b 100644
--- a/src/huggingface_hub/constants.py
+++ b/src/huggingface_hub/constants.py
@@ -33,14 +33,19 @@ HUGGINGFACE_CO_URL_TEMPLATE = ENDPOINT + "/{repo_id}/resolve/{revision}/{filenam
 
 REPO_TYPE_DATASET = "dataset"
 REPO_TYPE_SPACE = "space"
-REPO_TYPES = [None, REPO_TYPE_DATASET, REPO_TYPE_SPACE]
+REPO_TYPE_MODEL = "model"
+REPO_TYPES = [None, REPO_TYPE_MODEL, REPO_TYPE_DATASET, REPO_TYPE_SPACE]
 SPACES_SDK_TYPES = ["gradio", "streamlit", "static"]
 
 REPO_TYPES_URL_PREFIXES = {
     REPO_TYPE_DATASET: "datasets/",
     REPO_TYPE_SPACE: "spaces/",
 }
-REPO_TYPES_MAPPING = {"datasets": REPO_TYPE_DATASET, "spaces": REPO_TYPE_SPACE}
+REPO_TYPES_MAPPING = {
+    "datasets": REPO_TYPE_DATASET,
+    "spaces": REPO_TYPE_SPACE,
+    "models": REPO_TYPE_MODEL,
+}
 
 
 # default cache
diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py
index 0ec2bc5f..645b236e 100644
--- a/src/huggingface_hub/hf_api.py
+++ b/src/huggingface_hub/hf_api.py
@@ -880,7 +880,7 @@ class HfApi:
         """
         Get the list of files in a given repo.
         """
-        if repo_type is None:
+        if repo_type is None or repo_type == "model":
             info = self.model_info(
                 repo_id, revision=revision, token=token, timeout=timeout
             )
@@ -977,7 +977,7 @@ class HfApi:
         Params:
             private: Whether the model repo should be private (requires a paid huggingface.co account)
 
-            repo_type: Set to "dataset" or "space" if creating a dataset or space, default is model
+            repo_type: Set to :obj:`"dataset"` or :obj:`"space"` if uploading to a dataset or space, :obj:`None` or :obj:`"model"` if uploading to a model. Default is :obj:`None`.
 
             exist_ok: Do not raise an error if repo already exists
 
@@ -1037,19 +1037,19 @@ class HfApi:
             raise ValueError("Invalid repo type")
 
         json = {"name": name, "organization": organization, "private": private}
-        if repo_type is not None:
+        if repo_type is not None and repo_type != "model":
             json["type"] = repo_type
-            if repo_type == "space":
-                if space_sdk is None:
-                    raise ValueError(
-                        "No space_sdk provided. `create_repo` expects space_sdk to be one of "
-                        f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
-                    )
-                if space_sdk not in SPACES_SDK_TYPES:
-                    raise ValueError(
-                        f"Invalid space_sdk. Please choose one of {SPACES_SDK_TYPES}."
-                    )
-                json["sdk"] = space_sdk
+        elif repo_type == "space":
+            if space_sdk is None:
+                raise ValueError(
+                    "No space_sdk provided. `create_repo` expects space_sdk to be one of "
+                    f"{SPACES_SDK_TYPES} when repo_type is 'space'`"
+                )
+            if space_sdk not in SPACES_SDK_TYPES:
+                raise ValueError(
+                    f"Invalid space_sdk. Please choose one of {SPACES_SDK_TYPES}."
+                )
+            json["sdk"] = space_sdk
         if space_sdk is not None and repo_type != "space":
             warnings.warn(
                 "Ignoring provided space_sdk because repo_type is not 'space'."
@@ -1143,7 +1143,7 @@ class HfApi:
             raise ValueError("Invalid repo type")
 
         json = {"name": name, "organization": organization}
-        if repo_type is not None:
+        if repo_type is not None and repo_type != "model":
             json["type"] = repo_type
 
         r = requests.delete(
@@ -1234,7 +1234,7 @@ class HfApi:
                 Authentication token, obtained with :function:`HfApi.login` method. Will default to the stored token.
 
             repo_type (``str``, Optional):
-                Set to :obj:`"dataset"` or :obj:`"space"` if uploading to a dataset or space, :obj:`None` if uploading to a model. Default is :obj:`None`.
+                Set to :obj:`"dataset"` or :obj:`"space"` if uploading to a dataset or space, :obj:`None` or :obj:`"model"` if uploading to a model. Default is :obj:`None`.
 
             revision (``str``, Optional):
                 The git revision to commit from. Defaults to the :obj:`"main"` branch.
@@ -1370,7 +1370,7 @@ class HfApi:
                 Authentication token, obtained with :function:`HfApi.login` method. Will default to the stored token.
 
             repo_type (``str``, Optional):
-                Set to :obj:`"dataset"` or :obj:`"space"` if the file is in a dataset or space repository, :obj:`None` if in a model. Default is :obj:`None`.
+                Set to :obj:`"dataset"` or :obj:`"space"` if the file is in a dataset or space, :obj:`None` or :obj:`"model"` if in a model. Default is :obj:`None`.
 
             revision (``str``, Optional):
                 The git revision to commit from. Defaults to the :obj:`"main"` branch.


Test patch:
diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py
index 51ec9e69..b2ac9fd8 100644
--- a/tests/test_hf_api.py
+++ b/tests/test_hf_api.py
@@ -28,6 +28,7 @@ import requests
 from huggingface_hub.commands.user import _login
 from huggingface_hub.constants import (
     REPO_TYPE_DATASET,
+    REPO_TYPE_MODEL,
     REPO_TYPE_SPACE,
     SPACES_SDK_TYPES,
 )
@@ -165,6 +166,23 @@ class HfApiEndpointsTest(HfApiCommonTestWithLogin):
         self.assertFalse(res["private"])
         self._api.delete_repo(name=REPO_NAME, token=self._token)
 
+    def test_create_update_and_delete_model_repo(self):
+        REPO_NAME = repo_name("crud")
+        self._api.create_repo(
+            name=REPO_NAME, token=self._token, repo_type=REPO_TYPE_MODEL
+        )
+        res = self._api.update_repo_visibility(
+            name=REPO_NAME, token=self._token, private=True, repo_type=REPO_TYPE_MODEL
+        )
+        self.assertTrue(res["private"])
+        res = self._api.update_repo_visibility(
+            name=REPO_NAME, token=self._token, private=False, repo_type=REPO_TYPE_MODEL
+        )
+        self.assertFalse(res["private"])
+        self._api.delete_repo(
+            name=REPO_NAME, token=self._token, repo_type=REPO_TYPE_MODEL
+        )
+
     def test_create_update_and_delete_dataset_repo(self):
         DATASET_REPO_NAME = dataset_repo_name("crud")
         self._api.create_repo(