File size: 9,870 Bytes
0721a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Document processing module for loading and chunking documents.
Supports PDF, TXT, and MD files.
"""

import re
import os
from typing import List, Dict
from io import BytesIO
import pypdf


def clean_text(text: str) -> str:
    """Clean text by removing excessive whitespace and normalizing."""
    # Remove excessive whitespace
    text = re.sub(r'\s+', ' ', text)
    # Remove leading/trailing whitespace
    text = text.strip()
    return text


def estimate_tokens(text: str) -> int:
    """Rough token estimation: ~4 characters per token."""
    return len(text) // 4


def chunk_text(text: str, chunk_size: int = 800, overlap: int = 150) -> List[str]:
    """
    Chunk text into overlapping windows based on token count.
    
    Args:
        text: Input text to chunk
        chunk_size: Target token count per chunk
        overlap: Token overlap between chunks
    
    Returns:
        List of text chunks
    """
    if not text:
        return []
    
    # Estimate tokens and convert to character-based chunking
    # Approximate: 4 chars per token
    char_chunk_size = chunk_size * 4
    char_overlap = overlap * 4
    
    chunks = []
    start = 0
    text_length = len(text)
    
    while start < text_length:
        end = start + char_chunk_size
        
        # If this is not the last chunk, try to break at sentence boundary
        if end < text_length:
            # Look for sentence endings within the last 200 chars
            sentence_end = max(
                text.rfind('.', start, end),
                text.rfind('!', start, end),
                text.rfind('?', start, end),
                text.rfind('\n', start, end)
            )
            
            if sentence_end > start + char_chunk_size // 2:
                end = sentence_end + 1
        
        chunk = text[start:end].strip()
        if chunk:
            chunks.append(chunk)
        
        # Move start position with overlap
        start = end - char_overlap
        if start >= text_length:
            break
    
    return chunks


def load_pdf_from_path(file_path: str) -> str:
    """
    Load text from PDF file path.
    
    Args:
        file_path: Path to PDF file
    
    Returns:
        Extracted text content
    """
    try:
        with open(file_path, 'rb') as file:
            reader = pypdf.PdfReader(file)
            text_parts = []
            
            for page in reader.pages:
                text = page.extract_text()
                if text:
                    text_parts.append(text)
        
        full_text = '\n\n'.join(text_parts)
        return clean_text(full_text)
    except Exception as e:
        raise ValueError(f"Error loading PDF {file_path}: {str(e)}")


def load_pdf(file_content: bytes, filename: str) -> str:
    """
    Load text from PDF file bytes.
    
    Args:
        file_content: PDF file bytes
        filename: Original filename
    
    Returns:
        Extracted text content
    """
    try:
        pdf_file = BytesIO(file_content)
        reader = pypdf.PdfReader(pdf_file)
        text_parts = []
        
        for page in reader.pages:
            text = page.extract_text()
            if text:
                text_parts.append(text)
        
        full_text = '\n\n'.join(text_parts)
        return clean_text(full_text)
    except Exception as e:
        raise ValueError(f"Error loading PDF {filename}: {str(e)}")


def load_text_file_from_path(file_path: str) -> str:
    """
    Load text from TXT or MD file path.
    
    Args:
        file_path: Path to text file
    
    Returns:
        Text content
    """
    try:
        # Try UTF-8 first, fallback to latin-1
        try:
            with open(file_path, 'r', encoding='utf-8') as file:
                text = file.read()
        except UnicodeDecodeError:
            with open(file_path, 'r', encoding='latin-1') as file:
                text = file.read()
        
        return clean_text(text)
    except Exception as e:
        raise ValueError(f"Error loading text file {file_path}: {str(e)}")


def load_text_file(file_content: bytes, filename: str) -> str:
    """
    Load text from TXT or MD file bytes.
    
    Args:
        file_content: File bytes
        filename: Original filename
    
    Returns:
        Text content
    """
    try:
        # Try UTF-8 first, fallback to latin-1
        try:
            text = file_content.decode('utf-8')
        except UnicodeDecodeError:
            text = file_content.decode('latin-1')
        
        return clean_text(text)
    except Exception as e:
        raise ValueError(f"Error loading text file {filename}: {str(e)}")


def process_documents(uploaded_files: List) -> List[Dict]:
    """
    Process uploaded files and return chunked documents.
    Works with Gradio file objects (which are file paths as strings).
    
    Args:
        uploaded_files: List of file paths (strings) from Gradio
    
    Returns:
        List of dictionaries with 'text', 'source', and 'chunk_id' keys
    """
    all_chunks = []
    
    print(f"πŸ“„ Processing {len(uploaded_files)} uploaded files...")
    
    for file_path in uploaded_files:
        try:
            # Extract filename from path
            filename = os.path.basename(file_path)
            file_extension = filename.split('.')[-1].lower()
            
            print(f"πŸ”„ Processing: {filename}")
            
            # Load document based on type
            if file_extension == 'pdf':
                text = load_pdf_from_path(file_path)
            elif file_extension in ['txt', 'md']:
                text = load_text_file_from_path(file_path)
            else:
                print(f"⚠️ Skipping unsupported file: {filename}")
                continue  # Skip unsupported files
            
            if not text:
                print(f"⚠️ No text extracted from: {filename}")
                continue
            
            # Chunk the text
            chunks = chunk_text(text, chunk_size=800, overlap=150)
            
            # Store chunks with metadata
            for idx, chunk in enumerate(chunks):
                all_chunks.append({
                    'text': chunk,
                    'source': filename,
                    'chunk_id': f"{filename}_chunk_{idx}",
                    'chunk_index': idx
                })
            
            print(f"βœ… Created {len(chunks)} chunks from {filename}")
            
        except Exception as e:
            print(f"❌ Error processing {file_path}: {str(e)}")
            continue
    
    print(f"βœ… Total chunks created: {len(all_chunks)}")
    return all_chunks


# Additional functions for local directory processing
def get_available_files(data_dir: str = "data") -> List[str]:
    """
    Get list of available files in the data directory.
    
    Args:
        data_dir: Directory path containing documents
    
    Returns:
        List of filenames
    """
    if not os.path.exists(data_dir):
        return []
    
    supported_extensions = {'.pdf', '.txt', '.md'}
    files = []
    
    for filename in os.listdir(data_dir):
        file_path = os.path.join(data_dir, filename)
        if os.path.isfile(file_path):
            _, ext = os.path.splitext(filename)
            if ext.lower() in supported_extensions:
                files.append(filename)
    
    return sorted(files)


def process_documents_from_directory(data_dir: str = "data") -> List[Dict]:
    """
    Process all documents in the data directory and return chunked documents.
    
    Args:
        data_dir: Directory path containing documents
    
    Returns:
        List of dictionaries with 'text', 'source', and 'chunk_id' keys
    """
    if not os.path.exists(data_dir):
        raise FileNotFoundError(f"Data directory '{data_dir}' not found")
    
    all_chunks = []
    supported_extensions = {'.pdf', '.txt', '.md'}
    
    # Get all files in the data directory
    files = []
    for filename in os.listdir(data_dir):
        file_path = os.path.join(data_dir, filename)
        if os.path.isfile(file_path):
            _, ext = os.path.splitext(filename)
            if ext.lower() in supported_extensions:
                files.append((filename, file_path))
    
    if not files:
        raise ValueError(f"No supported files found in '{data_dir}' directory")
    
    print(f"πŸ“„ Processing {len(files)} files from '{data_dir}' directory...")
    
    for filename, file_path in files:
        try:
            file_extension = os.path.splitext(filename)[1].lower()
            
            # Load document based on type
            if file_extension == '.pdf':
                text = load_pdf_from_path(file_path)
                print(f"βœ… Loaded PDF: {filename}")
            elif file_extension in ['.txt', '.md']:
                text = load_text_file_from_path(file_path)
                print(f"βœ… Loaded text file: {filename}")
            else:
                continue  # Skip unsupported files
            
            if not text:
                print(f"⚠️ No text extracted from: {filename}")
                continue
            
            # Chunk the text
            chunks = chunk_text(text, chunk_size=800, overlap=150)
            
            # Store chunks with metadata
            for idx, chunk in enumerate(chunks):
                all_chunks.append({
                    'text': chunk,
                    'source': filename,
                    'chunk_id': f"{filename}_chunk_{idx}",
                    'chunk_index': idx
                })
            
            print(f"   β†’ Created {len(chunks)} chunks")
            
        except Exception as e:
            print(f"❌ Error processing {filename}: {str(e)}")
            continue
    
    print(f"βœ… Total chunks created: {len(all_chunks)}")
    return all_chunks