File size: 10,418 Bytes
12f0afd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
BM25 Sparse Index Build Script

This script generates BM25 sparse indices for all VDR data sources.
These indexes complement the existing FAISS dense indices for hybrid retrieval.

Run this script locally before pushing to repo to ensure sparse indices are up-to-date.
The generated indexes will be committed to the repo and loaded on Streamlit Cloud.
"""

import sys
import time
import argparse
from pathlib import Path
from typing import List, Dict, Any

# Progress indicators
from tqdm import tqdm

# Colors for output
RED = '\033[0;31m'
GREEN = '\033[0;32m'
YELLOW = '\033[1;33m'
BLUE = '\033[0;34m'
NC = '\033[0m'  # No Color

# Add app to path for imports
sys.path.insert(0, str(Path(__file__).parent / 'app'))

from app.core.config import get_config
from app.core.logging import setup_logging
from app.core.document_processor import DocumentProcessor
from app.core.sparse_index import BM25Index, build_sparse_index_for_store

# Set up logging
logger = setup_logging("build_sparse", log_level="INFO")


def parse_arguments():
    """Parse command line arguments"""
    parser = argparse.ArgumentParser(
        description='Build BM25 sparse indices for document search',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  python scripts/build_sparse_indexes.py                    # Build all sparse indexes
  python scripts/build_sparse_indexes.py --store summit    # Build specific store
  python scripts/build_sparse_indexes.py --clean           # Force rebuild
  python scripts/build_sparse_indexes.py --status          # Show build status
        """
    )

    parser.add_argument(
        '--store', '-s',
        help='Build index for specific store name only'
    )

    parser.add_argument(
        '--clean', '-c',
        action='store_true',
        help='Force clean rebuild of all indexes'
    )

    parser.add_argument(
        '--status',
        action='store_true',
        help='Show current build status and exit'
    )

    parser.add_argument(
        '--verbose', '-v',
        action='store_true',
        help='Enable verbose output'
    )

    return parser.parse_args()


def print_header():
    """Print the script header with colors"""
    print(f"{BLUE}πŸ” BM25 Sparse Index Build Script{NC}")
    print(f"{BLUE}{'='*60}{NC}")
    print("")


def get_vdr_directories(vdrs_path: Path) -> List[tuple]:
    """Get all VDR directories and their company names"""
    vdr_dirs = []

    if not vdrs_path.exists():
        logger.warning(f"VDRs directory not found: {vdrs_path}")
        return vdr_dirs

    for project_dir in vdrs_path.iterdir():
        if project_dir.is_dir():
            # Look for company directories within each project
            for item in project_dir.iterdir():
                if item.is_dir() and item.name != '__pycache__':
                    # Check if it's a company directory (has documents)
                    has_docs = any(
                        f.suffix.lower() in ['.pdf', '.docx', '.doc', '.txt', '.md']
                        for f in item.rglob('*') if f.is_file()
                    )
                    if has_docs:
                        # Use company directory name as store name
                        store_name = item.name.replace(' ', '-').lower()
                        vdr_dirs.append((item, store_name, project_dir.name))
                        logger.info(f"Found VDR: {item} -> {store_name}")

    return vdr_dirs


def build_sparse_index_for_vdr(vdr_path: Path, store_name: str, project_name: str,
                              clean: bool = False) -> Dict[str, Any]:
    """Build BM25 sparse index for a single VDR"""

    start_time = time.time()
    logger.info(f"Building sparse index for {store_name}")

    try:
        # Check if index already exists and we're not forcing clean rebuild
        index_path = f"data/search_indexes/{store_name}_bm25.pkl"
        if not clean and Path(index_path).exists():
            logger.info(f"βœ… Sparse index already exists for {store_name} (use --clean to rebuild)")
            return {
                'success': True,
                'store_name': store_name,
                'skipped': True,
                'processing_time': 0
            }

        # Load documents using existing processor
        processor = DocumentProcessor(store_name=store_name)
        result = processor.load_data_room(str(vdr_path))

        if result['chunks_count'] == 0:
            logger.warning(f"No chunks found for {store_name}")
            return {
                'success': False,
                'store_name': store_name,
                'error': 'No chunks found'
            }

        # Prepare documents for BM25
        documents = []
        for i, chunk in enumerate(processor.chunks):
            documents.append({
                'id': f"{store_name}_chunk_{i}",
                'content': chunk['text']
            })

        # Build sparse index
        bm25_index = build_sparse_index_for_store(store_name, documents)

        processing_time = time.time() - start_time

        logger.info(f"βœ… Built BM25 index for {store_name}: {len(documents)} chunks in {processing_time:.2f}s")

        return {
            'success': True,
            'store_name': store_name,
            'documents_count': len(documents),
            'chunks_count': result['chunks_count'],
            'processing_time': processing_time
        }

    except Exception as e:
        processing_time = time.time() - start_time
        logger.error(f"❌ Failed to build sparse index for {store_name}: {e}")
        return {
            'success': False,
            'store_name': store_name,
            'error': str(e),
            'processing_time': processing_time
        }


def show_build_status():
    """Show current sparse index build status"""
    print(f"\n{BLUE}πŸ“Š Sparse Index Build Status{NC}")
    print(f"{BLUE}{'='*50}{NC}")

    config = get_config()
    faiss_dir = config.paths['faiss_dir']

    if not faiss_dir.exists():
        print(f"{YELLOW}No index directory found: {faiss_dir}{NC}")
        return

    # Count existing sparse indexes
    sparse_indexes = list(faiss_dir.glob("*_bm25.pkl"))
    faiss_indexes = list(faiss_dir.glob("*.faiss"))

    print(f"Sparse indexes: {len(sparse_indexes)}")
    print(f"FAISS indexes: {len(faiss_indexes)}")

    if sparse_indexes:
        print(f"\n{GREEN}Existing Sparse Indexes:{NC}")
        total_size = 0
        for index_file in sparse_indexes:
            size_mb = index_file.stat().st_size / (1024 * 1024)
            total_size += size_mb
            store_name = index_file.stem.replace('_bm25', '')
            print(f"  β€’ {store_name}: {size_mb:.2f} MB")

        print(f"\n{GREEN}Total sparse index size: {total_size:.2f} MB{NC}")

    # Check for missing sparse indexes
    vdr_dirs = get_vdr_directories(config.paths['vdrs_dir'])
    vdr_store_names = {store_name for _, store_name, _ in vdr_dirs}

    existing_sparse = {idx.stem.replace('_bm25', '') for idx in sparse_indexes}
    missing = vdr_store_names - existing_sparse

    if missing:
        print(f"\n{YELLOW}Missing sparse indexes for:{NC}")
        for store_name in missing:
            print(f"  β€’ {store_name}")

    print(f"\n{BLUE}{'='*50}{NC}")


def main():
    """Main build script execution"""
    args = parse_arguments()

    print_header()

    if args.verbose:
        import logging
        logging.getLogger().setLevel(logging.DEBUG)

    if args.status:
        show_build_status()
        return

    config = get_config()

    # Get VDR directories
    vdr_dirs = get_vdr_directories(config.paths['vdrs_dir'])

    if not vdr_dirs:
        print(f"{YELLOW}No VDR directories found{NC}")
        return

    print(f"{BLUE}Found {len(vdr_dirs)} VDR data rooms{NC}")

    # Filter for specific store if requested
    if args.store:
        vdr_dirs = [(path, name, proj) for path, name, proj in vdr_dirs
                   if args.store.lower() in name.lower()]
        if not vdr_dirs:
            print(f"{RED}No VDR found matching '{args.store}'{NC}")
            return

    print(f"{GREEN}πŸš€ Building sparse indexes for {len(vdr_dirs)} data rooms...{NC}")
    print("")

    start_time = time.time()
    results = []

    with tqdm(total=len(vdr_dirs), desc="Building sparse indexes",
              unit="index", leave=False) as pbar:

        for vdr_path, store_name, project_name in vdr_dirs:
            pbar.set_description(f"Building {store_name}")

            result = build_sparse_index_for_vdr(vdr_path, store_name, project_name, args.clean)
            results.append(result)

            pbar.update(1)

    # Generate report
    successful = [r for r in results if r.get('success', False)]
    failed = [r for r in results if not r.get('success', False)]
    skipped = [r for r in results if r.get('skipped', False)]

    total_time = time.time() - start_time

    print(f"\n{BLUE}{'='*60}{NC}")
    print(f"{BLUE}SPARSE INDEX BUILD SUMMARY{NC}")
    print(f"{BLUE}{'='*60}{NC}")
    print(f"Total time: {total_time:.1f}s")
    print(f"Successful: {len(successful)}")
    print(f"Failed: {len(failed)}")
    print(f"Skipped: {len(skipped)}")

    if successful:
        print(f"\n{GREEN}βœ… SUCCESSFUL BUILDS:{NC}")
        total_docs = sum(r.get('documents_count', 0) for r in successful)
        print(f"Total documents indexed: {total_docs}")

        # Calculate total size
        faiss_dir = config.paths['faiss_dir']
        if faiss_dir.exists():
            sparse_files = list(faiss_dir.glob("*_bm25.pkl"))
            total_size = sum(f.stat().st_size for f in sparse_files)
            print(f"Total sparse index size: {total_size / (1024*1024):.2f} MB")

    if failed:
        print(f"\n{RED}❌ FAILED BUILDS:{NC}")
        for result in failed:
            print(f"  β€’ {result['store_name']}: {result.get('error', 'Unknown error')}")

    if successful:
        print(f"\n{GREEN}πŸŽ‰ Sparse indexes ready for commit!{NC}")
        print("")
        print("Next steps:")
        print("  git add data/search_indexes/*_bm25.pkl")
        print("  git commit -m 'Add BM25 sparse indexes for hybrid retrieval'")
        print("  git push")
        print("")
        print("The indexes will be automatically loaded on Streamlit Cloud.")

    print(f"\n{BLUE}{'='*60}{NC}")


if __name__ == "__main__":
    main()