-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_files_by_topic.py
More file actions
1393 lines (1146 loc) · 47.6 KB
/
sort_files_by_topic.py
File metadata and controls
1393 lines (1146 loc) · 47.6 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
sort_files_by_topic.py
Sorts thousands of files into topic-based subdirectories using Claude AI.
Supports PDF, TXT, RTF, DOC, DOCX, XLS, XLSX, CSV, and image files
(JPG, JPEG, PNG, TIFF, BMP, GIF, WEBP — via OCR).
Files with meaningless names (only digits, UUIDs, random char combos) are
automatically renamed to a descriptive name suggested by Claude — at no
extra API cost, since the rename is included in the same classification call.
Just run:
python sort_files_by_topic.py # does everything: submit -> wait -> collect
python sort_files_by_topic.py --dry-run # preview without moving files
python sort_files_by_topic.py --standard # real-time API (no batch, immediate)
python sort_files_by_topic.py --collect # manually collect a previously submitted batch
python sort_files_by_topic.py --wait # same as default (explicit)
Override directories if needed:
python sort_files_by_topic.py --input /other/dir --output /other/sorted
Requirements:
pip install pdfplumber anthropic tqdm python-docx openpyxl pillow pytesseract striprtf
For DOC (legacy Word) support:
Linux/Mac: sudo apt install antiword / brew install antiword
For image OCR:
Mac: brew install tesseract
Linux: sudo apt install tesseract-ocr
Environment:
ANTHROPIC_API_KEY must be set.
Mac/Linux: export ANTHROPIC_API_KEY="sk-ant-..."
Windows: set ANTHROPIC_API_KEY=sk-ant-...
PowerShell: $env:ANTHROPIC_API_KEY="sk-ant-..."
Add the export line to ~/.bashrc or ~/.zshrc to make it permanent.
Never hardcode the key in this file or commit it to Git.
Costs (indicative):
17/05/2026 : EUR 0.27 for 433 pdfs
10,000 files ~ EUR 7 with Haiku + Batch API
"""
import argparse
import json
import logging
import os
import re
import shutil
import subprocess
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass, field
from pathlib import Path
import anthropic
import pdfplumber
from tqdm import tqdm
# ---------------------------------------------------------------------------
# Suppress noisy library loggers (must be before basicConfig)
# ---------------------------------------------------------------------------
for _noisy in (
"pdfplumber", "pdfminer", "pdfminer.high_level", "pdfminer.layout",
"pypdf", "pypdf._reader", "pypdf._page", "pypdf.generic",
"PIL", "PIL.Image",
):
logging.getLogger(_noisy).setLevel(logging.ERROR)
# ---------------------------------------------------------------------------
# *** USER CONFIGURATION — edit these two lines, nothing else is required ***
# ---------------------------------------------------------------------------
# INPUT_DIR: Path = Path(".") # folder containing files to sort
# OUTPUT_DIR: Path = Path("sorted") # sorted files land here (created if needed)
# INPUT_DIR: Path = Path(r"C:\Users\rcxsm\Downloads\pdf_ongesorteerd\pdfs")
INPUT_DIR: Path = Path(r"C:\Users\rcxsm\Downloads\xls ongesorteerd")
OUTPUT_DIR: Path = Path(r"C:\Users\rcxsm\Downloads\xls ongesorteerd\sorted")
# ---------------------------------------------------------------------------
# Topic list
# ---------------------------------------------------------------------------
DEFAULT_TOPICS: list[str] = [
"Finance & Accounting",
"Legal & Contracts",
"Medical & Health",
"Technical & Engineering",
"Science & Research",
"Business & Management",
"Education & Training",
"Government & Policy",
"Human Resources",
"Marketing & Sales",
"Real Estate",
"Sheet music",
"Travel & Tourism",
"Art & Culture",
"Spiritual & Yoga",
"Service Quality",
"Bank accounts",
"Manuals",
"Tourism",
"Maps",
"Other",
]
# ---------------------------------------------------------------------------
# Supported file extensions
# ---------------------------------------------------------------------------
SUPPORTED_EXTENSIONS: set[str] = {
".pdf",
".txt", ".rtf", ".csv",".html",".htm"
".doc", ".docx",
".xls", ".xlsx",
".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp", ".gif", ".webp",
}
# ---------------------------------------------------------------------------
# Technical configuration
# ---------------------------------------------------------------------------
MODEL: str = "claude-haiku-4-5-20251001"
PAGES_TO_EXTRACT: int = 2 # pages to read per PDF/DOCX
MAX_CHARS_PER_FILE: int = 3_000 # max chars sent to Claude per file
BATCH_SIZE: int = 10 # files per API request
MAX_WORKERS: int = 16 # parallel extraction threads
API_RETRY_ATTEMPTS: int = 3
API_RETRY_DELAY: float = 5.0 # seconds between retries
BATCH_POLL_INTERVAL: int = 30 # seconds between batch status polls
PROGRESS_FILE: str = "sort_progress.json"
BATCH_STATE_FILE: str = "sort_batch_state.json"
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
)
log = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Data classes
# ---------------------------------------------------------------------------
@dataclass
class FileRecord:
"""Metadata, extracted text, and classification result for one file.
Attributes:
path: Absolute path to the file.
text: Extracted text snippet.
topic: Assigned topic after classification.
new_name: Suggested filename stem (without extension) for bad names.
error: Error message if extraction or classification failed.
custom_id: Unique ID correlating batch sub-requests with records.
"""
path: Path
text: str = ""
topic: str = ""
new_name: str = ""
error: str = ""
custom_id: str = ""
@dataclass
class SortStats:
"""Running counters for a sorting run.
Attributes:
total: Total files found.
classified: Successfully classified.
moved: Successfully moved/copied.
skipped: Already processed (resume).
failed: Extraction or API failures.
renamed: Files given a new name by Claude.
topic_counts: Counter per topic.
type_counts: Counter per file extension.
"""
total: int = 0
classified: int = 0
moved: int = 0
skipped: int = 0
failed: int = 0
renamed: int = 0
topic_counts: dict[str, int] = field(default_factory=dict)
type_counts: dict[str, int] = field(default_factory=dict)
# ---------------------------------------------------------------------------
# Filename quality check
# ---------------------------------------------------------------------------
def _is_bad_filename(stem: str) -> bool:
"""Return True if the filename stem is meaningless and should be renamed.
Catches: pure digit strings, UUIDs, random alphanumeric blobs, very short
consonant-only strings, and names with fewer than 40% letter characters.
Args:
stem: Filename without extension.
Returns:
True if the name should be replaced with a descriptive one.
"""
if not stem:
return True
# Pure digits (e.g. "1234567890")
if stem.isdigit():
return True
# UUID pattern (with or without hyphens)
if re.fullmatch(
r"[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}",
stem, re.I,
):
return True
# Mostly non-alpha (less than 40% letters)
letters = sum(c.isalpha() for c in stem)
if len(stem) >= 4 and letters / len(stem) < 0.4:
return True
# Short and no vowels (e.g. "xkbf", "z7q3")
if len(stem) <= 6 and not any(c in "aeiouAEIOU" for c in stem):
return True
return False
def _sanitize_suggested_name(raw: str) -> str:
"""Sanitize a Claude-suggested filename stem to be filesystem-safe.
Args:
raw: Raw suggested name from Claude (no extension).
Returns:
Alphanumeric + underscore string, max 30 characters.
"""
cleaned = re.sub(r"[^\w]", "_", raw.strip())
cleaned = re.sub(r"_+", "_", cleaned).strip("_")
return cleaned[:30]
# ---------------------------------------------------------------------------
# Text extraction — per file type
# ---------------------------------------------------------------------------
def _extract_pdf(file_path: Path, max_chars: int) -> str:
"""Extract text from the first PAGES_TO_EXTRACT pages via pdfplumber.
Args:
file_path: Path to the PDF.
max_chars: Maximum characters to return.
Returns:
Extracted text string.
"""
with pdfplumber.open(str(file_path)) as pdf:
chunks = [page.extract_text() or "" for page in pdf.pages[:PAGES_TO_EXTRACT]]
return "\n".join(chunks).strip()[:max_chars]
def _extract_pdf_pdftotext(file_path: Path, max_chars: int) -> str:
"""Extract text using the native pdftotext binary (faster alternative).
Requires poppler: brew install poppler / apt install poppler-utils.
Falls back to pdfplumber if not available.
Args:
file_path: Path to the PDF.
max_chars: Maximum characters to return.
Returns:
Extracted text string.
"""
try:
result = subprocess.run(
["pdftotext", "-l", str(PAGES_TO_EXTRACT), str(file_path), "-"],
capture_output=True, text=True, timeout=10,
)
return result.stdout.strip()[:max_chars]
except FileNotFoundError:
log.debug("pdftotext not found; falling back to pdfplumber for %s", file_path.name)
return _extract_pdf(file_path, max_chars)
def _extract_txt(file_path: Path, max_chars: int) -> str:
"""Read a plain-text or CSV file, trying common encodings.
Args:
file_path: Path to the file.
max_chars: Maximum characters to return.
Returns:
File content string.
"""
for encoding in ("utf-8", "latin-1", "cp1252"):
try:
return file_path.read_text(encoding=encoding)[:max_chars]
except UnicodeDecodeError:
continue
return ""
def _extract_rtf(file_path: Path, max_chars: int) -> str:
"""Extract plain text from an RTF file using striprtf.
Args:
file_path: Path to the RTF file.
max_chars: Maximum characters to return.
Returns:
Plain text content.
"""
from striprtf.striprtf import rtf_to_text # type: ignore
raw = file_path.read_text(encoding="latin-1")
return rtf_to_text(raw)[:max_chars]
def _extract_docx(file_path: Path, max_chars: int) -> str:
"""Extract paragraph text from a DOCX file via python-docx.
Args:
file_path: Path to the DOCX file.
max_chars: Maximum characters to return.
Returns:
Paragraph text joined by newlines.
"""
import docx # type: ignore
doc = docx.Document(str(file_path))
text = "\n".join(p.text for p in doc.paragraphs if p.text.strip())
return text[:max_chars]
def _extract_doc(file_path: Path, max_chars: int) -> str:
"""Extract text from a legacy DOC file via antiword.
Falls back to empty string if antiword is not installed.
Args:
file_path: Path to the DOC file.
max_chars: Maximum characters to return.
Returns:
Extracted text string.
"""
try:
result = subprocess.run(
["antiword", str(file_path)],
capture_output=True, text=True, timeout=15,
)
return result.stdout.strip()[:max_chars]
except FileNotFoundError:
log.debug("antiword not found; cannot extract .doc: %s", file_path.name)
return ""
def _extract_xlsx(file_path: Path, max_chars: int) -> str:
"""Extract cell values from the first 3 sheets (50 rows each) of an XLSX.
Args:
file_path: Path to the XLSX/XLS file.
max_chars: Maximum characters to return.
Returns:
Tab/newline separated cell values.
"""
import openpyxl # type: ignore
wb = openpyxl.load_workbook(str(file_path), read_only=True, data_only=True)
chunks: list[str] = []
for sheet in wb.worksheets[:3]:
for row in sheet.iter_rows(max_row=50, values_only=True):
row_text = " ".join(str(c) for c in row if c is not None)
if row_text.strip():
chunks.append(row_text)
return "\n".join(chunks)[:max_chars]
def _extract_image(file_path: Path, max_chars: int) -> str:
"""Extract text from an image via Tesseract OCR.
Requires: pip install pillow pytesseract
And: brew install tesseract / apt install tesseract-ocr
Args:
file_path: Path to the image file.
max_chars: Maximum characters to return.
Returns:
OCR text string, or empty string if tesseract is unavailable.
"""
try:
import pytesseract # type: ignore
from PIL import Image # type: ignore
img = Image.open(str(file_path))
return pytesseract.image_to_string(img)[:max_chars]
except ImportError:
log.debug("pytesseract/Pillow not installed; skipping OCR for %s", file_path.name)
return ""
except Exception as exc:
log.debug("OCR failed for %s: %s", file_path.name, exc)
return ""
def extract_text_from_file(file_path: Path, max_chars: int) -> str:
"""Dispatch text extraction to the correct handler based on file extension.
Args:
file_path: Path to the file.
max_chars: Maximum characters to return.
Returns:
Extracted text string, possibly empty if unreadable or unsupported.
"""
suffix = file_path.suffix.lower()
try:
if suffix == ".pdf":
return _extract_pdf(file_path, max_chars)
elif suffix in (".txt", ".csv",".htm",".html"):
return _extract_txt(file_path, max_chars)
elif suffix == ".rtf":
return _extract_rtf(file_path, max_chars)
elif suffix == ".docx":
return _extract_docx(file_path, max_chars)
elif suffix == ".doc":
return _extract_doc(file_path, max_chars)
elif suffix in (".xlsx", ".xls"):
return _extract_xlsx(file_path, max_chars)
elif suffix in (".jpg", ".jpeg", ".png", ".tiff", ".tif", ".bmp", ".gif", ".webp"):
return _extract_image(file_path, max_chars)
except Exception as exc:
log.debug("Extraction failed for %s: %s", file_path.name, exc)
return ""
# ---------------------------------------------------------------------------
# Parallel extraction
# ---------------------------------------------------------------------------
def extract_texts_parallel(records: list[FileRecord], max_chars: int, workers: int) -> None:
"""Fill FileRecord.text for each record in-place using a thread pool.
Args:
records: List of FileRecord objects to populate.
max_chars: Passed through to extract_text_from_file.
workers: Number of parallel threads.
"""
def _extract(record: FileRecord) -> None:
record.text = extract_text_from_file(record.path, max_chars)
if not record.text:
record.error = "empty_text"
with ThreadPoolExecutor(max_workers=workers) as pool:
futures = {pool.submit(_extract, r): r for r in records}
for future in tqdm(as_completed(futures), total=len(futures),
desc="Extracting text", unit="file"):
future.result()
# ---------------------------------------------------------------------------
# Prompt builder
# ---------------------------------------------------------------------------
def build_classification_prompt(batch: list[FileRecord], topics: list[str]) -> str:
"""Build the classification prompt, requesting a suggested name for bad filenames.
Args:
batch: FileRecord objects with text populated.
topics: Allowed topic label strings.
Returns:
Formatted prompt string.
"""
topic_list = "\n".join(f"- {t}" for t in topics)
needs_rename = any(_is_bad_filename(r.path.stem) for r in batch)
entries: list[str] = []
for i, record in enumerate(batch):
snippet = record.text if record.text else "(no text extracted)"
ext = record.path.suffix.upper()
tag = " [NEEDS_RENAME]" if _is_bad_filename(record.path.stem) else ""
entries.append(f"### File {i} [{ext}]{tag}\nFilename: {record.path.name}\n\n{snippet}")
docs_block = "\n\n".join(entries)
rename_instruction = (
'\nFor files marked [NEEDS_RENAME], also include a "suggested_name" key: '
"a descriptive filename stem of max 30 characters, no extension, "
'use_underscores_for_spaces, no special characters. Omit "suggested_name" for other files.\n'
) if needs_rename else ""
return (
"You are a document classifier. Assign exactly one topic from the list below to each file.\n\n"
f"Allowed topics:\n{topic_list}\n\n"
f"{rename_instruction}"
'Return ONLY a JSON array — one object per file in the same order provided.\n'
'Required keys: "index" (integer), "topic" (string from list above).\n'
'Optional key: "suggested_name" (string, only for [NEEDS_RENAME] files).\n'
"No explanation, no markdown — only raw JSON.\n\n"
f"Files to classify:\n\n{docs_block}\n"
)
# ---------------------------------------------------------------------------
# Response parser
# ---------------------------------------------------------------------------
def _parse_classification_response(
raw: str,
batch: list[FileRecord],
topics: list[str],
) -> bool:
"""Parse Claude's JSON response and populate batch records in-place.
Args:
raw: Raw text response from Claude.
batch: Records to populate with topic and new_name.
topics: Allowed topic strings for validation.
Returns:
True if parsing succeeded.
"""
text = raw.strip()
if text.startswith("```"):
parts = text.split("```")
text = parts[1] if len(parts) > 1 else text
if text.startswith("json"):
text = text[4:]
results: list[dict] = json.loads(text.strip())
for item in results:
idx: int = int(item["index"])
topic: str = item["topic"].strip()
if topic not in topics:
topic = _best_match(topic, topics)
batch[idx].topic = topic
if "suggested_name" in item:
batch[idx].new_name = _sanitize_suggested_name(str(item["suggested_name"]))
return True
def _best_match(topic: str, allowed: list[str]) -> str:
"""Return the best-matching allowed topic via case-insensitive substring.
Args:
topic: Returned topic not in the allowed list.
allowed: Valid topic strings.
Returns:
Best matching allowed topic, or 'Other' as fallback.
"""
topic_lower = topic.lower()
for candidate in allowed:
if topic_lower in candidate.lower() or candidate.lower() in topic_lower:
return candidate
return "Other"
# ---------------------------------------------------------------------------
# Standard (real-time) classification
# ---------------------------------------------------------------------------
def classify_batch_standard(
client: anthropic.Anthropic,
batch: list[FileRecord],
topics: list[str],
retries: int,
retry_delay: float,
) -> None:
"""Classify a batch via the synchronous Messages API.
Args:
client: Anthropic client instance.
batch: FileRecord list to classify in-place.
topics: Allowed topic strings.
retries: Number of retry attempts on transient failure.
retry_delay: Seconds to wait between retries.
"""
prompt = build_classification_prompt(batch, topics)
for attempt in range(1, retries + 1):
try:
response = client.messages.create(
model=MODEL,
max_tokens=512,
messages=[{"role": "user", "content": prompt}],
)
if _parse_classification_response(response.content[0].text, batch, topics):
return
except (json.JSONDecodeError, KeyError, IndexError, ValueError) as exc:
log.warning("Parse error attempt %d/%d: %s", attempt, retries, exc)
except anthropic.RateLimitError:
log.warning("Rate limited; waiting %.0fs (attempt %d/%d)", retry_delay * 2, attempt, retries)
time.sleep(retry_delay * 2)
except anthropic.APIStatusError as exc:
log.warning("API error %s attempt %d/%d", exc.status_code, attempt, retries)
time.sleep(retry_delay)
if attempt < retries:
time.sleep(retry_delay)
for record in batch:
if not record.topic:
record.error = "classification_failed"
record.topic = "Other"
# ---------------------------------------------------------------------------
# Batch API — submit
# ---------------------------------------------------------------------------
def build_batch_requests(
all_records: list[FileRecord],
topics: list[str],
batch_size: int,
) -> list[dict]:
"""Build Batch API request dicts; assign custom_ids to all records.
Args:
all_records: FileRecord objects with text populated.
topics: Allowed topic strings.
batch_size: Files per sub-batch request.
Returns:
List of request dicts for client.beta.messages.batches.create().
"""
requests: list[dict] = []
sub_batches = [all_records[i: i + batch_size] for i in range(0, len(all_records), batch_size)]
for sub_idx, sub_batch in enumerate(sub_batches):
for rec_idx, record in enumerate(sub_batch):
record.custom_id = f"sub{sub_idx:06d}_rec{rec_idx:04d}"
prompt = build_classification_prompt(sub_batch, topics)
requests.append({
"custom_id": f"sub{sub_idx:06d}",
"params": {
"model": MODEL,
"max_tokens": 512,
"messages": [{"role": "user", "content": prompt}],
},
})
return requests
def submit_batch(
client: anthropic.Anthropic,
all_records: list[FileRecord],
topics: list[str],
batch_size: int,
state_path: Path,
) -> str:
"""Submit all requests to the Batch API and save state for collect phase.
Args:
client: Anthropic client instance.
all_records: Records with text extracted.
topics: Allowed topic strings.
batch_size: Files per sub-batch request.
state_path: Path to write the batch state JSON.
Returns:
Anthropic batch ID string.
"""
log.info("Building batch requests…")
requests = build_batch_requests(all_records, topics, batch_size)
log.info("Submitting %d requests to Batch API…", len(requests))
response = client.beta.messages.batches.create(requests=requests)
batch_id: str = response.id
log.info("Batch submitted. ID: %s | Status: %s", batch_id, response.processing_status)
id_to_path: dict[str, str] = {
record.custom_id: str(record.path.resolve()) for record in all_records
}
state = {"batch_id": batch_id, "topics": topics, "batch_size": batch_size, "id_to_path": id_to_path}
with open(state_path, "w", encoding="utf-8") as fh:
json.dump(state, fh, indent=2)
log.info("Batch state saved to %s", state_path)
return batch_id
# ---------------------------------------------------------------------------
# Batch API — collect
# ---------------------------------------------------------------------------
def poll_batch_until_done(client: anthropic.Anthropic, batch_id: str, poll_interval: int) -> None:
"""Block until the batch reaches the 'ended' status.
Args:
client: Anthropic client instance.
batch_id: Batch ID to poll.
poll_interval: Seconds between status checks.
"""
log.info("Polling batch %s every %ds…", batch_id, poll_interval)
while True:
batch = client.beta.messages.batches.retrieve(batch_id)
status = batch.processing_status
counts = batch.request_counts
log.info(
"Status: %-12s processing=%d succeeded=%d errored=%d",
status, counts.processing, counts.succeeded, counts.errored,
)
if status == "ended":
break
time.sleep(poll_interval)
log.info("Batch %s finished.", batch_id)
def collect_batch_results(
client: anthropic.Anthropic,
batch_id: str,
topics: list[str],
id_to_path: dict[str, str],
batch_size: int,
) -> dict[str, tuple[str, str]]:
"""Retrieve batch results; return path → (topic, new_name) mapping.
Args:
client: Anthropic client instance.
batch_id: Batch ID to retrieve.
topics: Allowed topic strings.
id_to_path: custom_id → absolute path string (from state file).
batch_size: Files per sub-batch (for reconstructing record order).
Returns:
Dict mapping absolute path strings to (topic, new_name) tuples.
new_name is an empty string when no rename was suggested.
"""
# Reconstruct sub-batch structure
sub_batches: dict[int, list[tuple[int, str, str]]] = {}
for custom_id, abs_path in id_to_path.items():
parts = custom_id.split("_")
sub_idx = int(parts[0][3:])
rec_idx = int(parts[1][3:])
sub_batches.setdefault(sub_idx, []).append((rec_idx, custom_id, abs_path))
ordered: dict[int, list[tuple[int, str, str]]] = {
k: sorted(v, key=lambda x: x[0]) for k, v in sub_batches.items()
}
path_to_result: dict[str, tuple[str, str]] = {}
result_count = 0
log.info("Streaming batch results for %s…", batch_id)
for result in client.beta.messages.batches.results(batch_id):
sub_id = result.custom_id
sub_idx = int(sub_id[3:])
if result.result.type != "succeeded":
log.warning("Sub-batch %s failed: %s", sub_id, result.result.type)
for _, _, path in ordered.get(sub_idx, []):
path_to_result[path] = ("Other", "")
continue
raw = result.result.message.content[0].text
sub_entries = ordered.get(sub_idx, [])
stub_records = [FileRecord(path=Path(p), custom_id=cid) for _, cid, p in sub_entries]
try:
_parse_classification_response(raw, stub_records, topics)
for record in stub_records:
path_to_result[str(record.path.resolve())] = (record.topic or "Other", record.new_name)
result_count += 1
except (json.JSONDecodeError, KeyError, IndexError, ValueError) as exc:
log.warning("Parse error sub-batch %s: %s", sub_id, exc)
for record in stub_records:
path_to_result[str(record.path.resolve())] = ("Other", "")
log.info("Collected results for %d files.", result_count)
return path_to_result
# ---------------------------------------------------------------------------
# File operations
# ---------------------------------------------------------------------------
def sanitize_folder_name(topic: str) -> str:
"""Convert a topic string to a filesystem-safe directory name.
Args:
topic: Raw topic string.
Returns:
Safe folder name.
"""
for ch in r'/\:*?"<>|&':
topic = topic.replace(ch, "_")
return topic.strip().strip(".")
def move_or_copy_file(
record: FileRecord,
output_dir: Path,
copy: bool,
dry_run: bool,
) -> bool:
"""Move or copy a file into its topic subfolder, applying rename if set.
Args:
record: FileRecord with topic and optional new_name.
output_dir: Root output directory.
copy: Copy instead of move.
dry_run: Preview only; do not touch files.
Returns:
True if the operation succeeded (or would succeed in dry-run).
"""
folder_name = sanitize_folder_name(record.topic)
dest_dir = output_dir / folder_name
filename = (record.new_name + record.path.suffix) if record.new_name else record.path.name
dest_path = dest_dir / filename
# Resolve filename collisions
if dest_path.exists() and not dry_run:
stem = Path(filename).stem
suffix = record.path.suffix
counter = 1
while dest_path.exists():
dest_path = dest_dir / f"{stem}_{counter}{suffix}"
counter += 1
if dry_run:
action = "COPY" if copy else "MOVE"
rename_note = f" → {filename}" if record.new_name else ""
log.debug("%s [DRY-RUN] %s%s -> %s/", action, record.path.name, rename_note, folder_name)
return True
try:
dest_dir.mkdir(parents=True, exist_ok=True)
if copy:
shutil.copy2(str(record.path), str(dest_path))
else:
shutil.move(str(record.path), str(dest_path))
return True
except OSError as exc:
log.error("Failed to %s %s: %s", "copy" if copy else "move", record.path.name, exc)
return False
def apply_topics_and_move(
path_to_result: dict[str, tuple[str, str]],
output_dir: Path,
copy: bool,
dry_run: bool,
done: dict[str, str],
progress_path: Path,
) -> SortStats:
"""Move/copy all files based on path→(topic, new_name); update progress.
Args:
path_to_result: Mapping of absolute paths to (topic, new_name) tuples.
output_dir: Root output directory.
copy: Copy instead of move.
dry_run: Preview only.
done: Existing progress dict, updated in-place.
progress_path: Path to persist updated progress.
Returns:
SortStats with move counts and per-topic/type breakdown.
"""
stats = SortStats()
stats.total = len(path_to_result)
for abs_path_str, (topic, new_name) in tqdm(
path_to_result.items(), desc="Moving files", unit="file"
):
record = FileRecord(path=Path(abs_path_str), topic=topic, new_name=new_name)
success = move_or_copy_file(record, output_dir, copy=copy, dry_run=dry_run)
if success:
stats.moved += 1
stats.classified += 1
if new_name:
stats.renamed += 1
stats.topic_counts[topic] = stats.topic_counts.get(topic, 0) + 1
ext = record.path.suffix.lower()
stats.type_counts[ext] = stats.type_counts.get(ext, 0) + 1
if not dry_run:
done[abs_path_str] = topic
else:
stats.failed += 1
if not dry_run and stats.moved % 50 == 0:
save_progress(progress_path, done)
if not dry_run:
save_progress(progress_path, done)
return stats
# ---------------------------------------------------------------------------
# Progress / resume
# ---------------------------------------------------------------------------
def load_progress(progress_path: Path) -> dict[str, str]:
"""Load previously completed path→topic mappings from disk.
Args:
progress_path: Path to the JSON progress file.
Returns:
Dict mapping absolute path strings to topic strings.
"""
if progress_path.exists():
try:
with open(progress_path, "r", encoding="utf-8") as fh:
return json.load(fh)
except (json.JSONDecodeError, OSError):
log.warning("Could not read progress file; starting fresh.")
return {}
def save_progress(progress_path: Path, done: dict[str, str]) -> None:
"""Persist completed path→topic mappings to disk.
Args:
progress_path: Path to write the JSON progress file.
done: Dict mapping absolute path strings to topic strings.
"""
try:
with open(progress_path, "w", encoding="utf-8") as fh:
json.dump(done, fh, indent=2)
except OSError as exc:
log.warning("Could not save progress: %s", exc)
def load_batch_state(state_path: Path) -> dict:
"""Load the batch state file written during the submit phase.
Args:
state_path: Path to the batch state JSON file.
Returns:
Dict with keys: batch_id, topics, batch_size, id_to_path.
Raises:
SystemExit: If the state file is missing or unreadable.
"""
if not state_path.exists():
log.error(
"Batch state file not found: %s\n"
" → Run submit phase first (without --collect).",
state_path,
)
raise SystemExit(1)
try:
with open(state_path, "r", encoding="utf-8") as fh:
return json.load(fh)
except (json.JSONDecodeError, OSError) as exc:
log.error("Could not read batch state: %s", exc)
raise SystemExit(1)
# ---------------------------------------------------------------------------
# File discovery
# ---------------------------------------------------------------------------
def collect_files(input_dir: Path) -> list[Path]:
"""Recursively find all supported files under input_dir.
Args:
input_dir: Root directory to search.
Returns:
Sorted list of absolute file paths with supported extensions.
"""
return sorted(
p for p in input_dir.rglob("*")
if p.is_file() and p.suffix.lower() in SUPPORTED_EXTENSIONS
)
# ---------------------------------------------------------------------------
# Pipelines
# ---------------------------------------------------------------------------
def run_standard_pipeline(
input_dir: Path,
output_dir: Path,
topics: list[str],
dry_run: bool,
copy: bool,
batch_size: int,
workers: int,
) -> SortStats:
"""Standard pipeline: discover → extract → classify (real-time) → move.
Args:
input_dir: Source directory.
output_dir: Destination root directory.
topics: Allowed topic labels.