forked from CodexAITeam/PechaBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
699 lines (550 loc) · 28.1 KB
/
cli.py
File metadata and controls
699 lines (550 loc) · 28.1 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
#!/usr/bin/env python3
"""Unified PechaBridge CLI entrypoint for diffusion and retrieval-encoder workflows."""
from __future__ import annotations
import argparse
import logging
from tibetan_utils.arg_utils import (
create_eval_text_hierarchy_vit_parser,
create_faiss_text_hierarchy_search_parser,
create_prepare_texture_lora_dataset_parser,
create_prepare_donut_ocr_dataset_parser,
create_run_donut_ocr_workflow_parser,
create_train_donut_ocr_parser,
create_train_image_encoder_parser,
create_train_text_hierarchy_vit_parser,
create_train_text_encoder_parser,
create_texture_augment_parser,
create_train_texture_lora_parser,
)
from pechabridge.cli.batch_ocr import create_parser as create_batch_ocr_parser, run as run_batch_ocr
from pechabridge.cli.gen_patches import create_parser as create_gen_patches_parser, run as run_gen_patches
from pechabridge.cli.mine_mnn_pairs import create_parser as create_mnn_pairs_parser, run as run_mnn_pairs
from pechabridge.cli.weak_ocr_label import create_parser as create_weak_ocr_label_parser, run as run_weak_ocr_label
from pechabridge.eval.eval_faiss_crosspage import create_parser as create_eval_faiss_crosspage_parser
from pechabridge.eval.eval_faiss_crosspage import run as run_eval_faiss_crosspage
from pechabridge.semantic_search_workbench.cli import (
create_parser as create_semantic_search_workbench_parser,
)
from pechabridge.semantic_search_workbench.cli import run as run_semantic_search_workbench
from scripts.download_merge_openpecha_ocr_lines import (
create_parser as create_download_openpecha_ocr_lines_parser,
)
from scripts.download_openpecha_line_segmentation import (
create_parser as create_download_openpecha_line_segmentation_parser,
)
from scripts.expand_line_segmentation_dataset import (
create_parser as create_expand_line_segmentation_dataset_parser,
)
from scripts.filter_line_segmentation_dataset import (
create_parser as create_filter_line_segmentation_dataset_parser,
)
from scripts.download_bosentencepiece_tokenizer import (
create_parser as create_download_bosentencepiece_tokenizer_parser,
)
from scripts.download_pechabridge_models import (
create_parser as create_download_pechabridge_models_parser,
main as _download_pechabridge_models_main,
)
from scripts.download_sbb_images import (
create_parser as create_download_sbb_images_parser,
run as _run_download_sbb_images,
)
from scripts.eval_ocr_tokenizer import create_parser as create_eval_ocr_tokenizer_parser
from scripts.extract_donut_ocr_errors import create_parser as create_extract_donut_ocr_errors_parser
from scripts.ocr_error_review_workbench import create_parser as create_ocr_error_review_workbench_parser
from scripts.summarize_donut_ocr_extraction_metrics import (
create_parser as create_summarize_donut_ocr_extraction_metrics_parser,
)
from scripts.train_line_segmentation import create_parser as create_train_line_segmentation_parser
from scripts.warm_line_clip_workbench_cache import (
create_parser as create_warm_line_clip_workbench_cache_parser,
)
from scripts.probe_line_clip_workbench_random_samples import (
create_parser as create_probe_line_clip_workbench_random_samples_parser,
)
LOGGER = logging.getLogger("pechabridge_cli")
def _build_root_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="PechaBridge command line interface")
subparsers = parser.add_subparsers(dest="command", required=True)
prepare_parent = create_prepare_texture_lora_dataset_parser(add_help=False)
prepare_parser = subparsers.add_parser(
"prepare-texture-lora-dataset",
parents=[prepare_parent],
help="Prepare real-page texture crops + JSONL metadata for LoRA training",
description=prepare_parent.description,
)
prepare_parser.set_defaults(handler=_run_prepare_texture_lora_dataset)
train_parent = create_train_texture_lora_parser(add_help=False)
train_parser = subparsers.add_parser(
"train-texture-lora",
parents=[train_parent],
help="Train SDXL texture LoRA adapters using accelerate",
description=train_parent.description,
)
train_parser.set_defaults(handler=_run_train_texture_lora)
augment_parent = create_texture_augment_parser(add_help=False)
augment_parser = subparsers.add_parser(
"texture-augment",
parents=[augment_parent],
help="Apply SDXL + ControlNet Canny texture augmentation",
description=augment_parent.description,
)
augment_parser.set_defaults(handler=_run_texture_augment)
train_image_parent = create_train_image_encoder_parser(add_help=False)
train_image_parser = subparsers.add_parser(
"train-image-encoder",
parents=[train_image_parent],
help="Train self-supervised image encoder for Tibetan page retrieval",
description=train_image_parent.description,
)
train_image_parser.set_defaults(handler=_run_train_image_encoder)
train_text_parent = create_train_text_encoder_parser(add_help=False)
train_text_parser = subparsers.add_parser(
"train-text-encoder",
parents=[train_text_parent],
help="Train unsupervised Tibetan text encoder",
description=train_text_parent.description,
)
train_text_parser.set_defaults(handler=_run_train_text_encoder)
train_hierarchy_parent = create_train_text_hierarchy_vit_parser(add_help=False)
train_hierarchy_parser = subparsers.add_parser(
"train-text-hierarchy-vit",
parents=[train_hierarchy_parent],
help="Train ViT retrieval encoder on TextHierarchy or patch-parquet dataset",
description=train_hierarchy_parent.description,
)
train_hierarchy_parser.set_defaults(handler=_run_train_text_hierarchy_vit)
eval_hierarchy_parent = create_eval_text_hierarchy_vit_parser(add_help=False)
eval_hierarchy_parser = subparsers.add_parser(
"eval-text-hierarchy-vit",
parents=[eval_hierarchy_parent],
help="Evaluate ViT retrieval encoder on TextHierarchy or patch-parquet dataset",
description=eval_hierarchy_parent.description,
)
eval_hierarchy_parser.set_defaults(handler=_run_eval_text_hierarchy_vit)
faiss_hierarchy_parent = create_faiss_text_hierarchy_search_parser(add_help=False)
faiss_hierarchy_parser = subparsers.add_parser(
"faiss-text-hierarchy-search",
parents=[faiss_hierarchy_parent],
help="FAISS similarity search on TextHierarchy/patch-parquet embeddings",
description=faiss_hierarchy_parent.description,
)
faiss_hierarchy_parser.set_defaults(handler=_run_faiss_text_hierarchy_search)
prepare_donut_parent = create_prepare_donut_ocr_dataset_parser(add_help=False)
prepare_donut_parser = subparsers.add_parser(
"prepare-donut-ocr-dataset",
parents=[prepare_donut_parent],
help="Prepare label-filtered OCR manifests (JSONL) for Donut-style training",
description=prepare_donut_parent.description,
)
prepare_donut_parser.set_defaults(handler=_run_prepare_donut_ocr_dataset)
eval_ocr_tokenizer_parent = create_eval_ocr_tokenizer_parser(add_help=False)
eval_ocr_tokenizer_parser = subparsers.add_parser(
"eval-ocr-tokenizer",
parents=[eval_ocr_tokenizer_parent],
help="Evaluate tokenizer coverage/length behavior on OCR manifests (e.g. BoSentencePiece)",
description=eval_ocr_tokenizer_parent.description,
)
eval_ocr_tokenizer_parser.set_defaults(handler=_run_eval_ocr_tokenizer)
train_donut_parent = create_train_donut_ocr_parser(add_help=False)
train_donut_parser = subparsers.add_parser(
"train-donut-ocr",
parents=[train_donut_parent],
help="Train Donut-style OCR model (VisionEncoderDecoder) on OCR crops",
description=train_donut_parent.description,
)
train_donut_parser.set_defaults(handler=_run_train_donut_ocr)
extract_donut_errors_parent = create_extract_donut_ocr_errors_parser(add_help=False)
extract_donut_errors_parser = subparsers.add_parser(
"extract-donut-ocr-errors",
aliases=["extract-donut-errors", "extract-ocr-errors"],
parents=[extract_donut_errors_parent],
help="Extract high-CER OCR samples from a Donut/TrOCR checkpoint into JSONL or a fine-tune dataset",
description=extract_donut_errors_parent.description,
)
extract_donut_errors_parser.set_defaults(handler=_run_extract_donut_ocr_errors)
ocr_error_workbench_parent = create_ocr_error_review_workbench_parser(add_help=False)
ocr_error_workbench_parser = subparsers.add_parser(
"donut-ocr-error-workbench",
aliases=["ocr-error-workbench", "review-donut-ocr-errors"],
parents=[ocr_error_workbench_parent],
help="Launch a live Gradio workbench for reviewing extracted Donut OCR high-CER samples",
description=ocr_error_workbench_parent.description,
)
ocr_error_workbench_parser.set_defaults(handler=_run_ocr_error_review_workbench)
summarize_donut_extractions_parent = create_summarize_donut_ocr_extraction_metrics_parser(add_help=False)
summarize_donut_extractions_parser = subparsers.add_parser(
"summarize-donut-ocr-extractions",
aliases=["summarize-donut-errors", "summarize-ocr-extractions"],
parents=[summarize_donut_extractions_parent],
help="Summarize Donut OCR extraction CER metrics by checkpoint and source dataset",
description=summarize_donut_extractions_parent.description,
)
summarize_donut_extractions_parser.set_defaults(handler=_run_summarize_donut_ocr_extraction_metrics)
workflow_parent = create_run_donut_ocr_workflow_parser(add_help=False)
workflow_parser = subparsers.add_parser(
"run-donut-ocr-workflow",
parents=[workflow_parent],
help="Run full label-1 OCR workflow: generate -> prepare -> train",
description=workflow_parent.description,
)
workflow_parser.set_defaults(handler=_run_donut_ocr_workflow)
hierarchy_parser = subparsers.add_parser(
"export-text-hierarchy",
help="Run YOLO on an input folder and export line + word-block hierarchy crops",
description="Detect text regions and export Tibetan line hierarchy plus number crops.",
)
hierarchy_parser.add_argument("--model", type=str, required=True, help="Path to YOLO model (.pt)")
hierarchy_parser.add_argument("--input-dir", type=str, required=True, help="Input image directory (recursive scan)")
hierarchy_parser.add_argument("--output-dir", type=str, required=True, help="Output directory")
hierarchy_parser.add_argument(
"--no-samples",
"--no_samples",
dest="no_samples",
type=int,
default=0,
help="Randomly sample at most N images from input_dir (0 = use all images)",
)
hierarchy_parser.add_argument("--conf", type=float, default=0.25, help="YOLO confidence threshold")
hierarchy_parser.add_argument("--imgsz", type=int, default=1024, help="YOLO inference image size")
hierarchy_parser.add_argument("--device", type=str, default="", help="Inference device (e.g. cpu, cuda:0)")
hierarchy_parser.add_argument("--min-line-height", type=int, default=10, help="Minimum detected line height in pixels")
hierarchy_parser.add_argument("--line-projection-smooth", type=int, default=9, help="Smoothing window for vertical line profile")
hierarchy_parser.add_argument("--line-projection-threshold-rel", type=float, default=0.20, help="Relative threshold for vertical line profile")
hierarchy_parser.add_argument("--line-merge-gap-px", type=int, default=5, help="Merge gap for neighboring line segments")
hierarchy_parser.add_argument("--horizontal-profile-smooth-cols", type=int, default=21, help="Smoothing window for horizontal profile")
hierarchy_parser.add_argument("--horizontal-profile-threshold-rel", type=float, default=0.20, help="Relative threshold for horizontal profile")
hierarchy_parser.add_argument("--horizontal-seg-min-width-px", type=int, default=14, help="Minimum horizontal segment width")
hierarchy_parser.add_argument("--horizontal-seg-merge-gap-px", type=int, default=6, help="Merge gap for horizontal segments")
hierarchy_parser.add_argument(
"--hierarchy-levels",
type=str,
default="2,4,8",
help="Comma-separated hierarchy levels (e.g. 2,4,8)",
)
hierarchy_parser.set_defaults(handler=_run_export_text_hierarchy)
openpecha_ocr_parent = create_download_openpecha_ocr_lines_parser(add_help=False)
openpecha_ocr_parser = subparsers.add_parser(
"download-openpecha-ocr-lines",
aliases=["download-merge-openpecha-ocr-lines"],
parents=[openpecha_ocr_parent],
help="Download and merge OpenPecha OCR Hugging Face datasets into line dataset format",
description=openpecha_ocr_parent.description,
)
openpecha_ocr_parser.set_defaults(handler=_run_download_openpecha_ocr_lines)
openpecha_line_seg_parent = create_download_openpecha_line_segmentation_parser(add_help=False)
openpecha_line_seg_parser = subparsers.add_parser(
"download-openpecha-line-segmentation",
aliases=["download-openpecha-tibetan-line-segmentation"],
parents=[openpecha_line_seg_parent],
help="Download the OpenPecha Tibetan line segmentation dataset as Ultralytics segment data",
description=openpecha_line_seg_parent.description,
)
openpecha_line_seg_parser.set_defaults(handler=_run_download_openpecha_line_segmentation)
expand_line_seg_parent = create_expand_line_segmentation_dataset_parser(add_help=False)
expand_line_seg_parser = subparsers.add_parser(
"expand-line-segmentation-dataset",
aliases=["expand-line-seg-dataset", "inflate-line-segmentation-dataset"],
parents=[expand_line_seg_parent],
help="Write a new Ultralytics line-segmentation dataset with vertically expanded polygons",
description=expand_line_seg_parent.description,
)
expand_line_seg_parser.set_defaults(handler=_run_expand_line_segmentation_dataset)
filter_line_seg_parent = create_filter_line_segmentation_dataset_parser(add_help=False)
filter_line_seg_parser = subparsers.add_parser(
"filter-line-segmentation-dataset",
aliases=["filter-line-seg-dataset", "prune-line-segmentation-dataset"],
parents=[filter_line_seg_parent],
help="Write a new Ultralytics line-segmentation dataset with tall/narrow polygons removed",
description=filter_line_seg_parent.description,
)
filter_line_seg_parser.set_defaults(handler=_run_filter_line_segmentation_dataset)
train_line_seg_parent = create_train_line_segmentation_parser(add_help=False)
train_line_seg_parser = subparsers.add_parser(
"train-line-segmentation",
parents=[train_line_seg_parent],
help="Train a YOLO line segmentation model on an Ultralytics segment dataset",
description=train_line_seg_parent.description,
)
train_line_seg_parser.set_defaults(handler=_run_train_line_segmentation)
from pechabridge.ocr.bdrc_model_download import create_parser as create_download_bdrc_models_parser
download_bdrc_parent = create_download_bdrc_models_parser(add_help=False)
download_bdrc_parser = subparsers.add_parser(
"download-bdrc-models",
aliases=["download-bdrc-ocr-models", "download-bdrc-default-models"],
parents=[download_bdrc_parent],
help="Download the default BDRC line/layout and OCR model assets into models/bdrc",
description=download_bdrc_parent.description,
)
download_bdrc_parser.set_defaults(handler=_run_download_bdrc_models)
download_pb_parent = create_download_pechabridge_models_parser(add_help=False)
download_pb_parser = subparsers.add_parser(
"download-models",
aliases=["download-pechabridge-models"],
parents=[download_pb_parent],
help=(
"Download PechaBridge models from HuggingFace into models/. "
"Includes OCR, Line Segmentation, and Dual Encoder (--models ocr,line,encoder or 'all')."
),
description=download_pb_parent.description,
)
download_pb_parser.set_defaults(handler=_run_download_pechabridge_models)
download_sbb_parent = create_download_sbb_images_parser(add_help=False)
download_sbb_parser = subparsers.add_parser(
"download-sbb-images",
aliases=["download-stabi-images", "download-sbb"],
parents=[download_sbb_parent],
help="Download page images from the Staatsbibliothek zu Berlin (SBB / Stabi) by PPN",
description=download_sbb_parent.description,
)
download_sbb_parser.set_defaults(handler=_run_download_sbb_images_cmd)
bosentencepiece_parent = create_download_bosentencepiece_tokenizer_parser(add_help=False)
bosentencepiece_parser = subparsers.add_parser(
"download-bosentencepiece-tokenizer",
aliases=["download-bosentencepiece"],
parents=[bosentencepiece_parent],
help="Download and verify OpenPecha BoSentencePiece tokenizer into ext/BoSentencePiece",
description=bosentencepiece_parent.description,
)
bosentencepiece_parser.set_defaults(handler=_run_download_bosentencepiece_tokenizer)
batch_ocr_parent = create_batch_ocr_parser(add_help=False)
batch_ocr_parser = subparsers.add_parser(
"batch-ocr",
parents=[batch_ocr_parent],
help="Batch OCR a folder of Pecha images using a DONUT OCR model and a YOLO layout model",
description=batch_ocr_parent.description,
)
batch_ocr_parser.set_defaults(handler=_run_batch_ocr)
gen_patches_parent = create_gen_patches_parser(add_help=False)
gen_patches_parser = subparsers.add_parser(
"gen-patches",
parents=[gen_patches_parent],
help="Generate line sub-patch dataset with Option-A neighborhood metadata",
description=gen_patches_parent.description,
)
gen_patches_parser.set_defaults(handler=_run_gen_patches)
weak_ocr_parent = create_weak_ocr_label_parser(add_help=False)
weak_ocr_parser = subparsers.add_parser(
"weak-ocr-label",
parents=[weak_ocr_parent],
help="Generate weak OCR labels for patch datasets",
description=weak_ocr_parent.description,
)
weak_ocr_parser.set_defaults(handler=_run_weak_ocr_label)
semantic_search_parent = create_semantic_search_workbench_parser(add_help=False)
semantic_search_parser = subparsers.add_parser(
"semantic-search-workbench",
parents=[semantic_search_parent],
help="Launch the Gradio-based Semantic Search Workbench for Tibetan transcripts",
description=semantic_search_parent.description,
)
semantic_search_parser.set_defaults(handler=_run_semantic_search_workbench)
ocr_workbench_parser = subparsers.add_parser(
"ocr-workbench",
aliases=["ui-ocr-workbench"],
help="Launch the dedicated OCR Workbench UI",
description="Launch the Gradio OCR Workbench for interactive Tibetan OCR on pecha page images.",
)
_add_workbench_launch_args(ocr_workbench_parser, default_host="0.0.0.0", default_port=7865)
ocr_workbench_parser.set_defaults(handler=_run_ocr_workbench)
layout_workbench_parser = subparsers.add_parser(
"layout-workbench",
aliases=["ui-workbench"],
help="Launch the full PechaBridge layout/training Workbench UI",
description="Launch the full Gradio Workbench for dataset generation, layout training, OCR utilities, and audits.",
)
_add_workbench_launch_args(layout_workbench_parser, default_host="127.0.0.1", default_port=7860)
layout_workbench_parser.set_defaults(handler=_run_layout_workbench)
transformer_layout_parser = subparsers.add_parser(
"transformer-layout-workbench",
aliases=["transformer-layout-ui"],
help="Launch the transformer layout/OCR parser Workbench UI",
description="Launch the Gradio UI for transformer-based layout and OCR parser experiments.",
)
_add_workbench_launch_args(transformer_layout_parser, default_host="127.0.0.1", default_port=7866)
transformer_layout_parser.set_defaults(handler=_run_transformer_layout_workbench)
mnn_parent = create_mnn_pairs_parser(add_help=False)
mnn_parser = subparsers.add_parser(
"mine-mnn-pairs",
parents=[mnn_parent],
help="Mine robust cross-page MNN positives from patch dataset",
description=mnn_parent.description,
)
mnn_parser.set_defaults(handler=_run_mine_mnn_pairs)
eval_cross_parent = create_eval_faiss_crosspage_parser(add_help=False)
eval_cross_parser = subparsers.add_parser(
"eval-faiss-crosspage",
parents=[eval_cross_parent],
help="Evaluate cross-page retrieval with FAISS from exported embeddings",
description=eval_cross_parent.description,
)
eval_cross_parser.set_defaults(handler=_run_eval_faiss_crosspage)
warm_line_clip_cache_parent = create_warm_line_clip_workbench_cache_parser(add_help=False)
warm_line_clip_cache_parser = subparsers.add_parser(
"warm-line-clip-workbench-cache",
parents=[warm_line_clip_cache_parent],
help="Build/persist line_clip Workbench corpus embeddings for all available OCR splits using the best line_clip model",
description=warm_line_clip_cache_parent.description,
)
warm_line_clip_cache_parser.set_defaults(handler=_run_warm_line_clip_workbench_cache)
probe_line_clip_parent = create_probe_line_clip_workbench_random_samples_parser(add_help=False)
probe_line_clip_parser = subparsers.add_parser(
"probe-line-clip-workbench-random-samples",
parents=[probe_line_clip_parent],
help="Probe best line_clip Workbench retrieval on random in-corpus samples across splits",
description=probe_line_clip_parent.description,
)
probe_line_clip_parser.set_defaults(handler=_run_probe_line_clip_workbench_random_samples)
return parser
def _add_workbench_launch_args(parser: argparse.ArgumentParser, *, default_host: str, default_port: int) -> None:
parser.add_argument("--host", type=str, default=default_host, help=f"Server host (default: {default_host})")
parser.add_argument("--port", type=int, default=default_port, help=f"Server port (default: {default_port})")
parser.add_argument("--share", action="store_true", help="Enable Gradio public share link")
def _run_prepare_texture_lora_dataset(args: argparse.Namespace) -> int:
from scripts.prepare_texture_lora_dataset import run
run(args)
return 0
def _run_train_texture_lora(args: argparse.Namespace) -> int:
from scripts.train_texture_lora_sdxl import run
run(args)
return 0
def _run_texture_augment(args: argparse.Namespace) -> int:
from scripts.texture_augment import run
run(args)
return 0
def _run_train_image_encoder(args: argparse.Namespace) -> int:
from scripts.train_image_encoder import run
run(args)
return 0
def _run_train_text_encoder(args: argparse.Namespace) -> int:
from scripts.train_text_encoder import run
run(args)
return 0
def _run_train_text_hierarchy_vit(args: argparse.Namespace) -> int:
from scripts.train_text_hierarchy_vit import run
run(args)
return 0
def _run_eval_text_hierarchy_vit(args: argparse.Namespace) -> int:
from scripts.eval_text_hierarchy_vit import run
run(args)
return 0
def _run_faiss_text_hierarchy_search(args: argparse.Namespace) -> int:
from scripts.faiss_text_hierarchy_search import run
run(args)
return 0
def _run_prepare_donut_ocr_dataset(args: argparse.Namespace) -> int:
from scripts.prepare_donut_ocr_dataset import run
run(args)
return 0
def _run_eval_ocr_tokenizer(args: argparse.Namespace) -> int:
from scripts.eval_ocr_tokenizer import run
run(args)
return 0
def _run_train_donut_ocr(args: argparse.Namespace) -> int:
from scripts.train_donut_ocr import run
run(args)
return 0
def _run_extract_donut_ocr_errors(args: argparse.Namespace) -> int:
from scripts.extract_donut_ocr_errors import run
run(args)
return 0
def _run_ocr_error_review_workbench(args: argparse.Namespace) -> int:
from scripts.ocr_error_review_workbench import run
return int(run(args))
def _run_summarize_donut_ocr_extraction_metrics(args: argparse.Namespace) -> int:
from scripts.summarize_donut_ocr_extraction_metrics import run
run(args)
return 0
def _run_donut_ocr_workflow(args: argparse.Namespace) -> int:
from scripts.run_donut_ocr_workflow import run
run(args)
return 0
def _run_export_text_hierarchy(args: argparse.Namespace) -> int:
from scripts.export_text_hierarchy import run
run(args)
return 0
def _run_download_openpecha_ocr_lines(args: argparse.Namespace) -> int:
from scripts.download_merge_openpecha_ocr_lines import run
run(args)
return 0
def _run_download_openpecha_line_segmentation(args: argparse.Namespace) -> int:
from scripts.download_openpecha_line_segmentation import run
run(args)
return 0
def _run_expand_line_segmentation_dataset(args: argparse.Namespace) -> int:
from scripts.expand_line_segmentation_dataset import run
run(args)
return 0
def _run_filter_line_segmentation_dataset(args: argparse.Namespace) -> int:
from scripts.filter_line_segmentation_dataset import run
run(args)
return 0
def _run_train_line_segmentation(args: argparse.Namespace) -> int:
from scripts.train_line_segmentation import run
run(args)
return 0
def _run_download_bdrc_models(args: argparse.Namespace) -> int:
from scripts.download_bdrc_models import run
return int(run(args))
def _run_download_pechabridge_models(args: argparse.Namespace) -> int:
return int(_download_pechabridge_models_main([
"--models", str(getattr(args, "models", "all") or "all"),
"--dest", str(getattr(args, "dest", "") or ""),
*(["--token", str(args.token)] if getattr(args, "token", "") else []),
*(["--force"] if getattr(args, "force", False) else []),
]))
def _run_download_bosentencepiece_tokenizer(args: argparse.Namespace) -> int:
from scripts.download_bosentencepiece_tokenizer import run
return int(run(args))
def _run_download_sbb_images_cmd(args: argparse.Namespace) -> int:
return int(_run_download_sbb_images(args))
def _run_batch_ocr(args: argparse.Namespace) -> int:
return int(run_batch_ocr(args))
def _run_gen_patches(args: argparse.Namespace) -> int:
run_gen_patches(args)
return 0
def _run_weak_ocr_label(args: argparse.Namespace) -> int:
run_weak_ocr_label(args)
return 0
def _run_semantic_search_workbench(args: argparse.Namespace) -> int:
return int(run_semantic_search_workbench(args))
def _run_ocr_workbench(args: argparse.Namespace) -> int:
from scripts.ui_ocr_workbench import build_ui
app = build_ui()
app.launch(server_name=args.host, server_port=int(args.port), share=bool(args.share))
return 0
def _run_layout_workbench(args: argparse.Namespace) -> int:
from scripts.ui_workbench import build_ui
app = build_ui()
app.launch(server_name=args.host, server_port=int(args.port), share=bool(args.share))
return 0
def _run_transformer_layout_workbench(args: argparse.Namespace) -> int:
from scripts.ui_transformer_layout import build_demo
app = build_demo()
app.launch(server_name=args.host, server_port=int(args.port), share=bool(args.share))
return 0
def _run_mine_mnn_pairs(args: argparse.Namespace) -> int:
run_mnn_pairs(args)
return 0
def _run_eval_faiss_crosspage(args: argparse.Namespace) -> int:
run_eval_faiss_crosspage(args)
return 0
def _run_warm_line_clip_workbench_cache(args: argparse.Namespace) -> int:
from scripts.warm_line_clip_workbench_cache import run
return int(run(args))
def _run_probe_line_clip_workbench_random_samples(args: argparse.Namespace) -> int:
from scripts.probe_line_clip_workbench_random_samples import run
return int(run(args))
def main(argv: list[str] | None = None) -> int:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
parser = _build_root_parser()
args = parser.parse_args(argv)
handler = getattr(args, "handler", None)
if handler is None:
parser.error("No subcommand selected")
return handler(args)
if __name__ == "__main__":
raise SystemExit(main())