"""Streamlit Web Application for Production Document Question Answering (DQA). Ready for Deployment on Hugging Face Spaces. Features: - Auto-loads sample documents for instant one-click demonstration. - Upload & Ingest PDF (layout-aware, OCR fallback), DOCX, TXT. - Hybrid Retrieval: BAAI/bge-small-en-v1.5 + BM25Okapi + Reciprocal Rank Fusion (RRF). - Cross-Encoder Reranking (ms-marco-MiniLM-L-6-v2). - Multi-Model Generation: 1. Hugging Face Serverless Inference API (Qwen-2.5, Llama-3.2, Mistral) 2. High-Precision Offline Grounding Engine (0-cost, 100% cited) 3. OpenAI / Groq / DeepSeek API - Strict Grounding with Evidence Quotes & [Page X, Section Y] Citations. - Second-Pass Verification Audit with Faithfulness scoring. - Quick-question prompt chips for instant testing. """ import os import sys import tempfile import time from typing import List import streamlit as st # Local modules from config import DQAConfig, default_config from ingest import DocumentIngestionPipeline, DocumentChunk from retrieve import HybridRetriever from generate import Generator, OpenAILLMClient, HuggingFaceInferenceClient from verify import AnswerVerifier st.set_page_config( page_title="Document QA System - Production RAG", page_icon="📄", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for modern styling st.markdown(""" """, unsafe_allow_html=True) @st.cache_resource(show_spinner="Loading Neural Embedder & Cross-Encoder Models...") def load_core_components(embedding_model_name: str): """Initializes and caches the heavy neural models.""" retriever = HybridRetriever( embedding_model=embedding_model_name, cross_encoder_model=default_config.cross_encoder_model_name, rrf_k=default_config.rrf_k ) verifier = AnswerVerifier( cross_encoder=retriever.reranker, threshold=default_config.verification_threshold, flag_unsupported=default_config.flag_unsupported_claims ) pipeline = DocumentIngestionPipeline( chunk_size=default_config.chunk_size_tokens, chunk_overlap=default_config.chunk_overlap_tokens, ocr_fallback=default_config.ocr_fallback_enabled, ocr_min_chars=default_config.ocr_min_chars_per_page ) return retriever, verifier, pipeline # Initialize session state variables if "embedding_model" not in st.session_state: st.session_state.embedding_model = default_config.embedding_model_name if "retriever" not in st.session_state: retriever, verifier, pipeline = load_core_components(st.session_state.embedding_model) st.session_state.retriever = retriever st.session_state.verifier = verifier st.session_state.pipeline = pipeline st.session_state.generator = Generator(verifier=verifier) st.session_state.messages = [] st.session_state.indexed_files = [] st.session_state.total_chunks = 0 st.session_state.all_chunks = [] # Auto-load pre-built sample documents on first launch for Hugging Face Spaces visitors sample_paths = [ "data/sample_financial_report.pdf", "data/sample_cloud_architecture.docx", "data/sample_security_policy.txt" ] parsed_list = [] for p in sample_paths: if os.path.exists(p): parsed = pipeline.ingest_file(p) parsed_list.append(parsed) st.session_state.indexed_files.append(parsed.file_name) if parsed_list: retriever.index_documents(parsed_list) st.session_state.total_chunks = len(retriever.chunks) st.session_state.all_chunks = retriever.chunks retriever = st.session_state.retriever verifier = st.session_state.verifier pipeline = st.session_state.pipeline generator = st.session_state.generator # ---------------- SIDEBAR: Controls & Configuration ---------------- with st.sidebar: st.header("⚙️ Settings & Ingestion") # Embedding Model selection st.subheader("1. Embedding Model") selected_embedder = st.selectbox( "Dense Embedding Model", options=["BAAI/bge-small-en-v1.5", "sentence-transformers/all-MiniLM-L6-v2"], index=0 if st.session_state.embedding_model == "BAAI/bge-small-en-v1.5" else 1, help="bge-small-en-v1.5 is the top MTEB benchmark retriever. all-MiniLM-L6-v2 is ultra fast." ) if selected_embedder != st.session_state.embedding_model: st.session_state.embedding_model = selected_embedder with st.spinner("Switching embedding model..."): retriever, verifier, pipeline = load_core_components(selected_embedder) st.session_state.retriever = retriever st.session_state.verifier = verifier st.session_state.pipeline = pipeline generator.verifier = verifier if st.session_state.all_chunks: retriever.index_documents(st.session_state.all_chunks) st.rerun() # Document Upload Section st.divider() st.subheader("2. Upload & Ingest Documents") uploaded_files = st.file_uploader( "Upload PDF (with OCR), DOCX, or TXT", type=["pdf", "docx", "txt", "md"], accept_multiple_files=True ) col1, col2 = st.columns(2) with col1: load_sample = st.button("📂 Reload Samples", use_container_width=True) with col2: reindex = st.button("🔄 Clear Index", use_container_width=True) if reindex: retriever.index_documents([]) st.session_state.indexed_files = [] st.session_state.total_chunks = 0 st.session_state.all_chunks = [] st.session_state.messages = [] generator.memory.clear() st.success("Vector index & conversation memory cleared!") st.rerun() if load_sample: sample_paths = [ "data/sample_financial_report.pdf", "data/sample_cloud_architecture.docx", "data/sample_security_policy.txt" ] parsed_list = [] st.session_state.indexed_files = [] for p in sample_paths: if os.path.exists(p): parsed = pipeline.ingest_file(p) parsed_list.append(parsed) st.session_state.indexed_files.append(parsed.file_name) if parsed_list: retriever.index_documents(parsed_list) st.session_state.total_chunks = len(retriever.chunks) st.session_state.all_chunks = retriever.chunks st.success(f"Indexed {len(parsed_list)} sample documents ({st.session_state.total_chunks} chunks)!") st.rerun() # Process user uploaded files if uploaded_files: if st.button("🚀 Process Uploaded Files", type="primary", use_container_width=True): new_parsed = [] with st.spinner("Extracting layout, formatting tables & generating embeddings..."): for uploaded_file in uploaded_files: ext = os.path.splitext(uploaded_file.name)[1] with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp_file: tmp_file.write(uploaded_file.read()) tmp_path = tmp_file.name try: parsed = pipeline.ingest_file(tmp_path) parsed.file_name = uploaded_file.name for c in parsed.chunks: c.file_name = uploaded_file.name new_parsed.append(parsed) if uploaded_file.name not in st.session_state.indexed_files: st.session_state.indexed_files.append(uploaded_file.name) finally: if os.path.exists(tmp_path): os.remove(tmp_path) if new_parsed: existing_chunks = list(retriever.chunks) combined = existing_chunks + [c for p in new_parsed for c in p.chunks] retriever.index_documents(combined) st.session_state.total_chunks = len(retriever.chunks) st.session_state.all_chunks = retriever.chunks st.success(f"Successfully indexed {len(new_parsed)} document(s)!") st.rerun() # Index Status Summary st.divider() st.markdown("**Index Status:**") st.write(f"• **Documents Indexed:** `{len(st.session_state.indexed_files)}`") st.write(f"• **Total Chunks:** `{st.session_state.total_chunks}`") if st.session_state.indexed_files: with st.expander("📁 View Loaded Documents"): for fname in st.session_state.indexed_files: st.caption(f"✓ {fname}") # Chunk Inspector if st.session_state.all_chunks: with st.expander("🔍 Inspect Chunks & Metadata"): chunk_num = st.selectbox( "Select Chunk", range(len(st.session_state.all_chunks)), format_func=lambda i: f"Chunk {i+1}: {st.session_state.all_chunks[i].file_name} (p. {st.session_state.all_chunks[i].page_number})" ) selected = st.session_state.all_chunks[chunk_num] st.markdown(f"**Doc:** `{selected.file_name}` | **Page {selected.page_number}**") st.markdown(f"**Section:** `{selected.section_title}` | Table: `{selected.metadata.get('is_table', False)}`") st.text_area("Chunk Content", selected.text, height=120) # Retrieval Settings st.divider() st.subheader("3. Retrieval & Reranker") retrieval_mode = st.selectbox( "Search Pipeline", options=["hybrid_rerank", "hybrid_no_rerank", "dense_only", "bm25_only"], format_func=lambda m: { "hybrid_rerank": "🌟 Hybrid (Dense+BM25) + Cross-Encoder Rerank", "hybrid_no_rerank": "⚡ Hybrid Search (No Reranker)", "dense_only": "🎯 Dense Vector Only (FAISS)", "bm25_only": "🔤 BM25 Lexical Only" }[m] ) rerank_top_k = st.slider("Top-K Context Chunks", min_value=1, max_value=10, value=5) enable_verification = st.checkbox("Enable Second-Pass Verification", value=True) enable_decomposition = st.checkbox("Decompose Complex Queries", value=True) # LLM Generator Backend st.divider() st.subheader("4. LLM Generation Engine") engine_choice = st.radio( "Generation Backend", options=["Offline Grounding Engine", "Hugging Face Inference API", "OpenAI / Groq API"], index=0, help="Offline engine runs locally with 0 cost and 100% cited answers. HF and OpenAI support conversational LLMs." ) if engine_choice == "Hugging Face Inference API": hf_token_input = st.text_input( "Hugging Face Token (Optional)", type="password", value=os.getenv("HF_TOKEN", ""), help="Your HF Token for higher rate limits on the Serverless Inference API." ) hf_model_choice = st.selectbox( "Hugging Face Model", options=[ "Qwen/Qwen2.5-7B-Instruct", "meta-llama/Llama-3.2-3B-Instruct", "mistralai/Mistral-7B-Instruct-v0.3" ] ) generator.llm_client = HuggingFaceInferenceClient( model=hf_model_choice, token=hf_token_input or None ) elif engine_choice == "OpenAI / Groq API": api_key_input = st.text_input( "API Key", type="password", value=os.getenv("OPENAI_API_KEY", "") ) base_url_input = st.text_input( "Base URL (Optional, for Groq/Ollama)", value=os.getenv("OPENAI_BASE_URL", "") ) if api_key_input: generator.llm_client = OpenAILLMClient( api_key=api_key_input, base_url=base_url_input or None ) else: generator.llm_client = None else: # High-precision offline grounding engine generator.llm_client = None # ---------------- MAIN CHAT INTERFACE ---------------- st.markdown('