c4-en-processor / process_small.py
mohit-vashishta's picture
Upload folder using huggingface_hub
a9cd940 verified
Raw History Blame Contribute Delete
4.82 kB
#!/usr/bin/env python3
"""
HF Space Test - Small dataset processor
Tests if HF Spaces can handle Bhaari AI processing within 50GB disk limit
Uses small pending dataset: ADP CodeActInstruct (56.8MB)
"""
import json, time, os, sys
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download, upload_file
TOKEN = os.environ.get("HF_TOKEN", "")
HF_REPO = "mohit-vashishta/bhaari-training-data-100b"
TARGET_REPO = "mohit-vashishta/bhaari-training-data-100b"
WORK_DIR = Path("/workspace")
DATA_DIR = WORK_DIR / "data"
TOKENIZER_PATH = WORK_DIR / "tokenizer.json"
for d in [DATA_DIR]:
d.mkdir(parents=True, exist_ok=True)
def log(msg):
ts = time.strftime("%H:%M:%S")
print(f"[{ts}] {msg}")
sys.stdout.flush()
def check_disk():
stat = os.statvfs("/workspace")
free_gb = (stat.f_bavail * stat.f_frsize) / 1e9
used_gb = ((stat.f_blocks - stat.f_bfree) * stat.f_frsize) / 1e9
total_gb = (stat.f_blocks * stat.f_frsize) / 1e9
return total_gb, used_gb, free_gb
def main():
log("=" * 60)
log("HF Space Test - Small Dataset (ADP CodeActInstruct)")
log("=" * 60)
total, used, free = check_disk()
log(f"Disk: Total={total:.1f}GB, Used={used:.1f}GB, Free={free:.1f}GB")
if not TOKEN:
log("ERROR: HF_TOKEN not set in Space secrets")
return
# Download tokenizer
log("Downloading tokenizer...")
try:
hf_hub_download(HF_REPO, "tokenizer/tokenizer.json",
local_dir=str(WORK_DIR), token=TOKEN)
log("Tokenizer downloaded")
except Exception as e:
log(f"Tokenizer download failed: {e}")
return
# Test dataset: ADP CodeActInstruct (56.8MB JSONL)
test_file = "phase_6_agent/adp_codeactinstruct.jsonl"
log(f"Downloading {test_file}...")
try:
local_jsonl = hf_hub_download(HF_REPO, test_file,
repo_type="dataset", token=TOKEN)
log(f"Downloaded: {os.path.getsize(local_jsonl)/1e6:.1f}MB")
except Exception as e:
log(f"Download failed: {e}")
log("Trying alternative small dataset...")
# Fallback: Score Reasoning (236.9MB)
test_file = "phase_3_reasoning/score_reasoning.jsonl"
try:
local_jsonl = hf_hub_download(HF_REPO, test_file,
repo_type="dataset", token=TOKEN)
log(f"Downloaded: {os.path.getsize(local_jsonl)/1e6:.1f}MB")
except Exception as e2:
log(f"Fallback also failed: {e2}")
return
total, used, free = check_disk()
log(f"Disk after download: Used={used:.1f}GB, Free={free:.1f}GB")
# Tokenize with Python (no Rust needed for test)
log("Loading tokenizer...")
from tokenizers import Tokenizer
tok = Tokenizer.from_file(str(TOKENIZER_PATH))
log("Tokenizing...")
t0 = time.time()
all_tokens = []
row_count = 0
with open(local_jsonl) as f:
for line in f:
try:
obj = json.loads(line)
text = obj.get("text", "").strip()
if len(text) >= 50:
encoded = tok.encode(text)
all_tokens.extend(encoded.ids)
row_count += 1
except:
continue
tok_time = time.time() - t0
log(f"Tokenized {row_count:,} rows, {len(all_tokens):,} tokens in {tok_time:.1f}s")
log(f"Speed: {len(all_tokens)/tok_time/1000:.0f}K tokens/sec")
# Save as BHRI format
output_path = DATA_DIR / "adp_codeactinstruct.npy"
log(f"Saving BHRI format to {output_path}...")
import struct
with open(output_path, "wb") as f:
f.write(b"BHRI")
f.write(struct.pack("<I", 1)) # version
f.write(struct.pack("<I", len(all_tokens)))
f.write(struct.pack("<I", 500000)) # control token
for token in all_tokens:
f.write(struct.pack("<I", token))
output_size = output_path.stat().st_size
log(f"Output: {output_size/1e6:.1f}MB")
total, used, free = check_disk()
log(f"Disk after tokenize: Used={used:.1f}GB, Free={free:.1f}GB")
# Upload back to HF
log("Uploading to HF...")
try:
api = HfApi(token=TOKEN)
api.upload_file(
path_or_fileobj=str(output_path),
path_in_repo="phase_6_agent/adp_codeactinstruct.npy",
repo_id=TARGET_REPO,
repo_type="dataset"
)
log("Upload successful!")
except Exception as e:
log(f"Upload failed: {e}")
total, used, free = check_disk()
log(f"Final disk: Used={used:.1f}GB, Free={free:.1f}GB")
log("=" * 60)
log("TEST COMPLETE")
log("=" * 60)
if __name__ == "__main__":
main()