File size: 11,747 Bytes
1340c9c
 
528f3a5
 
 
1340c9c
 
 
 
528f3a5
1340c9c
 
528f3a5
 
 
 
 
1340c9c
528f3a5
 
1340c9c
 
528f3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340c9c
 
528f3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340c9c
528f3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675be9e
528f3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340c9c
528f3a5
 
 
 
 
675be9e
528f3a5
 
 
 
 
 
 
 
 
1340c9c
528f3a5
 
1340c9c
528f3a5
 
 
 
 
 
 
 
 
 
 
 
675be9e
528f3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340c9c
528f3a5
1340c9c
528f3a5
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import os
import json
import time
import socket
import threading
import requests
import pyarrow.parquet as pq
import gc
from pathlib import Path
from huggingface_hub import HfApi

# ── Config ───────────────────────────────────────────────────────────────────
HF_TOKEN       = os.environ.get("HF_TOKEN")
RAW_DIR        = "/data/raw"
STATE_FILE     = "/data/state.json"
WORKER_TIMEOUT = 700
MAX_BUFFERED   = 999999

os.makedirs(RAW_DIR, exist_ok=True)
api          = HfApi(token=HF_TOKEN)
AUTH_HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}

# ── Sources ───────────────────────────────────────────────────────────────────
SOURCES = [
    {
        "name"    : "fineweb",
        "type"    : "hf_list",
        "repo"    : "HuggingFaceFW/fineweb-edu",
        "prefix"  : "data/CC-MAIN-2025-26",
        "skip"    : 5,
        "take"    : 10,
        "text_col": "text",
    },
    {
        "name"    : "wikipedia",
        "type"    : "hf_list",
        "repo"    : "wikimedia/wikipedia",
        "prefix"  : "20231101.en/train-",
        "skip"    : 2,
        "take"    : 18,
        "text_col": "text",
    },
    {
        "name"    : "openwebmath",
        "type"    : "hf_list",
        "repo"    : "open-web-math/open-web-math",
        "prefix"  : "data/train-",
        "skip"    : 0,
        "take"    : 6,
        "text_col": "text",
    },
    {
        "name"    : "code",
        "type"    : "url_list",
        "text_col": "text",
        "fmt"     : "jsonl",
        "urls"    : [
            f"https://huggingface.co/buckets/Neon-tech/Dataset-arranger/resolve/by-language/{lang}/shard_{str(i).zfill(6)}.jsonl?download=true"
            for lang in ["C", "C++", "Java", "Go", "Rust", "Ruby", "PHP", "SQL", "C#", "Scala", "Lua", "Perl", "CSS"]
            for i in range(2)
        ],
    },
]

# ── Keep-alive ────────────────────────────────────────────────────────────────
def serve():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind(("0.0.0.0", 7860))
    s.listen(5)
    print("βœ“ Listening on port 7860")
    while True:
        conn, _ = s.accept()
        conn.send(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
        conn.close()

# ── State ─────────────────────────────────────────────────────────────────────
def load_state():
    if os.path.exists(STATE_FILE):
        with open(STATE_FILE) as f:
            state = json.load(f)
        shards  = state["shards"]
        queue   = state.get("queue", [])
        done    = sum(1 for v in shards.values() if v["status"] == "done")
        claimed = sum(1 for v in shards.values() if v["status"] == "claimed")
        pending = sum(1 for v in shards.values() if v["status"] == "pending")
        print(f"Resuming β€” {done} done / {claimed} claimed / {pending} buffered / {len(queue)} queued")
    else:
        state = {"shards": {}, "queue": []}
        print("Starting fresh")
    return state

def save_state(state):
    tmp = STATE_FILE + ".tmp"
    with open(tmp, "w") as f:
        json.dump(state, f, indent=2)
    os.replace(tmp, STATE_FILE)

# ── Discover ──────────────────────────────────────────────────────────────────
def discover_all(state):
    known_urls = {v["url"] for v in state["shards"].values()} | {e["url"] for e in state.get("queue", [])}
    new_count  = 0

    for src in SOURCES:
        name = src["name"]
        print(f"\nDiscovering: {name}")

        if src["type"] == "hf_list":
            all_files = sorted([
                f for f in api.list_repo_files(src["repo"], repo_type="dataset")
                if f.startswith(src["prefix"]) and f.endswith(".parquet")
            ])
            selected = all_files[src["skip"]: src["skip"] + src["take"]]
            base_url = f"https://huggingface.co/datasets/{src['repo']}/resolve/main/"
            urls     = [base_url + f for f in selected]
            fmt      = "parquet"
        else:
            urls = src["urls"]
            fmt  = src.get("fmt", "parquet")

        added = 0
        for url in urls:
            if url not in known_urls:
                state["queue"].append({
                    "url"      : url,
                    "source"   : name,
                    "text_col" : src["text_col"],
                    "fmt"      : fmt,
                })
                known_urls.add(url)
                new_count += 1
                added     += 1

        print(f"  {name}: {len(urls)} files | {added} new added to queue")

    save_state(state)
    print(f"\nTotal queued: {len(state['queue'])} | In state: {len(state['shards'])}")

# ── Reclaim stale ─────────────────────────────────────────────────────────────
def reclaim_stale(state):
    now       = time.time()
    reclaimed = 0
    for name, info in state["shards"].items():
        if info["status"] == "claimed" and info.get("claimed_at"):
            if now - info["claimed_at"] > WORKER_TIMEOUT:
                print(f"  ⚠ Reclaiming: {name}")
                info["status"]     = "pending"
                info["worker"]     = None
                info["claimed_at"] = None
                reclaimed         += 1
    if reclaimed:
        save_state(state)

# ── Parquet β†’ JSONL ───────────────────────────────────────────────────────────
def parquet_to_jsonl(parquet_path, jsonl_path, text_col):
    """Stream parquet batch by batch β†’ write one JSON line per doc. No full load."""
    pf        = pq.ParquetFile(parquet_path)
    n_written = 0
    with open(jsonl_path, "w", encoding="utf-8") as out:
        for batch in pf.iter_batches(batch_size=1_000, columns=[text_col]):
            texts = batch.column(text_col).to_pylist()
            for text in texts:
                if text and isinstance(text, str) and text.strip():
                    out.write(json.dumps({"text": text.strip()}, ensure_ascii=False) + "\n")
                    n_written += 1
            del texts
            gc.collect()
    return n_written

# ── Download loop ─────────────────────────────────────────────────────────────
def download_loop(state):
    while True:
        try:
            with open(STATE_FILE) as f:
                fresh = json.load(f)
            state["shards"] = fresh["shards"]
            state["queue"]  = fresh.get("queue", [])
        except Exception:
            pass

        reclaim_stale(state)

        buffered = sum(1 for v in state["shards"].values() if v["status"] == "pending")
        if buffered >= MAX_BUFFERED:
            time.sleep(30)
            continue

        if not state["queue"]:
            done  = sum(1 for v in state["shards"].values() if v["status"] == "done")
            total = len(state["shards"])
            if done == total and total > 0:
                print("βœ“ All shards complete!")
                break
            print("  Queue empty β€” sleeping...")
            time.sleep(60)
            continue

        entry    = state["queue"][0]
        url      = entry["url"]
        source   = entry["source"]
        text_col = entry["text_col"]
        fmt      = entry.get("fmt", "parquet")

        lang       = url.split("?")[0].split("/")[-2]
        base_name  = url.split("?")[0].split("/")[-1].replace(".parquet", "").replace(".jsonl", "")
        shard_name = f"{source}__{base_name}_{lang}.jsonl"
        jsonl_path = Path(RAW_DIR) / shard_name
        tmp_path   = Path(RAW_DIR) / f"{shard_name}.tmp"

        print(f"  Downloading: {source} | {base_name}")
        try:
            resp = requests.get(url, headers=AUTH_HEADERS, timeout=300, stream=True)
            resp.raise_for_status()
            with open(tmp_path, "wb") as f:
                for chunk in resp.iter_content(chunk_size=8 * 1024 * 1024):
                    f.write(chunk)
        except Exception as e:
            print(f"  βœ— Download failed: {e} β€” retrying in 30s")
            tmp_path.unlink(missing_ok=True)
            time.sleep(30)
            continue

        if fmt == "parquet":
            print(f"  Converting β†’ jsonl: {shard_name}")
            try:
                n = parquet_to_jsonl(tmp_path, jsonl_path, text_col)
                tmp_path.unlink(missing_ok=True)
                print(f"  βœ“ {n:,} docs")
            except Exception as e:
                print(f"  βœ— Convert failed: {e}")
                tmp_path.unlink(missing_ok=True)
                jsonl_path.unlink(missing_ok=True)
                time.sleep(30)
                continue
        else:
            tmp_path.rename(jsonl_path)

        state["queue"].pop(0)
        state["shards"][shard_name] = {
            "status"    : "pending",
            "url"       : url,
            "source"    : source,
            "worker"    : None,
            "claimed_at": None,
            "error"     : None,
        }
        save_state(state)
        print(f"  βœ“ Ready: {shard_name}")
        time.sleep(3)

# ── Monitor ───────────────────────────────────────────────────────────────────
def monitor_loop():
    while True:
        time.sleep(120)
        try:
            with open(STATE_FILE) as f:
                s = json.load(f)
            shards  = s["shards"]
            queue   = s.get("queue", [])
            done    = sum(1 for v in shards.values() if v["status"] == "done")
            claimed = sum(1 for v in shards.values() if v["status"] == "claimed")
            pending = sum(1 for v in shards.values() if v["status"] == "pending")
            total   = len(shards) + len(queue)
            pct     = (done / total * 100) if total else 0

            src_done = {}
            for v in shards.values():
                src = v.get("source", "?")
                if v["status"] == "done":
                    src_done[src] = src_done.get(src, 0) + 1

            print(f"[MONITOR] {done}/{total} ({pct:.1f}%) | {claimed} active | {pending} buffered | {len(queue)} queued")
            for src, cnt in sorted(src_done.items()):
                print(f"  {src}: {cnt} done")
        except Exception:
            pass

# ── Entry point ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
    threading.Thread(target=serve, daemon=True).start()
    state = load_state()
    discover_all(state)
    threading.Thread(target=monitor_loop, daemon=True).start()
    threading.Thread(target=download_loop, args=(state,), daemon=True).start()
    while True:
        time.sleep(60)