OpenTransformer commited on
Commit
00c4875
·
verified ·
1 Parent(s): b42aded

Upload source/scrape_and_upload_v3.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. source/scrape_and_upload_v3.py +181 -0
source/scrape_and_upload_v3.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Download data from HuggingFace datasets and upload to OpenTransformer/web-crawl-2026
3
+ V3: Large chunks (1M rows, ~1GB compressed) to reduce number of uploads"""
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 = 1000000 # 1M rows per chunk (~1GB compressed)
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 = [
23
+ ("HuggingFaceFW/fineweb-edu", "sample-10BT", "train", "text"),
24
+ ("allenai/c4", "en", "train", "text"),
25
+ ("cerebras/SlimPajama-627B", None, "train", "text"),
26
+ ("uonlp/CulturaX", "en", "train", "text"),
27
+ ]
28
+
29
+ def load_state():
30
+ if os.path.exists(STATE_FILE):
31
+ with open(STATE_FILE) as f:
32
+ return json.load(f)
33
+ return {}
34
+
35
+ def save_state(state):
36
+ with open(STATE_FILE, "w") as f:
37
+ json.dump(state, f)
38
+
39
+ def upload_chunk(filepath, remote_name):
40
+ fsize = os.path.getsize(filepath) / (1024*1024)
41
+ print(" Uploading %s (%.1f MB)..." % (remote_name, fsize), flush=True)
42
+ for attempt in range(5):
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 %s (%.1f MB)" % (remote_name, fsize), 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(30 * (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
+ start_chunk = state.get(state_key, {}).get("next_chunk_v3", 0)
70
+ skip_rows = state.get(state_key, {}).get("total_rows_v3", 0)
71
+ print(" V3 resuming from chunk %d (skipping %d rows)" % (start_chunk, skip_rows), flush=True)
72
+
73
+ try:
74
+ if config:
75
+ ds = load_dataset(name, config, split=split, streaming=True)
76
+ else:
77
+ ds = load_dataset(name, split=split, streaming=True)
78
+ except Exception as e:
79
+ print(" Failed to load: %s" % e, flush=True)
80
+ return
81
+
82
+ chunk_num = start_chunk
83
+ total_rows = 0
84
+ skipped = 0
85
+
86
+ # Stream directly to gzip file to save memory
87
+ chunk_name = "%s_big_chunk%04d.jsonl.gz" % (source_tag, chunk_num)
88
+ chunk_path = os.path.join(OUTPUT_DIR, chunk_name)
89
+ f = gzip.open(chunk_path, "wt", encoding="utf-8")
90
+ rows_in_chunk = 0
91
+
92
+ for example in ds:
93
+ if skipped < skip_rows:
94
+ skipped += 1
95
+ if skipped % 1000000 == 0:
96
+ print(" Skipping... %d/%d" % (skipped, skip_rows), flush=True)
97
+ continue
98
+
99
+ text = example.get(text_field) or example.get("text") or example.get("content") or ""
100
+ if len(text) < 100:
101
+ continue
102
+
103
+ row = json.dumps({
104
+ "text": text,
105
+ "source": name,
106
+ "url": example.get("url", ""),
107
+ }, ensure_ascii=False)
108
+ f.write(row + "\n")
109
+ rows_in_chunk += 1
110
+ total_rows += 1
111
+
112
+ if rows_in_chunk % 100000 == 0:
113
+ print(" Chunk %d progress: %dk rows, total: %dk" % (chunk_num, rows_in_chunk//1000, (total_rows+skip_rows)//1000), flush=True)
114
+
115
+ if rows_in_chunk >= CHUNK_SIZE:
116
+ f.close()
117
+ print(" Chunk %d complete: %d rows" % (chunk_num, rows_in_chunk), flush=True)
118
+
119
+ if upload_chunk(chunk_path, chunk_name):
120
+ os.remove(chunk_path)
121
+ chunk_num += 1
122
+ state[state_key] = state.get(state_key, {})
123
+ state[state_key]["next_chunk_v3"] = chunk_num
124
+ state[state_key]["total_rows_v3"] = total_rows + skip_rows
125
+ save_state(state)
126
+ else:
127
+ print(" Upload failed, will retry next run", flush=True)
128
+ try: os.remove(chunk_path)
129
+ except: pass
130
+ return
131
+
132
+ # Start new chunk
133
+ chunk_name = "%s_big_chunk%04d.jsonl.gz" % (source_tag, chunk_num)
134
+ chunk_path = os.path.join(OUTPUT_DIR, chunk_name)
135
+ f = gzip.open(chunk_path, "wt", encoding="utf-8")
136
+ rows_in_chunk = 0
137
+
138
+ # Final partial chunk
139
+ f.close()
140
+ if rows_in_chunk > 0:
141
+ print(" Final chunk %d: %d rows" % (chunk_num, rows_in_chunk), flush=True)
142
+ if upload_chunk(chunk_path, chunk_name):
143
+ os.remove(chunk_path)
144
+ chunk_num += 1
145
+ state[state_key] = state.get(state_key, {})
146
+ state[state_key]["next_chunk_v3"] = chunk_num
147
+ state[state_key]["total_rows_v3"] = total_rows + skip_rows
148
+ state[state_key]["done"] = True
149
+ save_state(state)
150
+ else:
151
+ try: os.remove(chunk_path)
152
+ except: pass
153
+ state[state_key] = state.get(state_key, {})
154
+ state[state_key]["done"] = True
155
+ save_state(state)
156
+
157
+ print(" Done: %s total rows from %s" % ("{:,}".format(total_rows + skip_rows), name), flush=True)
158
+
159
+ if __name__ == "__main__":
160
+ print("Web Crawl Data Collector V3 (Large Chunks)", flush=True)
161
+ print("Target: %s" % TARGET_REPO, flush=True)
162
+ print("Chunk size: %d rows" % CHUNK_SIZE, flush=True)
163
+ start = time.time()
164
+
165
+ for name, config, split, text_field in SOURCES:
166
+ state = load_state()
167
+ source_tag = name.replace("/", "_")
168
+ if config:
169
+ source_tag += "_" + config.replace("-", "_")
170
+ if state.get(source_tag, {}).get("done"):
171
+ print("Skipping %s (already done)" % name, flush=True)
172
+ continue
173
+ try:
174
+ process_source(name, config, split, text_field)
175
+ except Exception as e:
176
+ print("Error processing %s: %s" % (name, e), flush=True)
177
+ traceback.print_exc()
178
+ continue
179
+
180
+ elapsed = time.time() - start
181
+ print("\nFinished in %.1f hours" % (elapsed/3600), flush=True)