Spaces:
Running
Running
File size: 15,029 Bytes
aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 aee2bfd be32fd8 acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 be32fd8 acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 be32fd8 acdfaa9 be32fd8 acdfaa9 be32fd8 acdfaa9 be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 aee2bfd acdfaa9 aee2bfd acdfaa9 be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd be32fd8 aee2bfd acdfaa9 be32fd8 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 acdfaa9 be32fd8 acdfaa9 be32fd8 acdfaa9 be32fd8 acdfaa9 aee2bfd be32fd8 acdfaa9 be32fd8 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd acdfaa9 aee2bfd be32fd8 aee2bfd acdfaa9 aee2bfd acdfaa9 be32fd8 acdfaa9 aee2bfd be32fd8 aee2bfd be32fd8 acdfaa9 |
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 |
# src/utils/drive_document_processor.py
from pathlib import Path
from typing import Dict, List, Any, Tuple
import logging
from fastapi import HTTPException
from src.utils.google_drive_service import GoogleDriveService
from src.utils.document_processor import DocumentProcessor
from src.vectorstores.chroma_vectorstore import ChromaVectorStore
from src.utils.logger import logger
from src.db.mongodb_store import MongoDBStore
class DriveDocumentProcessor:
def __init__(
self,
google_service_account_path: str,
folder_id: str,
temp_dir: str,
doc_processor: DocumentProcessor,
mongodb: MongoDBStore # Add MongoDB
):
"""
Initialize Drive Document Processor
Args:
google_service_account_path (str): Path to Google service account credentials
folder_id (str): Google Drive folder ID to process
temp_dir (str): Directory for temporary files
doc_processor (DocumentProcessor): Instance of DocumentProcessor
"""
self.google_drive_service = GoogleDriveService(
google_service_account_path)
self.folder_id = folder_id
self.temp_dir = Path(temp_dir)
self.doc_processor = doc_processor
self.mongodb = mongodb # Store MongoDB instance
# Create temp directory if it doesn't exist
self.temp_dir.mkdir(exist_ok=True)
# Define supported MIME types
self.supported_mime_types = {
# Google Docs
'application/vnd.google-apps.document': '.docx',
# Microsoft Word Documents
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': '.docx',
'application/msword': '.doc',
# Microsoft Excel Documents
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': '.xlsx',
'application/vnd.ms-excel': '.xls',
# Text Documents
'text/plain': '.txt',
'text/csv': '.csv',
'text/markdown': '.md',
'text/html': '.html',
'text/xml': '.xml',
'application/json': '.json',
'application/rtf': '.rtf',
# PDF Documents
'application/pdf': '.pdf'
}
self.google_docs_export_types = {
'application/vnd.google-apps.document': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
}
async def _cleanup_orphaned_documents(
self,
drive_files: List[Dict[str, Any]],
vector_store: ChromaVectorStore
) -> Dict[str, Any]:
"""
Clean up documents that exist in MongoDB but not in Google Drive
Args:
drive_files (List[Dict[str, Any]]): List of files from Google Drive
vector_store (ChromaVectorStore): Vector store instance
Returns:
Dict[str, Any]: Cleanup statistics
"""
try:
# Get all documents from MongoDB
mongo_docs = await self.mongodb.get_all_documents()
# Create set of Google Drive file IDs
drive_file_ids = {file['id'] for file in drive_files}
deleted_count = 0
failed_deletions = []
# Check each MongoDB document
for doc in mongo_docs:
# Only process Google Drive documents
if doc.get('source') != 'google_drive':
continue
doc_id = doc.get('document_id')
if not doc_id or doc_id not in drive_file_ids:
try:
# Delete from MongoDB
await self.mongodb.delete_document(doc_id)
# Delete from vector store
vector_store.delete_document(doc_id)
deleted_count += 1
except Exception as e:
logger.error(
f"Error deleting orphaned document {doc_id}: {str(e)}")
failed_deletions.append({
'document_id': doc_id,
'error': str(e)
})
return {
'orphaned_documents_deleted': deleted_count,
'failed_deletions': failed_deletions
}
except Exception as e:
logger.error(f"Error in cleanup_orphaned_documents: {str(e)}")
raise
async def process_documents(
self,
vector_store: ChromaVectorStore,
# New parameter with default True for backward compatibility
include_subfolders: bool = True
) -> Dict[str, Any]:
"""
Process all documents in the specified Drive folder
Args:
vector_store (ChromaVectorStore): Vector store instance
include_subfolders (bool): Whether to process documents in subfolders
Returns:
Dict[str, Any]: Processing results
"""
try:
# Get documents from folder
files = self.google_drive_service.get_folder_contents(
self.folder_id,
include_subfolders=include_subfolders
)
# Clean up orphaned documents first
cleanup_results = await self._cleanup_orphaned_documents(files, vector_store)
processed_files = []
skipped_files = []
errors = []
for file in files:
# Skip if it's a folder
if file.get('mimeType') == 'application/vnd.google-apps.folder':
continue
# Get file path (including folder structure if available)
file_path = self._get_file_path(file)
file['display_path'] = file_path
result = await self._process_single_file(file, vector_store)
if result['status'] == 'processed':
processed_files.append(result['data'])
elif result['status'] == 'skipped':
skipped_files.append(result['data'])
else: # status == 'error'
errors.append(result['data'])
# Clean up temporary directory if empty
self._cleanup_temp_dir()
return {
"status": "completed",
"processed_files": {
"count": len(processed_files),
"details": processed_files
},
"skipped_files": {
"count": len(skipped_files),
"details": skipped_files
},
"errors": {
"count": len(errors),
"details": errors
}
}
except Exception as e:
logger.error(f"Error processing Drive documents: {str(e)}")
raise HTTPException(
status_code=500,
detail=f"Failed to process drive documents: {str(e)}"
)
def _get_file_path(self, file: Dict[str, Any]) -> str:
"""
Get the full path for a file including its folder structure
Args:
file (Dict[str, Any]): File metadata
Returns:
str: Display path of the file
"""
path_parts = [file['name']]
# Add folder path if available (new structure)
if folder_path := file.get('folder_path', []):
for folder in reversed(folder_path):
path_parts.insert(0, folder['name'])
return '/'.join(path_parts)
async def _process_single_file(
self,
file: Dict[str, Any],
vector_store: ChromaVectorStore
) -> Dict[str, Any]:
"""Process a single Drive file"""
mime_type = file.get('mimeType', '')
# Skip if mime type not supported
if mime_type not in self.supported_mime_types:
return {
'status': 'skipped',
'data': {
'name': file['name'],
'path': file.get('display_path', file['name']),
'reason': f'Unsupported mime type: {mime_type}'
}
}
try:
document_id = file['id']
modified_time = file.get('modifiedTime', 'N/A')
# Check if document should be processed
if self.save_document(document_id, vector_store, modified_time):
# Download and process file
temp_file_path = await self._download_and_save_file(
file['id'],
mime_type
)
try:
# Process document
processed_doc = await self.doc_processor.process_document(
str(temp_file_path)
)
# Add to vector store with path information
self._add_to_vector_store(
processed_doc['chunks'],
file,
mime_type,
vector_store
)
# Add MongoDB storage - Store Google Drive URL
await self.mongodb.store_document(
document_id=document_id,
filename=file['name'],
content_type=mime_type,
file_size=0, # Not needed for drive documents
url_path=f"https://drive.google.com/file/d/{document_id}/view",
source="google_drive"
)
return {
'status': 'processed',
'data': {
'name': file['name'],
'path': file.get('display_path', file['name']),
'id': file['id'],
'chunks_processed': len(processed_doc['chunks'])
}
}
finally:
# Clean up temporary file
if temp_file_path.exists():
temp_file_path.unlink()
else:
return {
'status': 'skipped',
'data': {
'name': file['name'],
'path': file.get('display_path', file['name']),
'reason': 'Document already exists in the memory.'
}
}
except Exception as e:
logger.error(f"Error processing file {file['name']}: {str(e)}")
return {
'status': 'error',
'data': {
'file_name': file['name'],
'path': file.get('display_path', file['name']),
'error': str(e)
}
}
def _add_to_vector_store(
self,
chunks: List[str],
file: Dict[str, Any],
mime_type: str,
vector_store: ChromaVectorStore
) -> None:
"""Add processed chunks to vector store with path information"""
chunk_metadatas = []
chunk_ids = []
modified_time = file.get('modifiedTime', 'N/A')
file_path = file.get('display_path', file['name'])
for i, chunk in enumerate(chunks):
chunk_id = f"{file['id']}-chunk-{i}"
chunk_ids.append(chunk_id)
chunk_metadatas.append({
"source": file_path, # Use full path instead of just name
"document_id": file['id'],
"chunk_index": i,
"mime_type": mime_type,
"modified_time": modified_time,
"total_chunks": len(chunks),
"file_type": self.supported_mime_types[mime_type],
"is_google_doc": mime_type.startswith('application/vnd.google-apps')
})
vector_store.add_documents(
documents=chunks,
metadatas=chunk_metadatas,
ids=chunk_ids
)
async def _download_and_save_file(
self,
file_id: str,
mime_type: str
) -> Path:
"""Download and save file to temporary location"""
extension = self.supported_mime_types[mime_type]
temp_file_path = self.temp_dir / f"{file_id}{extension}"
if mime_type in self.google_docs_export_types:
# Download Google Doc in the specified export format
content = self.google_drive_service.export_file(
file_id,
self.google_docs_export_types[mime_type]
)
else:
# Download regular file
content = self.google_drive_service.download_file(file_id)
with open(temp_file_path, 'wb') as f:
if isinstance(content, str):
f.write(content.encode('utf-8'))
else:
f.write(content)
return temp_file_path
def save_document(
self,
document_id: str,
vector_store: ChromaVectorStore,
modified_date: str
) -> bool:
"""
Check if document needs to be processed based on modification date
Args:
document_id (str): ID of the document to check
vector_store (ChromaVectorStore): Vector store instance
modified_date (str): Modified date to compare against
Returns:
bool: True if document should be processed, False otherwise
"""
try:
# Retrieve all chunks for the given document_id
chunks = vector_store.get_document_chunks(document_id)
if not chunks:
# Document doesn't exist in vector store
return True
# Check the modified_time of the first chunk
first_chunk_metadata = chunks[0].get("metadata", {})
if first_chunk_metadata.get("modified_time") != modified_date:
# If modified_time doesn't match, delete existing chunks
vector_store.delete_document(document_id)
logger.info(
f"Document {document_id} has been modified, will reprocess")
return True
logger.info(f"Document {document_id} is up to date, skipping")
return False
except Exception as e:
logger.error(f"Error checking document status: {str(e)}")
# In case of error, process the document to be safe
return True
def _cleanup_temp_dir(self) -> None:
"""Clean up temporary directory if empty"""
try:
if self.temp_dir.exists() and not any(self.temp_dir.iterdir()):
self.temp_dir.rmdir()
except Exception as e:
logger.error(f"Error cleaning up temp directory: {str(e)}")
# Don't raise the error as this is a cleanup operation
|