File size: 3,693 Bytes
407b660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pytesseract
from pdf2image import convert_from_path
import fitz  # PyMuPDF
import docx
from PIL import Image
import io

def extract_text_from_pdf(file_path):
    """
    Extract text from PDF using PyMuPDF (fitz).
    Falls back to OCR if needed.
    """
    try:
        # First try direct text extraction
        doc = fitz.open(file_path)
        text = ""
        for page in doc:
            text += page.get_text()

        # If no text was extracted, try OCR
        if not text.strip():
            return extract_text_with_ocr(file_path)

        return text
    except Exception as e:
        print(f"Error extracting text from PDF: {e}")
        return extract_text_with_ocr(file_path)

def extract_text_with_ocr(file_path):
    """
    Extract text using OCR for scanned documents.
    """
    try:
        # Convert PDF to images
        images = convert_from_path(file_path)
        text = ""

        # Process each page with OCR
        for img in images:
            text += pytesseract.image_to_string(img)

        return text
    except Exception as e:
        print(f"OCR extraction error: {e}")
        return ""

def extract_text_from_image(file_path):
    """
    Extract text from images using OCR.
    """
    try:
        img = Image.open(file_path)
        text = pytesseract.image_to_string(img)
        return text
    except Exception as e:
        print(f"Image OCR error: {e}")
        return ""

def extract_text_from_docx(file_path):
    """
    Extract text from DOCX files.
    """
    try:
        doc = docx.Document(file_path)
        full_text = []
        for para in doc.paragraphs:
            full_text.append(para.text)
        return "\n".join(full_text)
    except Exception as e:
        print(f"DOCX extraction error: {e}")
        return ""

def extract_text_from_txt(file_path):
    """
    Extract text from plain text files.
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            return file.read()
    except UnicodeDecodeError:
        # Try with a different encoding if UTF-8 fails
        try:
            with open(file_path, 'r', encoding='latin-1') as file:
                return file.read()
        except Exception as e:
            print(f"Text file reading error: {e}")
            return ""
    except Exception as e:
        print(f"Text file reading error: {e}")
        return ""

def extract_text_from_file(file_path):
    """
    Extract text from a file based on its extension.
    """
    file_ext = os.path.splitext(file_path)[1].lower()

    if file_ext in ['.pdf']:
        return extract_text_from_pdf(file_path)
    elif file_ext in ['.png', '.jpg', '.jpeg']:
        return extract_text_from_image(file_path)
    elif file_ext in ['.docx', '.doc']:
        return extract_text_from_docx(file_path)
    elif file_ext in ['.txt']:
        return extract_text_from_txt(file_path)
    else:
        return "Unsupported file format"

def process_document(file_object, file_path):
    """
    Process an uploaded document, extract text, and prepare for vectorization.
    """
    try:
        # Extract text based on file type
        text_content = extract_text_from_file(file_path)

        # Basic document preprocessing
        if text_content:
            # Remove excessive whitespace
            text_content = ' '.join(text_content.split())

        return {
            "content": text_content,
            "success": bool(text_content),
            "error": "" if text_content else "Failed to extract text"
        }
    except Exception as e:
        return {
            "content": "",
            "success": False,
            "error": str(e)
        }