from cerevox import Lexa, ProcessingModeclient = Lexa()# β DEFAULT mode - fast and efficient for most documentsdocuments = client.parse( "documents/*.pdf", mode=ProcessingMode.DEFAULT # Fast processing for most use cases)# β ADVANCED mode - maximum accuracy for complex documentsdocuments = client.parse( "complex-research-papers/*.pdf", mode=ProcessingMode.ADVANCED # Use for complex layouts, research papers)print("π‘ Rule: Start with DEFAULT, use ADVANCED for complex docs requiring maximum accuracy")
# Real performance data from Lexa:# DEFAULT Mode:# - Speed: ~3 seconds per document# - Best for: Most documents, general content# - Accuracy: 96% for typical documents# - Resource usage: Efficient# ADVANCED Mode: # - Speed: ~8 seconds per document # - Best for: Complex layouts, research papers, technical docs# - Accuracy: 99%+ for complex documents# - Resource usage: Higher# Choose based on your accuracy vs speed requirements
import osfrom cerevox import AsyncLexaimport asyncioasync def intelligent_batching(files): """Batch files based on size for optimal performance""" def analyze_files(file_list): file_info = [] for file in file_list: if os.path.exists(file): size_mb = os.path.getsize(file) / (1024 * 1024) # Categorize by size if size_mb < 1: category = 'small' batch_size = 50 # Small files: large batches elif size_mb < 10: category = 'medium' batch_size = 20 # Medium files: moderate batches else: category = 'large' batch_size = 5 # Large files: small batches file_info.append({ 'file': file, 'size_mb': size_mb, 'category': category, 'batch_size': batch_size }) return file_info # Analyze and group files file_info = analyze_files(files) # Group by category categories = {} for info in file_info: category = info['category'] if category not in categories: categories[category] = [] categories[category].append(info['file']) async with AsyncLexa() as client: all_documents = [] for category, category_files in categories.items(): batch_size = file_info[0]['batch_size'] if file_info else 20 print(f"π Processing {len(category_files)} {category} files in batches of {batch_size}") for i in range(0, len(category_files), batch_size): batch = category_files[i:i + batch_size] start_time = time.time() documents = await client.parse(batch) batch_time = time.time() - start_time all_documents.extend(documents) docs_per_sec = len(documents) / batch_time print(f" β {category} batch: {len(documents)} docs in {batch_time:.2f}s ({docs_per_sec:.1f} docs/sec)") return all_documents# Example usagemixed_files = [ "small-invoice.pdf", # 100KB "medium-report.pdf", # 5MB "large-presentation.pdf" # 25MB]documents = asyncio.run(intelligent_batching(mixed_files))
import asynciofrom cerevox import AsyncLexaimport gcasync def memory_efficient_processing(large_file_list, chunk_size=100): """Process large datasets without memory issues""" total_processed = 0 # Process in chunks to manage memory for i in range(0, len(large_file_list), chunk_size): chunk = large_file_list[i:i + chunk_size] print(f"π Processing chunk {i//chunk_size + 1}: {len(chunk)} files") async with AsyncLexa() as client: documents = await client.parse(chunk) # Process documents immediately (save to DB, extract data, etc.) processed_data = [] for doc in documents: # Extract only what you need processed_data.append({ 'source_file': doc.source_file, 'content_length': len(doc.content), 'table_count': len(doc.tables), 'summary': doc.content[:500] # Only first 500 chars }) # Save processed data await save_to_database(processed_data) # Your save function total_processed += len(documents) # Clear variables and force garbage collection del documents del processed_data gc.collect() print(f" β Chunk complete. Total processed: {total_processed}") # Brief pause between chunks await asyncio.sleep(0.5) print(f"π Memory-efficient processing complete: {total_processed} documents")async def save_to_database(processed_data): """Placeholder for your database save function""" # Implement your database save logic here await asyncio.sleep(0.1) # Simulate save time# Process 10,000 documents efficientlylarge_dataset = [f"document_{i:05d}.pdf" for i in range(10000)]asyncio.run(memory_efficient_processing(large_dataset))
from cerevox import Lexa, ProcessingModeimport timedef cost_optimized_processing(files): """Optimize for cost while maintaining quality""" client = Lexa() # Strategy 1: Use DEFAULT mode for simple documents simple_extensions = ['.txt', '.csv', '.md'] complex_extensions = ['.pdf', '.docx', '.pptx'] simple_files = [f for f in files if any(f.endswith(ext) for ext in simple_extensions)] complex_files = [f for f in files if any(f.endswith(ext) for ext in complex_extensions)] all_documents = [] # Process simple files with DEFAULT mode (cheaper) if simple_files: print(f"π° Processing {len(simple_files)} simple files with DEFAULT mode") start_time = time.time() simple_docs = client.parse( simple_files, mode=ProcessingMode.DEFAULT # Fast processing for most use cases ) processing_time = time.time() - start_time print(f" β DEFAULT mode: {len(simple_docs)} docs in {processing_time:.2f}s") all_documents.extend(simple_docs) # Process complex files with ADVANCED mode (balanced cost/quality) if complex_files: print(f"π Processing {len(complex_files)} complex files with ADVANCED mode") start_time = time.time() complex_docs = client.parse( complex_files, mode=ProcessingMode.ADVANCED # Use for complex layouts, research papers ) processing_time = time.time() - start_time print(f" β ADVANCED mode: {len(complex_docs)} docs in {processing_time:.2f}s") all_documents.extend(complex_docs) print(f"π‘ Cost optimization: Used DEFAULT mode for {len(simple_files)} files, ADVANCED for {len(complex_files)} files") return all_documents# Cost optimization examplemixed_files = [ "simple-data.txt", # Use DEFAULT mode "simple-list.csv", # Use DEFAULT mode "complex-report.pdf", # Use ADVANCED mode "presentation.pptx" # Use ADVANCED mode]documents = cost_optimized_processing(mixed_files)
import asynciofrom cerevox import AsyncLexaimport timeasync def optimize_batch_sizes(files): """Find optimal batch sizes for cost and performance""" batch_sizes = [5, 10, 20, 50] # Test different batch sizes for batch_size in batch_sizes: print(f"π§ͺ Testing batch size: {batch_size}") start_time = time.time() total_docs = 0 async with AsyncLexa() as client: for i in range(0, min(len(files), 100), batch_size): # Test with first 100 files batch = files[i:i + batch_size] batch_start = time.time() documents = await client.parse(batch) batch_time = time.time() - batch_start total_docs += len(documents) # Cost calculation (example - adjust based on your pricing) cost_per_doc = 0.01 # $0.01 per document batch_cost = len(documents) * cost_per_doc print(f" Batch {i//batch_size + 1}: {len(documents)} docs, {batch_time:.2f}s, ${batch_cost:.2f}") total_time = time.time() - start_time docs_per_second = total_docs / total_time cost_per_hour = docs_per_second * 3600 * 0.01 # Hourly cost estimate print(f" π Batch size {batch_size}: {docs_per_second:.1f} docs/sec, ~${cost_per_hour:.2f}/hour") print()# Find optimal batch size for your use caselarge_file_set = [f"doc_{i:03d}.pdf" for i in range(500)]asyncio.run(optimize_batch_sizes(large_file_set))# Typical results:# Batch size 5: 2.1 docs/sec, ~$75.60/hour# Batch size 10: 3.8 docs/sec, ~$136.80/hour β Often optimal# Batch size 20: 4.2 docs/sec, ~$151.20/hour# Batch size 50: 3.9 docs/sec, ~$140.40/hour β Diminishing returns