Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

push_to_hub_gguf: put the user's tags in the README card (#17438)

Vineeth Sai Varikuntlae0ba54ee9754
diff --git a/tests/saving/test_push_to_hub_gguf_single_commit.py b/tests/saving/test_push_to_hub_gguf_single_commit.py
index e0c648d1f4e..79b2bc7c39c 100644
--- a/tests/saving/test_push_to_hub_gguf_single_commit.py
+++ b/tests/saving/test_push_to_hub_gguf_single_commit.py
@@ -139,3 +139,46 @@ def test_readme_is_not_written_into_the_save_directory(push, tmp_path, monkeypat
commit = next(kw for name, kw in calls if name == "create_commit")
assert "Unsloth" in commit["readme"]
assert (checkpoint / "README.md").read_text() == "my model card"
+
+
[email protected]("tags", [["my-tag", "second"], "my-tag"])
+def test_tags_go_in_the_readme_card(push, tags):
+ """HfApi has no add_tags, so tags only reach the Hub through the README card."""
+ calls = push(create_pr = True, tags = tags)
+ assert "add_tags" not in [name for name, _ in calls]
+ readme = next(kw for name, kw in calls if name == "create_commit")["readme"]
+ card_tags = huggingface_hub.ModelCard(readme).data.tags
+ assert set([tags] if isinstance(tags, str) else tags) <= set(card_tags)
+ assert {"gguf", "llama-cpp", "unsloth"} <= set(card_tags)
+
+
+def test_tags_argument_is_not_modified(push):
+ tags = ["my-tag"]
+ push(tags = tags)
+ assert tags == ["my-tag"]
+
+
[email protected]("tags", [None, ["my-tag", "gguf"], "my-tag"])
+def test_sentence_transformer_tags_go_in_the_readme_card(monkeypatch, tags):
+ st_mod = pytest.importorskip("unsloth.models.sentence_transformer")
+ _RecordingApi.calls = []
+ readmes = []
+
+ def upload_file(self, **kwargs):
+ if kwargs["path_in_repo"] == "README.md":
+ readmes.append(open(kwargs["path_or_fileobj"], encoding = "utf-8").read())
+
+ def fake_convert(self, save_directory, **kwargs):
+ return {**_fake_convert(save_directory = save_directory), "modelfile_location": None}
+
+ monkeypatch.setattr(_RecordingApi, "upload_file", upload_file, raising = False)
+ monkeypatch.setattr(st_mod, "HfApi", _RecordingApi)
+ monkeypatch.setattr(st_mod, "_save_pretrained_gguf", fake_convert)
+ st_mod._push_to_hub_gguf(object(), "u/my-model", token = "x", tags = tags)
+
+ assert "add_tags" not in [name for name, _ in _RecordingApi.calls]
+ card_tags = huggingface_hub.ModelCard(readmes[0]).data.tags
+ expected = {"gguf", "llama-cpp", "unsloth", "sentence-transformers", "vision-language-model"}
+ expected |= {tags} if isinstance(tags, str) else set(tags or [])
+ assert expected <= set(card_tags)
+ assert len(card_tags) == len(set(card_tags))
diff --git a/unsloth/models/sentence_transformer.py b/unsloth/models/sentence_transformer.py
index 541bd326e79..49d2c56b9d3 100644
--- a/unsloth/models/sentence_transformer.py
+++ b/unsloth/models/sentence_transformer.py
@@ -702,14 +702,17 @@ def _push_to_hub_gguf(
)
gguf_basenames = [os.path.basename(f) for f in gguf_files if os.path.exists(f)]
+ if isinstance(tags, str):
+ tags = [tags]
+ card_tags = ["gguf", "llama.cpp", "llama-cpp", "unsloth", "sentence-transformers"]
+ if is_vlm:
+ card_tags.append("vision-language-model")
+ tags_yaml = "".join(
+ f"- {json.dumps(t)}\n" for t in dict.fromkeys(card_tags + list(tags or []))
+ )
readme_content = f"""---
tags:
-- gguf
-- llama.cpp
-- unsloth
-- sentence-transformers
-{"- vision-language-model" if is_vlm else ""}
----
+{tags_yaml}---
# {model_name} - GGUF
@@ -749,19 +752,6 @@ This sentence-transformers model was finetuned and converted to GGUF format usin
revision = revision,
)
- all_tags = ["gguf", "llama-cpp", "unsloth", "sentence-transformers"]
- if is_vlm:
- all_tags.append("vision-language-model")
- if tags is not None:
- if isinstance(tags, (list, tuple)):
- all_tags.extend(tags)
- else:
- all_tags.append(tags)
- try:
- api.add_tags(repo_id = full_repo_id, tags = all_tags, repo_type = "model")
- except:
- pass
-
print(f"Unsloth: Successfully uploaded GGUF to https://huggingface.co/{full_repo_id}")
return full_repo_id
@@ -1166,28 +1156,6 @@ class FastSentenceTransformer(FastModel):
modeling_distilbert.DistilBertModel.forward = forward
- @staticmethod
- def _add_unsloth_tags(
- repo_id,
- token,
- tags = None,
- ):
- """Add Unsloth + sentence-transformers tags to the HF Hub repo."""
- from huggingface_hub import HfApi
-
- api = HfApi(token = token)
- if tags is None:
- tags = []
- tags.extend(["unsloth", "sentence-transformers"])
- try:
- api.add_tags(
- repo_id = repo_id,
- tags = tags,
- repo_type = "model",
- )
- except:
- pass
-
@staticmethod
def _add_unsloth_branding(save_directory):
"""Add Unsloth branding to the sentence-transformers-generated README.md."""
@@ -2818,7 +2786,6 @@ class FastSentenceTransformer(FastModel):
)
except:
pass
- FastSentenceTransformer._add_unsloth_tags(repo_id, hub_token)
with tempfile.TemporaryDirectory() as temp_dir:
self.save_pretrained_merged(temp_dir, **push_kwargs)
api.upload_folder(
@@ -3093,8 +3060,6 @@ class FastSentenceTransformer(FastModel):
except:
pass
- FastSentenceTransformer._add_unsloth_tags(repo_id, token)
-
with tempfile.TemporaryDirectory() as temp_dir:
self.save_pretrained_merged(temp_dir, **kwargs)
api.upload_folder(
diff --git a/unsloth/save.py b/unsloth/save.py
index 90e4ff0e835..ea3addd93ec 100644
--- a/unsloth/save.py
+++ b/unsloth/save.py
@@ -5899,13 +5899,18 @@ def unsloth_push_to_hub_gguf(
datasets_yaml = "".join(f"- {json.dumps(d)}\n" for d in datasets or [])
if datasets_yaml:
datasets_yaml = "datasets:\n" + datasets_yaml
+ if isinstance(tags, str):
+ tags = [tags]
+ # HfApi has no add_tags: tags reach the Hub only through the card.
+ card_tags = ["gguf", "llama.cpp", "llama-cpp", "unsloth"]
+ if is_vlm:
+ card_tags.append("vision-language-model")
+ tags_yaml = "".join(
+ f"- {json.dumps(t)}\n" for t in dict.fromkeys(card_tags + list(tags or []))
+ )
readme_content = f"""---
tags:
-- gguf
-- llama.cpp
-- unsloth
-{"- vision-language-model" if is_vlm else ""}
-{datasets_yaml}---
+{tags_yaml}{datasets_yaml}---
# {repo_id.split("/")[-1]} : GGUF
@@ -5976,21 +5981,6 @@ This model was finetuned and converted to GGUF format using [Unsloth](https://gi
)
print(f"Unsloth: Successfully uploaded GGUF to {destination}")
- if tags is None:
- tags = []
- tags.extend(["gguf", "llama-cpp", "unsloth"])
- if is_vlm:
- tags.append("vision-language-model")
-
- try:
- api.add_tags(
- repo_id = full_repo_id,
- tags = tags,
- repo_type = "model",
- )
- except:
- pass
-
except Exception as e:
raise RuntimeError(f"Failed to upload to Hugging Face Hub: {_describe_exception(e)}") from e