Skip to content
Closed
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
31 changes: 22 additions & 9 deletions .github/scripts/publish_hf_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
The ovos-localize-intents README is left untouched: this repo carries no card
template for it.

Each repo is published on its own: a failure on one is printed and does not
stop the other, and the script exits non-zero when any repo failed. The corpus
goes first because it is the one a model trains on. A repo missing on the Hub
is reported, never created.

An export that holds no CSV row or no corpus row is refused before the Hub is
contacted: replacing a dataset in full from an empty tree would delete it.
"""
Expand Down Expand Up @@ -88,17 +93,25 @@ def main() -> None:
"refusing to publish an empty export, which would delete the published datasets.")

api = HfApi(token=token)
for repo_id, build in ((CSV_REPO_ID, csv_operations), (CORPUS_REPO_ID, corpus_operations)):
operations = build(export_dir, api.list_repo_files(repo_id=repo_id, repo_type="dataset"))
api.create_commit(
repo_id=repo_id,
repo_type="dataset",
operations=operations,
commit_message=f"chore: refresh from {repository} run {run_id}",
commit_description=run_url,
)
failed = []
for repo_id, build in ((CORPUS_REPO_ID, corpus_operations), (CSV_REPO_ID, csv_operations)):
try:
operations = build(export_dir, api.list_repo_files(repo_id=repo_id, repo_type="dataset"))
api.create_commit(
repo_id=repo_id,
repo_type="dataset",
operations=operations,
commit_message=f"chore: refresh from {repository} run {run_id}",
commit_description=run_url,
)
except Exception as exc:
print(f"FAILED {repo_id}: {type(exc).__name__}: {exc}")
failed.append(repo_id)
continue
added = sum(isinstance(op, CommitOperationAdd) for op in operations)
print(f"published {repo_id}: {added} files added, {len(operations) - added} deleted")
if failed:
sys.exit(f"failed to publish: {', '.join(failed)}")


if __name__ == "__main__":
Expand Down
63 changes: 63 additions & 0 deletions test/unittests/test_publish_hf_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,66 @@ def test_published_csv_holds_intent_rows_only(tmp_path):
["en-US", "ovos-skill-fixture", "foo.intent", "turn on the lamp"],
["en-US", "ovos-skill-fixture", "foo.intent", "turn on the light"],
]


def _repository_not_found():
from types import SimpleNamespace

from huggingface_hub.errors import RepositoryNotFoundError

response = SimpleNamespace(status_code=404, headers={}, text="", content=b"", json=dict, request=None)
return RepositoryNotFoundError("404 Client Error: repository not found", response=response)


def test_missing_csv_repo_does_not_stop_the_corpus_publish(monkeypatch, tmp_path, capsys):
"""A Hub 404 on the CSV repo is reported and fails the run, after the corpus repo is published."""
from huggingface_hub import HfApi

module = _load_module()
committed = {}

def list_repo_files(self, repo_id, **kwargs):
if repo_id == module.CSV_REPO_ID:
raise _repository_not_found()
return []

monkeypatch.setattr(HfApi, "list_repo_files", list_repo_files)
monkeypatch.setattr(HfApi, "create_commit",
lambda self, repo_id, operations, **kwargs: committed.update({repo_id: operations}))
monkeypatch.setenv("HF_TOKEN", "fake")
monkeypatch.setenv("INTENTS_EXPORT_DIR", str(_export_dir(tmp_path)))

with pytest.raises(SystemExit) as exit_info:
module.main()

assert exit_info.value.code not in (0, None)
assert list(committed) == [module.CORPUS_REPO_ID]
out = capsys.readouterr().out.splitlines()
assert out[0].startswith(f"published {module.CORPUS_REPO_ID}: 4 files added, 0 deleted")
assert out[1].startswith(f"FAILED {module.CSV_REPO_ID}: RepositoryNotFoundError: 404 Client Error")


def test_failed_corpus_repo_does_not_stop_the_csv_publish(monkeypatch, tmp_path, capsys):
from huggingface_hub import HfApi

module = _load_module()
committed = {}

def create_commit(self, repo_id, operations, **kwargs):
if repo_id == module.CORPUS_REPO_ID:
raise RuntimeError("boom")
committed[repo_id] = operations

monkeypatch.setattr(HfApi, "list_repo_files", lambda self, repo_id, **kwargs: [])
monkeypatch.setattr(HfApi, "create_commit", create_commit)
monkeypatch.setenv("HF_TOKEN", "fake")
monkeypatch.setenv("INTENTS_EXPORT_DIR", str(_export_dir(tmp_path)))

with pytest.raises(SystemExit) as exit_info:
module.main()

assert exit_info.value.code not in (0, None)
assert list(committed) == [module.CSV_REPO_ID]
out = capsys.readouterr().out.splitlines()
assert out[0] == f"FAILED {module.CORPUS_REPO_ID}: RuntimeError: boom"
assert out[1].startswith(f"published {module.CSV_REPO_ID}:")
Loading