Datasets:
File size: 5,580 Bytes
b42aded | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | #!/usr/bin/env python3
"""Download data from HuggingFace datasets and upload to OpenTransformer/web-crawl-2026
V2: replaced broken sources, added looping and resume logic"""
import os
import json
import gzip
import time
import traceback
from datasets import load_dataset
from huggingface_hub import HfApi, login
HF_TOKEN = "HF_TOKEN_REDACTED"
TARGET_REPO = "OpenTransformer/web-crawl-2026"
OUTPUT_DIR = "/workspace/scraped_data"
CHUNK_SIZE = 50000
STATE_FILE = "/workspace/scrape_state.json"
os.makedirs(OUTPUT_DIR, exist_ok=True)
login(token=HF_TOKEN)
api = HfApi(token=HF_TOKEN)
# Sources that work with streaming and don't use deprecated dataset scripts
SOURCES = [
# FineWeb already done (298 chunks), but FineWeb-Edu is separate
("HuggingFaceFW/fineweb-edu", "sample-10BT", "train", "text"),
("allenai/c4", "en", "train", "text"),
("cerebras/SlimPajama-627B", None, "train", "text"),
("uonlp/CulturaX", "en", "train", "text"),
]
def load_state():
if os.path.exists(STATE_FILE):
with open(STATE_FILE) as f:
return json.load(f)
return {}
def save_state(state):
with open(STATE_FILE, "w") as f:
json.dump(state, f)
def upload_chunk(filepath, remote_name):
for attempt in range(3):
try:
api.upload_file(
path_or_fileobj=filepath,
path_in_repo="data/" + remote_name,
repo_id=TARGET_REPO,
repo_type="dataset",
)
print(" Uploaded " + remote_name, flush=True)
return True
except Exception as e:
print(" Upload attempt %d failed: %s" % (attempt+1, e), flush=True)
time.sleep(10 * (attempt+1))
return False
def process_source(name, config, split, text_field):
sep = "=" * 60
print("\n" + sep, flush=True)
print("Source: %s (%s)" % (name, config or "default"), flush=True)
print(sep, flush=True)
state = load_state()
source_tag = name.replace("/", "_")
if config:
source_tag += "_" + config.replace("-", "_")
state_key = source_tag
# Resume from last chunk
start_chunk = state.get(state_key, {}).get("next_chunk", 0)
skip_rows = start_chunk * CHUNK_SIZE
print(" Resuming from chunk %d (skipping %d rows)" % (start_chunk, skip_rows), flush=True)
try:
if config:
ds = load_dataset(name, config, split=split, streaming=True)
else:
ds = load_dataset(name, split=split, streaming=True)
except Exception as e:
print(" Failed to load: %s" % e, flush=True)
return False
batch = []
chunk_num = start_chunk
total_rows = 0
skipped = 0
for example in ds:
if skipped < skip_rows:
skipped += 1
if skipped % 500000 == 0:
print(" Skipping... %d/%d" % (skipped, skip_rows), flush=True)
continue
text = example.get(text_field) or example.get("text") or example.get("content") or ""
if len(text) < 100:
continue
row = {
"text": text,
"source": name,
"url": example.get("url", ""),
}
batch.append(row)
total_rows += 1
if len(batch) >= CHUNK_SIZE:
chunk_name = "%s_chunk%04d.jsonl.gz" % (source_tag, chunk_num)
chunk_path = os.path.join(OUTPUT_DIR, chunk_name)
with gzip.open(chunk_path, "wt", encoding="utf-8") as f:
for item in batch:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
print(" Chunk %d: %s rows total" % (chunk_num, "{:,}".format(total_rows + skip_rows)), flush=True)
if upload_chunk(chunk_path, chunk_name):
os.remove(chunk_path)
chunk_num += 1
state[state_key] = {"next_chunk": chunk_num}
save_state(state)
else:
print(" Upload failed, will retry next run", flush=True)
os.remove(chunk_path)
return False
batch = []
# Final partial batch
if batch:
chunk_name = "%s_chunk%04d.jsonl.gz" % (source_tag, chunk_num)
chunk_path = os.path.join(OUTPUT_DIR, chunk_name)
with gzip.open(chunk_path, "wt", encoding="utf-8") as f:
for item in batch:
f.write(json.dumps(item, ensure_ascii=False) + "\n")
if upload_chunk(chunk_path, chunk_name):
os.remove(chunk_path)
chunk_num += 1
state[state_key] = {"next_chunk": chunk_num, "done": True}
save_state(state)
print(" Done: %s rows from %s" % ("{:,}".format(total_rows + skip_rows), name), flush=True)
return False
if __name__ == "__main__":
print("Web Crawl Data Collector V2", flush=True)
print("Target: %s" % TARGET_REPO, flush=True)
start = time.time()
for name, config, split, text_field in SOURCES:
state = load_state()
source_tag = name.replace("/", "_")
if config:
source_tag += "_" + config.replace("-", "_")
if state.get(source_tag, {}).get("done"):
print("Skipping %s (already done)" % name, flush=True)
continue
try:
process_source(name, config, split, text_field)
except Exception as e:
print("Error processing %s: %s" % (name, e), flush=True)
traceback.print_exc()
continue
elapsed = time.time() - start
print("\nFinished in %.1f hours" % (elapsed/3600), flush=True)
|