davanstrien HF Staff Claude Opus 4.7 (1M context) commited on
Commit
efa4eb1
·
1 Parent(s): 364d61b

falcon-ocr-bucket: add --max-samples with early-termination scan

Browse files

Mirrors pp-doclayout's --max-samples flag for falcon-ocr-bucket. When
set, discover_files() stops scanning once the limit is reached instead
of materializing-and-sorting the full directory tree first. Useful for
smoke tests and bounded slices on large mounted buckets.

Without --max-samples: full sorted list (deterministic, unchanged).
With --max-samples N: up to N files in filesystem-traversal order.

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

Files changed (1) hide show
  1. falcon-ocr-bucket.py +29 -4
falcon-ocr-bucket.py CHANGED
@@ -64,14 +64,26 @@ def check_cuda_availability():
64
  logger.info(f"CUDA available. GPU: {torch.cuda.get_device_name(0)}")
65
 
66
 
67
- def discover_files(input_dir: Path) -> list[Path]:
 
 
 
 
 
 
 
68
  files = []
69
- for path in sorted(input_dir.rglob("*")):
 
 
 
70
  if not path.is_file():
71
  continue
72
  ext = path.suffix.lower()
73
  if ext in IMAGE_EXTENSIONS or ext == ".pdf":
74
  files.append(path)
 
 
75
  return files
76
 
77
 
@@ -135,6 +147,13 @@ def main():
135
  parser.add_argument(
136
  "--no-cudagraph", action="store_true", help="Disable CUDA graph capture",
137
  )
 
 
 
 
 
 
 
138
  parser.add_argument(
139
  "--verbose", action="store_true", help="Print resolved package versions",
140
  )
@@ -155,8 +174,14 @@ def main():
155
  start_time = time.time()
156
 
157
  # Discover files
158
- logger.info(f"Scanning {input_dir} for images and PDFs...")
159
- files = discover_files(input_dir)
 
 
 
 
 
 
160
  if not files:
161
  logger.error(f"No image or PDF files found in {input_dir}")
162
  sys.exit(1)
 
64
  logger.info(f"CUDA available. GPU: {torch.cuda.get_device_name(0)}")
65
 
66
 
67
+ def discover_files(input_dir: Path, limit: int | None = None) -> list[Path]:
68
+ """Discover image and PDF files under input_dir.
69
+
70
+ Without `limit`, returns the full sorted list (deterministic order).
71
+ With `limit`, stops scanning once `limit` matching files are found
72
+ and returns them in filesystem order (much faster on huge mounted
73
+ buckets, but ordering is not deterministic).
74
+ """
75
  files = []
76
+ iterator = (
77
+ input_dir.rglob("*") if limit is not None else sorted(input_dir.rglob("*"))
78
+ )
79
+ for path in iterator:
80
  if not path.is_file():
81
  continue
82
  ext = path.suffix.lower()
83
  if ext in IMAGE_EXTENSIONS or ext == ".pdf":
84
  files.append(path)
85
+ if limit is not None and len(files) >= limit:
86
+ break
87
  return files
88
 
89
 
 
147
  parser.add_argument(
148
  "--no-cudagraph", action="store_true", help="Disable CUDA graph capture",
149
  )
150
+ parser.add_argument(
151
+ "--max-samples", type=int, default=None,
152
+ help="Limit number of input files to discover. Stops scanning early "
153
+ "once the limit is reached (much faster on large mounted buckets). "
154
+ "Applied before PDF page expansion. With --max-samples set, file "
155
+ "ordering is filesystem-dependent rather than sorted.",
156
+ )
157
  parser.add_argument(
158
  "--verbose", action="store_true", help="Print resolved package versions",
159
  )
 
174
  start_time = time.time()
175
 
176
  # Discover files
177
+ if args.max_samples is not None:
178
+ logger.info(
179
+ f"Scanning {input_dir} for up to {args.max_samples} images/PDFs "
180
+ f"(early termination, --max-samples)..."
181
+ )
182
+ else:
183
+ logger.info(f"Scanning {input_dir} for images and PDFs...")
184
+ files = discover_files(input_dir, limit=args.max_samples)
185
  if not files:
186
  logger.error(f"No image or PDF files found in {input_dir}")
187
  sys.exit(1)