Spaces:
Runtime error
Runtime error
File size: 11,555 Bytes
4e39be7 ea8da24 4e39be7 ea8da24 4e39be7 ea8da24 4e39be7 ea8da24 4e39be7 |
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 |
import Tesseract from 'tesseract.js';
import geminiClient from './gemini';
import redisClient from './redis';
import { ViolationPattern, getAllViolationPatterns, findViolationPatternById } from './housing-law-database';
// PDF.js will be imported dynamically when needed
let pdfjsLib: any = null;
let pdfjsLoaded = false;
export interface ProcessedClause {
id: string;
text: string;
section: string;
vector: number[];
metadata: {
leaseId: string;
flagged: boolean;
severity?: 'Critical' | 'High' | 'Medium' | 'Low';
violationType?: string;
legalReference?: string;
confidence: number;
};
}
export interface LeaseAnalysis {
leaseId: string;
clauses: ProcessedClause[];
violations: Array<{
clauseId: string;
type: string;
description: string;
legalReference: string;
severity: 'Critical' | 'High' | 'Medium' | 'Low';
}>;
summary: {
totalClauses: number;
flaggedClauses: number;
criticalViolations: number;
highViolations: number;
mediumViolations: number;
lowViolations: number;
};
}
/**
* Document processing pipeline for LeaseGuard
* Handles PDF text extraction, OCR, and clause analysis
*/
class DocumentProcessor {
/**
* Process uploaded document (PDF or image)
* @param file - Uploaded file
* @param leaseId - Unique lease identifier
* @returns Processed lease analysis
*/
async processDocument(file: File, leaseId: string): Promise<LeaseAnalysis> {
try {
console.log(`Processing document: ${file.name} (${file.size} bytes)`);
// Extract text from document
const extractedText = await this.extractText(file);
// Extract clauses using AI
const extractedClauses = await geminiClient.extractClauses(extractedText);
// Generate embeddings and analyze clauses
const processedClauses = await this.processClauses(extractedClauses, leaseId);
// Detect violations
const violations = await this.detectViolations(processedClauses);
// Store in Redis
await this.storeInRedis(processedClauses, leaseId);
// Generate summary
const summary = this.generateSummary(processedClauses, violations);
return {
leaseId,
clauses: processedClauses,
violations,
summary
};
} catch (error) {
console.error('Error processing document:', error);
throw new Error(`Failed to process document: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
/**
* Extract text from PDF or image file
*/
private async extractText(file: File): Promise<string> {
const fileType = file.type;
if (fileType === 'application/pdf') {
return await this.extractTextFromPDF(file);
} else if (fileType.startsWith('image/')) {
return await this.extractTextFromImage(file);
} else {
throw new Error('Unsupported file type. Please upload a PDF or image file.');
}
}
/**
* Extract text from PDF using PDF.js
*/
private async extractTextFromPDF(file: File): Promise<string> {
try {
// Dynamically import PDF.js only when needed
if (!pdfjsLoaded) {
try {
pdfjsLib = await import('pdfjs-dist');
// Configure PDF.js worker
pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.js`;
pdfjsLoaded = true;
} catch (importError) {
console.error('Failed to import PDF.js:', importError);
throw new Error('PDF processing is not available in this environment');
}
}
const arrayBuffer = await file.arrayBuffer();
const pdf = await pdfjsLib.getDocument({ data: arrayBuffer }).promise;
let fullText = '';
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
const page = await pdf.getPage(pageNum);
const textContent = await page.getTextContent();
const pageText = textContent.items
.map((item: any) => item.str)
.join(' ');
fullText += pageText + '\n';
}
return fullText.trim();
} catch (error) {
console.error('Error extracting text from PDF:', error);
throw new Error('Failed to extract text from PDF. The file may be corrupted or password-protected.');
}
}
/**
* Extract text from image using Tesseract.js OCR
*/
private async extractTextFromImage(file: File): Promise<string> {
try {
const result = await Tesseract.recognize(file, 'eng', {
logger: m => console.log(m)
});
return result.data.text.trim();
} catch (error) {
console.error('Error extracting text from image:', error);
throw new Error('Failed to extract text from image. Please ensure the image is clear and readable.');
}
}
/**
* Process extracted clauses with embeddings and violation detection
*/
private async processClauses(
extractedClauses: Array<{ text: string; section: string }>,
leaseId: string
): Promise<ProcessedClause[]> {
const processedClauses: ProcessedClause[] = [];
for (const clause of extractedClauses) {
try {
// Generate embedding
const vector = await geminiClient.generateEmbedding(clause.text);
// Detect violations
const violation = await this.detectClauseViolation(clause.text);
const processedClause: ProcessedClause = {
id: `${leaseId}_${processedClauses.length}`,
text: clause.text,
section: clause.section,
vector,
metadata: {
leaseId,
flagged: !!violation,
severity: violation?.severity,
violationType: violation?.violation_type,
legalReference: violation?.legal_violation,
confidence: violation ? 0.85 : 0.0
}
};
processedClauses.push(processedClause);
} catch (error) {
console.error('Error processing clause:', error);
// Continue with other clauses
}
}
return processedClauses;
}
/**
* Detect violations in a single clause
*/
private async detectClauseViolation(clauseText: string): Promise<ViolationPattern | null> {
try {
// First, try regex-based detection for speed
const violationPatterns = getAllViolationPatterns();
for (const pattern of violationPatterns) {
const regex = new RegExp(pattern.detection_regex, 'i');
if (regex.test(clauseText)) {
return pattern;
}
}
// If no regex match, try vector similarity search
const clauseEmbedding = await geminiClient.generateEmbedding(clauseText);
const redis = redisClient.getClient();
// Search for similar violation patterns in Redis
const searchResults = await redis.ft.search('clause_idx',
`*=>[KNN 5 @vector $vector AS score]`,
{
PARAMS: {
vector: Buffer.from(Float32Array.from(clauseEmbedding).buffer)
},
RETURN: ['text', 'metadata', 'score'],
SORTBY: 'score'
}
);
// Check if any violation patterns have high similarity
for (const result of searchResults.documents) {
const score = parseFloat(result.score as string);
if (score >= 0.85) {
const metadata = result.metadata as any;
if (metadata?.violationType) {
return findViolationPatternById(metadata.violationType);
}
}
}
return null;
} catch (error) {
console.error('Error detecting clause violation:', error);
return null;
}
}
/**
* Detect all violations in processed clauses
*/
private async detectViolations(clauses: ProcessedClause[]): Promise<LeaseAnalysis['violations']> {
const violations: LeaseAnalysis['violations'] = [];
for (const clause of clauses) {
if (clause.metadata.flagged && clause.metadata.violationType) {
violations.push({
clauseId: clause.id,
type: clause.metadata.violationType,
description: clause.text,
legalReference: clause.metadata.legalReference || 'Unknown',
severity: clause.metadata.severity || 'Low'
});
}
}
return violations;
}
/**
* Store processed clauses in Redis
*/
private async storeInRedis(clauses: ProcessedClause[], leaseId: string): Promise<void> {
try {
const redis = redisClient.getClient();
for (const clause of clauses) {
const key = `clause:${clause.id}`;
await redis.json.set(key, '$', {
text: clause.text,
vector: clause.vector,
metadata: clause.metadata
});
// Set expiration for 30 days
await redis.expire(key, 30 * 24 * 60 * 60);
}
// Store lease metadata
await redis.json.set(`lease:${leaseId}`, '$', {
id: leaseId,
processedAt: new Date().toISOString(),
clauseCount: clauses.length,
flaggedCount: clauses.filter(c => c.metadata.flagged).length
});
console.log(`Stored ${clauses.length} clauses in Redis for lease ${leaseId}`);
} catch (error) {
console.error('Error storing in Redis:', error);
// Don't throw error - Redis storage failure shouldn't block document processing
// The analysis results are still valid and can be returned to the user
console.warn('Redis storage failed, but document processing completed successfully');
}
}
/**
* Generate analysis summary
*/
private generateSummary(
clauses: ProcessedClause[],
violations: LeaseAnalysis['violations']
): LeaseAnalysis['summary'] {
const flaggedClauses = clauses.filter(c => c.metadata.flagged);
return {
totalClauses: clauses.length,
flaggedClauses: flaggedClauses.length,
criticalViolations: violations.filter(v => v.severity === 'Critical').length,
highViolations: violations.filter(v => v.severity === 'High').length,
mediumViolations: violations.filter(v => v.severity === 'Medium').length,
lowViolations: violations.filter(v => v.severity === 'Low').length
};
}
/**
* Validate file before processing
*/
validateFile(file: File): { valid: boolean; error?: string } {
const maxSize = 10 * 1024 * 1024; // 10MB
const allowedTypes = [
'application/pdf',
'image/jpeg',
'image/jpg',
'image/png',
'image/tiff',
'image/bmp'
];
if (file.size > maxSize) {
return { valid: false, error: 'File size must be less than 10MB' };
}
if (!allowedTypes.includes(file.type)) {
return { valid: false, error: 'File type not supported. Please upload a PDF or image file.' };
}
return { valid: true };
}
/**
* Health check for document processing
*/
async healthCheck(): Promise<boolean> {
try {
// Check if Tesseract is available
const tesseractAvailable = typeof Tesseract !== 'undefined';
// PDF.js will be loaded dynamically when needed
return tesseractAvailable;
} catch (error) {
console.error('Document processor health check failed:', error);
return false;
}
}
}
// Singleton instance
const documentProcessor = new DocumentProcessor();
export default documentProcessor; |