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