Spaces:
Sleeping
Sleeping
Update data_utils.py
Browse files- data_utils.py +60 -44
data_utils.py
CHANGED
|
@@ -1,28 +1,22 @@
|
|
| 1 |
-
# data_utils.py
|
| 2 |
-
# Lightweight dataset loaders + simple hashing vectorizer (no sklearn)
|
| 3 |
-
# Works on CPU-only Spaces and avoids heavy tokenizers.
|
| 4 |
|
| 5 |
from typing import List, Tuple
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
-
from datasets import load_dataset
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# -----------------------------
|
| 11 |
# Hashing vectorizer (unigram + bigram)
|
| 12 |
# -----------------------------
|
| 13 |
def hash_vectorize(texts: List[str], n_features: int = 4096, seed: int = 1234) -> np.ndarray:
|
| 14 |
-
"""
|
| 15 |
-
Very fast, tokenizer-free vectorizer.
|
| 16 |
-
- Lowercases text
|
| 17 |
-
- Splits on whitespace
|
| 18 |
-
- Uses Python's hash to place unigrams + bigrams into a fixed-size bag
|
| 19 |
-
- L2-normalizes each row
|
| 20 |
-
"""
|
| 21 |
n = len(texts)
|
| 22 |
X = np.zeros((n, n_features), dtype=np.float32)
|
| 23 |
-
|
| 24 |
for i, t in enumerate(texts):
|
| 25 |
-
if t
|
| 26 |
continue
|
| 27 |
toks = t.lower().split()
|
| 28 |
prev = None
|
|
@@ -34,30 +28,53 @@ def hash_vectorize(texts: List[str], n_features: int = 4096, seed: int = 1234) -
|
|
| 34 |
h2 = hash(bg) % n_features
|
| 35 |
X[i, h2] += 1.0
|
| 36 |
prev = tok
|
| 37 |
-
# L2 norm
|
| 38 |
norm = float(np.linalg.norm(X[i])) + 1e-8
|
| 39 |
X[i] /= norm
|
| 40 |
return X
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# -----------------------------
|
| 44 |
-
# PIQA
|
| 45 |
-
# Produces pair-expanded binary rows for a quick proxy classifier.
|
| 46 |
# -----------------------------
|
| 47 |
-
def load_piqa(subset: int = 800, seed: int = 42)
|
| 48 |
"""
|
| 49 |
Returns:
|
| 50 |
Xtr_txt, ytr, Xva_txt, yva
|
| 51 |
-
Where:
|
| 52 |
-
- For each original PIQA example, we emit TWO rows:
|
| 53 |
-
[goal + sol1] with label 1 if sol1 is correct else 0
|
| 54 |
-
[goal + sol2] with label 1 if sol2 is correct else 0
|
| 55 |
-
"""
|
| 56 |
-
ds = load_dataset("piqa")
|
| 57 |
-
tr = ds["train"]
|
| 58 |
-
va = ds["validation"]
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
rng = np.random.RandomState(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
idx_tr = rng.choice(len(tr), size=min(subset, len(tr)), replace=False)
|
| 62 |
idx_va = rng.choice(len(va), size=min(max(subset // 4, 200), len(va)), replace=False)
|
| 63 |
|
|
@@ -69,37 +86,37 @@ def load_piqa(subset: int = 800, seed: int = 42) -> Tuple[list, np.ndarray, list
|
|
| 69 |
sol1 = (p.get("sol1") or "").strip()
|
| 70 |
sol2 = (p.get("sol2") or "").strip()
|
| 71 |
label = int(p.get("label", 0))
|
| 72 |
-
|
| 73 |
-
X_text.append(f"{stem} {
|
| 74 |
-
y.append(1 if label == 0 else 0)
|
| 75 |
-
|
| 76 |
-
X_text.append(f"{stem} {sol2}")
|
| 77 |
-
y.append(1 if label == 1 else 0)
|
| 78 |
-
|
| 79 |
return X_text, np.array(y, dtype=np.int64)
|
| 80 |
|
| 81 |
Xtr_txt, ytr = pack(tr, idx_tr)
|
| 82 |
Xva_txt, yva = pack(va, idx_va)
|
| 83 |
return Xtr_txt, ytr, Xva_txt, yva
|
| 84 |
|
| 85 |
-
|
| 86 |
# -----------------------------
|
| 87 |
-
# HellaSwag
|
| 88 |
-
# Expands each example into 4 rows (one-vs-all), later regrouped into argmax.
|
| 89 |
# -----------------------------
|
| 90 |
-
def load_hellaswag(subset: int = 800, seed: int = 42)
|
| 91 |
"""
|
| 92 |
Returns:
|
| 93 |
Xtr_txt, ytr, Xva_txt, yva
|
| 94 |
-
Where:
|
| 95 |
-
- For each original example, we emit FOUR rows:
|
| 96 |
-
[context + ending_i] with label 1 if i is correct else 0
|
| 97 |
-
"""
|
| 98 |
-
ds = load_dataset("hellaswag")
|
| 99 |
-
tr = ds["train"]
|
| 100 |
-
va = ds["validation"]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
| 102 |
rng = np.random.RandomState(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
idx_tr = rng.choice(len(tr), size=min(subset, len(tr)), replace=False)
|
| 104 |
idx_va = rng.choice(len(va), size=min(max(subset // 4, 200), len(va)), replace=False)
|
| 105 |
|
|
@@ -107,7 +124,6 @@ def load_hellaswag(subset: int = 800, seed: int = 42) -> Tuple[list, np.ndarray,
|
|
| 107 |
X_text, y = [], []
|
| 108 |
for k in idxs:
|
| 109 |
p = rows[k]
|
| 110 |
-
# Some variants have keys like 'ctx' + 'ctx_a'; fall back defensively.
|
| 111 |
ctx = f"{(p.get('ctx') or '')} {(p.get('ctx_a') or '')}".strip()
|
| 112 |
endings = p.get("endings") or []
|
| 113 |
label = int(p.get("label", 0))
|
|
|
|
| 1 |
+
# data_utils.py — local-first dataset loaders + hashing vectorizer
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from typing import List, Tuple
|
| 4 |
+
import os, json
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
|
| 7 |
+
try:
|
| 8 |
+
from datasets import load_dataset # optional, used only as fallback
|
| 9 |
+
except Exception:
|
| 10 |
+
load_dataset = None
|
| 11 |
|
| 12 |
# -----------------------------
|
| 13 |
# Hashing vectorizer (unigram + bigram)
|
| 14 |
# -----------------------------
|
| 15 |
def hash_vectorize(texts: List[str], n_features: int = 4096, seed: int = 1234) -> np.ndarray:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
n = len(texts)
|
| 17 |
X = np.zeros((n, n_features), dtype=np.float32)
|
|
|
|
| 18 |
for i, t in enumerate(texts):
|
| 19 |
+
if not t:
|
| 20 |
continue
|
| 21 |
toks = t.lower().split()
|
| 22 |
prev = None
|
|
|
|
| 28 |
h2 = hash(bg) % n_features
|
| 29 |
X[i, h2] += 1.0
|
| 30 |
prev = tok
|
|
|
|
| 31 |
norm = float(np.linalg.norm(X[i])) + 1e-8
|
| 32 |
X[i] /= norm
|
| 33 |
return X
|
| 34 |
|
| 35 |
+
# -----------------------------
|
| 36 |
+
# Utilities for local JSONL
|
| 37 |
+
# -----------------------------
|
| 38 |
+
DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
|
| 39 |
+
|
| 40 |
+
def _read_jsonl(path: str):
|
| 41 |
+
out = []
|
| 42 |
+
with open(path, "r", encoding="utf-8") as f:
|
| 43 |
+
for line in f:
|
| 44 |
+
if line.strip():
|
| 45 |
+
out.append(json.loads(line))
|
| 46 |
+
return out
|
| 47 |
+
|
| 48 |
+
def _has_local(*names: str) -> bool:
|
| 49 |
+
return all(os.path.exists(os.path.join(DATA_DIR, n)) for n in names)
|
| 50 |
|
| 51 |
# -----------------------------
|
| 52 |
+
# PIQA loader (pair-expanded)
|
|
|
|
| 53 |
# -----------------------------
|
| 54 |
+
def load_piqa(subset: int = 800, seed: int = 42):
|
| 55 |
"""
|
| 56 |
Returns:
|
| 57 |
Xtr_txt, ytr, Xva_txt, yva
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
For each original PIQA example, we emit TWO rows:
|
| 60 |
+
[goal + sol1] with label 1 if sol1 correct, else 0
|
| 61 |
+
[goal + sol2] with label 1 if sol2 correct, else 0
|
| 62 |
+
"""
|
| 63 |
rng = np.random.RandomState(seed)
|
| 64 |
+
|
| 65 |
+
# Prefer local
|
| 66 |
+
tr_name, va_name = "piqa_train.jsonl", "piqa_valid.jsonl"
|
| 67 |
+
if _has_local(tr_name, va_name):
|
| 68 |
+
tr = _read_jsonl(os.path.join(DATA_DIR, tr_name))
|
| 69 |
+
va = _read_jsonl(os.path.join(DATA_DIR, va_name))
|
| 70 |
+
else:
|
| 71 |
+
# Fallback to datasets (if available)
|
| 72 |
+
if load_dataset is None:
|
| 73 |
+
raise RuntimeError("PIQA local files not found and 'datasets' not installed.")
|
| 74 |
+
ds = load_dataset("piqa")
|
| 75 |
+
tr, va = list(ds["train"]), list(ds["validation"])
|
| 76 |
+
|
| 77 |
+
# subsample
|
| 78 |
idx_tr = rng.choice(len(tr), size=min(subset, len(tr)), replace=False)
|
| 79 |
idx_va = rng.choice(len(va), size=min(max(subset // 4, 200), len(va)), replace=False)
|
| 80 |
|
|
|
|
| 86 |
sol1 = (p.get("sol1") or "").strip()
|
| 87 |
sol2 = (p.get("sol2") or "").strip()
|
| 88 |
label = int(p.get("label", 0))
|
| 89 |
+
X_text.append(f"{stem} {sol1}"); y.append(1 if label == 0 else 0)
|
| 90 |
+
X_text.append(f"{stem} {sol2}"); y.append(1 if label == 1 else 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return X_text, np.array(y, dtype=np.int64)
|
| 92 |
|
| 93 |
Xtr_txt, ytr = pack(tr, idx_tr)
|
| 94 |
Xva_txt, yva = pack(va, idx_va)
|
| 95 |
return Xtr_txt, ytr, Xva_txt, yva
|
| 96 |
|
|
|
|
| 97 |
# -----------------------------
|
| 98 |
+
# HellaSwag loader (4-way expanded)
|
|
|
|
| 99 |
# -----------------------------
|
| 100 |
+
def load_hellaswag(subset: int = 800, seed: int = 42):
|
| 101 |
"""
|
| 102 |
Returns:
|
| 103 |
Xtr_txt, ytr, Xva_txt, yva
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
For each example, we emit FOUR rows:
|
| 106 |
+
[context + ending_i] with label 1 if i is the correct ending else 0
|
| 107 |
+
"""
|
| 108 |
rng = np.random.RandomState(seed)
|
| 109 |
+
|
| 110 |
+
tr_name, va_name = "hellaswag_train.jsonl", "hellaswag_valid.jsonl"
|
| 111 |
+
if _has_local(tr_name, va_name):
|
| 112 |
+
tr = _read_jsonl(os.path.join(DATA_DIR, tr_name))
|
| 113 |
+
va = _read_jsonl(os.path.join(DATA_DIR, va_name))
|
| 114 |
+
else:
|
| 115 |
+
if load_dataset is None:
|
| 116 |
+
raise RuntimeError("HellaSwag local files not found and 'datasets' not installed.")
|
| 117 |
+
ds = load_dataset("hellaswag")
|
| 118 |
+
tr, va = list(ds["train"]), list(ds["validation"])
|
| 119 |
+
|
| 120 |
idx_tr = rng.choice(len(tr), size=min(subset, len(tr)), replace=False)
|
| 121 |
idx_va = rng.choice(len(va), size=min(max(subset // 4, 200), len(va)), replace=False)
|
| 122 |
|
|
|
|
| 124 |
X_text, y = [], []
|
| 125 |
for k in idxs:
|
| 126 |
p = rows[k]
|
|
|
|
| 127 |
ctx = f"{(p.get('ctx') or '')} {(p.get('ctx_a') or '')}".strip()
|
| 128 |
endings = p.get("endings") or []
|
| 129 |
label = int(p.get("label", 0))
|