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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
"""
Streamlit App for RAG Pipeline using FastAPI
This app uses the REST API endpoints instead of calling functions directly.
"""

import streamlit as st
import requests
from typing import Dict, Any, Optional
import os

# API Configuration
API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:8000")

# Page configuration
st.set_page_config(
    page_title="RAG Pipeline - PDF Query System (API)",
    page_icon="📚",
    layout="wide"
)

# Initialize session state
if 'session_id' not in st.session_state:
    st.session_state.session_id = None
if 'documents_processed' not in st.session_state:
    st.session_state.documents_processed = False
if 'chat_history' not in st.session_state:
    st.session_state.chat_history = []
if 'system_status' not in st.session_state:
    st.session_state.system_status = None


def check_api_connection():
    """Check if API is accessible."""
    try:
        response = requests.get(f"{API_BASE_URL}/", timeout=5)
        return response.status_code == 200
    except:
        return False


def get_system_status():
    """Get system status from API."""
    try:
        response = requests.get(f"{API_BASE_URL}/status", timeout=5)
        if response.status_code == 200:
            return response.json()
        return None
    except Exception as e:
        st.error(f"Error connecting to API: {str(e)}")
        return None


def upload_documents_api(files, chunk_size, chunk_overlap):
    """Upload and process documents via API."""
    try:
        files_data = []
        for file in files:
            files_data.append(('files', (file.name, file.getvalue(), 'application/pdf')))
        
        data = {
            'chunk_size': chunk_size,
            'chunk_overlap': chunk_overlap
        }
        
        response = requests.post(
            f"{API_BASE_URL}/upload",
            files=files_data,
            data=data,
            timeout=300  # 5 minutes for large files
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            error_detail = response.json().get('detail', 'Unknown error')
            raise Exception(f"API Error: {error_detail}")
    except requests.exceptions.RequestException as e:
        raise Exception(f"Connection error: {str(e)}")


def query_api(query, session_id, top_k, use_memory, metadata_filters=None):
    """Query documents via API."""
    payload = {
        "query": query,
        "session_id": session_id,
        "top_k": top_k,
        "use_memory": use_memory
    }
    
    if metadata_filters:
        payload["metadata_filters"] = metadata_filters
    
    try:
        response = requests.post(
            f"{API_BASE_URL}/query",
            json=payload,
            timeout=60
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            error_detail = response.json().get('detail', 'Unknown error')
            raise Exception(f"API Error: {error_detail}")
    except requests.exceptions.RequestException as e:
        raise Exception(f"Connection error: {str(e)}")


def get_chat_history_api(session_id):
    """Get chat history from API."""
    try:
        response = requests.get(f"{API_BASE_URL}/chat-history/{session_id}", timeout=5)
        if response.status_code == 200:
            return response.json()
        return None
    except:
        return None


def clear_chat_history_api(session_id):
    """Clear chat history via API."""
    try:
        response = requests.delete(f"{API_BASE_URL}/chat-history/{session_id}", timeout=5)
        return response.status_code == 200
    except:
        return False


def get_metadata_options():
    """Get available metadata fields from API status or query."""
    # This would require an API endpoint to get metadata fields
    # For now, we'll use a simplified approach
    status = get_system_status()
    if status and status.get('vector_store_count', 0) > 0:
        # We can't directly access vectorstore from API, so we'll use a workaround
        # Try to get metadata from a sample query or add a new API endpoint
        return {}
    return {}


def main():
    """Main Streamlit app."""
    st.title("📚 RAG Pipeline - PDF Query System (API)")
    st.markdown("Upload PDF files and query them using Retrieval-Augmented Generation (RAG) via REST API")
    
    # Check API connection
    if not check_api_connection():
        st.error(f"❌ Cannot connect to API at {API_BASE_URL}")
        st.info("Please make sure the API server is running:")
        st.code("uvicorn api:app --reload --host 0.0.0.0 --port 8000")
        st.stop()
    
    # Get system status
    if st.session_state.system_status is None:
        st.session_state.system_status = get_system_status()
    
    # Generate session ID if not exists
    if st.session_state.session_id is None:
        import uuid
        st.session_state.session_id = str(uuid.uuid4())
    
    # Sidebar for configuration
    with st.sidebar:
        st.header("⚙️ Configuration")
        
        st.subheader("API Settings")
        api_url = st.text_input(
            "API Base URL",
            value=API_BASE_URL,
            help="Base URL of the FastAPI server"
        )
        if api_url != API_BASE_URL:
            st.session_state.api_base_url = api_url
            st.rerun()
        
        if st.button("🔄 Refresh Status", type="secondary"):
            st.session_state.system_status = get_system_status()
            st.rerun()
        
        st.subheader("Chunking Parameters")
        chunk_size = st.slider(
            "Chunk Size",
            min_value=200,
            max_value=2000,
            value=800,
            step=100,
            help="Size of each text chunk in characters"
        )
        chunk_overlap = st.slider(
            "Chunk Overlap",
            min_value=0,
            max_value=500,
            value=200,
            step=50,
            help="Number of overlapping characters between chunks"
        )
        
        st.subheader("Query Parameters")
        top_k = st.slider(
            "Top K Results",
            min_value=1,
            max_value=10,
            value=5,
            help="Number of document chunks to retrieve"
        )
        
        st.subheader("🔍 Metadata Filters")
        st.caption("Filter documents by metadata for faster, more accurate retrieval")
        use_filters = st.checkbox("Enable Metadata Filtering", value=False)
        
        metadata_filters = {}
        if use_filters:
            st.info("💡 Metadata filtering via API - enter filter values below")
            with st.expander("Advanced: Custom Metadata Filter"):
                filter_key = st.text_input("Metadata Key", placeholder="e.g., source, page")
                filter_value = st.text_input("Metadata Value", placeholder="e.g., document.pdf or 1,2,3")
                if filter_key and filter_value:
                    # Try to parse as list if comma-separated
                    if ',' in filter_value:
                        try:
                            # Try to parse as integers
                            metadata_filters[filter_key] = [int(v.strip()) for v in filter_value.split(',')]
                        except:
                            # Keep as strings
                            metadata_filters[filter_key] = [v.strip() for v in filter_value.split(',')]
                    else:
                        try:
                            # Try integer
                            metadata_filters[filter_key] = int(filter_value)
                        except:
                            # Keep as string
                            metadata_filters[filter_key] = filter_value
        
        st.divider()
        
        st.subheader("System Status")
        status = st.session_state.system_status
        if status:
            if status.get('documents_processed'):
                st.success("✅ Documents Processed")
                st.info(f"🗄️ {status.get('vector_store_count', 0)} documents in vector store")
                if status.get('chunks_available'):
                    st.info(f"📄 {status['chunks_available']} chunks available")
            else:
                st.info("⏳ No documents processed")
            
            if status.get('embedding_model'):
                st.caption(f"Model: {status['embedding_model']}")
        else:
            st.warning("⚠️ Could not fetch system status")
        
        st.divider()
        
        if st.button("🗑️ Clear Chat History", type="secondary"):
            if st.session_state.session_id:
                if clear_chat_history_api(st.session_state.session_id):
                    st.session_state.chat_history = []
                    st.success("Chat history cleared!")
                    st.rerun()
        
        if st.button("🔄 Reset System", type="secondary"):
            try:
                response = requests.post(f"{API_BASE_URL}/reset", timeout=5)
                if response.status_code == 200:
                    st.session_state.documents_processed = False
                    st.session_state.chat_history = []
                    st.session_state.system_status = get_system_status()
                    st.success("System reset!")
                    st.rerun()
            except Exception as e:
                st.error(f"Error resetting system: {str(e)}")
    
    # Main content area
    tab1, tab2 = st.tabs(["📤 Upload & Process", "💬 Chat"])
    
    with tab1:
        st.header("Upload PDF Files")
        st.markdown("Upload one or more PDF files to process and add to the knowledge base via API.")
        
        uploaded_files = st.file_uploader(
            "Choose PDF files",
            type=['pdf'],
            accept_multiple_files=True,
            help="You can upload multiple PDF files at once"
        )
        
        if uploaded_files:
            st.info(f"📎 {len(uploaded_files)} file(s) selected")
            
            # Display file names
            with st.expander("View uploaded files"):
                for file in uploaded_files:
                    st.write(f"- {file.name} ({file.size:,} bytes)")
            
            if st.button("🚀 Process Documents", type="primary"):
                with st.spinner("Uploading and processing documents via API..."):
                    try:
                        result = upload_documents_api(uploaded_files, chunk_size, chunk_overlap)
                        if result.get('success'):
                            st.success("✅ Documents processed successfully!")
                            st.json(result)
                            st.session_state.documents_processed = True
                            st.session_state.system_status = get_system_status()
                        else:
                            st.error("❌ Failed to process documents.")
                    except Exception as e:
                        st.error(f"❌ Error: {str(e)}")
    
    with tab2:
        st.header("💬 Chat with Documents")
        st.markdown("Ask questions about the uploaded PDF documents. The chat remembers previous conversations.")
        
        # Check if documents are processed
        status = st.session_state.system_status
        if not status or not status.get('documents_processed'):
            st.warning("⚠️ Please upload and process documents first in the 'Upload & Process' tab.")
        else:
            # Display chat history
            chat_container = st.container()
            with chat_container:
                if st.session_state.chat_history:
                    for message in st.session_state.chat_history:
                        role = message.get("role", "user")
                        content = message.get("content", "")
                        
                        if role == "user":
                            with st.chat_message("user"):
                                st.write(content)
                        elif role == "assistant":
                            with st.chat_message("assistant"):
                                st.write(content)
                                # Show sources if available
                                if "sources" in message:
                                    with st.expander("📄 Sources"):
                                        for i, source in enumerate(message["sources"], 1):
                                            st.markdown(f"**Source {i}** (Score: {source.get('score', 0):.4f})")
                                            st.caption(f"Preview: {source.get('preview', '')[:200]}...")
                else:
                    st.info("👋 Start a conversation by asking a question below!")
            
            # Chat input
            query = st.chat_input(
                "Ask a question about the documents...",
                key="chat_input"
            )
            
            # Handle query
            if query:
                # Add user message to chat history
                st.session_state.chat_history.append({
                    "role": "user",
                    "content": query
                })
                
                with st.spinner("Thinking..."):
                    try:
                        # Query via API
                        result = query_api(
                            query=query,
                            session_id=st.session_state.session_id,
                            top_k=top_k,
                            use_memory=True,
                            metadata_filters=metadata_filters if use_filters and metadata_filters else None
                        )
                        
                        # Add assistant response to chat history
                        st.session_state.chat_history.append({
                            "role": "assistant",
                            "content": result["answer"],
                            "sources": result.get("sources", [])
                        })
                        
                        # Update session ID if API generated a new one
                        if result.get("session_id"):
                            st.session_state.session_id = result["session_id"]
                        
                        st.rerun()
                        
                    except Exception as e:
                        st.error(f"Error processing query: {str(e)}")
                        # Remove the user message if there was an error
                        if st.session_state.chat_history and st.session_state.chat_history[-1]["role"] == "user":
                            st.session_state.chat_history.pop()
            
            # Example queries
            if not st.session_state.chat_history:
                st.divider()
                st.subheader("💡 Example Queries")
                example_queries = [
                    "What is the main topic of the document?",
                    "Summarize the key points",
                    "What are the main findings?",
                ]
                
                cols = st.columns(len(example_queries))
                for i, example in enumerate(example_queries):
                    with cols[i]:
                        if st.button(f"📝 {example[:30]}...", key=f"example_{i}"):
                            st.session_state.chat_history.append({
                                "role": "user",
                                "content": example
                            })
                            st.rerun()


if __name__ == "__main__":
    main()