File size: 8,002 Bytes
18a68e7
 
 
 
 
 
2e83ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a68e7
 
 
 
 
 
 
2e83ef9
18a68e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e83ef9
18a68e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
 
 
 
 
 
 
 
 
 
18a68e7
2e83ef9
 
 
18a68e7
2e83ef9
 
 
 
18a68e7
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18a68e7
2e83ef9
 
18a68e7
2e83ef9
 
 
 
 
 
 
 
 
 
18a68e7
2e83ef9
18a68e7
 
 
2e83ef9
18a68e7
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
 
 
2e83ef9
18a68e7
 
2e83ef9
 
18a68e7
2e83ef9
18a68e7
 
 
 
2e83ef9
18a68e7
 
 
 
 
 
 
 
 
 
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
import os
import pymupdf4llm
import pandas as pd
import tempfile
from typing import Dict, Any, Optional, List

# Import Langchain document loaders
from langchain_community.document_loaders import (
    PyMuPDFLoader,
    UnstructuredWordDocumentLoader,
    UnstructuredPowerPointLoader,
    UnstructuredExcelLoader,
    UnstructuredMarkdownLoader,
    UnstructuredHTMLLoader,
    UnstructuredXMLLoader,
    UnstructuredEmailLoader,
    UnstructuredFileLoader,
    UnstructuredEPubLoader,
    CSVLoader,
    TextLoader
)

def get_processor_for_file(file_path: str) -> Optional[callable]:
    """
    Determine the appropriate processor function for the given file type
    """
    file_extension = os.path.splitext(file_path)[1].lower()
    
    # Map file extensions to specific processor functions
    processors = {
        ".pdf": process_pdf,
        ".docx": process_docx,
        ".doc": process_docx,
        ".pptx": process_pptx,
        ".ppt": process_pptx,
        ".xlsx": process_xlsx,
        ".xls": process_xlsx,
        ".md": process_markdown,
        ".html": process_html,
        ".htm": process_html,
        ".xml": process_xml,
        ".msg": process_email,
        ".eml": process_email,
        ".epub": process_epub,
        ".txt": process_text,
        ".csv": process_csv,
        ".rtf": process_text,
        
        # Code files
        ".py": process_text,
        ".js": process_text,
        ".java": process_text,
        ".ts": process_text,
        ".tsx": process_text,
        ".jsx": process_text,
        ".c": process_text,
        ".cpp": process_text,
        ".h": process_text,
        ".cs": process_text,
        ".rb": process_text,
        ".go": process_text,
        ".rs": process_text,
        ".php": process_text,
        ".sql": process_text,
        ".css": process_text,
    }
    
    return processors.get(file_extension, process_generic)

def process_document(file_path: str) -> Optional[str]:
    """
    Process a document using the appropriate processor based on file type
    """
    processor = get_processor_for_file(file_path)
    if processor:
        return processor(file_path)
    return None

def process_pdf(file_path: str) -> str:
    """
    Process PDF documents using pymupdf4llm for better PDF handling
    """
    # For PDFs, we'll still use pymupdf4llm as it handles tables and images better
    pdf_processor = pymupdf4llm.PdfProcessor(file_path)
    
    # Extract text, tables, and images
    extracted_text = pdf_processor.extract_text()
    extracted_tables = pdf_processor.extract_tables()
    extracted_images = pdf_processor.extract_images()

    # Combine extracted content
    combined_content = []

    if extracted_text:
        combined_content.append(extracted_text)
    
    if extracted_tables:
        for table in extracted_tables:
            combined_content.append(str(table))
    
    if extracted_images:
        combined_content.append(f"Extracted {len(extracted_images)} images.")

    return "\n\n".join(combined_content)

def process_docx(file_path: str) -> str:
    """
    Process DOCX documents using Langchain's UnstructuredWordDocumentLoader
    """
    loader = UnstructuredWordDocumentLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_pptx(file_path: str) -> str:
    """
    Process PPTX documents using Langchain's UnstructuredPowerPointLoader
    """
    loader = UnstructuredPowerPointLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_xlsx(file_path: str) -> str:
    """
    Process XLSX documents using Langchain's UnstructuredExcelLoader
    """
    loader = UnstructuredExcelLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_markdown(file_path: str) -> str:
    """
    Process Markdown documents using Langchain's UnstructuredMarkdownLoader
    """
    loader = UnstructuredMarkdownLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_html(file_path: str) -> str:
    """
    Process HTML documents using Langchain's UnstructuredHTMLLoader
    """
    loader = UnstructuredHTMLLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_xml(file_path: str) -> str:
    """
    Process XML documents using Langchain's UnstructuredXMLLoader
    """
    loader = UnstructuredXMLLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_email(file_path: str) -> str:
    """
    Process email documents using Langchain's UnstructuredEmailLoader
    """
    loader = UnstructuredEmailLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_text(file_path: str) -> str:
    """
    Process text documents using Langchain's TextLoader
    """
    loader = TextLoader(file_path, encoding="utf-8")
    try:
        docs = loader.load()
        
        texts = [doc.page_content for doc in docs if doc.page_content]
        combined_text = "\n\n".join(texts)
        
        return combined_text
    except UnicodeDecodeError:
        # Try with a different encoding if utf-8 fails
        loader = TextLoader(file_path, encoding="latin-1")
        docs = loader.load()
        
        texts = [doc.page_content for doc in docs if doc.page_content]
        combined_text = "\n\n".join(texts)
        
        return combined_text

def process_csv(file_path: str) -> str:
    """
    Process CSV documents using Langchain's CSVLoader
    """
    loader = CSVLoader(file_path)
    docs = loader.load()
    
    # Create a formatted string representation of the CSV data
    rows = []
    if docs:
        # Get column names from metadata if available
        if hasattr(docs[0], 'metadata') and 'columns' in docs[0].metadata:
            rows.append(",".join(docs[0].metadata['columns']))
        
        # Add content rows
        for doc in docs:
            rows.append(doc.page_content)
    
    return "\n".join(rows)

def process_epub(file_path: str) -> str:
    """
    Process EPUB documents using Langchain's UnstructuredEPubLoader
    """
    loader = UnstructuredEPubLoader(file_path)
    docs = loader.load()
    
    texts = [doc.page_content for doc in docs if doc.page_content]
    combined_text = "\n\n".join(texts)
    
    return combined_text

def process_generic(file_path: str) -> str:
    """
    Generic document processor using Langchain's UnstructuredFileLoader
    """
    try:
        loader = UnstructuredFileLoader(file_path)
        docs = loader.load()
        
        texts = [doc.page_content for doc in docs if doc.page_content]
        combined_text = "\n\n".join(texts)
        
        return combined_text
    except Exception as e:
        # Fall back to basic text processing if UnstructuredFileLoader fails
        try:
            with open(file_path, 'r', encoding='utf-8') as f:
                return f.read()
        except Exception:
            # Try with a different encoding if utf-8 fails
            try:
                with open(file_path, 'r', encoding='latin-1') as f:
                    return f.read()
            except Exception as e2:
                raise Exception(f"Could not process file: {str(e)} / {str(e2)}")