Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,45 @@
|
|
|
|
|
| 1 |
import re
|
| 2 |
-
import shutil
|
| 3 |
from pathlib import Path
|
|
|
|
| 4 |
|
| 5 |
TOKENIZED_DIR = "/data/tokenized"
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
| 9 |
for f in sorted(Path(TOKENIZED_DIR).glob("*.bin")):
|
| 10 |
-
|
| 11 |
-
continue
|
| 12 |
-
m = re.match(r"(cc\d{4}-\d+)_", f.name)
|
| 13 |
if m:
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
print(f"Found {len(
|
| 18 |
|
| 19 |
-
for
|
| 20 |
-
out_path = Path(TOKENIZED_DIR) /
|
| 21 |
if out_path.exists():
|
| 22 |
-
print(f" β Already exists, skipping: {
|
| 23 |
-
continue
|
| 24 |
-
|
| 25 |
-
def sort_key(f):
|
| 26 |
-
m_shard = re.search(r"_(\d{6})\.bin$", f.name)
|
| 27 |
-
return int(m_shard.group(1)) if m_shard else 999999
|
| 28 |
-
|
| 29 |
-
files_sorted = sorted(files, key=sort_key)
|
| 30 |
-
|
| 31 |
-
if not files_sorted:
|
| 32 |
-
print(f" β No merged files for {month}, skipping")
|
| 33 |
continue
|
| 34 |
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
except Exception as e:
|
| 46 |
-
print(f" β Failed: {e}")
|
| 47 |
-
tmp_path.unlink(missing_ok=True)
|
| 48 |
-
continue
|
| 49 |
|
| 50 |
tmp_path.rename(out_path)
|
| 51 |
-
print(f" β {
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
|
| 57 |
-
print("
|
|
|
|
| 1 |
+
import os
|
| 2 |
import re
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
TOKENIZED_DIR = "/data/tokenized"
|
| 7 |
|
| 8 |
+
# group chunks by base name
|
| 9 |
+
# cc2025-05_000000_chunk000.bin β cc2025-05_000000.bin
|
| 10 |
+
groups = {}
|
| 11 |
for f in sorted(Path(TOKENIZED_DIR).glob("*.bin")):
|
| 12 |
+
m = re.match(r"(.+)_chunk\d+\.bin$", f.name)
|
|
|
|
|
|
|
| 13 |
if m:
|
| 14 |
+
base = m.group(1) + ".bin"
|
| 15 |
+
groups.setdefault(base, []).append(f)
|
| 16 |
|
| 17 |
+
print(f"Found {len(groups)} base files to assemble")
|
| 18 |
|
| 19 |
+
for base_name, chunks in sorted(groups.items()):
|
| 20 |
+
out_path = Path(TOKENIZED_DIR) / base_name
|
| 21 |
if out_path.exists():
|
| 22 |
+
print(f" β Already exists, skipping: {base_name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
continue
|
| 24 |
|
| 25 |
+
chunks_sorted = sorted(chunks, key=lambda f: int(re.search(r"_chunk(\d+)\.bin$", f.name).group(1)))
|
| 26 |
+
tmp_path = Path(TOKENIZED_DIR) / f"{base_name}.tmp"
|
| 27 |
|
| 28 |
+
print(f" Merging {len(chunks_sorted)} chunks β {base_name}")
|
| 29 |
+
total_tokens = 0
|
| 30 |
|
| 31 |
+
with open(tmp_path, "wb") as out:
|
| 32 |
+
for chunk in chunks_sorted:
|
| 33 |
+
arr = np.fromfile(chunk, dtype=np.uint16)
|
| 34 |
+
arr.tofile(out)
|
| 35 |
+
total_tokens += len(arr)
|
| 36 |
+
del arr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
tmp_path.rename(out_path)
|
| 39 |
+
print(f" β {base_name} | {total_tokens:,} tokens")
|
| 40 |
|
| 41 |
+
# delete chunks after successful merge
|
| 42 |
+
for chunk in chunks_sorted:
|
| 43 |
+
chunk.unlink()
|
| 44 |
|
| 45 |
+
print("Done")
|