davanstrien HF Staff Claude Opus 4.6 (1M context) commited on
Commit
1b23718
·
1 Parent(s): 674eecd

Add Polars-based compile script for speed comparison

Browse files

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

Files changed (1) hide show
  1. compile_polars.py +106 -0
compile_polars.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.11"
3
+ # dependencies = [
4
+ # "huggingface-hub>=1.7",
5
+ # "polars",
6
+ # "python-dotenv",
7
+ # "structlog",
8
+ # "datasets",
9
+ # ]
10
+ # ///
11
+ """Quick test: sync bucket + Polars for compile speed comparison."""
12
+
13
+ import os
14
+ import shutil
15
+ import tempfile
16
+ import time
17
+
18
+ import polars as pl
19
+ import structlog
20
+ from datasets import Dataset
21
+ from dotenv import load_dotenv
22
+ from huggingface_hub import login, sync_bucket
23
+
24
+ load_dotenv(override=True)
25
+
26
+ HF_TOKEN = os.getenv("HF_TOKEN")
27
+ assert HF_TOKEN is not None
28
+
29
+ BUCKET_ID = os.getenv("BUCKET_ID", "librarian-bots/card-store")
30
+ OUTPUT_REPO = os.getenv(
31
+ "OUTPUT_REPO", "librarian-bots/dataset_cards_with_metadata_test"
32
+ )
33
+ DRY_RUN = os.getenv("DRY_RUN", "false").lower() == "true"
34
+
35
+ structlog.configure(
36
+ processors=[
37
+ structlog.stdlib.add_log_level,
38
+ structlog.processors.TimeStamper(fmt="iso"),
39
+ structlog.dev.ConsoleRenderer(),
40
+ ],
41
+ wrapper_class=structlog.make_filtering_bound_logger(20),
42
+ context_class=dict,
43
+ logger_factory=structlog.PrintLoggerFactory(),
44
+ cache_logger_on_first_use=True,
45
+ )
46
+ logger = structlog.get_logger()
47
+
48
+ login(token=HF_TOKEN)
49
+
50
+
51
+ def main():
52
+ t0 = time.time()
53
+
54
+ # Step 1: Sync bucket
55
+ local_dir = tempfile.mkdtemp(prefix="card-compile-")
56
+ bucket_path = f"hf://buckets/{BUCKET_ID}/datasets"
57
+ logger.info("Syncing bucket", bucket=bucket_path)
58
+ sync_bucket(source=bucket_path, dest=local_dir, token=HF_TOKEN)
59
+ t1 = time.time()
60
+ logger.info("Sync done", seconds=round(t1 - t0, 1))
61
+
62
+ # Step 2: Read all JSON with Polars
63
+ json_files = [
64
+ os.path.join(local_dir, f) for f in os.listdir(local_dir) if f.endswith(".json")
65
+ ]
66
+ logger.info("Reading JSON files", count=len(json_files))
67
+
68
+ # Read all files into a single DataFrame
69
+ dfs = []
70
+ for f in json_files:
71
+ try:
72
+ dfs.append(pl.read_json(f))
73
+ except Exception:
74
+ pass
75
+ df = pl.concat(dfs)
76
+ t2 = time.time()
77
+ logger.info("Polars read done", rows=len(df), seconds=round(t2 - t1, 1))
78
+
79
+ # Step 3: Filter
80
+ df = df.filter(pl.col("card").is_not_null())
81
+ df = df.filter(pl.col("card").str.len_bytes() > 30)
82
+ df = df.filter(~pl.col("card").str.contains("<h1>429</h1>"))
83
+ df = df.filter(~pl.col("card").str.contains("Entry not found"))
84
+
85
+ # Drop fetched_at
86
+ if "fetched_at" in df.columns:
87
+ df = df.drop("fetched_at")
88
+
89
+ t3 = time.time()
90
+ logger.info("Filter done", rows=len(df), seconds=round(t3 - t2, 1))
91
+
92
+ # Step 4: Push
93
+ if DRY_RUN:
94
+ logger.info("[DRY RUN]", rows=len(df), columns=df.columns)
95
+ else:
96
+ logger.info("Pushing to Hub", repo=OUTPUT_REPO)
97
+ ds = Dataset.from_polars(df)
98
+ ds.push_to_hub(OUTPUT_REPO, token=HF_TOKEN, max_shard_size="750MB")
99
+ t4 = time.time()
100
+ logger.info("Done", rows=len(df), total_seconds=round(t4 - t0, 1))
101
+
102
+ shutil.rmtree(local_dir, ignore_errors=True)
103
+
104
+
105
+ if __name__ == "__main__":
106
+ main()