#!/usr/bin/env python3 """ HF Space Test - C4-en mini batch processor Tests if HF Spaces can handle C4-en processing within 50GB disk limit """ import json, time, os, subprocess, sys, shutil from pathlib import Path from concurrent.futures import ThreadPoolExecutor, as_completed from huggingface_hub import HfApi, hf_hub_download TOKEN = os.environ.get("HF_TOKEN", "") HF_REPO = "mohit-vashishta/bhaari-training-data-100b" BATCH_DIR = Path("/workspace/data_raw") NPY_DIR = Path("/workspace/data/c4_en") BATCH_SIZE = 5 # Small batch for 50GB limit DOWNLOAD_WORKERS = 4 DECOMPRESS_WORKERS = 4 UPLOAD_WORKERS = 4 RUST_BIN = None # Will download or use Python fallback TOKENIZER_PATH = Path("/workspace/tokenizer.json") for d in [BATCH_DIR, NPY_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 download_shard(sf): local_path = BATCH_DIR / sf.replace("/", "_") if local_path.exists() and local_path.stat().st_size > 1000: return (sf, True) try: hf_hub_download("allenai/c4", sf, repo_type="dataset", local_dir=str(BATCH_DIR), token=TOKEN) return (sf, True) except Exception as e: return (sf, False, str(e)) def decompress_and_tokenize(gz_path, snum): """Python fallback tokenizer (no Rust needed)""" jsonl_path = BATCH_DIR / f"c4_en_{snum}.jsonl" if jsonl_path.exists(): return (snum, True) rows = 0 import gzip with gzip.open(gz_path, "rt", encoding="utf-8") as gz_f: with open(jsonl_path, "w") as jsonl_f: for line in gz_f: try: obj = json.loads(line) text = obj.get("text", "").strip() if len(text) >= 50: jsonl_f.write(json.dumps({"text": text}) + "\n") rows += 1 except: continue return (snum, True) 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 C4-en Test") log("=" * 60) total, used, free = check_disk() log(f"Disk: Total={total:.1f}GB, Used={used:.1f}GB, Free={free:.1f}GB") if free < 20: log("ERROR: Not enough disk space (<20GB free)") return # Download tokenizer log("Downloading tokenizer...") try: hf_hub_download("mohit-vashishta/bhaari-training-data-100b", "tokenizer/tokenizer.json", local_dir="/workspace", token=TOKEN) log("Tokenizer downloaded") except: log("WARNING: Tokenizer not found, using Python fallback") # Test with 5 shards test_shards = [ "en.noblocklist/c4-train.00000-of-01024.json.gz", "en.noblocklist/c4-train.00001-of-01024.json.gz", "en.noblocklist/c4-train.00002-of-01024.json.gz", "en.noblocklist/c4-train.00003-of-01024.json.gz", "en.noblocklist/c4-train.00004-of-01024.json.gz", ] log(f"Testing with {len(test_shards)} shards...") # Download log("Downloading shards...") t0 = time.time() with ThreadPoolExecutor(max_workers=DOWNLOAD_WORKERS) as ex: futures = {ex.submit(download_shard, sf): sf for sf in test_shards} for fut in as_completed(futures): sf, success, *rest = fut.result() if not success: log(f" Failed: {sf}") log(f"Download done in {time.time()-t0:.0f}s") total, used, free = check_disk() log(f"Disk after download: Used={used:.1f}GB, Free={free:.1f}GB") # Decompress log("Decompressing...") t0 = time.time() with ThreadPoolExecutor(max_workers=DECOMPRESS_WORKERS) as ex: futures = {} for sf in test_shards: snum = sf.split("/")[-1].split("-")[1].split(".")[1] gz_path = BATCH_DIR / sf.replace("/", "_") if gz_path.exists(): futures[ex.submit(decompress_and_tokenize, gz_path, snum)] = gz_path for fut in as_completed(futures): gz_path = futures[fut] snum, success = fut.result() if success: gz_path.unlink() log(f"Decompress done in {time.time()-t0:.0f}s") total, used, free = check_disk() log(f"Disk after decompress: Used={used:.1f}GB, Free={free:.1f}GB") # List JSONL files jsonl_files = list(BATCH_DIR.glob("c4_en_*.jsonl")) log(f"JSONL files created: {len(jsonl_files)}") for f in jsonl_files: log(f" {f.name}: {f.stat().st_size/1e6:.0f}MB") log("=" * 60) log("TEST COMPLETE - Check disk usage above") log("=" * 60) if __name__ == "__main__": main()