File size: 18,279 Bytes
8099442
 
16691ee
8099442
16691ee
8099442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16691ee
 
 
 
 
 
 
 
 
 
 
 
8099442
 
 
 
16691ee
 
 
8099442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16691ee
8099442
 
 
 
 
 
 
16691ee
 
 
 
 
 
 
8099442
 
 
 
16691ee
8099442
16691ee
8099442
16691ee
 
8099442
 
16691ee
8099442
 
16691ee
 
8099442
 
 
 
16691ee
 
 
 
 
 
 
 
8099442
 
 
16691ee
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
 
16691ee
8099442
 
16691ee
 
8099442
16691ee
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
 
16691ee
8099442
 
16691ee
8099442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16691ee
 
 
 
 
 
 
 
 
 
 
8099442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
16691ee
 
 
 
 
8099442
16691ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8099442
 
 
 
 
 
 
 
16691ee
8099442
 
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import streamlit as st
import os
import tempfile
from pathlib import Path
import torch
from pdf_parser import PDFParser
from embedder import ChromaDBManager
from rag_pipeline import RAGPipeline


# ============================================================================
# PAGE CONFIGURATION
# ============================================================================

st.set_page_config(
    page_title="Multimodal PDF RAG System",
    page_icon="๐Ÿ“„",
    layout="wide",
    initial_sidebar_state="expanded"
)

# ============================================================================
# CUSTOM STYLING
# ============================================================================

st.markdown("""
<style>
.main {
    padding: 2rem;
}
.stTabs [data-baseweb="tab-list"] button [data-testid="stMarkdownContainer"] p {
    font-size: 1.2rem;
}
.upload-area {
    border: 2px dashed #ccc;
    border-radius: 5px;
    padding: 20px;
    text-align: center;
}
.success-box {
    background-color: #d4edda;
    border: 1px solid #28a745;
    border-radius: 4px;
    padding: 10px;
    margin: 10px 0;
}
.error-box {
    background-color: #f8d7da;
    border: 1px solid #f5c6cb;
    border-radius: 4px;
    padding: 10px;
    margin: 10px 0;
}
</style>
""", unsafe_allow_html=True)

# ============================================================================
# SESSION STATE INITIALIZATION
# ============================================================================

@st.cache_resource
def initialize_system():
    """Initialize RAG system components once."""
    try:
        parser = PDFParser(extraction_dir="./pdf_extractions")
        chroma = ChromaDBManager(db_dir="./chroma_db")
        device = "cuda" if torch.cuda.is_available() else "cpu"
        rag = RAGPipeline(chroma, device=device)
        return parser, chroma, rag, device
    except Exception as e:
        st.error(f"Error initializing system: {e}")
        return None, None, None, None


# Initialize
pdf_parser, chroma_manager, rag_pipeline, device = initialize_system()

if pdf_parser is None:
    st.error("Failed to initialize RAG system. Please check your installation.")
    st.stop()

# Initialize session state for uploaded files
if 'uploaded_files' not in st.session_state:
    st.session_state.uploaded_files = []

if 'processing_status' not in st.session_state:
    st.session_state.processing_status = {}

# ============================================================================
# MAIN UI
# ============================================================================

st.title("๐Ÿ“„ Multimodal PDF RAG System")
st.markdown("**Local AI-powered document analysis with Qwen2.5-VL and ChromaDB**")
st.markdown("*Upload PDFs directly and ask questions about them*")

# Create main tabs
tab_upload, tab_query, tab_manage, tab_about = st.tabs(["๐Ÿ“ค Upload PDFs", "๐Ÿ” Ask Questions", "๐Ÿ› ๏ธ Manage", "โ„น๏ธ About"])

# ============================================================================
# TAB 1: UPLOAD PDFs
# ============================================================================

with tab_upload:
    st.header("๐Ÿ“ค Upload PDF Documents")
    
    col1, col2 = st.columns([3, 1])
    
    with col1:
        st.markdown("**Upload your PDF files below. They will be automatically processed and stored.**")
        
        # File uploader
        uploaded_files = st.file_uploader(
            "Choose PDF files",
            type=["pdf"],
            accept_multiple_files=True,
            help="You can upload multiple PDF files at once"
        )
    
    with col2:
        st.info(f"๐Ÿ“Š Documents in DB: {chroma_manager.get_collection_info()['document_count']}")
    
    # Process uploaded files
    if uploaded_files:
        st.divider()
        st.subheader("Processing Uploaded Files")
        
        # Create a temporary directory for uploads
        temp_dir = tempfile.mkdtemp()
        
        progress_bar = st.progress(0)
        status_text = st.empty()
        results_container = st.container()
        
        total_files = len(uploaded_files)
        processed_files = []
        failed_files = []
        
        for idx, uploaded_file in enumerate(uploaded_files):
            try:
                # Update progress
                status_text.text(f"Processing {idx + 1}/{total_files}: {uploaded_file.name}")
                
                # Save uploaded file to temp directory
                temp_file_path = os.path.join(temp_dir, uploaded_file.name)
                with open(temp_file_path, "wb") as f:
                    f.write(uploaded_file.getbuffer())
                
                # Process PDF
                with st.spinner(f"Extracting content from {uploaded_file.name}..."):
                    try:
                        result = pdf_parser.process_pdf(temp_file_path)
                        
                        # Add to ChromaDB
                        chroma_manager.add_documents([result])
                        
                        processed_files.append({
                            'name': uploaded_file.name,
                            'size': uploaded_file.size,
                            'text_length': len(result.get('text', '')),
                            'tables': len(result.get('tables', [])),
                            'images': len(result.get('images', []))
                        })
                        
                        st.success(f"โœ… {uploaded_file.name} processed successfully")
                    
                    except Exception as e:
                        failed_files.append({
                            'name': uploaded_file.name,
                            'error': str(e)
                        })
                        st.error(f"โŒ Error processing {uploaded_file.name}: {e}")
                
                # Update progress
                progress_bar.progress((idx + 1) / total_files)
            
            except Exception as e:
                failed_files.append({
                    'name': uploaded_file.name,
                    'error': str(e)
                })
                st.error(f"โŒ Error with {uploaded_file.name}: {e}")
        
        # Show summary
        st.divider()
        st.subheader("Upload Summary")
        
        col1, col2, col3 = st.columns(3)
        
        with col1:
            st.metric("Successfully Processed", len(processed_files))
        
        with col2:
            st.metric("Failed", len(failed_files))
        
        with col3:
            collection_info = chroma_manager.get_collection_info()
            st.metric("Total in Database", collection_info['document_count'])
        
        # Show details of processed files
        if processed_files:
            st.markdown("#### โœ… Processed Files:")
            for file_info in processed_files:
                col1, col2, col3, col4 = st.columns(4)
                with col1:
                    st.text(file_info['name'])
                with col2:
                    st.text(f"{file_info['size'] / 1024:.1f} KB")
                with col3:
                    st.text(f"{file_info['text_length']:,} chars")
                with col4:
                    st.text(f"{file_info['tables']} tables, {file_info['images']} imgs")
        
        # Show failed files
        if failed_files:
            st.markdown("#### โŒ Failed Files:")
            for file_info in failed_files:
                st.error(f"**{file_info['name']}**: {file_info['error']}")

# ============================================================================
# TAB 2: ASK QUESTIONS
# ============================================================================

with tab_query:
    st.header("๐Ÿ” Ask Questions About Your Documents")
    
    collection_info = chroma_manager.get_collection_info()
    
    if collection_info['document_count'] == 0:
        st.warning("โš ๏ธ No documents uploaded yet. Please upload PDFs in the 'Upload PDFs' tab first.")
    else:
        st.success(f"โœ… {collection_info['document_count']} documents in database")
        
        col1, col2, col3 = st.columns([2, 1, 1])
        
        with col1:
            query = st.text_input(
                "Enter your question:",
                placeholder="ะะฐะฟั€ะธะผะตั€: ะšะฐะบะธะต ะบะปัŽั‡ะตะฒั‹ะต ะผะพะผะตะฝั‚ั‹ ะพะฟะธัะฐะฝั‹ ะฒ ะดะพะบัƒะผะตะฝั‚ะต?",
                help="Ask any question about your uploaded documents"
            )
        
        with col2:
            n_docs = st.number_input("Retrieved docs:", value=3, min_value=1, max_value=10)
        
        with col3:
            max_tokens = st.number_input("Max tokens:", value=256, min_value=128, max_value=512, step=128)
        
        if st.button("๐Ÿš€ Get Answer", use_container_width=True, type="primary"):
            if not query:
                st.warning("โš ๏ธ Please enter a question.")
            else:
                try:
                    with st.spinner("๐Ÿค– Generating answer... (this may take 10-30 seconds)"):
                        st.info("Processing query - please wait...")
                        
                        # Generate answer with error handling
                        try:
                            result = rag_pipeline.answer_question(
                                query=query,
                                n_retrieved=n_docs,
                                max_new_tokens=max_tokens
                            )
                            
                            # Check for errors
                            if "error" in result and result["error"]:
                                st.error(f"โš ๏ธ {result['error']}")
                            
                            # Display answer
                            st.success("โœ… Answer Generated")
                            st.markdown("### Answer")
                            st.write(result['answer'])
                            
                            # Display retrieved documents
                            with st.expander("๐Ÿ“š Retrieved Documents", expanded=False):
                                st.markdown(f"#### {result['doc_count']} Relevant Document Chunks:")
                                for idx, doc in enumerate(result['retrieved_docs'], 1):
                                    with st.container():
                                        col_rel, col_score = st.columns([3, 1])
                                        with col_rel:
                                            st.markdown(f"**Document {idx}**")
                                        with col_score:
                                            st.caption(f"Score: {doc['relevance_score']:.1%}")
                                        
                                        # Truncate for display
                                        preview = doc['document'][:400]
                                        if len(doc['document']) > 400:
                                            preview += "..."
                                        st.write(preview)
                                        
                                        if doc['metadata']:
                                            st.caption(f"Source: {doc['metadata'].get('filename', 'Unknown')}")
                        
                        except Exception as e:
                            st.error(f"โŒ Error during generation: {e}")
                            st.info("Possible causes:")
                            st.write("- Out of memory (try reducing 'Max tokens' or 'Retrieved docs')")
                            st.write("- Model inference timeout")
                            st.write("- Invalid input format")
                
                except Exception as e:
                    st.error(f"โŒ Unexpected error: {e}")

# ============================================================================
# TAB 3: MANAGE DATABASE
# ============================================================================

with tab_manage:
    st.header("๐Ÿ› ๏ธ Database Management")
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        if st.button("โ„น๏ธ Database Info", use_container_width=True):
            try:
                info = chroma_manager.get_collection_info()
                st.json(info)
            except Exception as e:
                st.error(f"Error: {e}")
    
    with col2:
        if st.button("๐Ÿ“‹ List Documents", use_container_width=True):
            try:
                all_docs = chroma_manager.collection.get(include=['documents'])
                if all_docs['ids']:
                    st.write(f"Total documents: {len(all_docs['ids'])}")
                    
                    col1_list, col2_list = st.columns(2)
                    
                    with col1_list:
                        st.write("**First 10:**")
                        for idx, doc_id in enumerate(all_docs['ids'][:10], 1):
                            st.write(f"{idx}. {doc_id[:50]}...")
                    
                    with col2_list:
                        if len(all_docs['ids']) > 10:
                            st.write(f"**... and {len(all_docs['ids']) - 10} more**")
                else:
                    st.info("No documents in database")
            except Exception as e:
                st.error(f"Error: {e}")
    
    with col3:
        if st.button("๐Ÿ—‘๏ธ Clear Database", use_container_width=True):
            try:
                collection_info = chroma_manager.get_collection_info()
                if collection_info['document_count'] > 0:
                    chroma_manager.clear_collection()
                    st.success("โœ… Database cleared!")
                    st.rerun()
                else:
                    st.info("Database is already empty")
            except Exception as e:
                st.error(f"Error: {e}")
    
    st.divider()
    
    st.markdown("### Storage Information")
    col1, col2 = st.columns(2)
    
    with col1:
        extraction_size = sum(
            os.path.getsize(os.path.join(dirpath, filename))
            for dirpath, dirnames, filenames in os.walk("./pdf_extractions")
            for filename in filenames
        ) / (1024 * 1024) if os.path.exists("./pdf_extractions") else 0
        st.metric("PDF Extractions", f"{extraction_size:.1f} MB")
    
    with col2:
        chroma_size = sum(
            os.path.getsize(os.path.join(dirpath, filename))
            for dirpath, dirnames, filenames in os.walk("./chroma_db")
            for filename in filenames
        ) / (1024 * 1024) if os.path.exists("./chroma_db") else 0
        st.metric("ChromaDB Storage", f"{chroma_size:.1f} MB")

# ============================================================================
# TAB 4: ABOUT
# ============================================================================

with tab_about:
    st.header("โ„น๏ธ About This System")
    
    st.markdown("""
    ### Multimodal RAG System with PDF Upload
    
    This is a **local, privacy-first AI document analysis system** that allows you to:
    
    #### โœจ Features
    - **๐Ÿ“ค Easy PDF Upload**: Drag & drop or select multiple PDF files
    - **๐Ÿ” Smart Search**: Semantic search across documents with CLIP embeddings
    - **๐Ÿค– AI-Powered Answers**: Ask questions and get answers from Qwen2.5-VL-3B
    - **๐ŸŒ Russian & English**: Full support for both languages
    - **๐Ÿ’พ Local Storage**: All data stays on your machine
    - **โšก Fast Processing**: Automatic caching to avoid re-processing
    
    #### ๐Ÿ—๏ธ How It Works
    1. Upload PDF documents
    2. System extracts text, tables, and images
    3. Content is embedded with CLIP and stored in ChromaDB
    4. Ask questions about your documents
    5. AI retrieves relevant sections and generates answers
    
    #### ๐Ÿ” Privacy & Security
    - โœ… All processing happens locally
    - โœ… No internet required (after model download)
    - โœ… No cloud APIs used
    - โœ… Full data control
    - โœ… Open-source code
    
    #### ๐Ÿ’ป Technology Stack
    - **LLM**: Qwen2.5-VL-3B (multimodal)
    - **Embeddings**: CLIP (clip-vit-base-patch32)
    - **Vector DB**: ChromaDB
    - **UI**: Streamlit
    - **PDF Processing**: pdfplumber + PyMuPDF
    
    #### ๐Ÿ“Š System Info
    """)
    
    col1, col2, col3 = st.columns(3)
    
    with col1:
        device_name = "GPU (CUDA)" if torch.cuda.is_available() else "CPU"
        st.metric("Device", device_name)
    
    with col2:
        collection_info = chroma_manager.get_collection_info()
        st.metric("Documents in DB", collection_info['document_count'])
    
    with col3:
        st.metric("Version", "1.2 (Upload)")
    
    st.divider()
    
    st.markdown("""
    #### ๐Ÿ“ How to Use
    
    1. **Upload PDFs**: Go to the "Upload PDFs" tab and select your files
    2. **Wait for Processing**: System automatically extracts content
    3. **Ask Questions**: Switch to "Ask Questions" tab and type your query
    4. **Review Results**: See generated answers and relevant document chunks
    5. **Manage**: Use "Manage" tab to view or clear database
    
    #### โš™๏ธ Tips for Best Results
    - Start with smaller PDFs to test
    - Ask specific questions for better answers
    - Reduce "Retrieved docs" if responses are slow
    - Use Russian for Russian documents (better accuracy)
    
    #### ๐Ÿ”ง Performance Tuning
    - **Slow responses**: Reduce "Max tokens" from 512 to 256
    - **Out of memory**: Use fewer "Retrieved docs" (1-3)
    - **Better quality**: Increase "Max tokens" to 512
    
    #### โ“ Troubleshooting
    - **App closes**: Reduce "Max tokens" and "Retrieved docs"
    - **Slow processing**: First upload takes time (model loading)
    - **Memory issues**: Use CPU mode (edit in sidebar)
    """)

# ============================================================================
# FOOTER
# ============================================================================

st.divider()
st.markdown("""
<div style='text-align: center; color: #666; font-size: 0.9rem;'>
    Multimodal RAG System with PDF Upload | Qwen2.5-VL + ChromaDB + Streamlit | v1.2
</div>
""", unsafe_allow_html=True)