Skip to content

Commit e14eb30

Browse files
authored
Merge pull request #182 from interscript/sadeedbench-hf-fetch
feat(sadeedbench): --data accepts the HF dataset id
2 parents 32a694e + 9a33adb commit e14eb30

3 files changed

Lines changed: 42 additions & 7 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sadeed = [
4141
"prettytable>=3.9",
4242
"pandas>=2.0",
4343
"pyarrow>=14.0",
44+
"huggingface_hub>=0.23",
4445
]
4546
dev = [
4647
"pytest>=8.0",

src/sadeedbench/cli.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,33 @@ def _read_preds(path: Path, key: str) -> list[str]:
3737
return preds
3838

3939

40-
def _load_gold(path: Path) -> list[str]:
40+
def _load_gold(data: str) -> list[str]:
41+
"""Gold outputs from a local parquet or an HF dataset id.
42+
43+
A path (existing file) is read directly. Anything else is treated
44+
as a Hugging Face dataset id (default split file train.parquet) and
45+
resolved through huggingface_hub.snapshot_download with the local
46+
cache."""
4147
import pandas as pd
4248

43-
table = pd.read_parquet(path)
44-
return table["output"].tolist()
49+
p = Path(data)
50+
if p.exists():
51+
return pd.read_parquet(p)["output"].tolist()
52+
import huggingface_hub
53+
54+
snapshot = Path(
55+
huggingface_hub.snapshot_download(data, repo_type="dataset")
56+
)
57+
return pd.read_parquet(snapshot / "train.parquet")["output"].tolist()
4558

4659

4760
def main(argv: list[str] | None = None) -> int:
4861
parser = argparse.ArgumentParser(prog="interscript-sadeed-eval")
4962
sub = parser.add_subparsers(dest="cmd", required=True)
5063
score = sub.add_parser("score", help="score a predictions file")
5164
score.add_argument("--preds", type=Path, required=True)
52-
score.add_argument("--data", type=Path, required=True,
53-
help="SadeedDiac-25 parquet (input/output columns)")
65+
score.add_argument("--data", required=True,
66+
help="parquet path or HF dataset id (Misraj/SadeedDiac-25)")
5467
score.add_argument("--key", default="student",
5568
help="JSONL row key carrying the prediction")
5669
score.add_argument("--vs", type=Path, help="reference predictions file")

tests/test_sadeedbench_cli.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
GT = ["قَوْلُهُ فَحُكْمُهَا", "مُكْتَبَّةٌ جَمِيلَةٌ"]
2121

2222

23-
def _parquet(tmp_path: Path) -> Path:
24-
p = tmp_path / "bench.parquet"
23+
def _parquet(tmp_path: Path, name: str = "bench.parquet") -> Path:
24+
p = tmp_path / name
2525
pd.DataFrame({"input": ["قوله فحكمها", "مكتبة جميلة"], "output": GT}).to_parquet(p)
2626
return p
2727

@@ -64,3 +64,24 @@ def test_bootstrap_vs_reference(tmp_path: Path, capsys) -> None:
6464
out = json.loads(capsys.readouterr().out)
6565
assert rc == 0
6666
assert out["vs"]["delta"] < 0 # candidate (perfect) better than stripped reference
67+
68+
69+
def test_data_accepts_hf_dataset_id(monkeypatch, tmp_path, capsys) -> None:
70+
# --data may name the HF dataset instead of a local parquet; the
71+
# loader resolves it through huggingface_hub with a local cache
72+
import sadeedbench.cli as cli
73+
74+
cached = _parquet(tmp_path, name="train.parquet")
75+
calls = {}
76+
77+
def fake_snapshot(repo_id, repo_type):
78+
calls["repo_id"] = repo_id
79+
return str(cached.parent)
80+
81+
monkeypatch.setattr("huggingface_hub.snapshot_download", fake_snapshot, raising=False)
82+
rc = cli.main(["score", "--preds", str(_preds(tmp_path, "p.jsonl", [
83+
{"idx": i, "student": g} for i, g in enumerate(GT)])), "--data", "Misraj/SadeedDiac-25"])
84+
assert rc == 0
85+
assert calls["repo_id"] == "Misraj/SadeedDiac-25"
86+
out = json.loads(capsys.readouterr().out)
87+
assert out["der_ce"] == 0.0

0 commit comments

Comments
 (0)