File size: 11,954 Bytes
d19f277
 
 
 
 
 
 
3f4637e
d19f277
 
 
 
2d0f5ab
6b8b552
7bb5c98
6b8b552
7bb5c98
 
 
 
 
 
 
 
 
 
 
 
2d0f5ab
 
d6c6abe
7efb501
d19f277
7efb501
3f4637e
 
d19f277
 
 
7a06886
6b8b552
 
2d0f5ab
7bb5c98
6b8b552
7bb5c98
7a06886
 
 
7bb5c98
7a06886
 
 
6b8b552
7a06886
6b8b552
7a06886
 
6b8b552
7a06886
 
6b8b552
7a06886
6b8b552
 
 
7a06886
6b8b552
 
 
 
7bb5c98
6b8b552
 
 
 
7bb5c98
6b8b552
 
 
 
 
 
 
 
 
 
7bb5c98
6b8b552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d19f277
7efb501
6b8b552
7efb501
6b8b552
 
 
 
340c03d
6b8b552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d19f277
6b8b552
 
 
 
340c03d
6b8b552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d19f277
6b8b552
340c03d
6b8b552
 
 
 
 
 
 
 
7bb5c98
6b8b552
d19f277
7efb501
6b8b552
7efb501
7a06886
7efb501
 
3f4637e
d6c6abe
6b8b552
d6c6abe
 
6b8b552
7a06886
 
6b8b552
 
7a06886
6b8b552
 
 
 
 
7a06886
6b8b552
 
 
7a06886
6b8b552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2d0f5ab
 
7a06886
2d0f5ab
7bb5c98
6b8b552
2d0f5ab
6b8b552
 
 
 
 
 
baeb9d2
2d0f5ab
7bb5c98
 
6b8b552
 
 
 
7bb5c98
6b8b552
d19f277
7efb501
6b8b552
7efb501
6b8b552
 
7bb5c98
6b8b552
 
 
 
 
 
 
 
3f4637e
6b8b552
 
d19f277
 
7bb5c98
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
import os
import tempfile
import gradio as gr
import json
import pandas as pd
import requests
from bs4 import BeautifulSoup
from docx import Document
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
from transformers import pipeline
import logging
import io

# PDF libraries
try:
    from pypdf import PdfReader
    HAS_PYPDF = True
except:
    HAS_PYPDF = False

try:
    import pdfplumber
    HAS_PDFPLUMBER = True
except:
    HAS_PDFPLUMBER = False

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# ==============================
# CONFIG
# ==============================
HF_GENERATION_MODEL = os.environ.get("HF_GENERATION_MODEL", "google/flan-t5-large")
EMBEDDING_MODEL_NAME = "sentence-transformers/paraphrase-MiniLM-L3-v2"
INDEX_PATH = "faiss_index.index"
METADATA_PATH = "metadata.json"

# Initialize models
embed_model = SentenceTransformer(EMBEDDING_MODEL_NAME)
gen_pipeline = pipeline("text2text-generation", model=HF_GENERATION_MODEL, device=-1)

# ==============================
# SIMPLE TEXT SPLITTER
# ==============================
def simple_text_splitter(text, chunk_size=1000, chunk_overlap=100):
    if len(text) <= chunk_size:
        return [text.strip()]
    
    chunks = []
    start = 0
    while start < len(text):
        end = min(start + chunk_size, len(text))
        chunk = text[start:end].strip()
        if len(chunk) > 50:
            chunks.append(chunk)
        start = end - chunk_overlap
    return [c for c in chunks if len(c) > 20]

# ==============================
# CORRECTED FILE HANDLING FOR GRADIO
# ==============================
def get_file_data(file_obj):
    """Handle different Gradio file formats correctly"""
    debug = []
    
    # Method 1: File has .name attribute (temp file path)
    if hasattr(file_obj, 'name') and file_obj.name:
        debug.append(f"Using file path: {file_obj.name}")
        return file_obj.name, "path"
    
    # Method 2: File has .data attribute (base64 or bytes)
    if hasattr(file_obj, 'data') and file_obj.data:
        debug.append(f"Using file.data: {len(file_obj.data)} bytes")
        return file_obj.data, "bytes"
    
    # Method 3: Try to read as bytes
    try:
        if hasattr(file_obj, 'read'):
            file_obj.seek(0)  # Reset file pointer
            data = file_obj.read()
            if data:
                debug.append(f"Read {len(data)} bytes from file object")
                return data, "read"
    except Exception as e:
        debug.append(f"Read failed: {e}")
    
    # Method 4: Check if it's a dict with content
    if isinstance(file_obj, dict):
        if 'data' in file_obj and file_obj['data']:
            debug.append(f"Using dict data: {len(file_obj['data'])} bytes")
            return file_obj['data'], "dict"
        if 'name' in file_obj and file_obj['name']:
            debug.append(f"Using dict path: {file_obj['name']}")
            return file_obj['name'], "dict_path"
    
    # Method 5: String path
    if isinstance(file_obj, str) and os.path.exists(file_obj):
        debug.append(f"Using string path: {file_obj}")
        return file_obj, "string_path"
    
    debug.append("❌ No valid file data found")
    return None, debug

# ==============================
# PDF EXTRACTION
# ==============================
def extract_pdf_text(file_data, source_type, debug_info):
    """Extract text from PDF using multiple methods"""
    temp_path = None
    
    try:
        # If we have a file path, use it directly
        if source_type in ["path", "string_path", "dict_path"]:
            file_path = file_data
            if not os.path.exists(file_path):
                debug_info.append(f"❌ File path doesn't exist: {file_path}")
                return "File not found"
            
            # Try pdftotext first (if available)
            try:
                import subprocess
                result = subprocess.run(['pdftotext', file_path, '-'], 
                                      capture_output=True, text=True, timeout=15)
                if result.returncode == 0 and len(result.stdout.strip()) > 30:
                    debug_info.append(f"βœ… pdftotext: {len(result.stdout)} chars")
                    return result.stdout
            except:
                pass
        
        # Create temp file from bytes
        if source_type in ["bytes", "read", "dict"]:
            temp_path = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf').name
            with open(temp_path, 'wb') as f:
                if isinstance(file_data, str):
                    f.write(file_data.encode('latin1'))  # PDFs are binary
                else:
                    f.write(file_data)
            file_path = temp_path
            debug_info.append(f"Created temp file: {temp_path}")
        
        # Try pdfplumber
        if HAS_PDFPLUMBER:
            try:
                with pdfplumber.open(file_path) as pdf:
                    text = ""
                    for i, page in enumerate(pdf.pages[:5]):
                        page_text = page.extract_text()
                        if page_text:
                            text += page_text + "\n"
                    if len(text.strip()) > 50:
                        debug_info.append(f"βœ… pdfplumber: {len(text)} chars")
                        return text
            except Exception as e:
                debug_info.append(f"pdfplumber failed: {e}")
        
        # Try pypdf
        if HAS_PYPDF:
            try:
                reader = PdfReader(file_path)
                text = ""
                for i, page in enumerate(reader.pages[:3]):
                    try:
                        page_text = page.extract_text()
                        if page_text and page_text.strip():
                            text += page_text + "\n"
                    except:
                        continue
                if len(text.strip()) > 30:
                    debug_info.append(f"βœ… pypdf: {len(text)} chars")
                    return text
            except Exception as e:
                debug_info.append(f"pypdf failed: {e}")
        
        return "No text extracted - likely scanned PDF images"
        
    finally:
        if temp_path and os.path.exists(temp_path):
            try:
                os.unlink(temp_path)
            except:
                pass

# ==============================
# OTHER EXTRACTIONS
# ==============================
def extract_docx_text(file_data, source_type, debug_info):
    try:
        if source_type == "path":
            doc = Document(file_data)
        else:
            # Write to temp file
            with tempfile.NamedTemporaryFile(delete=False, suffix='.docx') as tmp:
                if isinstance(file_data, bytes):
                    tmp.write(file_data)
                tmp_path = tmp.name
            doc = Document(tmp_path)
            os.unlink(tmp_path)
        
        text = "\n\n".join([p.text.strip() for p in doc.paragraphs if p.text.strip()])
        if len(text) > 20:
            return text
        return "No text in DOCX"
    except Exception as e:
        return f"DOCX error: {e}"

def extract_text_file(file_data, source_type, debug_info):
    try:
        if source_type == "path":
            with open(file_data, 'r', encoding='utf-8', errors='ignore') as f:
                return f.read()
        else:
            # Decode bytes
            if isinstance(file_data, bytes):
                return file_data.decode('utf-8', errors='ignore')
            return str(file_data)
    except:
        return "Text extraction failed"

# ==============================
# MAIN INGESTION
# ==============================
def ingest_sources(files, urls=""):
    docs = []
    metadata = []
    debug_info = []
    
    # Clear existing
    for path in [INDEX_PATH, METADATA_PATH]:
        if os.path.exists(path):
            os.remove(path)
    
    # Process files
    for i, file_obj in enumerate(files or []):
        debug_info.append(f"\nπŸ“„ Processing file {i+1}")
        
        # Get file data correctly
        file_data, source_info = get_file_data(file_obj)
        if isinstance(source_info, list):
            debug_info.extend(source_info)
            continue
        
        if not file_data:
            debug_info.append("❌ No file data")
            continue
        
        # Get filename and extension
        filename = getattr(file_obj, 'name', f'file_{i+1}')
        if isinstance(filename, bytes):
            filename = filename.decode('utf-8', errors='ignore')
        ext = os.path.splitext(filename.lower())[1] if filename else ''
        
        debug_info.append(f"File: {filename}, Type: {source_info}")
        
        # Extract text
        text = ""
        if ext == '.pdf':
            text = extract_pdf_text(file_data, source_info, debug_info)
        elif ext in ['.docx', '.doc']:
            text = extract_docx_text(file_data, source_info, debug_info)
        elif ext in ['.txt', '.md']:
            text = extract_text_file(file_data, source_info, debug_info)
        else:
            debug_info.append(f"Unknown extension: {ext}")
            continue
        
        # Preview
        preview = text[:100].replace('\n', ' ').strip()
        if len(preview) > 80:
            preview = preview[:80] + "..."
        debug_info.append(f"Extracted {len(text)} chars")
        debug_info.append(f"Preview: '{preview}'")
        
        # Create chunks
        if len(text.strip()) > 30:
            chunks = simple_text_splitter(text)
            for j, chunk in enumerate(chunks):
                docs.append(chunk)
                metadata.append({
                    "source": filename,
                    "chunk": j,
                    "text": chunk
                })
            debug_info.append(f"βœ… {len(chunks)} chunks created")
        else:
            debug_info.append("⚠️ Insufficient content")
    
    debug_info.append(f"\nπŸ“Š Total: {len(docs)} chunks")
    
    if docs:
        embeddings = embed_model.encode(docs)
        index = faiss.IndexFlatL2(embeddings.shape[1])
        index.add(embeddings)
        faiss.write_index(index, INDEX_PATH)
        with open(METADATA_PATH, 'w') as f:
            json.dump(metadata, f)
        return f"βœ… SUCCESS: {len(docs)} chunks!"
    
    return "❌ No content.\n\n" + "\n".join(debug_info[-15:])

# ==============================
# RETRIEVAL & GENERATION
# ==============================
def retrieve_topk(query, k=3):
    if not os.path.exists(INDEX_PATH):
        return []
    q_emb = embed_model.encode([query])
    index = faiss.read_index(INDEX_PATH)
    D, I = index.search(q_emb, k)
    with open(METADATA_PATH, 'r') as f:
        metadata = json.load(f)
    return [metadata[i] for i in I[0] if i < len(metadata)]

def ask_prompt(query):
    hits = retrieve_topk(query)
    if not hits:
        return "No documents found."
    context = "\n\n".join([h['text'][:600] for h in hits])
    prompt = f"Context: {context}\nQuestion: {query}\nAnswer:"
    result = gen_pipeline(prompt, max_length=300)[0]['generated_text']
    sources = [f"{h['source']} (chunk {h['chunk']})" for h in hits]
    return f"{result}\n\nSources:\n" + "\n".join(sources)

# ==============================
# UI
# ==============================
with gr.Blocks() as demo:
    gr.Markdown("# πŸ” Document QA")
    with gr.Row():
        with gr.Column():
            file_input = gr.File(file_count="multiple")
            ingest_btn = gr.Button("Ingest", variant="primary")
            status = gr.Textbox(lines=15)
        with gr.Column():
            query_input = gr.Textbox(label="Question")
            ask_btn = gr.Button("Ask")
            answer = gr.Textbox(lines=10)
    
    ingest_btn.click(ingest_sources, [file_input, gr.State("")], status)
    ask_btn.click(ask_prompt, query_input, answer)

if __name__ == "__main__":
    demo.launch()