-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·1170 lines (1005 loc) · 48.2 KB
/
cli.py
File metadata and controls
executable file
·1170 lines (1005 loc) · 48.2 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
#!/usr/bin/env python3
"""
Interactive CLI for AI-Powered OCR Tool (OLMoCR)
A customizable command-line interface for managing OCR operations with Docker integration.
"""
import os
import sys
import json
import subprocess
import argparse
import re
import time
import select
import termios
import tty
from pathlib import Path
from typing import Dict, List, Optional, Any
import yaml
from rich.console import Console
def load_env_file(env_path: str = ".env") -> None:
"""Load environment variables from .env file if it exists."""
env_file = Path(env_path)
if env_file.exists():
try:
with open(env_file, 'r') as f:
for line in f:
line = line.strip()
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
key = key.strip()
value = value.strip()
# Remove quotes if present
if (value.startswith('"') and value.endswith('"')) or \
(value.startswith("'") and value.endswith("'")):
value = value[1:-1]
# Only set if not already in environment
if key not in os.environ:
os.environ[key] = value
except Exception as e:
print(f"Warning: Could not load .env file: {e}")
# Load .env file
load_env_file()
from rich.progress import Progress, BarColumn, TextColumn, TimeRemainingColumn, MofNCompleteColumn
from rich.live import Live
from rich.panel import Panel
from rich.text import Text
from rich.spinner import Spinner
from rich.table import Table
from rich.layout import Layout
from rich.align import Align
from rich.columns import Columns
import random
class OCRConfig:
"""Configuration manager for OCR CLI settings."""
def __init__(self):
pass
self.default_config = {
"data_directory": "./data",
"workspace_directory": "./data/workspace",
"docker_image": "alleninstituteforai/olmocr:latest",
"output_format": "markdown",
"gpu_enabled": True,
"batch_size": 1,
"parallel_workers": 1,
"debug_mode": False,
"auto_cleanup": True,
"container_name": "olmocr",
"ssl_cert_path": "/path/to/your/certificate.crt",
"ssl_enabled": False,
"show_logs": False
}
self.config = self.load_config()
def load_config(self) -> Dict[str, Any]:
"""Load configuration from environment variables and .env file."""
config = self.default_config.copy()
# Load from environment variables
env_mapping = {
"data_directory": "DATA_DIRECTORY",
"workspace_directory": "WORKSPACE_DIRECTORY",
"docker_image": "DOCKER_IMAGE",
"output_format": "OUTPUT_FORMAT",
"gpu_enabled": "GPU_ENABLED",
"batch_size": "BATCH_SIZE",
"parallel_workers": "PARALLEL_WORKERS",
"debug_mode": "DEBUG_MODE",
"auto_cleanup": "AUTO_CLEANUP",
"container_name": "CONTAINER_NAME",
"ssl_cert_path": "SSL_CERT_PATH",
"ssl_enabled": "SSL_ENABLED",
"show_logs": "SHOW_LOGS"
}
for config_key, env_key in env_mapping.items():
env_value = os.getenv(env_key)
if env_value is not None:
# Type conversion
if isinstance(config[config_key], bool):
config[config_key] = env_value.lower() in ('true', '1', 'yes', 'on')
elif isinstance(config[config_key], int):
try:
config[config_key] = int(env_value)
except ValueError:
pass
else:
config[config_key] = env_value
return config
def save_config(self) -> None:
"""Configuration is managed via .env file - no action needed."""
print("Configuration is managed via .env file. Edit .env to change settings.")
def get(self, key: str, default: Any = None) -> Any:
"""Get configuration value."""
return self.config.get(key, default)
def set(self, key: str, value: Any) -> None:
"""Set configuration value."""
self.config[key] = value
def reset_to_defaults(self) -> None:
"""Reset configuration to defaults."""
self.config = self.default_config.copy()
class OCRInterface:
"""Main CLI interface for OCR operations."""
def __init__(self):
self.config = OCRConfig()
self.data_dir = Path(self.config.get("data_directory"))
self.workspace_dir = Path(self.config.get("workspace_directory"))
self.show_logs_mode = self.config.get("show_logs", False)
def check_keypress(self) -> Optional[str]:
"""Check for keypress in non-blocking mode. Returns key or None."""
if not sys.stdin.isatty():
return None
try:
# Save current terminal settings
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
# Set terminal to non-blocking mode
try:
tty.cbreak(fd)
except AttributeError:
# tty.cbreak not available on this system
return None
# Check if input is available
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
key = sys.stdin.read(1)
# Restore terminal settings
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return key
# Restore terminal settings
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return None
except (OSError, termios.error, AttributeError):
return None
def ensure_directories(self) -> None:
"""Ensure required directories exist."""
self.data_dir.mkdir(exist_ok=True)
self.workspace_dir.mkdir(exist_ok=True)
print(f"Data directory: {self.data_dir.absolute()}")
print(f"Workspace directory: {self.workspace_dir.absolute()}")
def list_pdfs(self) -> List[Path]:
"""List all PDF files in data directory."""
pdf_files = list(self.data_dir.glob("*.pdf"))
return sorted(pdf_files)
def check_docker(self) -> bool:
"""Check if Docker is available and running."""
try:
result = subprocess.run(['docker', '--version'],
capture_output=True, text=True, timeout=10)
return result.returncode == 0
except (subprocess.TimeoutExpired, FileNotFoundError):
return False
def check_gpu_support(self) -> bool:
"""Check if NVIDIA GPU support is available."""
try:
result = subprocess.run(['nvidia-smi'],
capture_output=True, text=True, timeout=10)
return result.returncode == 0
except (subprocess.TimeoutExpired, FileNotFoundError):
return False
def pull_docker_image(self) -> bool:
"""Pull the latest OCR Docker image."""
image = self.config.get("docker_image")
print(f"Pulling Docker image: {image}")
try:
result = subprocess.run(['docker', 'pull', image],
capture_output=True, text=True)
if result.returncode == 0:
print("Docker image pulled successfully")
return True
else:
print(f"Error pulling image: {result.stderr}")
return False
except Exception as e:
print(f"Error: {e}")
return False
def check_docker_compose(self) -> bool:
"""Check if docker-compose is available and docker-compose.yml exists."""
try:
# Check for Docker Compose V2 first (docker compose)
result = subprocess.run(['docker', 'compose', 'version'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
# Check if appropriate docker-compose file exists
return self.get_compose_file().exists()
# Fallback to Docker Compose V1 (docker-compose)
result = subprocess.run(['docker-compose', '--version'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
# Check if appropriate docker-compose file exists
return self.get_compose_file().exists()
return False
except (subprocess.TimeoutExpired, FileNotFoundError):
return False
def get_compose_file(self) -> Path:
"""Get the appropriate docker-compose file based on SSL configuration."""
if self.config.get("ssl_enabled") and self.config.get("ssl_cert_path") and Path(self.config.get("ssl_cert_path")).exists():
return Path("docker-compose.ssl.yml")
else:
return Path("docker-compose.yml")
def get_docker_compose_cmd(self) -> List[str]:
"""Get the appropriate docker compose command."""
try:
# Try Docker Compose V2 first
result = subprocess.run(['docker', 'compose', 'version'],
capture_output=True, text=True, timeout=5)
if result.returncode == 0:
return ['docker', 'compose']
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
# Fallback to V1
return ['docker-compose']
def build_docker_command(self, pdfs: List[str], output_format: str = "markdown") -> List[str]:
"""Build Docker command for OCR processing."""
# Prefer docker-compose if available (better for model caching)
if self.check_docker_compose():
compose_cmd = self.get_docker_compose_cmd()
compose_file = self.get_compose_file()
# Convert workspace path to container path (relative to /app/input)
container_workspace = "workspace" # Inside container, workspace is just "workspace"
cmd = compose_cmd + ["-f", str(compose_file), "exec", "-T", "olmocr",
"python", "-m", "olmocr.pipeline",
container_workspace
]
# Only add --markdown flag if markdown is selected (json is default)
if output_format == "markdown":
cmd.append("--markdown")
cmd.extend(["--pdfs"] + pdfs)
return cmd
# Fallback to docker run (less efficient, downloads models each time)
print("Warning: Using docker run - models will be downloaded each time. Consider using docker-compose for better performance.")
gpu_flag = ["--gpus", "all"] if self.config.get("gpu_enabled") and self.check_gpu_support() else []
# SSL certificate mounting and environment variables
ssl_volume = []
ssl_env = []
if self.config.get("ssl_enabled"):
cert_path = self.config.get("ssl_cert_path")
if cert_path and Path(cert_path).exists():
ssl_volume = ["-v", f"{cert_path}:/etc/ssl/certs/corporate_ca.crt:ro"]
ssl_env = [
"-e", "REQUESTS_CA_BUNDLE=/etc/ssl/certs/corporate_ca.crt",
"-e", "SSL_CERT_FILE=/etc/ssl/certs/corporate_ca.crt",
"-e", "HF_HUB_CERTIFICATE=/etc/ssl/certs/corporate_ca.crt",
"-e", "CURL_CA_BUNDLE=/etc/ssl/certs/corporate_ca.crt"
]
elif self.config.get("debug_mode"):
print(f"Warning: SSL certificate not found at {cert_path}")
# Convert workspace path to container path
container_workspace = "workspace" # Inside container, workspace is just "workspace"
cmd = [
"docker", "run", "--rm", "-it"
] + gpu_flag + [
"-v", f"{self.data_dir.absolute()}:/app/data",
"-w", "/app/data"
] + ssl_volume + ssl_env + [
"--name", self.config.get("container_name"),
self.config.get("docker_image"),
"python", "-m", "olmocr.pipeline",
container_workspace
]
# Only add --markdown flag if markdown is selected (json is default)
if output_format == "markdown":
cmd.append("--markdown")
cmd.extend(["--pdfs"] + pdfs)
return cmd
def ensure_compose_container(self) -> bool:
"""Ensure docker-compose container is running."""
if not self.check_docker_compose():
return True # Skip if not using compose
try:
# Set environment variables for docker-compose
env = os.environ.copy()
compose_file = self.get_compose_file()
# Check if container is running
compose_cmd = self.get_docker_compose_cmd()
result = subprocess.run(compose_cmd + ['-f', str(compose_file), 'ps', '-q', 'olmocr'],
capture_output=True, text=True, env=env)
if not result.stdout.strip():
ssl_status = "with SSL" if compose_file.name == "docker-compose.ssl.yml" else "without SSL"
print(f"Starting docker-compose container ({ssl_status})...")
start_result = subprocess.run(compose_cmd + ['-f', str(compose_file), 'up', '-d'],
capture_output=True, text=True, env=env)
if start_result.returncode != 0:
print(f"Failed to start container: {start_result.stderr}")
return False
print("Container started successfully")
return True
except Exception as e:
print(f"Error managing container: {e}")
return False
def parse_olmocr_log(self, line: str) -> Dict[str, Any]:
"""Parse olmocr pipeline log lines for progress information."""
progress_info = {}
# File processing start: "Worker 0 processing s3://..."
if "processing" in line and ".pdf" in line:
# Extract filename from path
filename_match = re.search(r'([^/]+\.pdf)', line)
if filename_match:
progress_info['current_file'] = filename_match.group(1)
# Page progress: "INFO:olmocr.pipeline:Finished page 5/20 of document.pdf"
page_match = re.search(r'Finished page (\d+)/(\d+)', line)
if page_match:
completed, total = map(int, page_match.groups())
progress_info['pages_completed'] = completed
progress_info['pages_total'] = total
progress_info['page_progress'] = (completed / total) * 100
# Document completion: "Worker 0 finished work item"
if "finished work item" in line or "completed successfully" in line:
progress_info['document_completed'] = True
# Error detection
if "ERROR" in line or "Failed" in line:
progress_info['error'] = line.strip()
# Status updates
if "Starting" in line and "processing" in line:
progress_info['status'] = 'Starting processing'
elif "Extracting text" in line:
progress_info['status'] = 'Extracting text'
elif "Converting to markdown" in line:
progress_info['status'] = 'Converting to markdown'
elif "Saving output" in line:
progress_info['status'] = 'Saving output'
return progress_info
def get_motivational_message(self) -> str:
"""Get a random motivational message for completed documents."""
messages = [
"✨ Another one bites the dust!",
"🎯 Nailed it!",
"🚀 One small step for OCR...",
"💎 Polished to perfection!",
"🏆 Champion level processing!",
"⚡ Lightning fast results!",
"🎪 And the crowd goes wild!",
"🌟 Pure excellence in motion!"
]
return random.choice(messages)
def create_status_dashboard(self, processed_files: int, total_files: int, current_file: str,
processed_pages: int, total_pages: int, elapsed_time: float) -> Table:
"""Create a live status dashboard."""
table = Table.grid(expand=True)
table.add_column(justify="left", style="cyan", no_wrap=True)
table.add_column(justify="center", style="magenta", no_wrap=True)
table.add_column(justify="right", style="green", no_wrap=True)
# File progress
files_emoji = "📁" if processed_files == 0 else "📂" if processed_files < total_files else "📋"
files_status = f"{files_emoji} Files: {processed_files}/{total_files}"
# Current document
doc_emoji = "📄" if current_file else "⏳"
current_doc = f"{doc_emoji} {current_file[:20]}..." if len(current_file) > 20 else f"{doc_emoji} {current_file}"
# Pages and timing
pages_status = f"📃 Pages: {processed_pages}/{total_pages}" if total_pages > 0 else "📃 Pages: --"
time_status = f"⏱️ Elapsed: {elapsed_time:.1f}s"
table.add_row(files_status, current_doc, f"{pages_status} {time_status}")
return table
def highlight_log_line(self, line: str, console: Console) -> None:
"""Apply color coding to log lines based on content."""
line = line.strip()
if not line:
return
if "ERROR" in line or "FAILED" in line.upper():
console.print(f"[red]❌ {line}[/]")
elif "WARNING" in line or "WARN" in line:
console.print(f"[yellow]⚠️ {line}[/]")
elif "SUCCESS" in line.upper() or "COMPLETED" in line.upper():
console.print(f"[green]✅ {line}[/]")
elif "INFO" in line:
console.print(f"[dim cyan]{line}[/]")
elif "DEBUG" in line:
console.print(f"[dim]{line}[/]")
else:
console.print(f"[dim white]{line}[/]")
def show_completion_celebration(self, console: Console, processed_files: int, total_time: float) -> None:
"""Show a celebration animation when all processing is complete."""
# Celebration panel
celebration_text = Text.assemble(
("🎉 ", "bold yellow"),
("MISSION ACCOMPLISHED!", "bold green"),
(" 🎉\n", "bold yellow"),
(f"Successfully processed {processed_files} file(s)\n", "green"),
(f"Total time: {total_time:.1f} seconds", "cyan")
)
celebration_panel = Panel(
Align.center(celebration_text),
border_style="green",
title="[bold green]🏆 SUCCESS 🏆[/]",
title_align="center"
)
console.print("\n")
console.print(celebration_panel)
# ASCII confetti effect
confetti = ["🎊", "🎉", "✨", "🌟", "💫", "⭐", "🎈", "🎆"]
confetti_line = " ".join([random.choice(confetti) for _ in range(15)])
console.print(f"\n[bold yellow]{confetti_line}[/]")
console.print(f"[bold green]All files are ready in your workspace directory![/]")
console.print(f"[bold yellow]{confetti_line}[/]\n")
def process_pdfs_with_progress(self, pdf_files: List[str], output_format: str = "markdown") -> bool:
"""Process PDF files with toggle between progress visualization and logs."""
if not pdf_files:
print("No PDF files specified")
return False
console = Console()
start_time = time.time()
show_logs = self.show_logs_mode
# Initial setup with spinner
with console.status("[bold green]🔧 Preparing OCR engine...") as status:
status.update("[bold cyan]🐳 Checking Docker containers...")
time.sleep(0.5) # Brief pause for visual effect
if not self.ensure_compose_container():
console.print("❌ [bold red]Failed to start containers[/]")
return False
status.update("[bold magenta]⚙️ Building processing pipeline...")
cmd = self.build_docker_command(pdf_files, output_format)
time.sleep(0.3)
if self.config.get("debug_mode"):
console.print("Docker command:", style="dim")
console.print(" ".join(cmd), style="dim")
# Welcome banner
welcome_panel = Panel(
Text.assemble(
("🤖 AI-POWERED OCR ENGINE\n", "bold blue"),
(f"Ready to process {len(pdf_files)} document(s)\n", "green"),
(f"Output format: {output_format.upper()}", "cyan")
),
border_style="blue",
title="[bold white]🚀 LAUNCHING[/]",
title_align="center"
)
console.print(welcome_panel)
# Show initial instructions
instructions_text = Text.assemble(
("💡 Press ", "dim cyan"),
("'l'", "bold yellow"),
(" to toggle between progress bars and logs", "dim cyan")
)
instructions_panel = Panel(
instructions_text,
border_style="dim blue",
title="[dim]Controls[/]"
)
console.print(instructions_panel)
# Enhanced progress bars with emojis and styling
progress_manager = Progress(
TextColumn("📊 [bold blue]{task.description}", justify="left"),
BarColumn(bar_width=35, complete_style="green", finished_style="bold green"),
MofNCompleteColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.1f}%"),
"•",
TimeRemainingColumn(),
console=console,
)
# Start progress context manager
with progress_manager as progress:
# Overall progress across all files
overall_task = progress.add_task(
"🗂️ Overall Progress",
total=len(pdf_files),
visible=not show_logs
)
# Current document progress
doc_task = progress.add_task(
"📄 Current Document",
total=100,
visible=False
)
processed_files = 0
current_file = ""
processed_pages = 0
total_pages = 0
try:
# Start processing
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True
)
if process.stdout:
for line in iter(process.stdout.readline, ''):
if not line:
break
# Check for toggle keypress
key = self.check_keypress()
if key and key.lower() == 'l':
show_logs = not show_logs
mode_text = "LOG MODE" if show_logs else "PROGRESS BAR MODE"
mode_color = "yellow" if show_logs else "green"
console.print(f"\n[bold {mode_color}]📺 Switched to {mode_text}[/]\n")
# Toggle progress bar visibility
progress.update(overall_task, visible=not show_logs)
progress.update(doc_task, visible=not show_logs and current_file)
# Parse the log line for progress information
log_info = self.parse_olmocr_log(line)
# New document started
if 'current_file' in log_info:
current_file = log_info['current_file']
if not show_logs:
# Show document transition banner
doc_banner = Panel.fit(
f"📑 Starting OCR: [bold yellow]{current_file}[/]",
border_style="magenta",
title="[bold white]📄 New Document[/]"
)
console.print(doc_banner)
progress.update(
doc_task,
description=f"📑 Processing: [yellow]{current_file}[/]",
completed=0,
total=100,
visible=True
)
# Update page progress within current document
if 'page_progress' in log_info and not show_logs:
processed_pages = log_info.get('pages_completed', processed_pages)
total_pages = log_info.get('pages_total', total_pages)
progress.update(
doc_task,
completed=log_info['page_progress'],
description=f"📃 {current_file}: [cyan]Page {processed_pages}/{total_pages}[/]"
)
# Document completed
if log_info.get('document_completed'):
processed_files += 1
motivational_msg = self.get_motivational_message()
if not show_logs:
progress.update(overall_task, completed=processed_files)
progress.update(
doc_task,
completed=100,
description=f"✅ [bold green]Completed: {current_file}[/] [dim]- {motivational_msg}[/]"
)
# Brief celebration pause
time.sleep(0.8)
# Show status updates with enhanced descriptions
if 'status' in log_info and not show_logs:
status_emoji = {
'Starting processing': '🏁',
'Extracting text': '🔤',
'Converting to markdown': '📝',
'Saving output': '💾'
}
emoji = status_emoji.get(log_info['status'], '⚙️')
progress.update(
doc_task,
description=f"{emoji} [cyan]{current_file}: {log_info['status']}[/]"
)
# Enhanced log highlighting
if 'error' in log_info:
self.highlight_log_line(f"ERROR: {log_info['error']}", console)
elif show_logs:
# In log mode, show all log lines
self.highlight_log_line(line, console)
elif not any(key in log_info for key in ['current_file', 'page_progress', 'status', 'document_completed']):
# In progress mode, only show debug/error/warning lines
if self.config.get("debug_mode") or "ERROR" in line or "WARNING" in line:
self.highlight_log_line(line, console)
# Wait for process completion
return_code = process.wait()
total_time = time.time() - start_time
# Ensure progress bars show completion
if not show_logs:
progress.update(overall_task, completed=len(pdf_files))
progress.update(doc_task, completed=100, visible=False)
if return_code == 0:
self.show_completion_celebration(console, len(pdf_files), total_time)
return True
else:
console.print(Panel(
f"❌ [bold red]Processing failed with exit code {return_code}[/]\n"
f"Check the logs above for details.",
border_style="red",
title="[bold red]⚠️ PROCESSING FAILED[/]"
))
return False
except KeyboardInterrupt:
console.print(Panel(
"⚠️ [bold yellow]Operation cancelled by user[/]\n"
"Partial results may be available in workspace directory.",
border_style="yellow",
title="[bold yellow]🛑 CANCELLED[/]"
))
if 'process' in locals() and process.poll() is None:
process.terminate()
process.wait()
return False
except Exception as e:
console.print(Panel(
f"💥 [bold red]Unexpected error: {e}[/]\n"
f"Please check your configuration and try again.",
border_style="red",
title="[bold red]❌ ERROR[/]"
))
return False
def process_pdfs(self, pdf_files: List[str], output_format: str = "markdown") -> bool:
"""Process PDF files using OCR with progress visualization."""
# Use rich progress visualization if available, fallback to simple version
try:
return self.process_pdfs_with_progress(pdf_files, output_format)
except ImportError:
# Fallback to simple processing if rich is not available
return self.process_pdfs_simple(pdf_files, output_format)
def process_pdfs_simple(self, pdf_files: List[str], output_format: str = "markdown") -> bool:
"""Simple PDF processing without rich progress (fallback)."""
if not pdf_files:
print("No PDF files specified")
return False
print(f"Processing {len(pdf_files)} PDF file(s):")
for pdf in pdf_files:
print(f" - {pdf}")
# Ensure container is running if using compose
if not self.ensure_compose_container():
return False
cmd = self.build_docker_command(pdf_files, output_format)
if self.config.get("debug_mode"):
print("Docker command:")
print(" ".join(cmd))
try:
print("Starting OCR processing...")
result = subprocess.run(cmd)
return result.returncode == 0
except KeyboardInterrupt:
print("\nOperation cancelled by user")
return False
except Exception as e:
print(f"Error during processing: {e}")
return False
def show_results(self) -> None:
"""Display processing results."""
markdown_dir = self.workspace_dir / "markdown"
results_dir = self.workspace_dir / "results"
if markdown_dir.exists():
markdown_files = list(markdown_dir.glob("*.md"))
if markdown_files:
print(f"\nMarkdown output files ({len(markdown_files)}):")
for md_file in sorted(markdown_files):
print(f" - {md_file.name}")
if results_dir.exists():
result_files = list(results_dir.glob("*.jsonl"))
if result_files:
print(f"\nResult files ({len(result_files)}):")
for result_file in sorted(result_files):
print(f" - {result_file.name}")
def create_system_status_table(self) -> Table:
"""Create a beautiful system status table."""
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Component", style="cyan", no_wrap=True)
table.add_column("Status", justify="center")
table.add_column("Details", style="dim")
# Docker status
docker_ok = self.check_docker()
docker_status = "✅ Ready" if docker_ok else "❌ Not Available"
table.add_row("🐳 Docker", docker_status, "Container orchestration")
# GPU support
if self.config.get("gpu_enabled"):
gpu_ok = self.check_gpu_support()
gpu_status = "✅ Available" if gpu_ok else "❌ Not Detected"
gpu_details = "NVIDIA acceleration" if gpu_ok else "CPU fallback mode"
table.add_row("🚀 GPU", gpu_status, gpu_details)
# SSL certificate
if self.config.get("ssl_enabled"):
cert_path = self.config.get("ssl_cert_path")
cert_ok = cert_path and Path(cert_path).exists()
cert_status = "✅ Valid" if cert_ok else "❌ Missing"
cert_details = f"Corporate CA" if cert_ok else "Certificate not found"
table.add_row("🔒 SSL", cert_status, cert_details)
# Docker Compose
compose_ok = self.check_docker_compose()
compose_status = "✅ Available" if compose_ok else "❌ Missing"
compose_details = "Preferred mode" if compose_ok else "Will use docker run"
table.add_row("📋 Compose", compose_status, compose_details)
return table
def create_file_summary_panel(self) -> Panel:
"""Create a panel showing PDF file summary."""
pdf_files = self.list_pdfs()
if not pdf_files:
content = Text("📂 No PDF files found in data directory", style="yellow")
else:
total_size = sum(pdf.stat().st_size for pdf in pdf_files) / (1024 * 1024) # MB
content = Text.assemble(
("📄 ", "blue"),
(f"{len(pdf_files)} PDF file(s) ready", "green"),
(f" ({total_size:.1f} MB total)", "cyan")
)
return Panel(
content,
title="[bold blue]📁 Data Directory[/]",
border_style="blue"
)
def interactive_menu(self) -> None:
"""Display enhanced interactive menu with rich styling."""
console = Console()
while True:
console.clear()
# Main title
title = Text.assemble(
("🤖 ", "bold blue"),
("AI-POWERED OCR TOOL", "bold white"),
(" - Interactive CLI", "bold cyan")
)
title_panel = Panel(
Align.center(title),
border_style="blue",
title="[bold white]⚡ POWERED BY OLMOCR ⚡[/]"
)
console.print(title_panel)
# System status
console.print("\n[bold white]📋 System Status[/]")
status_table = self.create_system_status_table()
console.print(status_table)
# File summary
console.print()
file_panel = self.create_file_summary_panel()
console.print(file_panel)
# Menu options in a beautiful layout
menu_table = Table(show_header=False, box=None, padding=(0, 2))
menu_table.add_column("Option", style="bold cyan", no_wrap=True)
menu_table.add_column("Description", style="white")
menu_table.add_row("1.", "📄 List PDF files")
menu_table.add_row("2.", "🔄 Process PDF files")
menu_table.add_row("3.", "📊 Show results")
menu_table.add_row("4.", "⚙️ Configuration")
menu_table.add_row("5.", "🐳 Docker management")
menu_table.add_row("6.", "📺 Toggle display mode (currently: " + ("LOGS" if self.show_logs_mode else "PROGRESS") + ")")
menu_table.add_row("7.", "❓ Help")
menu_table.add_row("0.", "🚪 Exit")
menu_panel = Panel(
menu_table,
title="[bold green]🎯 Main Menu[/]",
border_style="green"
)
console.print(menu_panel)
# Get user choice with enhanced prompt
choice = console.input("\n[bold yellow]🎲 Select option (0-7): [/]").strip()
if choice == "0":
# Goodbye message with style
goodbye = Panel(
Align.center(Text.assemble(
("👋 ", "yellow"),
("Thanks for using OCR CLI!", "bold green"),
(" See you next time! ", "cyan"),
("🚀", "yellow")
)),
border_style="yellow"
)
console.print(goodbye)
break
elif choice == "1":
self.list_pdfs_menu()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "2":
self.process_pdfs_menu()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "3":
self.show_results()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "4":
self.configuration_menu()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "5":
self.docker_management_menu()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "6":
self.toggle_display_mode()
input("\n[dim]Press Enter to continue...[/]")
elif choice == "7":
self.show_help()
input("\n[dim]Press Enter to continue...[/]")
else:
console.print("[bold red]❌ Invalid option. Please try again.[/]")
time.sleep(1)
def list_pdfs_menu(self) -> None:
"""Display PDF files menu."""
pdf_files = self.list_pdfs()
if not pdf_files:
print(f"\nNo PDF files found in {self.data_dir}")
print("Please place PDF files in the data directory.")
return
print(f"\nPDF files in {self.data_dir}:")
for i, pdf_file in enumerate(pdf_files, 1):
file_size = pdf_file.stat().st_size / (1024 * 1024) # MB
print(f"{i:2}. {pdf_file.name} ({file_size:.1f} MB)")
def process_pdfs_menu(self) -> None:
"""PDF processing menu."""
pdf_files = self.list_pdfs()
if not pdf_files:
print(f"\nNo PDF files found in {self.data_dir}")
return
print(f"\nFound {len(pdf_files)} PDF file(s):")
print("a. Process all files")
for i, pdf_file in enumerate(pdf_files, 1):
print(f"{i}. {pdf_file.name}")
choice = input("\nSelect files to process (a for all, or numbers separated by commas): ").strip()
selected_files = []
if choice.lower() == 'a':
selected_files = [pdf.name for pdf in pdf_files]
else:
try:
indices = [int(x.strip()) - 1 for x in choice.split(',')]
selected_files = [pdf_files[i].name for i in indices if 0 <= i < len(pdf_files)]
except ValueError:
print("Invalid selection")
return
if not selected_files:
print("No files selected")
return
# Output format selection
format_choice = input("\nOutput format (1=markdown, 2=default json): ").strip()
output_format = "markdown" if format_choice == "1" else "json"
self.ensure_directories()
success = self.process_pdfs(selected_files, output_format)
if success:
print("\n✅ Processing completed successfully!")
self.show_results()
else:
print("\n❌ Processing failed")
def configuration_menu(self) -> None:
"""Configuration management menu."""
while True:
print("\n" + "-"*40)
print("⚙️ Configuration Menu")
print("-"*40)
print("Current settings:")
for key, value in self.config.config.items():
print(f" {key}: {value}")
print("\nOptions:")
print("1. Modify setting")
print("2. Reset to defaults")
print("3. Save configuration")
print("0. Back to main menu")
choice = input("\nSelect option: ").strip()
if choice == "0":
break
elif choice == "1":
self.modify_setting()
elif choice == "2":
confirm = input("Reset all settings to defaults? (y/N): ").lower()
if confirm == 'y':
self.config.reset_to_defaults()
print("Configuration reset to defaults")
elif choice == "3":
self.config.save_config()
def modify_setting(self) -> None:
"""Modify a configuration setting."""
print("\nAvailable settings:")
settings = list(self.config.config.keys())
for i, key in enumerate(settings, 1):
print(f"{i}. {key}: {self.config.config[key]}")