Spaces:
Build error
Build error
File size: 14,878 Bytes
4d037b0 8b763c3 4d037b0 8b763c3 4d037b0 |
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 |
import os
import json
from typing import List, Dict, Any, Optional
from datetime import datetime
from dotenv import load_dotenv
from pathlib import Path
# LangChain imports
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
from langchain.schema import Document
load_dotenv()
class LangChainMultimodalVectorizer:
def __init__(self):
self.embeddings = OpenAIEmbeddings(
# openai_api_key=os.getenv("OPENAI_API_KEY"),
# model=os.getenv("OPENAI_EMBEDDING_MODEL", "text-embedding-ada-002")
)
# self.persist_dir = os.getenv("CHROMA_PERSIST_DIR", "./chroma_persist")
def get_or_create_vectorstore(self, year: int) -> Chroma:
"""Get or create Chroma vectorstore for specific year"""
collection_name = f"optima_multimodal_{year}"
# Create persist directory for this year
year_persist_dir = os.path.join(self.persist_dir, f"year_{year}")
os.makedirs(year_persist_dir, exist_ok=True)
try:
# Try to load existing vectorstore
vectorstore = Chroma(
collection_name=collection_name,
embedding_function=self.embeddings,
persist_directory=year_persist_dir
)
# Check if collection exists and has documents
if vectorstore._collection.count() > 0:
print(f"π Using existing vectorstore: {collection_name} ({vectorstore._collection.count()} docs)")
else:
print(f"π Created new vectorstore: {collection_name}")
except Exception as e:
print(f"π Creating new vectorstore: {collection_name}")
vectorstore = Chroma(
collection_name=collection_name,
embedding_function=self.embeddings,
persist_directory=year_persist_dir
)
return vectorstore
def create_embedding_text(self, item: Dict[str, Any]) -> str:
"""Create optimized text for embedding based on content_type"""
content_type = item.get("content_type", "")
content = item.get("content", "")
context_text = item.get("context_text", "")
# Create rich embedding text based on content_type
if content_type == "silabus":
mata_kuliah = item.get("mata_kuliah", "")
course_code = item.get("course_code", "")
silabus_type = item.get("silabus_type", "")
program = item.get("program", "")
semester = item.get("semester", "")
embedding_text = f"Silabus {program} semester {semester} {mata_kuliah} {course_code} {silabus_type}: {content} {context_text}"
elif content_type == "curriculum":
program = item.get("program", "")
semester = item.get("semester", "")
table_type = item.get("table_type", "")
embedding_text = f"Kurikulum {program} semester {semester} {table_type}: {content} {context_text}"
elif content_type == "image":
title = item.get("title", "")
caption = item.get("caption", "")
embedding_text = f"Gambar: {title} {caption} {content} {context_text}"
elif content_type == "table":
title = item.get("title", "")
caption = item.get("caption", "")
rows = item.get("rows", 0)
cols = item.get("cols", 0)
embedding_text = f"Tabel {rows}x{cols}: {title} {caption} {content} {context_text}"
else: # text_chunk
chapter = item.get("chapter", "")
section = item.get("section", "")
embedding_text = f"Teks {chapter} {section}: {content} {context_text}"
return embedding_text
def prepare_document_metadata(self, item: Dict[str, Any]) -> Dict[str, Any]:
"""Prepare metadata for LangChain Document"""
content_type = item.get("content_type", "")
# Base metadata (common for all types)
metadata = {
"id": item.get("id", ""),
"content_type": content_type,
"year": item.get("year", 0),
"page": item.get("page", 0),
"filename": item.get("filename", "")[:200],
"filepath": item.get("filepath", "")[:300],
"extracted_at": item.get("extracted_at", "")
}
# Add specific metadata based on content_type
if content_type == "silabus":
metadata.update({
"mata_kuliah": item.get("mata_kuliah", "")[:200],
"course_code": item.get("course_code", ""),
"sks": item.get("sks", ""),
"program": item.get("program", ""),
"semester": item.get("semester", ""),
"silabus_type": item.get("silabus_type", "")
})
elif content_type == "curriculum":
metadata.update({
"program": item.get("program", ""),
"semester": item.get("semester", ""),
"table_type": item.get("table_type", ""),
"content_type_detail": item.get("content_type_detail", ""),
"rows_count": item.get("rows_count", 0)
})
elif content_type == "image":
metadata.update({
"title": item.get("title", "")[:200],
"caption": item.get("caption", "")[:300],
"image_index": item.get("image_index", 0),
"image_path": item.get("filepath", "")
})
elif content_type == "table":
metadata.update({
"title": item.get("title", "")[:200],
"caption": item.get("caption", "")[:300],
"table_index": item.get("table_index", 0),
"rows": item.get("rows", 0),
"cols": item.get("cols", 0),
"table_path": item.get("filepath", "")
})
else: # text_chunk
metadata.update({
"chapter": item.get("chapter", "")[:200],
"section": item.get("section", "")[:200],
"subsection": item.get("subsection", "")[:200],
"chunk_type": item.get("chunk_type", ""),
"quality_score": item.get("quality_score", 0.0)
})
return metadata
def process_unified_json(self, json_file_path: str, year: int) -> Dict[str, int]:
"""Process unified multimodal JSON file using LangChain"""
if not os.path.exists(json_file_path):
print(f"β File not found: {json_file_path}")
return {}
print(f"π Processing: {json_file_path}")
with open(json_file_path, 'r', encoding='utf-8') as f:
raw_data = json.load(f)
# π§ Handle different JSON structures
if isinstance(raw_data, dict):
if 'content' in raw_data:
data = raw_data['content'] # Extract from content array
print(f"π¦ Detected structured JSON with 'content' key")
else:
print(f"β Unexpected JSON structure: {list(raw_data.keys())}")
return {}
elif isinstance(raw_data, list):
data = raw_data # Direct array
print(f"π¦ Detected direct array JSON")
else:
print(f"β Unexpected JSON type: {type(raw_data)}")
return {}
# Get vectorstore for this year
vectorstore = self.get_or_create_vectorstore(year)
# Statistics
stats = {
"text_chunk": 0,
"image": 0,
"table": 0,
"curriculum": 0,
"silabus": 0,
"total": 0,
"errors": 0,
"skipped": 0
}
print(f"π Found {len(data)} items for year {year}")
# Prepare documents for batch processing
documents = []
batch_size = 50
for idx, item in enumerate(data):
try:
# π§ Ensure item is dict
if not isinstance(item, dict):
print(f"β οΈ Skipping non-dict item at index {idx}: {type(item)}")
stats["skipped"] += 1
continue
content_type = item.get("content_type", "unknown")
content = item.get("content", "")
context_text = item.get("context_text", "")
# Skip if no meaningful content
if not content and not context_text:
stats["skipped"] += 1
continue
if len(str(content).strip()) < 3 and len(str(context_text).strip()) < 10:
stats["skipped"] += 1
continue
# Create embedding text
embedding_text = self.create_embedding_text(item)
# Prepare metadata
metadata = self.prepare_document_metadata(item)
# Create LangChain Document
doc = Document(
page_content=embedding_text,
metadata=metadata
)
documents.append(doc)
# Update stats
if content_type in stats:
stats[content_type] += 1
else:
stats["unknown"] = stats.get("unknown", 0) + 1
stats["total"] += 1
# Process batch when full
if len(documents) >= batch_size:
self.add_documents_to_vectorstore(vectorstore, documents)
print(f" β
Processed batch {stats['total']//batch_size} ({stats['total']} items)")
documents = [] # Reset batch
except Exception as e:
print(f"β Error processing item {idx}: {e}")
print(f" Item type: {type(item)}")
if isinstance(item, dict):
print(f" Item keys: {list(item.keys())[:5]}...")
else:
print(f" Item content preview: {str(item)[:100]}...")
stats["errors"] += 1
# Process remaining documents
if documents:
self.add_documents_to_vectorstore(vectorstore, documents)
# Persist the vectorstore
vectorstore.persist()
print(f"π Processing complete for year {year}:")
for key, value in stats.items():
if value > 0:
print(f" π {key}: {value}")
return stats
def add_documents_to_vectorstore(self, vectorstore: Chroma, documents: List[Document]):
"""Add documents to vectorstore"""
try:
vectorstore.add_documents(documents)
except Exception as e:
print(f"β Error adding documents to vectorstore: {e}")
def query_multimodal(self, query_text: str, year: Optional[int] = None,
content_types: Optional[List[str]] = None,
n_results: int = 10) -> List[Dict]:
results = []
years_to_search = [year] if year else [2022, 2023, 2024]
for search_year in years_to_search:
try:
vectorstore = self.get_or_create_vectorstore(search_year)
# Build filter for content types
search_kwargs = {"k": n_results}
if content_types:
search_kwargs["filter"] = {"content_type": {"$in": content_types}}
# Perform similarity search
docs = vectorstore.similarity_search_with_score(
query_text,
k=n_results,
filter=search_kwargs.get("filter")
)
# Format results
for doc, score in docs:
result = {
"content": doc.page_content,
"metadata": doc.metadata,
"score": score,
"year": search_year
}
# Add special handling for images
if result["metadata"]["content_type"] == "image":
result["image_path"] = result["metadata"].get("image_path", "")
result["retrievable"] = os.path.exists(result["image_path"]) if result["image_path"] else False
# Add special handling for tables
elif result["metadata"]["content_type"] == "table":
result["table_path"] = result["metadata"].get("table_path", "")
result["retrievable"] = os.path.exists(result["table_path"]) if result["table_path"] else False
results.append(result)
except Exception as e:
print(f"β Error querying year {search_year}: {e}")
# Sort by score (lower is better for distance-based scoring)
results.sort(key=lambda x: x["score"])
return results[:n_results]
def get_vectorstore_stats(self, year: int) -> Dict:
"""Get statistics for a vectorstore"""
try:
vectorstore = self.get_or_create_vectorstore(year)
count = vectorstore._collection.count()
return {
"year": year,
"total_documents": count,
"collection_name": f"optima_multimodal_{year}"
}
except Exception as e:
print(f"β Error getting stats for year {year}: {e}")
return {"year": year, "total_documents": 0, "error": str(e)}
def process_all_unified_files(data_dir: str = "./chunked"):
vectorizer = LangChainMultimodalVectorizer()
years = [2022, 2023, 2024]
total_stats = {"total": 0, "errors": 0}
for year in years:
json_file = os.path.join(data_dir, f"multimodal_unified_{year}.json")
if not os.path.exists(json_file):
print(f"β οΈ File not found: {json_file}")
continue
print(f"\nπ Processing year {year}...")
stats = vectorizer.process_unified_json(json_file, year)
if stats:
print(f"π Year {year} Final Statistics:")
for content_type, count in stats.items():
print(f" π {content_type}: {count}")
total_stats["total"] += stats.get("total", 0)
total_stats["errors"] += stats.get("errors", 0)
print(f"\nπ FINAL PROCESSING SUMMARY:")
print(f" π― Total documents processed: {total_stats['total']}")
print(f" β Total errors: {total_stats['errors']}")
# Show vectorstore stats
print(f"\nπ VECTORSTORE STATISTICS:")
for year in years:
stats = vectorizer.get_vectorstore_stats(year)
print(f" {year}: {stats['total_documents']} documents")
if __name__ == "__main__":
process_all_unified_files()
|