Spaces:
Paused
Paused
Update documents_prep.py
Browse files- documents_prep.py +496 -573
documents_prep.py
CHANGED
|
@@ -1,574 +1,497 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import zipfile
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from huggingface_hub import hf_hub_download, list_repo_files
|
| 5 |
-
from llama_index.core import Document
|
| 6 |
-
from llama_index.core.text_splitter import SentenceSplitter
|
| 7 |
-
from my_logging import log_message
|
| 8 |
-
from config import CHUNK_SIZE, CHUNK_OVERLAP, MAX_CHARS_TABLE, MAX_ROWS_TABLE
|
| 9 |
-
|
| 10 |
-
def chunk_text_documents(documents):
|
| 11 |
-
text_splitter = SentenceSplitter(
|
| 12 |
-
chunk_size=CHUNK_SIZE,
|
| 13 |
-
chunk_overlap=CHUNK_OVERLAP
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
chunked = []
|
| 17 |
-
for doc in documents:
|
| 18 |
-
chunks = text_splitter.get_nodes_from_documents([doc])
|
| 19 |
-
for i, chunk in enumerate(chunks):
|
| 20 |
-
chunk.metadata.update({
|
| 21 |
-
'chunk_id': i,
|
| 22 |
-
'total_chunks': len(chunks),
|
| 23 |
-
'chunk_size': len(chunk.text) # Add chunk size
|
| 24 |
-
})
|
| 25 |
-
chunked.append(chunk)
|
| 26 |
-
|
| 27 |
-
# Log statistics
|
| 28 |
-
if chunked:
|
| 29 |
-
avg_size = sum(len(c.text) for c in chunked) / len(chunked)
|
| 30 |
-
min_size = min(len(c.text) for c in chunked)
|
| 31 |
-
max_size = max(len(c.text) for c in chunked)
|
| 32 |
-
log_message(f"✓ Text: {len(documents)} docs → {len(chunked)} chunks")
|
| 33 |
-
log_message(f" Size stats: avg={avg_size:.0f}, min={min_size}, max={max_size} chars")
|
| 34 |
-
|
| 35 |
-
return chunked
|
| 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 |
-
if
|
| 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 |
-
content += f"
|
| 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 |
-
if
|
| 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 |
-
documents
|
| 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 |
-
log_message("
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
documents = []
|
| 498 |
-
for file_path in csv_files:
|
| 499 |
-
try:
|
| 500 |
-
local_path = hf_hub_download(
|
| 501 |
-
repo_id=repo_id,
|
| 502 |
-
filename=file_path,
|
| 503 |
-
repo_type="dataset",
|
| 504 |
-
token=hf_token
|
| 505 |
-
)
|
| 506 |
-
|
| 507 |
-
df = pd.read_csv(local_path)
|
| 508 |
-
|
| 509 |
-
for _, row in df.iterrows():
|
| 510 |
-
content = f"Документ: {row.get('Обозначение документа', 'unknown')}\n"
|
| 511 |
-
content += f"Рисунок: {row.get('№ Изображения', 'unknown')}\n"
|
| 512 |
-
content += f"Название: {row.get('Название изображения', '')}\n"
|
| 513 |
-
content += f"Описание: {row.get('Описание изображение', '')}\n"
|
| 514 |
-
content += f"Раздел: {row.get('Раздел документа', '')}\n"
|
| 515 |
-
|
| 516 |
-
chunk_size = len(content)
|
| 517 |
-
|
| 518 |
-
documents.append(Document(
|
| 519 |
-
text=content,
|
| 520 |
-
metadata={
|
| 521 |
-
'type': 'image',
|
| 522 |
-
'document_id': str(row.get('Обозначение документа', 'unknown')),
|
| 523 |
-
'image_number': str(row.get('№ Изображения', 'unknown')),
|
| 524 |
-
'section': str(row.get('Раздел документа', '')),
|
| 525 |
-
'chunk_size': chunk_size
|
| 526 |
-
}
|
| 527 |
-
))
|
| 528 |
-
except Exception as e:
|
| 529 |
-
log_message(f"Error loading {file_path}: {e}")
|
| 530 |
-
|
| 531 |
-
if documents:
|
| 532 |
-
avg_size = sum(d.metadata['chunk_size'] for d in documents) / len(documents)
|
| 533 |
-
log_message(f"✓ Loaded {len(documents)} images (avg size: {avg_size:.0f} chars)")
|
| 534 |
-
|
| 535 |
-
return documents
|
| 536 |
-
|
| 537 |
-
def load_all_documents(repo_id, hf_token, json_dir, table_dir, image_dir):
|
| 538 |
-
log_message("="*60)
|
| 539 |
-
log_message("STARTING DOCUMENT LOADING")
|
| 540 |
-
log_message("="*60)
|
| 541 |
-
|
| 542 |
-
# Load text sections
|
| 543 |
-
text_docs = load_json_documents(repo_id, hf_token, json_dir)
|
| 544 |
-
text_chunks = chunk_text_documents(text_docs)
|
| 545 |
-
|
| 546 |
-
# Load tables (already chunked)
|
| 547 |
-
table_chunks = load_table_documents(repo_id, hf_token, table_dir)
|
| 548 |
-
|
| 549 |
-
# NEW: Analyze connection types in tables
|
| 550 |
-
connection_types = {}
|
| 551 |
-
for chunk in table_chunks:
|
| 552 |
-
conn_type = chunk.metadata.get('connection_type', '')
|
| 553 |
-
if conn_type:
|
| 554 |
-
connection_types[conn_type] = connection_types.get(conn_type, 0) + 1
|
| 555 |
-
|
| 556 |
-
log_message("="*60)
|
| 557 |
-
log_message("CONNECTION TYPES FOUND IN TABLES:")
|
| 558 |
-
for conn_type, count in sorted(connection_types.items()):
|
| 559 |
-
log_message(f" {conn_type}: {count} chunks")
|
| 560 |
-
log_message("="*60)
|
| 561 |
-
|
| 562 |
-
# Load images (no chunking needed)
|
| 563 |
-
image_docs = load_image_documents(repo_id, hf_token, image_dir)
|
| 564 |
-
|
| 565 |
-
all_docs = text_chunks + table_chunks + image_docs
|
| 566 |
-
|
| 567 |
-
log_message("="*60)
|
| 568 |
-
log_message(f"TOTAL DOCUMENTS: {len(all_docs)}")
|
| 569 |
-
log_message(f" Text chunks: {len(text_chunks)}")
|
| 570 |
-
log_message(f" Table chunks: {len(table_chunks)}")
|
| 571 |
-
log_message(f" Images: {len(image_docs)}")
|
| 572 |
-
log_message("="*60)
|
| 573 |
-
|
| 574 |
return all_docs
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import zipfile
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from huggingface_hub import hf_hub_download, list_repo_files
|
| 5 |
+
from llama_index.core import Document
|
| 6 |
+
from llama_index.core.text_splitter import SentenceSplitter
|
| 7 |
+
from my_logging import log_message
|
| 8 |
+
from config import CHUNK_SIZE, CHUNK_OVERLAP, MAX_CHARS_TABLE, MAX_ROWS_TABLE
|
| 9 |
+
|
| 10 |
+
def chunk_text_documents(documents):
|
| 11 |
+
text_splitter = SentenceSplitter(
|
| 12 |
+
chunk_size=CHUNK_SIZE,
|
| 13 |
+
chunk_overlap=CHUNK_OVERLAP
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
chunked = []
|
| 17 |
+
for doc in documents:
|
| 18 |
+
chunks = text_splitter.get_nodes_from_documents([doc])
|
| 19 |
+
for i, chunk in enumerate(chunks):
|
| 20 |
+
chunk.metadata.update({
|
| 21 |
+
'chunk_id': i,
|
| 22 |
+
'total_chunks': len(chunks),
|
| 23 |
+
'chunk_size': len(chunk.text) # Add chunk size
|
| 24 |
+
})
|
| 25 |
+
chunked.append(chunk)
|
| 26 |
+
|
| 27 |
+
# Log statistics
|
| 28 |
+
if chunked:
|
| 29 |
+
avg_size = sum(len(c.text) for c in chunked) / len(chunked)
|
| 30 |
+
min_size = min(len(c.text) for c in chunked)
|
| 31 |
+
max_size = max(len(c.text) for c in chunked)
|
| 32 |
+
log_message(f"✓ Text: {len(documents)} docs → {len(chunked)} chunks")
|
| 33 |
+
log_message(f" Size stats: avg={avg_size:.0f}, min={min_size}, max={max_size} chars")
|
| 34 |
+
|
| 35 |
+
return chunked
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def chunk_table_by_content(table_data, doc_id, max_chars=MAX_CHARS_TABLE, max_rows=MAX_ROWS_TABLE):
|
| 39 |
+
headers = table_data.get('headers', [])
|
| 40 |
+
rows = table_data.get('data', [])
|
| 41 |
+
table_num = table_data.get('table_number', 'unknown')
|
| 42 |
+
table_title = table_data.get('table_title', '')
|
| 43 |
+
section = table_data.get('section', '')
|
| 44 |
+
|
| 45 |
+
table_num_clean = str(table_num).strip()
|
| 46 |
+
|
| 47 |
+
import re
|
| 48 |
+
if 'приложени' in section.lower():
|
| 49 |
+
appendix_match = re.search(r'приложени[еия]\s*(\d+|[а-яА-Я])', section.lower())
|
| 50 |
+
if appendix_match:
|
| 51 |
+
appendix_num = appendix_match.group(1).upper()
|
| 52 |
+
table_identifier = f"{table_num_clean} Приложение {appendix_num}"
|
| 53 |
+
else:
|
| 54 |
+
table_identifier = table_num_clean
|
| 55 |
+
else:
|
| 56 |
+
table_identifier = table_num_clean
|
| 57 |
+
|
| 58 |
+
if not rows:
|
| 59 |
+
return []
|
| 60 |
+
|
| 61 |
+
log_message(f" 📊 Processing: {doc_id} - {table_identifier} ({len(rows)} rows)")
|
| 62 |
+
|
| 63 |
+
# Calculate base metadata size
|
| 64 |
+
base_content = format_table_header(doc_id, table_identifier, table_num, table_title, section, headers)
|
| 65 |
+
base_size = len(base_content)
|
| 66 |
+
available_space = max_chars - base_size - 200
|
| 67 |
+
|
| 68 |
+
# If entire table fits, return as one chunk
|
| 69 |
+
full_rows_content = format_table_rows([{**row, '_idx': i+1} for i, row in enumerate(rows)])
|
| 70 |
+
if base_size + len(full_rows_content) <= max_chars and len(rows) <= max_rows:
|
| 71 |
+
content = base_content + full_rows_content + format_table_footer(table_identifier, doc_id)
|
| 72 |
+
|
| 73 |
+
metadata = {
|
| 74 |
+
'type': 'table',
|
| 75 |
+
'document_id': doc_id,
|
| 76 |
+
'table_number': table_num_clean,
|
| 77 |
+
'table_identifier': table_identifier,
|
| 78 |
+
'table_title': table_title,
|
| 79 |
+
'section': section,
|
| 80 |
+
'total_rows': len(rows),
|
| 81 |
+
'chunk_size': len(content),
|
| 82 |
+
'is_complete_table': True
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
log_message(f" Single chunk: {len(content)} chars, {len(rows)} rows")
|
| 86 |
+
return [Document(text=content, metadata=metadata)]
|
| 87 |
+
|
| 88 |
+
chunks = []
|
| 89 |
+
current_rows = []
|
| 90 |
+
current_size = 0
|
| 91 |
+
chunk_num = 0
|
| 92 |
+
|
| 93 |
+
for i, row in enumerate(rows):
|
| 94 |
+
row_text = format_single_row(row, i + 1)
|
| 95 |
+
row_size = len(row_text)
|
| 96 |
+
|
| 97 |
+
should_split = (current_size + row_size > available_space or len(current_rows) >= max_rows) and current_rows
|
| 98 |
+
|
| 99 |
+
if should_split:
|
| 100 |
+
content = base_content + format_table_rows(current_rows)
|
| 101 |
+
content += f"\n\nСтроки {current_rows[0]['_idx']}-{current_rows[-1]['_idx']} из {len(rows)}\n"
|
| 102 |
+
content += format_table_footer(table_identifier, doc_id)
|
| 103 |
+
|
| 104 |
+
metadata = {
|
| 105 |
+
'type': 'table',
|
| 106 |
+
'document_id': doc_id,
|
| 107 |
+
'table_number': table_num_clean,
|
| 108 |
+
'table_identifier': table_identifier,
|
| 109 |
+
'table_title': table_title,
|
| 110 |
+
'section': section,
|
| 111 |
+
'chunk_id': chunk_num,
|
| 112 |
+
'row_start': current_rows[0]['_idx'] - 1,
|
| 113 |
+
'row_end': current_rows[-1]['_idx'],
|
| 114 |
+
'total_rows': len(rows),
|
| 115 |
+
'chunk_size': len(content),
|
| 116 |
+
'is_complete_table': False
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
chunks.append(Document(text=content, metadata=metadata))
|
| 120 |
+
log_message(f" Chunk {chunk_num + 1}: {len(content)} chars, {len(current_rows)} rows")
|
| 121 |
+
|
| 122 |
+
chunk_num += 1
|
| 123 |
+
current_rows = []
|
| 124 |
+
current_size = 0
|
| 125 |
+
|
| 126 |
+
# Add row with index
|
| 127 |
+
row_copy = row.copy() if isinstance(row, dict) else {'data': row}
|
| 128 |
+
row_copy['_idx'] = i + 1
|
| 129 |
+
current_rows.append(row_copy)
|
| 130 |
+
current_size += row_size
|
| 131 |
+
|
| 132 |
+
# Add final chunk
|
| 133 |
+
if current_rows:
|
| 134 |
+
content = base_content + format_table_rows(current_rows)
|
| 135 |
+
content += f"\n\nСтроки {current_rows[0]['_idx']}-{current_rows[-1]['_idx']} из {len(rows)}\n"
|
| 136 |
+
content += format_table_footer(table_identifier, doc_id)
|
| 137 |
+
|
| 138 |
+
metadata = {
|
| 139 |
+
'type': 'table',
|
| 140 |
+
'document_id': doc_id,
|
| 141 |
+
'table_number': table_num_clean,
|
| 142 |
+
'table_identifier': table_identifier,
|
| 143 |
+
'table_title': table_title,
|
| 144 |
+
'section': section,
|
| 145 |
+
'chunk_id': chunk_num,
|
| 146 |
+
'row_start': current_rows[0]['_idx'] - 1,
|
| 147 |
+
'row_end': current_rows[-1]['_idx'],
|
| 148 |
+
'total_rows': len(rows),
|
| 149 |
+
'chunk_size': len(content),
|
| 150 |
+
'is_complete_table': False
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
chunks.append(Document(text=content, metadata=metadata))
|
| 154 |
+
log_message(f" Chunk {chunk_num + 1}: {len(content)} chars, {len(current_rows)} rows")
|
| 155 |
+
|
| 156 |
+
return chunks
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def format_table_header(doc_id, table_identifier, table_num, table_title, section, headers):
|
| 160 |
+
content = f"ТАБЛИЦА {table_identifier} из документа {doc_id}\n"
|
| 161 |
+
|
| 162 |
+
# Add table type/number prominently for matching
|
| 163 |
+
if table_num:
|
| 164 |
+
content += f"ТИП: {table_num}\n"
|
| 165 |
+
|
| 166 |
+
if table_title:
|
| 167 |
+
content += f"НАЗВАНИЕ: {table_title}\n"
|
| 168 |
+
|
| 169 |
+
if section:
|
| 170 |
+
content += f"РАЗДЕЛ: {section}\n"
|
| 171 |
+
|
| 172 |
+
content += f"{'='*70}\n"
|
| 173 |
+
|
| 174 |
+
if headers:
|
| 175 |
+
header_str = ' | '.join(str(h) for h in headers)
|
| 176 |
+
content += f"ЗАГОЛОВКИ: {header_str}\n\n"
|
| 177 |
+
|
| 178 |
+
content += "ДАННЫЕ:\n"
|
| 179 |
+
return content
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def format_single_row(row, idx):
|
| 183 |
+
"""Format a single row"""
|
| 184 |
+
if isinstance(row, dict):
|
| 185 |
+
parts = [f"{k}: {v}" for k, v in row.items()
|
| 186 |
+
if v and str(v).strip() and str(v).lower() not in ['nan', 'none', '']]
|
| 187 |
+
if parts:
|
| 188 |
+
return f"{idx}. {' | '.join(parts)}\n"
|
| 189 |
+
elif isinstance(row, list):
|
| 190 |
+
parts = [str(v) for v in row if v and str(v).strip() and str(v).lower() not in ['nan', 'none', '']]
|
| 191 |
+
if parts:
|
| 192 |
+
return f"{idx}. {' | '.join(parts)}\n"
|
| 193 |
+
return ""
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
def format_table_rows(rows):
|
| 197 |
+
"""Format multiple rows"""
|
| 198 |
+
content = ""
|
| 199 |
+
for row in rows:
|
| 200 |
+
idx = row.get('_idx', 0)
|
| 201 |
+
content += format_single_row(row, idx)
|
| 202 |
+
return content
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def format_table_footer(table_identifier, doc_id):
|
| 206 |
+
"""Format table footer"""
|
| 207 |
+
return f"\n{'='*70}\nКОНЕЦ ТАБЛИЦЫ {table_identifier} ИЗ {doc_id}\n"
|
| 208 |
+
|
| 209 |
+
def load_json_documents(repo_id, hf_token, json_dir):
|
| 210 |
+
import zipfile
|
| 211 |
+
import tempfile
|
| 212 |
+
import os
|
| 213 |
+
|
| 214 |
+
log_message("Loading JSON documents...")
|
| 215 |
+
|
| 216 |
+
files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
|
| 217 |
+
json_files = [f for f in files if f.startswith(json_dir) and f.endswith('.json')]
|
| 218 |
+
zip_files = [f for f in files if f.startswith(json_dir) and f.endswith('.zip')]
|
| 219 |
+
|
| 220 |
+
log_message(f"Found {len(json_files)} JSON files and {len(zip_files)} ZIP files")
|
| 221 |
+
|
| 222 |
+
documents = []
|
| 223 |
+
stats = {'success': 0, 'failed': 0, 'empty': 0}
|
| 224 |
+
|
| 225 |
+
for file_path in json_files:
|
| 226 |
+
try:
|
| 227 |
+
log_message(f" Loading: {file_path}")
|
| 228 |
+
local_path = hf_hub_download(
|
| 229 |
+
repo_id=repo_id,
|
| 230 |
+
filename=file_path,
|
| 231 |
+
repo_type="dataset",
|
| 232 |
+
token=hf_token
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
docs = extract_sections_from_json(local_path)
|
| 236 |
+
if docs:
|
| 237 |
+
documents.extend(docs)
|
| 238 |
+
stats['success'] += 1
|
| 239 |
+
log_message(f" ✓ Extracted {len(docs)} sections")
|
| 240 |
+
else:
|
| 241 |
+
stats['empty'] += 1
|
| 242 |
+
log_message(f" ⚠ No sections found")
|
| 243 |
+
|
| 244 |
+
except Exception as e:
|
| 245 |
+
stats['failed'] += 1
|
| 246 |
+
log_message(f" ✗ Error: {e}")
|
| 247 |
+
|
| 248 |
+
for zip_path in zip_files:
|
| 249 |
+
try:
|
| 250 |
+
log_message(f" Processing ZIP: {zip_path}")
|
| 251 |
+
local_zip = hf_hub_download(
|
| 252 |
+
repo_id=repo_id,
|
| 253 |
+
filename=zip_path,
|
| 254 |
+
repo_type="dataset",
|
| 255 |
+
token=hf_token
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
with zipfile.ZipFile(local_zip, 'r') as zf:
|
| 259 |
+
json_files_in_zip = [f for f in zf.namelist()
|
| 260 |
+
if f.endswith('.json')
|
| 261 |
+
and not f.startswith('__MACOSX')
|
| 262 |
+
and not f.startswith('.')
|
| 263 |
+
and not '._' in f]
|
| 264 |
+
|
| 265 |
+
log_message(f" Found {len(json_files_in_zip)} JSON files in ZIP")
|
| 266 |
+
|
| 267 |
+
for json_file in json_files_in_zip:
|
| 268 |
+
try:
|
| 269 |
+
file_content = zf.read(json_file)
|
| 270 |
+
|
| 271 |
+
# Skip if file is too small
|
| 272 |
+
if len(file_content) < 10:
|
| 273 |
+
log_message(f" ✗ Skipping: {json_file} (file too small)")
|
| 274 |
+
stats['failed'] += 1
|
| 275 |
+
continue
|
| 276 |
+
|
| 277 |
+
# Try UTF-8 first (most common)
|
| 278 |
+
try:
|
| 279 |
+
text_content = file_content.decode('utf-8')
|
| 280 |
+
except UnicodeDecodeError:
|
| 281 |
+
try:
|
| 282 |
+
text_content = file_content.decode('utf-8-sig')
|
| 283 |
+
except UnicodeDecodeError:
|
| 284 |
+
try:
|
| 285 |
+
# Try UTF-16 (the issue you're seeing)
|
| 286 |
+
text_content = file_content.decode('utf-16')
|
| 287 |
+
except UnicodeDecodeError:
|
| 288 |
+
try:
|
| 289 |
+
text_content = file_content.decode('windows-1251')
|
| 290 |
+
except UnicodeDecodeError:
|
| 291 |
+
log_message(f" ✗ Skipping: {json_file} (encoding failed)")
|
| 292 |
+
stats['failed'] += 1
|
| 293 |
+
continue
|
| 294 |
+
|
| 295 |
+
# Validate JSON structure
|
| 296 |
+
if not text_content.strip().startswith('{') and not text_content.strip().startswith('['):
|
| 297 |
+
log_message(f" ✗ Skipping: {json_file} (not valid JSON)")
|
| 298 |
+
stats['failed'] += 1
|
| 299 |
+
continue
|
| 300 |
+
|
| 301 |
+
with tempfile.NamedTemporaryFile(mode='w', delete=False,
|
| 302 |
+
suffix='.json', encoding='utf-8') as tmp:
|
| 303 |
+
tmp.write(text_content)
|
| 304 |
+
tmp_path = tmp.name
|
| 305 |
+
|
| 306 |
+
docs = extract_sections_from_json(tmp_path)
|
| 307 |
+
if docs:
|
| 308 |
+
documents.extend(docs)
|
| 309 |
+
stats['success'] += 1
|
| 310 |
+
log_message(f" ✓ {json_file}: {len(docs)} sections")
|
| 311 |
+
else:
|
| 312 |
+
stats['empty'] += 1
|
| 313 |
+
log_message(f" ⚠ {json_file}: No sections")
|
| 314 |
+
|
| 315 |
+
os.unlink(tmp_path)
|
| 316 |
+
|
| 317 |
+
except json.JSONDecodeError as e:
|
| 318 |
+
stats['failed'] += 1
|
| 319 |
+
log_message(f" ✗ {json_file}: Invalid JSON")
|
| 320 |
+
except Exception as e:
|
| 321 |
+
stats['failed'] += 1
|
| 322 |
+
log_message(f" ✗ {json_file}: {str(e)[:100]}")
|
| 323 |
+
|
| 324 |
+
except Exception as e:
|
| 325 |
+
log_message(f" ✗ Error with ZIP: {e}")
|
| 326 |
+
|
| 327 |
+
log_message(f"="*60)
|
| 328 |
+
log_message(f"JSON Loading Stats:")
|
| 329 |
+
log_message(f" Success: {stats['success']}")
|
| 330 |
+
log_message(f" Empty: {stats['empty']}")
|
| 331 |
+
log_message(f" Failed: {stats['failed']}")
|
| 332 |
+
log_message(f" Total sections: {len(documents)}")
|
| 333 |
+
log_message(f"="*60)
|
| 334 |
+
|
| 335 |
+
return documents
|
| 336 |
+
|
| 337 |
+
def extract_sections_from_json(json_path):
|
| 338 |
+
"""Extract sections from a single JSON file"""
|
| 339 |
+
documents = []
|
| 340 |
+
|
| 341 |
+
try:
|
| 342 |
+
with open(json_path, 'r', encoding='utf-8') as f:
|
| 343 |
+
data = json.load(f)
|
| 344 |
+
|
| 345 |
+
doc_id = data.get('document_metadata', {}).get('document_id', 'unknown')
|
| 346 |
+
|
| 347 |
+
# Extract all section levels
|
| 348 |
+
for section in data.get('sections', []):
|
| 349 |
+
if section.get('section_text', '').strip():
|
| 350 |
+
documents.append(Document(
|
| 351 |
+
text=section['section_text'],
|
| 352 |
+
metadata={
|
| 353 |
+
'type': 'text',
|
| 354 |
+
'document_id': doc_id,
|
| 355 |
+
'section_id': section.get('section_id', '')
|
| 356 |
+
}
|
| 357 |
+
))
|
| 358 |
+
|
| 359 |
+
# Subsections
|
| 360 |
+
for subsection in section.get('subsections', []):
|
| 361 |
+
if subsection.get('subsection_text', '').strip():
|
| 362 |
+
documents.append(Document(
|
| 363 |
+
text=subsection['subsection_text'],
|
| 364 |
+
metadata={
|
| 365 |
+
'type': 'text',
|
| 366 |
+
'document_id': doc_id,
|
| 367 |
+
'section_id': subsection.get('subsection_id', '')
|
| 368 |
+
}
|
| 369 |
+
))
|
| 370 |
+
|
| 371 |
+
# Sub-subsections
|
| 372 |
+
for sub_sub in subsection.get('sub_subsections', []):
|
| 373 |
+
if sub_sub.get('sub_subsection_text', '').strip():
|
| 374 |
+
documents.append(Document(
|
| 375 |
+
text=sub_sub['sub_subsection_text'],
|
| 376 |
+
metadata={
|
| 377 |
+
'type': 'text',
|
| 378 |
+
'document_id': doc_id,
|
| 379 |
+
'section_id': sub_sub.get('sub_subsection_id', '')
|
| 380 |
+
}
|
| 381 |
+
))
|
| 382 |
+
|
| 383 |
+
except Exception as e:
|
| 384 |
+
log_message(f"Error extracting from {json_path}: {e}")
|
| 385 |
+
|
| 386 |
+
return documents
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def load_table_documents(repo_id, hf_token, table_dir):
|
| 390 |
+
log_message("Loading tables...")
|
| 391 |
+
|
| 392 |
+
files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
|
| 393 |
+
table_files = [f for f in files if f.startswith(table_dir) and f.endswith('.json')]
|
| 394 |
+
|
| 395 |
+
all_chunks = []
|
| 396 |
+
for file_path in table_files:
|
| 397 |
+
try:
|
| 398 |
+
local_path = hf_hub_download(
|
| 399 |
+
repo_id=repo_id,
|
| 400 |
+
filename=file_path,
|
| 401 |
+
repo_type="dataset",
|
| 402 |
+
token=hf_token
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
with open(local_path, 'r', encoding='utf-8') as f:
|
| 406 |
+
data = json.load(f)
|
| 407 |
+
|
| 408 |
+
file_doc_id = data.get('document_id', data.get('document', 'unknown'))
|
| 409 |
+
|
| 410 |
+
for sheet in data.get('sheets', []):
|
| 411 |
+
sheet_doc_id = sheet.get('document_id', sheet.get('document', file_doc_id))
|
| 412 |
+
|
| 413 |
+
# Use the consistent MAX_CHARS_TABLE from config
|
| 414 |
+
chunks = chunk_table_by_content(sheet, sheet_doc_id, max_chars=MAX_CHARS_TABLE, max_rows=MAX_ROWS_TABLE)
|
| 415 |
+
all_chunks.extend(chunks)
|
| 416 |
+
|
| 417 |
+
except Exception as e:
|
| 418 |
+
log_message(f"Error loading {file_path}: {e}")
|
| 419 |
+
|
| 420 |
+
log_message(f"✓ Loaded {len(all_chunks)} table chunks")
|
| 421 |
+
return all_chunks
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
def load_image_documents(repo_id, hf_token, image_dir):
|
| 425 |
+
"""Load image descriptions"""
|
| 426 |
+
log_message("Loading images...")
|
| 427 |
+
|
| 428 |
+
files = list_repo_files(repo_id=repo_id, repo_type="dataset", token=hf_token)
|
| 429 |
+
csv_files = [f for f in files if f.startswith(image_dir) and f.endswith('.csv')]
|
| 430 |
+
|
| 431 |
+
documents = []
|
| 432 |
+
for file_path in csv_files:
|
| 433 |
+
try:
|
| 434 |
+
local_path = hf_hub_download(
|
| 435 |
+
repo_id=repo_id,
|
| 436 |
+
filename=file_path,
|
| 437 |
+
repo_type="dataset",
|
| 438 |
+
token=hf_token
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
df = pd.read_csv(local_path)
|
| 442 |
+
|
| 443 |
+
for _, row in df.iterrows():
|
| 444 |
+
content = f"Документ: {row.get('Обозначение документа', 'unknown')}\n"
|
| 445 |
+
content += f"Рисунок: {row.get('№ Изображения', 'unknown')}\n"
|
| 446 |
+
content += f"Название: {row.get('Название изображения', '')}\n"
|
| 447 |
+
content += f"Описание: {row.get('Описание изображение', '')}\n"
|
| 448 |
+
content += f"Раздел: {row.get('Раздел документа', '')}\n"
|
| 449 |
+
|
| 450 |
+
chunk_size = len(content)
|
| 451 |
+
|
| 452 |
+
documents.append(Document(
|
| 453 |
+
text=content,
|
| 454 |
+
metadata={
|
| 455 |
+
'type': 'image',
|
| 456 |
+
'document_id': str(row.get('Обозначение документа', 'unknown')),
|
| 457 |
+
'image_number': str(row.get('№ Изображения', 'unknown')),
|
| 458 |
+
'section': str(row.get('Раздел документа', '')),
|
| 459 |
+
'chunk_size': chunk_size
|
| 460 |
+
}
|
| 461 |
+
))
|
| 462 |
+
except Exception as e:
|
| 463 |
+
log_message(f"Error loading {file_path}: {e}")
|
| 464 |
+
|
| 465 |
+
if documents:
|
| 466 |
+
avg_size = sum(d.metadata['chunk_size'] for d in documents) / len(documents)
|
| 467 |
+
log_message(f"✓ Loaded {len(documents)} images (avg size: {avg_size:.0f} chars)")
|
| 468 |
+
|
| 469 |
+
return documents
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
def load_all_documents(repo_id, hf_token, json_dir, table_dir, image_dir):
|
| 473 |
+
"""Main loader - combines all document types"""
|
| 474 |
+
log_message("="*60)
|
| 475 |
+
log_message("STARTING DOCUMENT LOADING")
|
| 476 |
+
log_message("="*60)
|
| 477 |
+
|
| 478 |
+
# Load text sections
|
| 479 |
+
text_docs = load_json_documents(repo_id, hf_token, json_dir)
|
| 480 |
+
text_chunks = chunk_text_documents(text_docs)
|
| 481 |
+
|
| 482 |
+
# Load tables (already chunked)
|
| 483 |
+
table_chunks = load_table_documents(repo_id, hf_token, table_dir)
|
| 484 |
+
|
| 485 |
+
# Load images (no chunking needed)
|
| 486 |
+
image_docs = load_image_documents(repo_id, hf_token, image_dir)
|
| 487 |
+
|
| 488 |
+
all_docs = text_chunks + table_chunks + image_docs
|
| 489 |
+
|
| 490 |
+
log_message("="*60)
|
| 491 |
+
log_message(f"TOTAL DOCUMENTS: {len(all_docs)}")
|
| 492 |
+
log_message(f" Text chunks: {len(text_chunks)}")
|
| 493 |
+
log_message(f" Table chunks: {len(table_chunks)}")
|
| 494 |
+
log_message(f" Images: {len(image_docs)}")
|
| 495 |
+
log_message("="*60)
|
| 496 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
return all_docs
|