Upload code/train_sft.py with huggingface_hub
Browse files181 unmodified lines1821831841851861871881891901 unmodified line192193194195196197198199200201202203204205206207208209210211212213214215216217218219181 unmodified lines"val_domains": os.path.join(found_dir, "sft_v13_val_domains.npy"),}# Auto-download from Hugging Face if not found locallyprint(f"📥 Local dataset not found, downloading SFT v13 from Hugging Face: {data_repo_id}...")dl_dir = "sft_v13_data"os.makedirs(dl_dir, exist_ok=True)files = [1 unmodified line"sft_v13_val_tokens.npy", "sft_v13_val_labels.npy", "sft_v13_val_offsets.npy", "sft_v13_val_domains.npy"]paths = {}for f in files:key = f.replace("sft_v13_", "").replace(".npy", "")# Map tokens -> ids for SFTDataset constructorif key == "train_tokens": key = "train_ids"elif key == "val_tokens": key = "val_ids"try:paths[key] = hf_hub_download(repo_id=data_repo_id,filename=f"sft_v13/{f}",repo_type="dataset",local_dir=dl_dir,token=hf_token)except Exception:paths[key] = hf_hub_download(repo_id=data_repo_id,filename=f,repo_type="dataset",local_dir=dl_dir,token=hf_token)return paths# --- 5. Main Training Execution Loop ---181 unmodified lines1821831841851861871881891901 unmodified line192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229181 unmodified lines"val_domains": os.path.join(found_dir, "sft_v13_val_domains.npy"),}# Auto-download from Hugging Face via Direct HTTP Streaming (Zero Locks / Zero xet_get bugs)print(f"📥 Local dataset not found, downloading SFT v13 from Hugging Face: {data_repo_id}...")dl_dir = os.path.abspath(data_dir if data_dir and not data_dir.startswith("tokenized_data") else "sft_v13_data")os.makedirs(dl_dir, exist_ok=True)files = [1 unmodified line"sft_v13_val_tokens.npy", "sft_v13_val_labels.npy", "sft_v13_val_offsets.npy", "sft_v13_val_domains.npy"]paths = {}import requestsfrom tqdm import tqdmheaders = {"Authorization": f"Bearer {hf_token}"} if hf_token else {}for f in files:key = f.replace("sft_v13_", "").replace(".npy", "")if key == "train_tokens": key = "train_ids"elif key == "val_tokens": key = "val_ids"target_fpath = os.path.join(dl_dir, f)if os.path.exists(target_fpath) and os.path.getsize(target_fpath) > 0:paths[key] = target_fpathcontinueurl = f"https://huggingface.co/datasets/{data_repo_id}/resolve/main/sft_v13/{f}"print(f"📥 Streaming: sft_v13/{f} -> {target_fpath}...")try:with requests.get(url, headers=headers, stream=True, timeout=60) as r:r.raise_for_status()total_size = int(r.headers.get("content-length", 0))with open(target_fpath, "wb") as fp, tqdm(total=total_size, unit="B", unit_scale=True, desc=f, ncols=80) as pbar:for chunk in r.iter_content(chunk_size=8 * 1024 * 1024):if chunk:fp.write(chunk)pbar.update(len(chunk))paths[key] = target_fpathexcept Exception as e:print(f" ⚠️ Error downloading {f}: {e}")raise ereturn paths# --- 5. Main Training Execution Loop ---
code/train_sft.py+28-18