Download code/harness/data.py from quocbao747/embedding-model-benchmark: direct link, hf CLI and curl.
- Browser
- Download file 2.72 kB
-
https://huggingface.co/quocbao747/embedding-model-benchmark/resolve/main/code/harness/data.py
- Command line
-
hf download hf://quocbao747/embedding-model-benchmark/code/harness/data.py
-
curl -L -o data.py https://huggingface.co/quocbao747/embedding-model-benchmark/resolve/main/code/harness/data.py
2.72 kB
| """Task loading for the GPU run. Task spec: 'Name' or 'Name:hf_subset'. | |
| Handles both mteb dataset layouts (task.dataset[subset][split] and legacy task.corpus/queries/relevant_docs).""" | |
| import os, pickle | |
| from pathlib import Path | |
| import mteb | |
| DATA = Path(os.path.expanduser(os.environ.get("EMBED_DATA", "~/embed/data"))); DATA.mkdir(parents=True, exist_ok=True) | |
| TIER1 = ["ZacLegalTextRetrieval", "TVPLRetrieval", "GreenNodeTableMarkdownRetrieval", "WebFAQRetrieval:vie", | |
| "VieQuADRetrieval", "MLQARetrieval:vie-vie", "XQuADRetrieval:vi", "BelebeleRetrieval:vie_Latn-vie_Latn", | |
| "PublicHealthQA:vietnamese"] | |
| TIER2 = ["SciFact-VN", "NFCorpus-VN", "ArguAna-VN", "FiQA2018-VN"] | |
| def _pick(d, sub, split): | |
| if d is None: | |
| return None | |
| if sub is not None and sub in d: | |
| d = d[sub] | |
| elif sub is None and len(d) == 1 and split not in d: | |
| d = d[list(d.keys())[0]] | |
| if split in d: | |
| d = d[split] | |
| return d | |
| def _load(spec): | |
| name, sub = (spec.split(":", 1) + [None])[:2] | |
| t = mteb.get_task(name, hf_subsets=[sub]) if sub else mteb.get_task(name) | |
| t.load_data() | |
| split = t.metadata.eval_splits[0] | |
| if t.dataset: | |
| ds = t.dataset | |
| s = ds[sub] if sub and sub in ds else ds[list(ds.keys())[0]] | |
| s = s[split] if split in s else s[list(s.keys())[0]] | |
| corpus = [{"id": str(r["id"]), "title": r.get("title") or "", "text": r["text"] or ""} for r in s["corpus"]] | |
| queries = [{"id": str(r["id"]), "text": r["text"]} for r in s["queries"]] | |
| rel = s["relevant_docs"] | |
| else: | |
| c, q, rel = _pick(t.corpus, sub, split), _pick(t.queries, sub, split), _pick(t.relevant_docs, sub, split) | |
| corpus = [{"id": str(k), "title": (v.get("title") or "") if isinstance(v, dict) else "", | |
| "text": (v.get("text") or "") if isinstance(v, dict) else str(v)} for k, v in c.items()] | |
| queries = [{"id": str(k), "text": v if isinstance(v, str) else v.get("text", "")} for k, v in q.items()] | |
| qrels = {str(q): {str(d): int(v) for d, v in docs.items()} for q, docs in rel.items()} | |
| queries = [q for q in queries if q["id"] in qrels] | |
| return corpus, queries, qrels | |
| def load_task(spec): | |
| f = DATA / (spec.replace(":", "__").replace("/", "_") + ".pkl") | |
| if f.exists(): | |
| return pickle.load(open(f, "rb")) | |
| out = _load(spec) | |
| pickle.dump(out, open(f, "wb"), protocol=4) | |
| return out | |
| def doc_plain(d): | |
| t = (d.get("title") or "").strip() | |
| return (t + " " + d["text"]).strip() if t else d["text"] | |
| if __name__ == "__main__": | |
| for s in TIER1 + TIER2: | |
| c, q, r = load_task(s) | |
| print(f"DS\t{s}\t{len(c)}\t{len(q)}\t{sum(len(v) for v in r.values())}", flush=True) | |