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
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
import json
import pandas as pd
import os
import zipfile
from huggingface_hub import hf_hub_download, list_repo_files
from llama_index.core import Document
import logging

logger = logging.getLogger(__name__)

def log_message(message):
    logger.info(message)
    print(message, flush=True)

class DocumentsPreparation:
    def __init__(self, repo_id, hf_token):
        self.repo_id = repo_id
        self.hf_token = hf_token
        self.json_files_dir = "JSON"
        self.table_data_dir = "Табличные данные_JSON"
        self.image_data_dir = "Изображения"
        self.download_dir = "rag_files"
        
    def extract_text_from_json(self, data, document_id, document_name):
        documents = []
        
        if 'sections' in data:
            for section in data['sections']:
                section_id = section.get('section_id', 'Unknown')
                section_text = section.get('section_text', '')
                
                if section_text.strip():
                    doc = Document(
                        text=section_text,
                        metadata={
                            "type": "text",
                            "document_id": document_id,
                            "document_name": document_name,
                            "section_id": section_id,
                            "level": "section"
                        }
                    )
                    documents.append(doc)
                
                if 'subsections' in section:
                    for subsection in section['subsections']:
                        subsection_id = subsection.get('subsection_id', 'Unknown')
                        subsection_text = subsection.get('subsection_text', '')
                        
                        if subsection_text.strip():
                            doc = Document(
                                text=subsection_text,
                                metadata={
                                    "type": "text",
                                    "document_id": document_id,
                                    "document_name": document_name,
                                    "section_id": section_id,
                                    "subsection_id": subsection_id,
                                    "level": "subsection"
                                }
                            )
                            documents.append(doc)
                        
                        if 'sub_subsections' in subsection:
                            for sub_subsection in subsection['sub_subsections']:
                                sub_subsection_id = sub_subsection.get('sub_subsection_id', 'Unknown')
                                sub_subsection_text = sub_subsection.get('sub_subsection_text', '')
                                
                                if sub_subsection_text.strip():
                                    doc = Document(
                                        text=sub_subsection_text,
                                        metadata={
                                            "type": "text",
                                            "document_id": document_id,
                                            "document_name": document_name,
                                            "section_id": section_id,
                                            "subsection_id": subsection_id,
                                            "sub_subsection_id": sub_subsection_id,
                                            "level": "sub_subsection"
                                        }
                                    )
                                    documents.append(doc)
                                
                                if 'sub_sub_subsections' in sub_subsection:
                                    for sub_sub_subsection in sub_subsection['sub_sub_subsections']:
                                        sub_sub_subsection_id = sub_sub_subsection.get('sub_sub_subsection_id', 'Unknown')
                                        sub_sub_subsection_text = sub_sub_subsection.get('sub_sub_subsection_text', '')
                                        
                                        if sub_sub_subsection_text.strip():
                                            doc = Document(
                                                text=sub_sub_subsection_text,
                                                metadata={
                                                    "type": "text",
                                                    "document_id": document_id,
                                                    "document_name": document_name,
                                                    "section_id": section_id,
                                                    "subsection_id": subsection_id,
                                                    "sub_subsection_id": sub_subsection_id,
                                                    "sub_sub_subsection_id": sub_sub_subsection_id,
                                                    "level": "sub_sub_subsection"
                                                }
                                            )
                                            documents.append(doc)
        
        return documents

    def extract_zip_and_process_json(self, zip_path):
        """Extract ZIP file and process JSON files inside"""
        documents = []
        
        try:
            with zipfile.ZipFile(zip_path, 'r') as zip_ref:
                # Get list of files in ZIP
                zip_files = zip_ref.namelist()
                json_files = [f for f in zip_files if f.endswith('.json') and not f.startswith('__MACOSX')]
                
                log_message(f"Найдено {len(json_files)} JSON файлов в архиве")
                
                for json_file in json_files:
                    try:
                        log_message(f"Обрабатываю файл из архива: {json_file}")
                        
                        # Read JSON file from ZIP
                        with zip_ref.open(json_file) as f:
                            json_data = json.load(f)
                        
                        document_metadata = json_data.get('document_metadata', {})
                        document_id = document_metadata.get('document_id', 'unknown')
                        document_name = document_metadata.get('document_name', 'unknown')
                        
                        docs = self.extract_text_from_json(json_data, document_id, document_name)
                        documents.extend(docs)
                        
                        log_message(f"Извлечено {len(docs)} документов из {json_file}")
                        
                    except Exception as e:
                        log_message(f"Ошибка обработки файла {json_file}: {str(e)}")
                        continue
        
        except Exception as e:
            log_message(f"Ошибка извлечения ZIP архива {zip_path}: {str(e)}")
        
        return documents

    def load_json_documents(self):
        log_message("Начинаю загрузку JSON документов")
        
        try:
            files = list_repo_files(repo_id=self.repo_id, repo_type="dataset", token=self.hf_token)
            
            # Look for ZIP files in the JSON directory
            zip_files = [f for f in files if f.startswith(self.json_files_dir) and f.endswith('.zip')]
            # Also look for direct JSON files (fallback)
            json_files = [f for f in files if f.startswith(self.json_files_dir) and f.endswith('.json')]
            
            log_message(f"Найдено {len(zip_files)} ZIP файлов и {len(json_files)} прямых JSON файлов")
            
            all_documents = []
            
            # Process ZIP files first
            for zip_file_path in zip_files:
                try:
                    log_message(f"Загружаю ZIP архив: {zip_file_path}")
                    local_zip_path = hf_hub_download(
                        repo_id=self.repo_id,
                        filename=zip_file_path,
                        local_dir=self.download_dir,
                        repo_type="dataset",
                        token=self.hf_token
                    )
                    
                    documents = self.extract_zip_and_process_json(local_zip_path)
                    all_documents.extend(documents)
                    
                except Exception as e:
                    log_message(f"Ошибка обработки ZIP файла {zip_file_path}: {str(e)}")
                    continue
            
            # Process direct JSON files (if any)
            for file_path in json_files:
                try:
                    log_message(f"Обрабатываю прямой JSON файл: {file_path}")
                    local_path = hf_hub_download(
                        repo_id=self.repo_id,
                        filename=file_path,
                        local_dir=self.download_dir,
                        repo_type="dataset",
                        token=self.hf_token
                    )
                    
                    with open(local_path, 'r', encoding='utf-8') as f:
                        json_data = json.load(f)
                    
                    document_metadata = json_data.get('document_metadata', {})
                    document_id = document_metadata.get('document_id', 'unknown')
                    document_name = document_metadata.get('document_name', 'unknown')
                    
                    documents = self.extract_text_from_json(json_data, document_id, document_name)
                    all_documents.extend(documents)
                    
                    log_message(f"Извлечено {len(documents)} документов из {file_path}")
                    
                except Exception as e:
                    log_message(f"Ошибка обработки файла {file_path}: {str(e)}")
                    continue
            
            log_message(f"Всего создано {len(all_documents)} текстовых документов")
            return all_documents
            
        except Exception as e:
            log_message(f"Ошибка загрузки JSON документов: {str(e)}")
            return []

    def table_to_document(self, table_data, document_id=None):
        content = ""
        if isinstance(table_data, dict):
            doc_id = document_id or table_data.get('document_id', table_data.get('document', 'Неизвестно'))
            
            table_num = table_data.get('table_number', 'Неизвестно')
            table_title = table_data.get('table_title', 'Неизвестно')
            section = table_data.get('section', 'Неизвестно')
            
            content += f"Таблица: {table_num}\n"
            content += f"Название: {table_title}\n"
            content += f"Документ: {doc_id}\n"
            content += f"Раздел: {section}\n"
            
            if 'data' in table_data and isinstance(table_data['data'], list):
                for row in table_data['data']:
                    if isinstance(row, dict):
                        row_text = " | ".join([f"{k}: {v}" for k, v in row.items()])
                        content += f"{row_text}\n"
        
        return Document(
            text=content,
            metadata={
                "type": "table",
                "table_number": table_data.get('table_number', 'unknown'),
                "table_title": table_data.get('table_title', 'unknown'),
                "document_id": doc_id or table_data.get('document_id', table_data.get('document', 'unknown')),
                "section": table_data.get('section', 'unknown')
            }
        )

    def extract_zip_and_process_tables(self, zip_path):
        """Extract ZIP file and process table JSON files inside"""
        documents = []
        
        try:
            with zipfile.ZipFile(zip_path, 'r') as zip_ref:
                # Get list of files in ZIP
                zip_files = zip_ref.namelist()
                json_files = [f for f in zip_files if f.endswith('.json') and not f.startswith('__MACOSX')]
                
                log_message(f"Найдено {len(json_files)} JSON файлов таблиц в архиве")
                
                for json_file in json_files:
                    try:
                        log_message(f"Обрабатываю файл таблицы из архива: {json_file}")
                        
                        # Read JSON file from ZIP
                        with zip_ref.open(json_file) as f:
                            table_data = json.load(f)
                        
                        if isinstance(table_data, dict):
                            document_id = table_data.get('document', 'unknown')
                            
                            if 'sheets' in table_data:
                                for sheet in table_data['sheets']:
                                    sheet['document'] = document_id
                                    doc = self.table_to_document(sheet, document_id)
                                    documents.append(doc)
                            else:
                                doc = self.table_to_document(table_data, document_id)
                                documents.append(doc)
                        elif isinstance(table_data, list):
                            for table_json in table_data:
                                doc = self.table_to_document(table_json)
                                documents.append(doc)
                        
                    except Exception as e:
                        log_message(f"Ошибка обработки файла таблицы {json_file}: {str(e)}")
                        continue
        
        except Exception as e:
            log_message(f"Ошибка извлечения ZIP архива таблиц {zip_path}: {str(e)}")
        
        return documents

    def load_table_documents(self):
        log_message("Начинаю загрузку табличных данных")
        
        try:
            files = list_repo_files(repo_id=self.repo_id, repo_type="dataset", token=self.hf_token)
            
            # Look for ZIP files in the table directory
            zip_files = [f for f in files if f.startswith(self.table_data_dir) and f.endswith('.zip')]
            # Also look for direct JSON files (fallback)
            table_files = [f for f in files if f.startswith(self.table_data_dir) and f.endswith('.json')]
            
            log_message(f"Найдено {len(zip_files)} ZIP файлов с таблицами и {len(table_files)} прямых JSON файлов")
            
            table_documents = []
            
            # Process ZIP files first
            for zip_file_path in zip_files:
                try:
                    log_message(f"Загружаю ZIP архив таблиц: {zip_file_path}")
                    local_zip_path = hf_hub_download(
                        repo_id=self.repo_id,
                        filename=zip_file_path,
                        local_dir=self.download_dir,
                        repo_type="dataset",
                        token=self.hf_token
                    )
                    
                    documents = self.extract_zip_and_process_tables(local_zip_path)
                    table_documents.extend(documents)
                    
                except Exception as e:
                    log_message(f"Ошибка обработки ZIP файла таблиц {zip_file_path}: {str(e)}")
                    continue
            
            # Process direct JSON files (if any)
            for file_path in table_files:
                try:
                    log_message(f"Обрабатываю прямой файл таблицы: {file_path}")
                    local_path = hf_hub_download(
                        repo_id=self.repo_id,
                        filename=file_path,
                        local_dir=self.download_dir,
                        repo_type="dataset",
                        token=self.hf_token
                    )
                    
                    with open(local_path, 'r', encoding='utf-8') as f:
                        table_data = json.load(f)
                        
                        if isinstance(table_data, dict):
                            document_id = table_data.get('document', 'unknown')
                            
                            if 'sheets' in table_data:
                                for sheet in table_data['sheets']:
                                    sheet['document'] = document_id
                                    doc = self.table_to_document(sheet, document_id)
                                    table_documents.append(doc)
                            else:
                                doc = self.table_to_document(table_data, document_id)
                                table_documents.append(doc)
                        elif isinstance(table_data, list):
                            for table_json in table_data:
                                doc = self.table_to_document(table_json)
                                table_documents.append(doc)
                            
                except Exception as e:
                    log_message(f"Ошибка обработки файла {file_path}: {str(e)}")
                    continue
            
            log_message(f"Создано {len(table_documents)} документов из таблиц")
            return table_documents
            
        except Exception as e:
            log_message(f"Ошибка загрузки табличных данных: {str(e)}")
            return []

    def load_image_documents(self):
        log_message("Начинаю загрузку данных изображений")
        
        try:
            files = list_repo_files(repo_id=self.repo_id, repo_type="dataset", token=self.hf_token)
            image_files = [f for f in files if f.startswith(self.image_data_dir) and f.endswith('.csv')]
            
            log_message(f"Найдено {len(image_files)} CSV файлов с изображениями")
            
            image_documents = []
            for file_path in image_files:
                try:
                    log_message(f"Обрабатываю файл изображений: {file_path}")
                    local_path = hf_hub_download(
                        repo_id=self.repo_id,
                        filename=file_path,
                        local_dir=self.download_dir,
                        repo_type="dataset",
                        token=self.hf_token
                    )
                    
                    df = pd.read_csv(local_path)
                    log_message(f"Загружено {len(df)} записей изображений из файла {file_path}")
                    
                    for _, row in df.iterrows():
                        content = f"Изображение: {row.get('№ Изображения', 'Неизвестно')}\n"
                        content += f"Название: {row.get('Название изображения', 'Неизвестно')}\n"
                        content += f"Описание: {row.get('Описание изображение', 'Неизвестно')}\n"
                        content += f"Документ: {row.get('Обозначение документа', 'Неизвестно')}\n"
                        content += f"Раздел: {row.get('Раздел документа', 'Неизвестно')}\n"
                        content += f"Файл: {row.get('Файл изображения', 'Неизвестно')}\n"
                        
                        doc = Document(
                            text=content,
                            metadata={
                                "type": "image",
                                "image_number": row.get('№ Изображения', 'unknown'),
                                "document_id": row.get('Обозначение документа', 'unknown'),
                                "file_path": row.get('Файл изображения', 'unknown'),
                                "section": row.get('Раздел документа', 'unknown')
                            }
                        )
                        image_documents.append(doc)
                            
                except Exception as e:
                    log_message(f"Ошибка обработки файла {file_path}: {str(e)}")
                    continue
            
            log_message(f"Создано {len(image_documents)} документов из изображений")
            return image_documents
            
        except Exception as e:
            log_message(f"Ошибка загрузки данных изображений: {str(e)}")
            return []

    def prepare_all_documents(self):
        log_message("Подготовка всех документов")
        
        all_documents = []
        
        json_documents = self.load_json_documents()
        all_documents.extend(json_documents)
        
        table_documents = self.load_table_documents()
        all_documents.extend(table_documents)
        
        image_documents = self.load_image_documents()
        all_documents.extend(image_documents)
        
        log_message(f"Всего подготовлено {len(all_documents)} документов")
        return all_documents