File size: 8,730 Bytes
eb53bb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Setup script for the Document Text Extraction system.

Creates directories, checks dependencies, and initializes the project.

"""

import os
import sys
import subprocess
from pathlib import Path
import importlib.util


def check_python_version():
    """Check if Python version is compatible."""
    if sys.version_info < (3, 8):
        print("Python 3.8 or higher is required.")
        print(f"Current version: {sys.version}")
        return False
    
    print(f"Python {sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}")
    return True


def create_directories():
    """Create necessary project directories."""
    directories = [
        "data/raw",
        "data/processed", 
        "models",
        "results/plots",
        "results/metrics",
        "logs"
    ]
    
    print("\n📁 Creating project directories...")
    for directory in directories:
        Path(directory).mkdir(parents=True, exist_ok=True)
        print(f"   {directory}")


def check_dependencies():
    """Check if required dependencies are installed."""
    print("\n📦 Checking dependencies...")
    
    required_packages = [
        ('torch', 'PyTorch'),
        ('transformers', 'Transformers'),
        ('PIL', 'Pillow'),
        ('cv2', 'OpenCV'),
        ('pandas', 'Pandas'),
        ('numpy', 'NumPy'),
        ('sklearn', 'Scikit-learn')
    ]
    
    missing_packages = []
    
    for package, name in required_packages:
        spec = importlib.util.find_spec(package)
        if spec is None:
            missing_packages.append(name)
            print(f"   {name} not found")
        else:
            print(f"   {name}")
    
    return missing_packages


def check_ocr_dependencies():
    """Check OCR-related dependencies."""
    print("\nChecking OCR dependencies...")
    
    # Check EasyOCR
    try:
        import easyocr
        print("   EasyOCR")
    except ImportError:
        print("   EasyOCR not found")
    
    # Check Tesseract
    try:
        import pytesseract
        print("   PyTesseract")
        
        # Try to run tesseract
        try:
            pytesseract.get_tesseract_version()
            print("   Tesseract OCR engine")
        except Exception:
            print("   Tesseract OCR engine not found or not in PATH")
            print("      Please install Tesseract OCR:")
            print("      - Windows: https://github.com/UB-Mannheim/tesseract/wiki")
            print("      - Ubuntu: sudo apt install tesseract-ocr")
            print("      - macOS: brew install tesseract")
            
    except ImportError:
        print("   PyTesseract not found")


def install_dependencies():
    """Install missing dependencies."""
    print("\nInstalling dependencies from requirements.txt...")
    
    try:
        result = subprocess.run([
            sys.executable, "-m", "pip", "install", "-r", "requirements.txt"
        ], capture_output=True, text=True, check=True)
        
        print("   Dependencies installed successfully")
        return True
        
    except subprocess.CalledProcessError as e:
        print(f"   Failed to install dependencies: {e}")
        print(f"   Output: {e.stdout}")
        print(f"   Error: {e.stderr}")
        return False


def check_gpu_support():
    """Check if GPU support is available."""
    print("\n🖥️  Checking GPU support...")
    
    try:
        import torch
        if torch.cuda.is_available():
            gpu_count = torch.cuda.device_count()
            gpu_name = torch.cuda.get_device_name(0)
            print(f"   CUDA available - {gpu_count} GPU(s)")
            print(f"   Primary GPU: {gpu_name}")
        else:
            print("   CUDA not available - will use CPU")
    except ImportError:
        print("   PyTorch not installed")


def create_sample_documents():
    """Create sample documents for testing."""
    print("\nCreating sample test documents...")
    
    sample_texts = [
        "Invoice sent to John Doe on 01/15/2025\nInvoice No: INV-1001\nAmount: $1,500.00\nPhone: (555) 123-4567",
        "Bill for Dr. Sarah Johnson dated March 10, 2025.\nInvoice Number: BL-2045.\nTotal: $2,300.50\nEmail: sarah@email.com",
        "Receipt for Michael Brown\n456 Oak Street, Boston MA 02101\nInvoice: REC-3089\nDate: 2025-04-22\nAmount: $890.75"
    ]
    
    sample_dir = Path("data/raw/samples")
    sample_dir.mkdir(parents=True, exist_ok=True)
    
    for i, text in enumerate(sample_texts, 1):
        sample_file = sample_dir / f"sample_document_{i}.txt"
        with open(sample_file, 'w', encoding='utf-8') as f:
            f.write(text)
        print(f"   {sample_file.name}")


def run_initial_test():
    """Run a basic test to verify setup."""
    print("\nRunning initial setup test...")
    
    try:
        # Test imports
        from src.data_preparation import DocumentProcessor, NERDatasetCreator
        from src.model import ModelConfig
        print("   Core modules imported successfully")
        
        # Test document processor
        processor = DocumentProcessor()
        test_text = "Invoice sent to John Doe on 01/15/2025 Amount: $500.00"
        cleaned_text = processor.clean_text(test_text)
        print("   Document processor working")
        
        # Test dataset creator
        dataset_creator = NERDatasetCreator(processor)
        sample_dataset = dataset_creator.create_sample_dataset()
        print(f"   Dataset creator working - {len(sample_dataset)} samples")
        
        # Test model config
        config = ModelConfig()
        print(f"   Model config created - {config.num_labels} labels")
        
        return True
        
    except Exception as e:
        print(f"   Setup test failed: {e}")
        return False


def display_next_steps():
    """Display next steps for the user."""
    print("\n" + "=" * 30)
    print("SETUP COMPLETED SUCCESSFULLY!")
    print("=" * 30)
    
    print("\nNext Steps:")
    print("1. Quick Demo:")
    print("   python demo.py")
    
    print("\n2. Train Your Model:")
    print("   # Add your documents to data/raw/")
    print("   # Then run:")
    print("   python src/training_pipeline.py")
    
    print("\n3. 🌐 Start Web API:")
    print("   python api/app.py")
    print("   # Then open: http://localhost:8000")
    
    print("\n4. Run Tests:")
    print("   python tests/test_extraction.py")
    
    print("\n5. 📚 Documentation:")
    print("   # View README.md for detailed usage")
    print("   # API docs: http://localhost:8000/docs")
    
    print("\nPro Tips:")
    print("   - Place your documents in data/raw/ for training")
    print("   - Use GPU for faster training (if available)")
    print("   - Adjust batch_size in config if you get memory errors")
    print("   - Check logs/ directory for debugging information")


def main():
    """Main setup function."""
    print("DOCUMENT TEXT EXTRACTION - SETUP SCRIPT")
    print("=" * 60)
    
    # Check Python version
    if not check_python_version():
        return False
    
    # Create directories
    create_directories()
    
    # Check and install dependencies
    missing_packages = check_dependencies()
    if missing_packages:
        print(f"\nMissing packages: {', '.join(missing_packages)}")
        install_deps = input("Install missing dependencies? (y/n): ").lower().strip()
        
        if install_deps == 'y':
            if not install_dependencies():
                print("Failed to install dependencies. Please install manually:")
                print("   pip install -r requirements.txt")
                return False
        else:
            print("Some features may not work without required dependencies.")
    
    # Check OCR dependencies
    check_ocr_dependencies()
    
    # Check GPU support
    check_gpu_support()
    
    # Create sample documents
    create_sample_documents()
    
    # Run initial test
    if not run_initial_test():
        print("Setup test failed. Some features may not work correctly.")
        print("   Check error messages above and ensure all dependencies are installed.")
    
    # Display next steps
    display_next_steps()
    
    return True


if __name__ == "__main__":
    success = main()
    
    if success:
        print(f"\nSetup completed! Ready to extract text from documents!")
    else:
        print(f"\nSetup encountered issues. Please check the messages above.")
        sys.exit(1)