File size: 16,125 Bytes
1a27ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================================================

import asyncio
import hashlib
import logging
import re
from concurrent.futures import ThreadPoolExecutor
from io import BytesIO
from typing import Dict, List, Optional

import cv2
import fitz  # PyMuPDF
import numpy as np
import pdfplumber
import pytesseract
import requests
from PIL import Image
from sentence_transformers import SentenceTransformer

logger = logging.getLogger(__name__)

class AdvancedDocumentProcessor:
    """Multi-method document extraction with high reliability"""
    
    def __init__(self):
        self.extraction_cache = {}
        self.executor = ThreadPoolExecutor(max_workers=4)
        self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
        logger.info("โœ… Document processor initialized")
    
    async def process_document(self, url: str) -> Dict:
        """Main document processing pipeline"""
        logger.info(f"๐Ÿ” Processing document: {url[:50]}...")
        
        # Extract text using multiple methods
        extraction_result = await self.extract_with_all_methods(url)
        
        if not extraction_result.get('text'):
            logger.error(f"โŒ Failed to extract text from document")
            return {"chunks": [], "metadata": {"error": "text_extraction_failed"}}
        
        # Chunk the document
        chunks = await self.create_smart_chunks(
            text=extraction_result['text'],
            metadata=extraction_result
        )
        
        # Generate embeddings for chunks
        chunks_with_embeddings = await self.generate_embeddings(chunks)
        
        logger.info(f"โœ… Processed document: {len(chunks_with_embeddings)} chunks created")
        
        return {
            "chunks": chunks_with_embeddings,
            "metadata": {
                "total_chunks": len(chunks_with_embeddings),
                "extraction_method": extraction_result.get('method', 'unknown'),
                "confidence": extraction_result.get('confidence', 0.0),
                "document_length": len(extraction_result['text'])
            }
        }
    
    async def extract_with_all_methods(self, url: str) -> Dict:
        """Extract using ALL available methods and merge results"""
        doc_hash = hashlib.md5(url.encode()).hexdigest()
        
        if doc_hash in self.extraction_cache:
            logger.info(f"๐Ÿ“‹ Using cached extraction")
            return self.extraction_cache[doc_hash]
        
        try:
            # Download document
            pdf_bytes = await self._download_document(url)
            if not pdf_bytes:
                return {"text": "", "confidence": 0.0, "method": "download_failed"}
            
            # Try multiple extraction methods
            methods = [
                self._extract_with_pymupdf(pdf_bytes),
                self._extract_with_pdfplumber(pdf_bytes)
            ]
            
            # Add OCR for difficult documents
            if len(pdf_bytes.getvalue()) < 5 * 1024 * 1024:  # Only OCR files < 5MB
                methods.append(self._extract_with_ocr(pdf_bytes))
            
            results = []
            for method in methods:
                try:
                    result = await method
                    if result and result.get('text') and len(result['text']) > 100:
                        results.append(result)
                        logger.info(f"โœ… {result['method']}: {len(result['text'])} chars")
                except Exception as e:
                    logger.warning(f"โš ๏ธ Extraction method failed: {e}")
            
            if not results:
                logger.error("โŒ All extraction methods failed")
                return {"text": "", "confidence": 0.0, "method": "all_failed"}
            
            # Merge results
            merged_result = self._merge_extraction_results(results)
            self.extraction_cache[doc_hash] = merged_result
            
            return merged_result
            
        except Exception as e:
            logger.error(f"๐Ÿ’ฅ Document extraction failed: {e}")
            return {"text": "", "confidence": 0.0, "method": "failed", "error": str(e)}
    
    async def _download_document(self, url: str, max_retries: int = 3) -> Optional[BytesIO]:
        """Download document with retries"""
        for attempt in range(max_retries):
            try:
                response = requests.get(url, timeout=30, stream=True)
                response.raise_for_status()
                return BytesIO(response.content)
            except Exception as e:
                logger.warning(f"โš ๏ธ Download attempt {attempt + 1} failed: {e}")
                if attempt == max_retries - 1:
                    return None
                await asyncio.sleep(1)
        return None
    
    async def _extract_with_pymupdf(self, pdf_bytes: BytesIO) -> Dict:
        """Enhanced PyMuPDF extraction"""
        try:
            pdf_bytes.seek(0)
            doc = fitz.open(stream=pdf_bytes.read(), filetype="pdf")
            
            full_text = ""
            page_count = 0
            
            for page_num in range(min(doc.page_count, 50)):
                try:
                    page = doc[page_num]
                    page_count += 1
                    
                    # Extract text
                    text = page.get_text("text")
                    
                    if not text.strip():
                        # Try dict method for complex layouts
                        blocks = page.get_text("dict")
                        page_text = ""
                        for block in blocks.get("blocks", []):
                            if "lines" in block:
                                for line in block["lines"]:
                                    for span in line.get("spans", []):
                                        page_text += span.get("text", "") + " "
                                    page_text += "\n"
                        text = page_text
                    
                    if text and text.strip():
                        cleaned_text = re.sub(r'\s+', ' ', text.strip())
                        full_text += cleaned_text + "\n"
                
                except Exception as e:
                    logger.warning(f"โš ๏ธ Page {page_num} extraction failed: {e}")
                    continue
            
            doc.close()
            
            confidence = 0.95 if len(full_text) > 500 else 0.3
            confidence *= min(1.0, page_count / 10)
            
            return {
                "method": "pymupdf",
                "text": full_text.strip(),
                "confidence": confidence,
                "pages_processed": page_count
            }
            
        except Exception as e:
            logger.warning(f"โš ๏ธ PyMuPDF extraction failed: {e}")
            return {"method": "pymupdf", "text": "", "confidence": 0.0}
    
    async def _extract_with_pdfplumber(self, pdf_bytes: BytesIO) -> Dict:
        """PDFPlumber extraction with table handling"""
        try:
            pdf_bytes.seek(0)
            full_text = ""
            page_count = 0
            table_count = 0
            
            with pdfplumber.open(pdf_bytes) as pdf:
                for page in pdf.pages[:30]:
                    try:
                        page_count += 1
                        
                        # Extract text
                        text = page.extract_text()
                        if text:
                            cleaned_text = re.sub(r'\s+', ' ', text.strip())
                            full_text += cleaned_text + "\n"
                        
                        # Extract tables
                        tables = page.extract_tables()
                        for table in tables:
                            if table:
                                table_count += 1
                                table_text = "\n[TABLE START]\n"
                                for row in table:
                                    if row:
                                        clean_row = [str(cell).strip() if cell else "" for cell in row]
                                        table_text += " | ".join(clean_row) + "\n"
                                table_text += "[TABLE END]\n"
                                full_text += table_text
                    
                    except Exception as e:
                        logger.warning(f"โš ๏ธ PDFPlumber page error: {e}")
                        continue
            
            confidence = 0.9 if len(full_text) > 500 else 0.3
            if table_count > 0:
                confidence += 0.1
            
            return {
                "method": "pdfplumber",
                "text": full_text.strip(),
                "confidence": confidence,
                "pages_processed": page_count,
                "tables_found": table_count
            }
            
        except Exception as e:
            logger.warning(f"โš ๏ธ PDFPlumber extraction failed: {e}")
            return {"method": "pdfplumber", "text": "", "confidence": 0.0}
    
    async def _extract_with_ocr(self, pdf_bytes: BytesIO) -> Dict:
        """OCR extraction for image-heavy PDFs"""
        try:
            pdf_bytes.seek(0)
            doc = fitz.open(stream=pdf_bytes.read(), filetype="pdf")
            
            full_text = ""
            max_pages = min(doc.page_count, 3)
            pages_processed = 0
            
            for page_num in range(max_pages):
                try:
                    page = doc[page_num]
                    pages_processed += 1
                    
                    # Convert to image
                    pix = page.get_pixmap(matrix=fitz.Matrix(2, 2))
                    img_data = pix.tobytes("png")
                    img = Image.open(BytesIO(img_data))
                    
                    # Preprocessing
                    img_cv = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
                    gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
                    denoised = cv2.fastNlMeansDenoising(gray)
                    _, thresh = cv2.threshold(denoised, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                    
                    # OCR
                    custom_config = r'--oem 3 --psm 6 -l eng'
                    text = pytesseract.image_to_string(thresh, config=custom_config)
                    
                    if text and text.strip():
                        cleaned_text = re.sub(r'\s+', ' ', text.strip())
                        full_text += cleaned_text + "\n"
                
                except Exception as e:
                    logger.warning(f"โš ๏ธ OCR page {page_num} failed: {e}")
                    continue
            
            doc.close()
            confidence = 0.7 if len(full_text) > 200 else 0.2
            
            return {
                "method": "ocr",
                "text": full_text.strip(),
                "confidence": confidence,
                "pages_processed": pages_processed
            }
            
        except Exception as e:
            logger.warning(f"โš ๏ธ OCR extraction failed: {e}")
            return {"method": "ocr", "text": "", "confidence": 0.0}
    
    def _merge_extraction_results(self, results: List[Dict]) -> Dict:
        """Intelligently merge extraction results"""
        if not results:
            return {"text": "", "confidence": 0.0, "method": "none"}
        
        # Sort by confidence
        results.sort(key=lambda x: x.get('confidence', 0), reverse=True)
        primary = results[0]
        merged_text = primary['text']
        
        # Merge unique content from other results
        for result in results[1:]:
            if result['confidence'] > 0.4:
                primary_sentences = set(s.strip() for s in merged_text.split('.') if len(s.strip()) > 20)
                result_sentences = set(s.strip() for s in result['text'].split('.') if len(s.strip()) > 20)
                unique_sentences = result_sentences - primary_sentences
                
                if unique_sentences:
                    additional_text = ". ".join(list(unique_sentences)[:10])
                    if additional_text:
                        merged_text += "\n\n" + additional_text
        
        final_confidence = primary['confidence']
        if len(results) > 1:
            final_confidence = min(0.98, final_confidence + 0.1)
        
        return {
            "text": merged_text,
            "confidence": final_confidence,
            "method": f"merged_{len(results)}_methods",
            "sources": [r['method'] for r in results],
            "total_length": len(merged_text)
        }
    
    async def create_smart_chunks(self, text: str, metadata: Dict, chunk_size: int = 1000, overlap: int = 200) -> List[Dict]:
        """Create intelligent text chunks with semantic boundaries"""
        if not text:
            return []
        
        # Split into sentences first
        sentences = re.split(r'(?<=[.!?])\s+', text)
        chunks = []
        current_chunk = ""
        current_sentences = []
        
        for sentence in sentences:
            # Check if adding this sentence would exceed chunk size
            if len(current_chunk + sentence) > chunk_size and current_chunk:
                # Create chunk
                chunk_id = hashlib.md5(current_chunk.encode()).hexdigest()[:16]
                chunks.append({
                    "chunk_id": chunk_id,
                    "content": current_chunk.strip(),
                    "metadata": {
                        "sentence_count": len(current_sentences),
                        "char_count": len(current_chunk),
                        "source_confidence": metadata.get('confidence', 0.0)
                    }
                })
                
                # Start new chunk with overlap
                if overlap > 0 and len(current_sentences) > 1:
                    overlap_sentences = current_sentences[-(overlap // 100):]  # Approximate overlap
                    current_chunk = " ".join(overlap_sentences) + " " + sentence
                    current_sentences = overlap_sentences + [sentence]
                else:
                    current_chunk = sentence
                    current_sentences = [sentence]
            else:
                current_chunk += " " + sentence if current_chunk else sentence
                current_sentences.append(sentence)
        
        # Add final chunk
        if current_chunk:
            chunk_id = hashlib.md5(current_chunk.encode()).hexdigest()[:16]
            chunks.append({
                "chunk_id": chunk_id,
                "content": current_chunk.strip(),
                "metadata": {
                    "sentence_count": len(current_sentences),
                    "char_count": len(current_chunk),
                    "source_confidence": metadata.get('confidence', 0.0)
                }
            })
        
        logger.info(f"๐Ÿ“ Created {len(chunks)} intelligent chunks")
        return chunks
    
    async def generate_embeddings(self, chunks: List[Dict]) -> List[Dict]:
        """Generate embeddings for document chunks"""
        if not chunks:
            return []
        
        try:
            # Extract text content for embedding
            texts = [chunk["content"] for chunk in chunks]
            
            # Generate embeddings
            embeddings = self.embedding_model.encode(texts, convert_to_numpy=True)
            
            # Add embeddings to chunks
            for i, chunk in enumerate(chunks):
                chunk["embedding"] = embeddings[i].tolist()
            
            logger.info(f"๐Ÿ”ข Generated embeddings for {len(chunks)} chunks")
            return chunks
            
        except Exception as e:
            logger.error(f"โŒ Embedding generation failed: {e}")
            return chunks

# ============================================================================