File size: 27,355 Bytes
fa02ae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import zipfile
import pandas as pd
from huggingface_hub import hf_hub_download, list_repo_files
from llama_index.core import Document
from llama_index.core.text_splitter import SentenceSplitter
from my_logging import log_message
from config import CHUNK_SIZE, CHUNK_OVERLAP, MAX_CHARS_TABLE, MAX_ROWS_TABLE
import re

def normalize_text(text):
    if not text:
        return text
    
    # Replace Cyrillic 'C' with Latin 'С' (U+0421)
    # This is for welding types like C-25 -> С-25
    text = text.replace('С-', 'C')
    text = re.sub(r'\bС(\d)', r'С\1', text)
    return text

def normalize_steel_designations(text):
    if not text:
        return text, 0, []

    import re
    
    changes_count = 0
    changes_list = []

    # Mapping of Cyrillic to Latin for steel designations
    replacements = {
        'Х': 'X',  
        'Н': 'H', 
        'Т': 'T', 
        'С': 'C', 
        'В': 'B',  
        'К': 'K',  
        'М': 'M', 
        'А': 'A',  
        'Р': 'P', 
    }

    # Pattern: starts with digits, then letters+digits (steel grade pattern)
    # Examples: 08Х18Н10Т, 12Х18Н9, 10Н17Н13М2Т, СВ-08Х19Н10
    pattern = r'\b\d{1,3}(?:[A-ZА-ЯЁ]\d*)+\b'
    
    # Also match welding wire patterns like СВ-08Х19Н10
    pattern_wire = r'\b[СC][ВB]-\d{1,3}(?:[A-ZА-ЯЁ]\d*)+\b'

    def replace_in_steel_grade(match):
        nonlocal changes_count, changes_list
        original = match.group(0)
        converted = ''.join(replacements.get(ch, ch) for ch in original)
        if converted != original:
            changes_count += 1
            changes_list.append(f"{original}{converted}")
        return converted
    normalized_text = re.sub(pattern, replace_in_steel_grade, text)
    normalized_text = re.sub(pattern_wire, replace_in_steel_grade, normalized_text)

    return normalized_text, changes_count, changes_list

def chunk_text_documents(documents):
    text_splitter = SentenceSplitter(
        chunk_size=CHUNK_SIZE,
        chunk_overlap=CHUNK_OVERLAP
    )
    total_normalizations = 0
    chunks_with_changes = 0
    
    chunked = []
    for doc in documents:
        chunks = text_splitter.get_nodes_from_documents([doc])
        for i, chunk in enumerate(chunks):
            original_text = chunk.text
            chunk.text, changes, change_list = normalize_steel_designations(chunk.text)  
            
            if changes > 0:
                chunks_with_changes += 1
                total_normalizations += changes
            
            chunk.metadata.update({
                'chunk_id': i,
                'total_chunks': len(chunks),
                'chunk_size': len(chunk.text)
            })
            chunked.append(chunk)
    
    # Log statistics
    if chunked:
        avg_size = sum(len(c.text) for c in chunked) / len(chunked)
        min_size = min(len(c.text) for c in chunked)
        max_size = max(len(c.text) for c in chunked)
        log_message(f"✓ Text: {len(documents)} docs → {len(chunked)} chunks")
        log_message(f"  Size stats: avg={avg_size:.0f}, min={min_size}, max={max_size} chars")
        log_message(f"  Steel designation normalization:")
        log_message(f"    - Chunks with changes: {chunks_with_changes}/{len(chunked)}")
        log_message(f"    - Total steel grades normalized: {total_normalizations}")
        log_message(f"    - Avg per affected chunk: {total_normalizations/chunks_with_changes:.1f}" if chunks_with_changes > 0 else "    - No normalizations needed")
    
    log_message("="*60)
    
    return chunked

def chunk_table_by_content(table_data, doc_id, max_chars=MAX_CHARS_TABLE, max_rows=MAX_ROWS_TABLE):
    headers = table_data.get('headers', [])
    rows = table_data.get('data', [])
    table_num = table_data.get('table_number', 'unknown')
    table_title = table_data.get('table_title', '')
    section = table_data.get('section', '')
    sheet_name = table_data.get('sheet_name', '')
   
    # Apply steel designation normalization to title and section
    table_title, title_changes, title_list = normalize_steel_designations(str(table_title))
    section, section_changes, section_list = normalize_steel_designations(section)
    
    table_num_clean = str(table_num).strip()
    
    import re
    
    if table_num_clean in ['-', '', 'unknown', 'nan']:
        if 'приложени' in sheet_name.lower() or 'приложени' in section.lower():
            appendix_match = re.search(r'приложени[еия]\s*[№]?\s*(\d+)', 
                                      (sheet_name + ' ' + section).lower())
            if appendix_match:
                appendix_num = appendix_match.group(1)
                table_identifier = f"Приложение {appendix_num}"
            else:
                table_identifier = "Приложение"
        else:
            if table_title:
                first_words = ' '.join(table_title.split()[:5])
                table_identifier = f"{first_words}"
            else:
                table_identifier = section.split(',')[0] if section else "БезНомера"
    else:
        if 'приложени' in section.lower():
            appendix_match = re.search(r'приложени[еия]\s*[№]?\s*(\d+)', section.lower())
            if appendix_match:
                appendix_num = appendix_match.group(1)
                table_identifier = f"{table_num_clean} Приложение {appendix_num}"
            else:
                table_identifier = table_num_clean
        else:
            table_identifier = table_num_clean
    
    if not rows:
        return []
    
    log_message(f"  📊 Processing: {doc_id} - {table_identifier} ({len(rows)} rows)")
    
    # Normalize all row content (including steel designations)
    normalized_rows = []
    total_row_changes = 0
    rows_with_changes = 0
    all_row_changes = []  
    
    for row in rows:
        if isinstance(row, dict):
            normalized_row = {}
            row_had_changes = False
            for k, v in row.items():
                normalized_val, changes, change_list = normalize_steel_designations(str(v))
                normalized_row[k] = normalized_val
                if changes > 0:
                    total_row_changes += changes
                    row_had_changes = True
                    all_row_changes.extend(change_list)  # NEW
            if row_had_changes:
                rows_with_changes += 1
            normalized_rows.append(normalized_row)
        else:
            normalized_rows.append(row)
    
    # Log normalization stats with examples
    if total_row_changes > 0 or title_changes > 0 or section_changes > 0:
        log_message(f"    Steel normalization: title={title_changes}, section={section_changes}, "
                   f"rows={rows_with_changes}/{len(rows)} ({total_row_changes} total)")
        if title_list:
            log_message(f"      Title changes: {', '.join(title_list[:3])}")
        if section_list:
            log_message(f"      Section changes: {', '.join(section_list[:3])}")
        if all_row_changes:
            log_message(f"      Row examples: {', '.join(all_row_changes[:5])}")    
    base_content = format_table_header(doc_id, table_identifier, table_num, 
                                       table_title, section, headers, 
                                       sheet_name)
    base_size = len(base_content)
    available_space = max_chars - base_size - 200 
    
    # If entire table fits, return as one chunk
    full_rows_content = format_table_rows([{**row, '_idx': i+1} 
                                           for i, row in enumerate(normalized_rows)])
    
    if base_size + len(full_rows_content) <= max_chars and len(normalized_rows) <= max_rows:
        content = base_content + full_rows_content + format_table_footer(table_identifier, doc_id)
        
        metadata = {
            'type': 'table',
            'document_id': doc_id,
            'table_number': table_num_clean if table_num_clean not in ['-', 'unknown'] else table_identifier,
            'table_identifier': table_identifier,
            'table_title': table_title,
            'section': section,
            'sheet_name': sheet_name,
            'total_rows': len(normalized_rows),
            'chunk_size': len(content),
            'is_complete_table': True,
            'keywords': f"{doc_id} {table_identifier} {table_title} {section} сталь материал"
        }
        
        log_message(f"    Single chunk: {len(content)} chars, {len(normalized_rows)} rows")
        return [Document(text=content, metadata=metadata)]

    chunks = []
    current_rows = []
    current_size = 0
    chunk_num = 0
    
    for i, row in enumerate(normalized_rows):
        row_text = format_single_row(row, i + 1)
        row_size = len(row_text)
        
        should_split = (current_size + row_size > available_space or 
                       len(current_rows) >= max_rows) and current_rows
        
        if should_split:
            content = base_content + format_table_rows(current_rows)
            content += f"\n\nСтроки {current_rows[0]['_idx']}-{current_rows[-1]['_idx']} из {len(normalized_rows)}\n"
            content += format_table_footer(table_identifier, doc_id)
            
            metadata = {
                'type': 'table',
                'document_id': doc_id,
                'table_number': table_num_clean if table_num_clean not in ['-', 'unknown'] else table_identifier,
                'table_identifier': table_identifier,
                'table_title': table_title,
                'section': section,
                'sheet_name': sheet_name,
                'chunk_id': chunk_num,
                'row_start': current_rows[0]['_idx'] - 1,
                'row_end': current_rows[-1]['_idx'],
                'total_rows': len(normalized_rows),
                'chunk_size': len(content),
                'is_complete_table': False,
                'keywords': f"{doc_id} {table_identifier} {table_title} {section} сталь материал"
            }
            
            chunks.append(Document(text=content, metadata=metadata))
            log_message(f"    Chunk {chunk_num + 1}: {len(content)} chars, {len(current_rows)} rows")
            
            chunk_num += 1
            current_rows = []
            current_size = 0
        
        row_copy = row.copy() if isinstance(row, dict) else {'data': row}
        row_copy['_idx'] = i + 1
        current_rows.append(row_copy)
        current_size += row_size

    if current_rows:
        content = base_content + format_table_rows(current_rows)
        content += f"\n\nСтроки {current_rows[0]['_idx']}-{current_rows[-1]['_idx']} из {len(normalized_rows)}\n"
        content += format_table_footer(table_identifier, doc_id)
        
        metadata = {
            'type': 'table',
            'document_id': doc_id,
            'table_number': table_num_clean if table_num_clean not in ['-', 'unknown'] else table_identifier,
            'table_identifier': table_identifier,
            'table_title': table_title,
            'section': section,
            'sheet_name': sheet_name,
            'chunk_id': chunk_num,
            'row_start': current_rows[0]['_idx'] - 1,
            'row_end': current_rows[-1]['_idx'],
            'total_rows': len(normalized_rows),
            'chunk_size': len(content),
            'is_complete_table': False,
            'keywords': f"{doc_id} {table_identifier} {table_title} {section} сталь материал"
        }
        
        chunks.append(Document(text=content, metadata=metadata))
        log_message(f"    Chunk {chunk_num + 1}: {len(content)} chars, {len(current_rows)} rows")
    
    return chunks

def format_table_header(doc_id, table_identifier, table_num, table_title, section, headers, sheet_name=''):
    content = f"ТАБЛИЦА {normalize_text(table_identifier)} из документа {doc_id}\n"
    
    # Add multiple searchable identifiers
    if table_num and table_num not in ['-', 'unknown']:
        content += f"НОМЕР ТАБЛИЦЫ: {normalize_text(table_num)}\n"
    
    if sheet_name:
        content += f"ЛИСТ: {sheet_name}\n"
    
    if table_title:
        content += f"НАЗВАНИЕ: {normalize_text(table_title)}\n"
    
    if section:
        content += f"РАЗДЕЛ: {section}\n"

    content += f"КЛЮЧЕВЫЕ СЛОВА: материалы стали марки стандарты {doc_id}\n"
    
    content += f"{'='*70}\n"
    
    if headers:
        # Normalize headers too
        normalized_headers = [normalize_text(str(h)) for h in headers]
        header_str = ' | '.join(normalized_headers)
        content += f"ЗАГОЛОВКИ: {header_str}\n\n"
    
    content += "ДАННЫЕ:\n"
    return content

def format_single_row(row, idx):
    if isinstance(row, dict):
        parts = [f"{k}: {v}" for k, v in row.items() 
                if v and str(v).strip() and str(v).lower() not in ['nan', 'none', '']]
        if parts:
            return f"{idx}. {' | '.join(parts)}\n"
    elif isinstance(row, list):
        parts = [str(v) for v in row if v and str(v).strip() and str(v).lower() not in ['nan', 'none', '']]
        if parts:
            return f"{idx}. {' | '.join(parts)}\n"
    return ""

def format_table_rows(rows):
    content = ""
    for row in rows:
        idx = row.get('_idx', 0)
        content += format_single_row(row, idx)
    return content

def format_table_footer(table_identifier, doc_id):
    return f"\n{'='*70}\nКОНЕЦ ТАБЛИЦЫ {table_identifier} ИЗ {doc_id}\n"

def load_json_documents(repo_id, hf_token, json_dir):
    import zipfile
    import tempfile
    import os
    
    log_message("Loading JSON documents...")
    
    files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
    json_files = [f for f in files if f.startswith(json_dir) and f.endswith('.json')]
    zip_files = [f for f in files if f.startswith(json_dir) and f.endswith('.zip')]
    
    log_message(f"Found {len(json_files)} JSON files and {len(zip_files)} ZIP files")
    
    documents = []
    stats = {'success': 0, 'failed': 0, 'empty': 0}
    
    for file_path in json_files:
        try:
            log_message(f"  Loading: {file_path}")
            local_path = hf_hub_download(
                repo_id=repo_id,
                filename=file_path,
                repo_type="dataset",
                token=hf_token
            )
            
            docs = extract_sections_from_json(local_path)
            if docs:
                documents.extend(docs)
                stats['success'] += 1
                log_message(f"    ✓ Extracted {len(docs)} sections")
            else:
                stats['empty'] += 1
                log_message(f"    ⚠ No sections found")
            
        except Exception as e:
            stats['failed'] += 1
            log_message(f"    ✗ Error: {e}")
    
    for zip_path in zip_files:
        try:
            log_message(f"  Processing ZIP: {zip_path}")
            local_zip = hf_hub_download(
                repo_id=repo_id,
                filename=zip_path,
                repo_type="dataset",
                token=hf_token,
                force_download=True
            )
            
            with zipfile.ZipFile(local_zip, 'r') as zf:
                json_files_in_zip = [f for f in zf.namelist() 
                                    if f.endswith('.json') 
                                    and not f.startswith('__MACOSX')
                                    and not f.startswith('.')
                                    and not '._' in f]
                
                log_message(f"    Found {len(json_files_in_zip)} JSON files in ZIP")
                
                for json_file in json_files_in_zip:
                    try:
                        file_content = zf.read(json_file)
                        
                        # Skip if file is too small
                        if len(file_content) < 10:
                            log_message(f"      ✗ Skipping: {json_file} (file too small)")
                            stats['failed'] += 1
                            continue
                        
                        try:
                            text_content = file_content.decode('utf-8')
                        except UnicodeDecodeError:
                            try:
                                text_content = file_content.decode('utf-8-sig')
                            except UnicodeDecodeError:
                                try:
                                    text_content = file_content.decode('utf-16')
                                except UnicodeDecodeError:
                                    try:
                                        text_content = file_content.decode('windows-1251')
                                    except UnicodeDecodeError:
                                        log_message(f"      ✗ Skipping: {json_file} (encoding failed)")
                                        stats['failed'] += 1
                                        continue
                        
                        # Validate JSON structure
                        if not text_content.strip().startswith('{') and not text_content.strip().startswith('['):
                            log_message(f"      ✗ Skipping: {json_file} (not valid JSON)")
                            stats['failed'] += 1
                            continue
                        
                        with tempfile.NamedTemporaryFile(mode='w', delete=False, 
                                                        suffix='.json', encoding='utf-8') as tmp:
                            tmp.write(text_content)
                            tmp_path = tmp.name
                        
                        docs = extract_sections_from_json(tmp_path)
                        if docs:
                            documents.extend(docs)
                            stats['success'] += 1
                            log_message(f"      ✓ {json_file}: {len(docs)} sections")
                        else:
                            stats['empty'] += 1
                            log_message(f"      ⚠ {json_file}: No sections")
                        
                        os.unlink(tmp_path)
                        
                    except json.JSONDecodeError as e:
                        stats['failed'] += 1
                        log_message(f"      ✗ {json_file}: Invalid JSON")
                    except Exception as e:
                        stats['failed'] += 1
                        log_message(f"      ✗ {json_file}: {str(e)[:100]}")
                        
        except Exception as e:
            log_message(f"    ✗ Error with ZIP: {e}")
    
    log_message(f"="*60)
    log_message(f"JSON Loading Stats:")
    log_message(f"  Success: {stats['success']}")
    log_message(f"  Empty: {stats['empty']}")
    log_message(f"  Failed: {stats['failed']}")
    log_message(f"="*60)
    
    return documents

def extract_sections_from_json(json_path):
    documents = []
    
    try:
        with open(json_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
        
        doc_id = data.get('document_metadata', {}).get('document_id', 'unknown')
        
        # Extract all section levels
        for section in data.get('sections', []):
            if section.get('section_text', '').strip():
                documents.append(Document(
                    text=section['section_text'],
                    metadata={
                        'type': 'text',
                        'document_id': doc_id,
                        'section_id': section.get('section_id', '')
                    }
                ))
            
            # Subsections
            for subsection in section.get('subsections', []):
                if subsection.get('subsection_text', '').strip():
                    documents.append(Document(
                        text=subsection['subsection_text'],
                        metadata={
                            'type': 'text',
                            'document_id': doc_id,
                            'section_id': subsection.get('subsection_id', '')
                        }
                    ))
                
                # Sub-subsections
                for sub_sub in subsection.get('sub_subsections', []):
                    if sub_sub.get('sub_subsection_text', '').strip():
                        documents.append(Document(
                            text=sub_sub['sub_subsection_text'],
                            metadata={
                                'type': 'text',
                                'document_id': doc_id,
                                'section_id': sub_sub.get('sub_subsection_id', '')
                            }
                        ))
    
    except Exception as e:
        log_message(f"Error extracting from {json_path}: {e}")
    
    return documents

def load_table_documents(repo_id, hf_token, table_dir):
    log_message("Loading tables...")
    log_message("="*60)    
    files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
    table_files = [f for f in files if f.startswith(table_dir) and (f.endswith('.json') or f.endswith('.xlsx') or f.endswith('.xls'))]
    
    all_chunks = []
    tables_processed = 0
    
    for file_path in table_files:
        try:
            local_path = hf_hub_download(
                repo_id=repo_id,
                filename=file_path,
                repo_type="dataset",
                token=hf_token
            )
            
            # Convert Excel to JSON if needed
            if file_path.endswith(('.xlsx', '.xls')):
                from converters.converter import convert_single_excel_to_json
                import tempfile
                import os
                
                with tempfile.TemporaryDirectory() as temp_dir:
                    json_path = convert_single_excel_to_json(local_path, temp_dir)
                    local_path = json_path
            
            with open(local_path, 'r', encoding='utf-8') as f:
                data = json.load(f)
            
            file_doc_id = data.get('document_id', data.get('document', 'unknown'))
            
            for sheet in data.get('sheets', []):
                sheet_doc_id = sheet.get('document_id', sheet.get('document', file_doc_id))
                tables_processed += 1
                
                chunks = chunk_table_by_content(sheet, sheet_doc_id, 
                                               max_chars=MAX_CHARS_TABLE, 
                                               max_rows=MAX_ROWS_TABLE)
                all_chunks.extend(chunks)
                
        except Exception as e:
            log_message(f"Error loading {file_path}: {e}")
    
    log_message(f"✓ Loaded {len(all_chunks)} table chunks from {tables_processed} tables")
    log_message("="*60)
    
    return all_chunks


def load_image_documents(repo_id, hf_token, image_dir):
    log_message("Loading images...")
    
    files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
    csv_files = [f for f in files if f.startswith(image_dir) and (f.endswith('.csv') or f.endswith('.xlsx') or f.endswith('.xls'))]
    
    documents = []
    for file_path in csv_files:
        try:
            local_path = hf_hub_download(
                repo_id=repo_id,
                filename=file_path,
                repo_type="dataset",
                token=hf_token
            )
            
            # Convert Excel to CSV if needed
            if file_path.endswith(('.xlsx', '.xls')):
                from converters.converter import convert_single_excel_to_csv
                import tempfile
                import os
                
                with tempfile.TemporaryDirectory() as temp_dir:
                    csv_path = convert_single_excel_to_csv(local_path, temp_dir)
                    local_path = csv_path
            
            df = pd.read_csv(local_path)
            
            for _, row in df.iterrows():
                content = f"Документ: {row.get('Обозначение документа', 'unknown')}\n"
                content += f"Рисунок: {row.get('№ Изображения', 'unknown')}\n"
                content += f"Название: {row.get('Название изображения', '')}\n"
                content += f"Описание: {row.get('Описание изображение', '')}\n"
                content += f"Раздел: {row.get('Раздел документа', '')}\n"
                
                chunk_size = len(content)
                
                documents.append(Document(
                    text=content,
                    metadata={
                        'type': 'image',
                        'document_id': str(row.get('Обозначение документа', 'unknown')),
                        'image_number': str(row.get('№ Изображения', 'unknown')),
                        'section': str(row.get('Раздел документа', '')),
                        'chunk_size': chunk_size
                    }
                ))
        except Exception as e:
            log_message(f"Error loading {file_path}: {e}")
    
    if documents:
        avg_size = sum(d.metadata['chunk_size'] for d in documents) / len(documents)
        log_message(f"✓ Loaded {len(documents)} images (avg size: {avg_size:.0f} chars)")
    
    return documents

def load_all_documents(repo_id, hf_token, json_dir, table_dir, image_dir):
    """Main loader - combines all document types"""
    log_message("="*60)
    log_message("STARTING DOCUMENT LOADING")
    log_message("="*60)
    
    # Load text sections
    text_docs = load_json_documents(repo_id, hf_token, json_dir)
    text_chunks = chunk_text_documents(text_docs)
    
    # Load tables (already chunked)
    table_chunks = load_table_documents(repo_id, hf_token, table_dir)
    
    # Load images (no chunking needed)
    image_docs = load_image_documents(repo_id, hf_token, image_dir)
    
    all_docs = text_chunks + table_chunks + image_docs
    
    log_message("="*60)
    log_message(f"TOTAL DOCUMENTS: {len(all_docs)}")
    log_message(f"  Text chunks: {len(text_chunks)}")
    log_message(f"  Table chunks: {len(table_chunks)}")
    log_message(f"  Images: {len(image_docs)}")
    log_message("="*60)
    
    return all_docs