davanstrien HF Staff commited on
Commit
7d840d3
·
1 Parent(s): 8e83419

feat: Use IterableDataset for true streaming (no disk writes)

Browse files
Files changed (1) hide show
  1. long-context-pdfs.py +18 -6
long-context-pdfs.py CHANGED
@@ -1,7 +1,7 @@
1
  # /// script
2
  # requires-python = ">=3.10"
3
  # dependencies = [
4
- # "polars",
5
  # "datasets",
6
  # ]
7
  # ///
@@ -30,7 +30,19 @@ Examples:
30
 
31
  import argparse
32
  import polars as pl
33
- from datasets import Dataset
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
 
36
  def polars_to_generator(lf: pl.LazyFrame, chunk_size: int = 100):
@@ -157,12 +169,12 @@ def main():
157
  lf = lf.limit(args.limit)
158
 
159
  print(f"\nStreaming to dataset (chunk_size={args.chunk_size})...")
160
- ds = Dataset.from_generator(
161
- lambda: polars_to_generator(lf, chunk_size=args.chunk_size)
 
162
  )
163
- print(f"Created dataset with {len(ds)} rows")
164
 
165
- print(f"\nPushing to {args.output}...")
166
  ds.push_to_hub(args.output, private=args.private)
167
  print(f"\nDone! https://huggingface.co/datasets/{args.output}")
168
 
 
1
  # /// script
2
  # requires-python = ">=3.10"
3
  # dependencies = [
4
+ # "polars>=1.0",
5
  # "datasets",
6
  # ]
7
  # ///
 
30
 
31
  import argparse
32
  import polars as pl
33
+ from datasets import Features, IterableDataset, Value
34
+
35
+ FEATURES = Features(
36
+ {
37
+ "id": Value("string"),
38
+ "url": Value("string"),
39
+ "text": Value("string"),
40
+ "language": Value("string"),
41
+ "token_count": Value("int64"),
42
+ "dump": Value("string"),
43
+ "page_average_lid_score": Value("float64"),
44
+ }
45
+ )
46
 
47
 
48
  def polars_to_generator(lf: pl.LazyFrame, chunk_size: int = 100):
 
169
  lf = lf.limit(args.limit)
170
 
171
  print(f"\nStreaming to dataset (chunk_size={args.chunk_size})...")
172
+ ds = IterableDataset.from_generator(
173
+ lambda: polars_to_generator(lf, chunk_size=args.chunk_size),
174
+ features=FEATURES,
175
  )
 
176
 
177
+ print(f"\nPushing to {args.output} (streaming)...")
178
  ds.push_to_hub(args.output, private=args.private)
179
  print(f"\nDone! https://huggingface.co/datasets/{args.output}")
180