Spaces:
Sleeping
Sleeping
Commit
·
822ef8c
1
Parent(s):
c0bcb11
added the new chunking and loggings
Browse files- documents_prep.py +92 -47
- table_prep.py +61 -15
documents_prep.py
CHANGED
|
@@ -44,68 +44,113 @@ def process_documents_with_chunking(documents):
|
|
| 44 |
all_chunked_docs = []
|
| 45 |
chunk_info = []
|
| 46 |
table_count = 0
|
|
|
|
| 47 |
image_count = 0
|
|
|
|
| 48 |
text_chunks_count = 0
|
| 49 |
|
| 50 |
for doc in documents:
|
| 51 |
doc_type = doc.metadata.get('type', 'text')
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
else:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
for i, chunk_doc in enumerate(chunked_docs):
|
| 67 |
chunk_info.append({
|
| 68 |
-
'document_id':
|
| 69 |
-
'section_id':
|
| 70 |
-
'chunk_id':
|
| 71 |
-
'chunk_size': len(
|
| 72 |
-
'chunk_preview':
|
| 73 |
-
'type':
|
| 74 |
-
'table_number':
|
| 75 |
-
'table_title': chunk_doc.metadata.get('table_title', '') if doc_type == 'table' else None,
|
| 76 |
-
'image_number': chunk_doc.metadata.get('image_number', 'unknown') if doc_type == 'image' else None,
|
| 77 |
-
'image_title': chunk_doc.metadata.get('image_title', '') if doc_type == 'image' else None
|
| 78 |
})
|
| 79 |
-
else:
|
| 80 |
-
# Document is small enough, add as-is
|
| 81 |
-
all_chunked_docs.append(doc)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
'
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
log_message(f"\n{'='*60}")
|
| 104 |
log_message(f"ИТОГО ОБРАБОТАНО ДОКУМЕНТОВ:")
|
| 105 |
-
log_message(f" • Таблицы (
|
| 106 |
-
log_message(f" •
|
|
|
|
|
|
|
| 107 |
log_message(f" • Текстовые чанки: {text_chunks_count}")
|
| 108 |
-
log_message(f" • Всего
|
| 109 |
log_message(f"{'='*60}\n")
|
| 110 |
|
| 111 |
return all_chunked_docs, chunk_info
|
|
|
|
| 44 |
all_chunked_docs = []
|
| 45 |
chunk_info = []
|
| 46 |
table_count = 0
|
| 47 |
+
table_chunks_count = 0
|
| 48 |
image_count = 0
|
| 49 |
+
image_chunks_count = 0
|
| 50 |
text_chunks_count = 0
|
| 51 |
|
| 52 |
for doc in documents:
|
| 53 |
doc_type = doc.metadata.get('type', 'text')
|
| 54 |
+
is_already_chunked = doc.metadata.get('is_chunked', False)
|
| 55 |
|
| 56 |
+
if doc_type == 'table':
|
| 57 |
+
if is_already_chunked:
|
| 58 |
+
table_chunks_count += 1
|
| 59 |
+
all_chunked_docs.append(doc)
|
| 60 |
+
chunk_info.append({
|
| 61 |
+
'document_id': doc.metadata.get('document_id', 'unknown'),
|
| 62 |
+
'section_id': doc.metadata.get('section_id', 'unknown'),
|
| 63 |
+
'chunk_id': doc.metadata.get('chunk_id', 0),
|
| 64 |
+
'total_chunks': doc.metadata.get('total_chunks', 1),
|
| 65 |
+
'chunk_size': len(doc.text),
|
| 66 |
+
'chunk_preview': doc.text[:200] + "..." if len(doc.text) > 200 else doc.text,
|
| 67 |
+
'type': 'table',
|
| 68 |
+
'table_number': doc.metadata.get('table_number', 'unknown')
|
| 69 |
+
})
|
| 70 |
else:
|
| 71 |
+
table_count += 1
|
| 72 |
+
all_chunked_docs.append(doc)
|
|
|
|
| 73 |
chunk_info.append({
|
| 74 |
+
'document_id': doc.metadata.get('document_id', 'unknown'),
|
| 75 |
+
'section_id': doc.metadata.get('section_id', 'unknown'),
|
| 76 |
+
'chunk_id': 0,
|
| 77 |
+
'chunk_size': len(doc.text),
|
| 78 |
+
'chunk_preview': doc.text[:200] + "..." if len(doc.text) > 200 else doc.text,
|
| 79 |
+
'type': 'table',
|
| 80 |
+
'table_number': doc.metadata.get('table_number', 'unknown')
|
|
|
|
|
|
|
|
|
|
| 81 |
})
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
elif doc_type == 'image':
|
| 84 |
+
image_count += 1
|
| 85 |
+
doc_size = len(doc.text)
|
| 86 |
+
if doc_size > CHUNK_SIZE:
|
| 87 |
+
log_message(f"📷 CHUNKING: Изображение {doc.metadata.get('image_number', 'unknown')} | "
|
| 88 |
+
f"Размер: {doc_size} > {CHUNK_SIZE}")
|
| 89 |
+
chunked_docs = chunk_document(doc)
|
| 90 |
+
image_chunks_count += len(chunked_docs)
|
| 91 |
+
all_chunked_docs.extend(chunked_docs)
|
| 92 |
+
log_message(f" ✂️ Разделено на {len(chunked_docs)} чанков")
|
| 93 |
+
|
| 94 |
+
for i, chunk_doc in enumerate(chunked_docs):
|
| 95 |
+
chunk_info.append({
|
| 96 |
+
'document_id': chunk_doc.metadata.get('document_id', 'unknown'),
|
| 97 |
+
'section_id': chunk_doc.metadata.get('section_id', 'unknown'),
|
| 98 |
+
'chunk_id': i,
|
| 99 |
+
'chunk_size': len(chunk_doc.text),
|
| 100 |
+
'chunk_preview': chunk_doc.text[:200] + "..." if len(chunk_doc.text) > 200 else chunk_doc.text,
|
| 101 |
+
'type': 'image',
|
| 102 |
+
'image_number': chunk_doc.metadata.get('image_number', 'unknown')
|
| 103 |
+
})
|
| 104 |
else:
|
| 105 |
+
all_chunked_docs.append(doc)
|
| 106 |
+
chunk_info.append({
|
| 107 |
+
'document_id': doc.metadata.get('document_id', 'unknown'),
|
| 108 |
+
'section_id': doc.metadata.get('section_id', 'unknown'),
|
| 109 |
+
'chunk_id': 0,
|
| 110 |
+
'chunk_size': doc_size,
|
| 111 |
+
'chunk_preview': doc.text[:200] + "..." if len(doc.text) > 200 else doc.text,
|
| 112 |
+
'type': 'image',
|
| 113 |
+
'image_number': doc.metadata.get('image_number', 'unknown')
|
| 114 |
+
})
|
| 115 |
|
| 116 |
+
else:
|
| 117 |
+
doc_size = len(doc.text)
|
| 118 |
+
if doc_size > CHUNK_SIZE:
|
| 119 |
+
log_message(f"📝 CHUNKING: Текст из '{doc.metadata.get('document_id', 'unknown')}' | "
|
| 120 |
+
f"Размер: {doc_size} > {CHUNK_SIZE}")
|
| 121 |
+
chunked_docs = chunk_document(doc)
|
| 122 |
+
text_chunks_count += len(chunked_docs)
|
| 123 |
+
all_chunked_docs.extend(chunked_docs)
|
| 124 |
+
log_message(f" ✂️ Разделен на {len(chunked_docs)} чанков")
|
| 125 |
+
|
| 126 |
+
for i, chunk_doc in enumerate(chunked_docs):
|
| 127 |
+
chunk_info.append({
|
| 128 |
+
'document_id': chunk_doc.metadata.get('document_id', 'unknown'),
|
| 129 |
+
'section_id': chunk_doc.metadata.get('section_id', 'unknown'),
|
| 130 |
+
'chunk_id': i,
|
| 131 |
+
'chunk_size': len(chunk_doc.text),
|
| 132 |
+
'chunk_preview': chunk_doc.text[:200] + "..." if len(chunk_doc.text) > 200 else chunk_doc.text,
|
| 133 |
+
'type': 'text'
|
| 134 |
+
})
|
| 135 |
+
else:
|
| 136 |
+
all_chunked_docs.append(doc)
|
| 137 |
+
chunk_info.append({
|
| 138 |
+
'document_id': doc.metadata.get('document_id', 'unknown'),
|
| 139 |
+
'section_id': doc.metadata.get('section_id', 'unknown'),
|
| 140 |
+
'chunk_id': 0,
|
| 141 |
+
'chunk_size': doc_size,
|
| 142 |
+
'chunk_preview': doc.text[:200] + "..." if len(doc.text) > 200 else doc.text,
|
| 143 |
+
'type': 'text'
|
| 144 |
+
})
|
| 145 |
+
|
| 146 |
log_message(f"\n{'='*60}")
|
| 147 |
log_message(f"ИТОГО ОБРАБОТАНО ДОКУМЕНТОВ:")
|
| 148 |
+
log_message(f" • Таблицы (целые): {table_count}")
|
| 149 |
+
log_message(f" • Таблицы (чанки): {table_chunks_count}")
|
| 150 |
+
log_message(f" • Изображения (целые): {image_count - (image_chunks_count > 0)}")
|
| 151 |
+
log_message(f" • Изображения (чанки): {image_chunks_count}")
|
| 152 |
log_message(f" • Текстовые чанки: {text_chunks_count}")
|
| 153 |
+
log_message(f" • Всего документов: {len(all_chunked_docs)}")
|
| 154 |
log_message(f"{'='*60}\n")
|
| 155 |
|
| 156 |
return all_chunked_docs, chunk_info
|
table_prep.py
CHANGED
|
@@ -29,26 +29,61 @@ def create_table_content(table_data):
|
|
| 29 |
|
| 30 |
return content
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def table_to_document(table_data, document_id=None):
|
| 33 |
-
"""Convert table data to a single Document with rich metadata"""
|
| 34 |
if not isinstance(table_data, dict):
|
|
|
|
| 35 |
return []
|
| 36 |
|
| 37 |
-
doc_id = document_id or table_data.get('document_id'
|
| 38 |
table_num = table_data.get('table_number', 'Неизвестно')
|
| 39 |
table_title = table_data.get('table_title', 'Неизвестно')
|
| 40 |
section = table_data.get('section', 'Неизвестно')
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
content = create_table_content(table_data)
|
| 43 |
content_size = len(content)
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
row_count = len(table_data.get('data', [])) if 'data' in table_data else 0
|
| 47 |
-
log_message(f"✓ ДОБАВЛЕНА: Таблица {table_num} из документа '{doc_id}' | "
|
| 48 |
-
f"Размер: {content_size} символов | Строк: {row_count}")
|
| 49 |
-
|
| 50 |
-
# Store all table metadata including headers for preservation during chunking
|
| 51 |
-
return [Document(
|
| 52 |
text=content,
|
| 53 |
metadata={
|
| 54 |
"type": "table",
|
|
@@ -57,14 +92,25 @@ def table_to_document(table_data, document_id=None):
|
|
| 57 |
"document_id": doc_id,
|
| 58 |
"section": section,
|
| 59 |
"section_id": section,
|
| 60 |
-
"section_path": section, # Add for consistency with text chunks
|
| 61 |
"total_rows": row_count,
|
| 62 |
-
"content_size": content_size
|
| 63 |
-
"headers": table_data.get('headers', []), # Preserve headers
|
| 64 |
-
"original_table_data": True # Mark as original table
|
| 65 |
}
|
| 66 |
-
)
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def load_table_data(repo_id, hf_token, table_data_dir):
|
| 69 |
log_message("=" * 60)
|
| 70 |
log_message("НАЧАЛО ЗАГРУЗКИ ТАБЛИЧНЫХ ДАННЫХ")
|
|
|
|
| 29 |
|
| 30 |
return content
|
| 31 |
|
| 32 |
+
from llama_index.core.text_splitter import SentenceSplitter
|
| 33 |
+
from config import CHUNK_SIZE, CHUNK_OVERLAP
|
| 34 |
+
|
| 35 |
+
def chunk_table_document(doc, chunk_size=None, chunk_overlap=None):
|
| 36 |
+
if chunk_size is None:
|
| 37 |
+
chunk_size = CHUNK_SIZE
|
| 38 |
+
if chunk_overlap is None:
|
| 39 |
+
chunk_overlap = CHUNK_OVERLAP
|
| 40 |
+
|
| 41 |
+
text_splitter = SentenceSplitter(
|
| 42 |
+
chunk_size=chunk_size,
|
| 43 |
+
chunk_overlap=chunk_overlap,
|
| 44 |
+
separator="\n"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
text_chunks = text_splitter.split_text(doc.text)
|
| 48 |
+
|
| 49 |
+
chunked_docs = []
|
| 50 |
+
for i, chunk_text in enumerate(text_chunks):
|
| 51 |
+
chunk_metadata = doc.metadata.copy()
|
| 52 |
+
chunk_metadata.update({
|
| 53 |
+
"chunk_id": i,
|
| 54 |
+
"total_chunks": len(text_chunks),
|
| 55 |
+
"chunk_size": len(chunk_text),
|
| 56 |
+
"is_chunked": True
|
| 57 |
+
})
|
| 58 |
+
|
| 59 |
+
chunked_doc = Document(
|
| 60 |
+
text=chunk_text,
|
| 61 |
+
metadata=chunk_metadata
|
| 62 |
+
)
|
| 63 |
+
chunked_docs.append(chunked_doc)
|
| 64 |
+
|
| 65 |
+
return chunked_docs
|
| 66 |
+
|
| 67 |
def table_to_document(table_data, document_id=None):
|
|
|
|
| 68 |
if not isinstance(table_data, dict):
|
| 69 |
+
log_message(f"⚠️ ПРОПУЩЕНА: table_data не является словарем")
|
| 70 |
return []
|
| 71 |
|
| 72 |
+
doc_id = document_id or table_data.get('document_id') or table_data.get('document', 'Неизвестно')
|
| 73 |
table_num = table_data.get('table_number', 'Неизвестно')
|
| 74 |
table_title = table_data.get('table_title', 'Неизвестно')
|
| 75 |
section = table_data.get('section', 'Неизвестно')
|
| 76 |
|
| 77 |
+
table_rows = table_data.get('data', [])
|
| 78 |
+
if not table_rows or len(table_rows) == 0:
|
| 79 |
+
log_message(f"⚠️ ПРОПУЩЕНА: Таблица {table_num} из '{doc_id}' - нет данных в 'data'")
|
| 80 |
+
return []
|
| 81 |
+
|
| 82 |
content = create_table_content(table_data)
|
| 83 |
content_size = len(content)
|
| 84 |
+
row_count = len(table_rows)
|
| 85 |
|
| 86 |
+
base_doc = Document(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
text=content,
|
| 88 |
metadata={
|
| 89 |
"type": "table",
|
|
|
|
| 92 |
"document_id": doc_id,
|
| 93 |
"section": section,
|
| 94 |
"section_id": section,
|
|
|
|
| 95 |
"total_rows": row_count,
|
| 96 |
+
"content_size": content_size
|
|
|
|
|
|
|
| 97 |
}
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
if content_size > CHUNK_SIZE:
|
| 101 |
+
log_message(f"📊 CHUNKING: Таблица {table_num} из '{doc_id}' | "
|
| 102 |
+
f"Размер: {content_size} > {CHUNK_SIZE} | Строк: {row_count}")
|
| 103 |
+
chunked_docs = chunk_table_document(base_doc)
|
| 104 |
+
log_message(f" ✂️ Разделена на {len(chunked_docs)} чанков")
|
| 105 |
+
for i, chunk_doc in enumerate(chunked_docs):
|
| 106 |
+
log_message(f" Чанк {i+1}: {chunk_doc.metadata['chunk_size']} символов")
|
| 107 |
+
return chunked_docs
|
| 108 |
+
else:
|
| 109 |
+
log_message(f"✓ ДОБАВЛЕНА: Таблица {table_num} из документа '{doc_id}' | "
|
| 110 |
+
f"Размер: {content_size} символов | Строк: {row_count}")
|
| 111 |
+
return [base_doc]
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def load_table_data(repo_id, hf_token, table_data_dir):
|
| 115 |
log_message("=" * 60)
|
| 116 |
log_message("НАЧАЛО ЗАГРУЗКИ ТАБЛИЧНЫХ ДАННЫХ")
|