Spaces:
Running on Zero
Running on Zero
Configure native Gradio SDK for Hugging Face Spaces deployment
Browse files- README.md +25 -38
- app.py +404 -455
- requirements.txt +1 -0
- streamlit_app.py +502 -0
README.md
CHANGED
|
@@ -3,8 +3,8 @@ title: Production Document Question Answering (DQA)
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
-
sdk:
|
| 7 |
-
sdk_version: "
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
@@ -74,7 +74,8 @@ flowchart TD
|
|
| 74 |
| [`generate.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/generate.py) | Strict context-grounded generator quoting evidence and generating `[Page X, Section Y]` citations. Multi-backend: Hugging Face Serverless Inference API, OpenAI-compatible APIs, and offline deterministic grounding engine. |
|
| 75 |
| [`verify.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/verify.py) | Second-pass verifier extracting atomic factual claims and validating them against retrieved passages via cross-encoder entailment, flagging or removing unsupported claims, and reporting Faithfulness Scores. |
|
| 76 |
| [`evaluate.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/evaluate.py) | 50-pair benchmark evaluation suite testing Recall@k, Answer Faithfulness, Answer Correctness, and Hallucination Rate across 4 baseline/proposed configurations. |
|
| 77 |
-
| [`app.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/app.py) |
|
|
|
|
| 78 |
|
| 79 |
---
|
| 80 |
|
|
@@ -91,52 +92,38 @@ The system was evaluated against a rigorous test set of **50 Q&A pairs** across
|
|
| 91 |
|
| 92 |
---
|
| 93 |
|
| 94 |
-
## π€ Deploying to Hugging Face Spaces
|
| 95 |
|
| 96 |
-
This repository is pre-configured for
|
| 97 |
|
| 98 |
-
###
|
| 99 |
-
1. Go to [huggingface.co/new-space](https://huggingface.co/new-space).
|
| 100 |
-
2. Choose **Streamlit** as the Space SDK and choose a name (e.g. `document-qa-system`).
|
| 101 |
-
3. Clone your new Hugging Face Space repository to your machine or push directly:
|
| 102 |
```bash
|
| 103 |
-
|
| 104 |
-
git add .
|
| 105 |
-
git commit -m "Deploy Production Document QA system"
|
| 106 |
-
git push space main
|
| 107 |
```
|
| 108 |
|
| 109 |
-
###
|
|
|
|
|
|
|
|
|
|
| 110 |
```bash
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
| 114 |
```
|
| 115 |
|
| 116 |
-
### Hugging Face Secrets (Optional)
|
| 117 |
-
If you wish to use Hugging Face Serverless Inference API with larger open LLMs (like `Qwen/Qwen2.5-7B-Instruct` or `meta-llama/Llama-3.2-3B-Instruct`), add your token in **Space Settings $\rightarrow$ Variables and secrets**:
|
| 118 |
-
- Secret Name: `HF_TOKEN`
|
| 119 |
-
- Secret Value: Your Hugging Face user access token (`hf_...`)
|
| 120 |
-
|
| 121 |
-
*(Even without any secret token, the system runs with 100% functionality using the high-precision offline grounding engine!)*
|
| 122 |
-
|
| 123 |
---
|
| 124 |
|
| 125 |
-
## π οΈ
|
| 126 |
|
|
|
|
| 127 |
```bash
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
# 2. Install Python requirements
|
| 132 |
-
pip install -r requirements.txt
|
| 133 |
-
|
| 134 |
-
# 3. Generate sample documents
|
| 135 |
-
python3 create_sample_docs.py
|
| 136 |
-
|
| 137 |
-
# 4. Run tests
|
| 138 |
-
python3 -m unittest discover -s tests -p "test_*.py"
|
| 139 |
|
| 140 |
-
#
|
| 141 |
-
|
|
|
|
| 142 |
```
|
|
|
|
|
|
| 3 |
emoji: π
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "6.32.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
|
|
|
| 74 |
| [`generate.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/generate.py) | Strict context-grounded generator quoting evidence and generating `[Page X, Section Y]` citations. Multi-backend: Hugging Face Serverless Inference API, OpenAI-compatible APIs, and offline deterministic grounding engine. |
|
| 75 |
| [`verify.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/verify.py) | Second-pass verifier extracting atomic factual claims and validating them against retrieved passages via cross-encoder entailment, flagging or removing unsupported claims, and reporting Faithfulness Scores. |
|
| 76 |
| [`evaluate.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/evaluate.py) | 50-pair benchmark evaluation suite testing Recall@k, Answer Faithfulness, Answer Correctness, and Hallucination Rate across 4 baseline/proposed configurations. |
|
| 77 |
+
| [`app.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/app.py) | Gradio interactive web application with file upload, real-time token streaming, citation accordion, verification audit display, and prompt chips. (Default for Hugging Face Spaces). |
|
| 78 |
+
| [`streamlit_app.py`](file:///home/ramakrishna/Desktop/Projects/DQA_Project/streamlit_app.py) | Alternative Streamlit web interface for users who prefer Streamlit. |
|
| 79 |
|
| 80 |
---
|
| 81 |
|
|
|
|
| 92 |
|
| 93 |
---
|
| 94 |
|
| 95 |
+
## π€ Deploying to Hugging Face Spaces (Gradio SDK)
|
| 96 |
|
| 97 |
+
This repository is pre-configured for **Gradio on Hugging Face Spaces**:
|
| 98 |
|
| 99 |
+
### Quick Deployment via Script
|
|
|
|
|
|
|
|
|
|
| 100 |
```bash
|
| 101 |
+
./deploy_to_hf.sh <your-username>/<your-space-name>
|
|
|
|
|
|
|
|
|
|
| 102 |
```
|
| 103 |
|
| 104 |
+
### Manual Git Deployment
|
| 105 |
+
1. Go to [huggingface.co/new-space](https://huggingface.co/new-space).
|
| 106 |
+
2. Choose a name and select **Gradio** as the Space SDK.
|
| 107 |
+
3. Add the remote and push:
|
| 108 |
```bash
|
| 109 |
+
git remote add space https://huggingface.co/spaces/<your-username>/<your-space-name>.git
|
| 110 |
+
git add .
|
| 111 |
+
git commit -m "Deploy Document QA with Gradio on Hugging Face Spaces"
|
| 112 |
+
git push -u space main
|
| 113 |
```
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
---
|
| 116 |
|
| 117 |
+
## π οΈ Running Locally
|
| 118 |
|
| 119 |
+
### Gradio (Default)
|
| 120 |
```bash
|
| 121 |
+
python3 app.py
|
| 122 |
+
```
|
| 123 |
+
Open `http://localhost:7860` in your browser.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
+
### Streamlit (Alternative)
|
| 126 |
+
```bash
|
| 127 |
+
streamlit run streamlit_app.py
|
| 128 |
```
|
| 129 |
+
Open `http://localhost:8501` in your browser.
|
app.py
CHANGED
|
@@ -1,27 +1,26 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
-
|
| 4 |
Features:
|
| 5 |
-
- Auto-loads sample documents for instant
|
| 6 |
-
-
|
| 7 |
- Hybrid Retrieval: BAAI/bge-small-en-v1.5 + BM25Okapi + Reciprocal Rank Fusion (RRF).
|
| 8 |
-
- Cross-Encoder Reranking
|
| 9 |
-
- Multi-
|
| 10 |
-
1.
|
| 11 |
-
2.
|
| 12 |
-
3. OpenAI / Groq / DeepSeek
|
| 13 |
- Strict Grounding with Evidence Quotes & [Page X, Section Y] Citations.
|
| 14 |
-
- Second-Pass Verification Audit with Faithfulness
|
| 15 |
-
-
|
| 16 |
"""
|
| 17 |
|
| 18 |
import os
|
| 19 |
import sys
|
| 20 |
-
import tempfile
|
| 21 |
import time
|
| 22 |
-
from typing import List
|
| 23 |
|
| 24 |
-
import
|
| 25 |
|
| 26 |
# Local modules
|
| 27 |
from config import DQAConfig, default_config
|
|
@@ -31,307 +30,148 @@ from generate import Generator, OpenAILLMClient, HuggingFaceInferenceClient
|
|
| 31 |
from verify import AnswerVerifier
|
| 32 |
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
st.markdown("""
|
| 43 |
-
<style>
|
| 44 |
-
.main-header {
|
| 45 |
-
font-size: 2.2rem;
|
| 46 |
-
font-weight: 700;
|
| 47 |
-
color: #1E3A8A;
|
| 48 |
-
margin-bottom: 0.2rem;
|
| 49 |
-
}
|
| 50 |
-
.sub-header {
|
| 51 |
-
font-size: 1.05rem;
|
| 52 |
-
color: #4B5563;
|
| 53 |
-
margin-bottom: 1.2rem;
|
| 54 |
-
}
|
| 55 |
-
.citation-box {
|
| 56 |
-
background-color: #F8FAFC;
|
| 57 |
-
border-left: 4px solid #3B82F6;
|
| 58 |
-
padding: 0.8rem 1rem;
|
| 59 |
-
border-radius: 4px;
|
| 60 |
-
margin-top: 0.6rem;
|
| 61 |
-
font-size: 0.9rem;
|
| 62 |
-
}
|
| 63 |
-
.verified-pill {
|
| 64 |
-
display: inline-block;
|
| 65 |
-
padding: 3px 10px;
|
| 66 |
-
border-radius: 12px;
|
| 67 |
-
font-size: 0.82rem;
|
| 68 |
-
font-weight: 600;
|
| 69 |
-
background-color: #DEF7EC;
|
| 70 |
-
color: #03543F;
|
| 71 |
-
}
|
| 72 |
-
.unverified-pill {
|
| 73 |
-
display: inline-block;
|
| 74 |
-
padding: 3px 10px;
|
| 75 |
-
border-radius: 12px;
|
| 76 |
-
font-size: 0.82rem;
|
| 77 |
-
font-weight: 600;
|
| 78 |
-
background-color: #FDE8E8;
|
| 79 |
-
color: #9B1C1C;
|
| 80 |
-
}
|
| 81 |
-
.chip-btn {
|
| 82 |
-
margin: 2px;
|
| 83 |
-
}
|
| 84 |
-
</style>
|
| 85 |
-
""", unsafe_allow_html=True)
|
| 86 |
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
embedding_model=embedding_model_name,
|
| 93 |
-
cross_encoder_model=default_config.cross_encoder_model_name,
|
| 94 |
-
rrf_k=default_config.rrf_k
|
| 95 |
-
)
|
| 96 |
-
verifier = AnswerVerifier(
|
| 97 |
-
cross_encoder=retriever.reranker,
|
| 98 |
-
threshold=default_config.verification_threshold,
|
| 99 |
-
flag_unsupported=default_config.flag_unsupported_claims
|
| 100 |
-
)
|
| 101 |
-
pipeline = DocumentIngestionPipeline(
|
| 102 |
-
chunk_size=default_config.chunk_size_tokens,
|
| 103 |
-
chunk_overlap=default_config.chunk_overlap_tokens,
|
| 104 |
-
ocr_fallback=default_config.ocr_fallback_enabled,
|
| 105 |
-
ocr_min_chars=default_config.ocr_min_chars_per_page
|
| 106 |
-
)
|
| 107 |
-
return retriever, verifier, pipeline
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# Initialize session state variables
|
| 111 |
-
if "embedding_model" not in st.session_state:
|
| 112 |
-
st.session_state.embedding_model = default_config.embedding_model_name
|
| 113 |
-
|
| 114 |
-
if "retriever" not in st.session_state:
|
| 115 |
-
retriever, verifier, pipeline = load_core_components(st.session_state.embedding_model)
|
| 116 |
-
st.session_state.retriever = retriever
|
| 117 |
-
st.session_state.verifier = verifier
|
| 118 |
-
st.session_state.pipeline = pipeline
|
| 119 |
-
st.session_state.generator = Generator(verifier=verifier)
|
| 120 |
-
st.session_state.messages = []
|
| 121 |
-
st.session_state.indexed_files = []
|
| 122 |
-
st.session_state.total_chunks = 0
|
| 123 |
-
st.session_state.all_chunks = []
|
| 124 |
-
|
| 125 |
-
# Auto-load pre-built sample documents on first launch for Hugging Face Spaces visitors
|
| 126 |
-
sample_paths = [
|
| 127 |
"data/sample_financial_report.pdf",
|
| 128 |
"data/sample_cloud_architecture.docx",
|
| 129 |
"data/sample_security_policy.txt"
|
| 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 |
-
if selected_embedder != st.session_state.embedding_model:
|
| 162 |
-
st.session_state.embedding_model = selected_embedder
|
| 163 |
-
with st.spinner("Switching embedding model..."):
|
| 164 |
-
retriever, verifier, pipeline = load_core_components(selected_embedder)
|
| 165 |
-
st.session_state.retriever = retriever
|
| 166 |
-
st.session_state.verifier = verifier
|
| 167 |
-
st.session_state.pipeline = pipeline
|
| 168 |
-
generator.verifier = verifier
|
| 169 |
-
if st.session_state.all_chunks:
|
| 170 |
-
retriever.index_documents(st.session_state.all_chunks)
|
| 171 |
-
st.rerun()
|
| 172 |
-
|
| 173 |
-
# Document Upload Section
|
| 174 |
-
st.divider()
|
| 175 |
-
st.subheader("2. Upload & Ingest Documents")
|
| 176 |
-
uploaded_files = st.file_uploader(
|
| 177 |
-
"Upload PDF (with OCR), DOCX, or TXT",
|
| 178 |
-
type=["pdf", "docx", "txt", "md"],
|
| 179 |
-
accept_multiple_files=True
|
| 180 |
-
)
|
| 181 |
-
|
| 182 |
-
col1, col2 = st.columns(2)
|
| 183 |
-
with col1:
|
| 184 |
-
load_sample = st.button("π Reload Samples", use_container_width=True)
|
| 185 |
-
with col2:
|
| 186 |
-
reindex = st.button("π Clear Index", use_container_width=True)
|
| 187 |
-
|
| 188 |
-
if reindex:
|
| 189 |
-
retriever.index_documents([])
|
| 190 |
-
st.session_state.indexed_files = []
|
| 191 |
-
st.session_state.total_chunks = 0
|
| 192 |
-
st.session_state.all_chunks = []
|
| 193 |
-
st.session_state.messages = []
|
| 194 |
-
generator.memory.clear()
|
| 195 |
-
st.success("Vector index & conversation memory cleared!")
|
| 196 |
-
st.rerun()
|
| 197 |
-
|
| 198 |
-
if load_sample:
|
| 199 |
-
sample_paths = [
|
| 200 |
-
"data/sample_financial_report.pdf",
|
| 201 |
-
"data/sample_cloud_architecture.docx",
|
| 202 |
-
"data/sample_security_policy.txt"
|
| 203 |
-
]
|
| 204 |
-
parsed_list = []
|
| 205 |
-
st.session_state.indexed_files = []
|
| 206 |
-
for p in sample_paths:
|
| 207 |
-
if os.path.exists(p):
|
| 208 |
-
parsed = pipeline.ingest_file(p)
|
| 209 |
-
parsed_list.append(parsed)
|
| 210 |
-
st.session_state.indexed_files.append(parsed.file_name)
|
| 211 |
-
|
| 212 |
-
if parsed_list:
|
| 213 |
-
retriever.index_documents(parsed_list)
|
| 214 |
-
st.session_state.total_chunks = len(retriever.chunks)
|
| 215 |
-
st.session_state.all_chunks = retriever.chunks
|
| 216 |
-
st.success(f"Indexed {len(parsed_list)} sample documents ({st.session_state.total_chunks} chunks)!")
|
| 217 |
-
st.rerun()
|
| 218 |
-
|
| 219 |
-
# Process user uploaded files
|
| 220 |
-
if uploaded_files:
|
| 221 |
-
if st.button("π Process Uploaded Files", type="primary", use_container_width=True):
|
| 222 |
-
new_parsed = []
|
| 223 |
-
with st.spinner("Extracting layout, formatting tables & generating embeddings..."):
|
| 224 |
-
for uploaded_file in uploaded_files:
|
| 225 |
-
ext = os.path.splitext(uploaded_file.name)[1]
|
| 226 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp_file:
|
| 227 |
-
tmp_file.write(uploaded_file.read())
|
| 228 |
-
tmp_path = tmp_file.name
|
| 229 |
-
|
| 230 |
-
try:
|
| 231 |
-
parsed = pipeline.ingest_file(tmp_path)
|
| 232 |
-
parsed.file_name = uploaded_file.name
|
| 233 |
-
for c in parsed.chunks:
|
| 234 |
-
c.file_name = uploaded_file.name
|
| 235 |
-
new_parsed.append(parsed)
|
| 236 |
-
if uploaded_file.name not in st.session_state.indexed_files:
|
| 237 |
-
st.session_state.indexed_files.append(uploaded_file.name)
|
| 238 |
-
finally:
|
| 239 |
-
if os.path.exists(tmp_path):
|
| 240 |
-
os.remove(tmp_path)
|
| 241 |
-
|
| 242 |
-
if new_parsed:
|
| 243 |
-
existing_chunks = list(retriever.chunks)
|
| 244 |
-
combined = existing_chunks + [c for p in new_parsed for c in p.chunks]
|
| 245 |
-
retriever.index_documents(combined)
|
| 246 |
-
st.session_state.total_chunks = len(retriever.chunks)
|
| 247 |
-
st.session_state.all_chunks = retriever.chunks
|
| 248 |
-
st.success(f"Successfully indexed {len(new_parsed)} document(s)!")
|
| 249 |
-
st.rerun()
|
| 250 |
-
|
| 251 |
-
# Index Status Summary
|
| 252 |
-
st.divider()
|
| 253 |
-
st.markdown("**Index Status:**")
|
| 254 |
-
st.write(f"β’ **Documents Indexed:** `{len(st.session_state.indexed_files)}`")
|
| 255 |
-
st.write(f"β’ **Total Chunks:** `{st.session_state.total_chunks}`")
|
| 256 |
-
if st.session_state.indexed_files:
|
| 257 |
-
with st.expander("π View Loaded Documents"):
|
| 258 |
-
for fname in st.session_state.indexed_files:
|
| 259 |
-
st.caption(f"β {fname}")
|
| 260 |
-
|
| 261 |
-
# Chunk Inspector
|
| 262 |
-
if st.session_state.all_chunks:
|
| 263 |
-
with st.expander("π Inspect Chunks & Metadata"):
|
| 264 |
-
chunk_num = st.selectbox(
|
| 265 |
-
"Select Chunk",
|
| 266 |
-
range(len(st.session_state.all_chunks)),
|
| 267 |
-
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})"
|
| 268 |
-
)
|
| 269 |
-
selected = st.session_state.all_chunks[chunk_num]
|
| 270 |
-
st.markdown(f"**Doc:** `{selected.file_name}` | **Page {selected.page_number}**")
|
| 271 |
-
st.markdown(f"**Section:** `{selected.section_title}` | Table: `{selected.metadata.get('is_table', False)}`")
|
| 272 |
-
st.text_area("Chunk Content", selected.text, height=120)
|
| 273 |
-
|
| 274 |
-
# Retrieval Settings
|
| 275 |
-
st.divider()
|
| 276 |
-
st.subheader("3. Retrieval & Reranker")
|
| 277 |
-
retrieval_mode = st.selectbox(
|
| 278 |
-
"Search Pipeline",
|
| 279 |
-
options=["hybrid_rerank", "hybrid_no_rerank", "dense_only", "bm25_only"],
|
| 280 |
-
format_func=lambda m: {
|
| 281 |
-
"hybrid_rerank": "π Hybrid (Dense+BM25) + Cross-Encoder Rerank",
|
| 282 |
-
"hybrid_no_rerank": "β‘ Hybrid Search (No Reranker)",
|
| 283 |
-
"dense_only": "π― Dense Vector Only (FAISS)",
|
| 284 |
-
"bm25_only": "π€ BM25 Lexical Only"
|
| 285 |
-
}[m]
|
| 286 |
-
)
|
| 287 |
|
| 288 |
-
rerank_top_k = st.slider("Top-K Context Chunks", min_value=1, max_value=10, value=5)
|
| 289 |
-
enable_verification = st.checkbox("Enable Second-Pass Verification", value=True)
|
| 290 |
-
enable_decomposition = st.checkbox("Decompose Complex Queries", value=True)
|
| 291 |
-
|
| 292 |
-
# LLM Generator Backend
|
| 293 |
-
st.divider()
|
| 294 |
-
st.subheader("4. LLM Generation Engine")
|
| 295 |
-
engine_choice = st.radio(
|
| 296 |
-
"Generation Backend",
|
| 297 |
-
options=["Offline Grounding Engine", "Hugging Face Inference API", "OpenAI / Groq API"],
|
| 298 |
-
index=0,
|
| 299 |
-
help="Offline engine runs locally with 0 cost and 100% cited answers. HF and OpenAI support conversational LLMs."
|
| 300 |
-
)
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
generator.llm_client = HuggingFaceInferenceClient(
|
| 318 |
-
model=
|
| 319 |
-
token=
|
| 320 |
-
)
|
| 321 |
-
elif engine_choice == "OpenAI / Groq API":
|
| 322 |
-
api_key_input = st.text_input(
|
| 323 |
-
"API Key",
|
| 324 |
-
type="password",
|
| 325 |
-
value=os.getenv("OPENAI_API_KEY", "")
|
| 326 |
)
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
value=os.getenv("OPENAI_BASE_URL", "")
|
| 330 |
-
)
|
| 331 |
-
if api_key_input:
|
| 332 |
generator.llm_client = OpenAILLMClient(
|
| 333 |
-
api_key=
|
| 334 |
-
base_url=
|
| 335 |
)
|
| 336 |
else:
|
| 337 |
generator.llm_client = None
|
|
@@ -339,164 +179,273 @@ with st.sidebar:
|
|
| 339 |
# High-precision offline grounding engine
|
| 340 |
generator.llm_client = None
|
| 341 |
|
|
|
|
| 342 |
|
| 343 |
-
#
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
|
| 351 |
-
#
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
selected_prompt = None
|
| 362 |
-
with chip_cols[0]:
|
| 363 |
-
if st.button("π Q3 Revenue & Margin", use_container_width=True):
|
| 364 |
-
selected_prompt = sample_prompts[0]
|
| 365 |
-
with chip_cols[1]:
|
| 366 |
-
if st.button("β‘ Latency & SLA Targets", use_container_width=True):
|
| 367 |
-
selected_prompt = sample_prompts[1]
|
| 368 |
-
with chip_cols[2]:
|
| 369 |
-
if st.button("π MFA & TLS Standards", use_container_width=True):
|
| 370 |
-
selected_prompt = sample_prompts[2]
|
| 371 |
-
with chip_cols[3]:
|
| 372 |
-
if st.button("π« Oracle Cloud (Negative Test)", use_container_width=True):
|
| 373 |
-
selected_prompt = sample_prompts[3]
|
| 374 |
-
|
| 375 |
-
# Display Conversation History
|
| 376 |
-
for turn in st.session_state.messages:
|
| 377 |
-
with st.chat_message(turn["role"]):
|
| 378 |
-
st.markdown(turn["content"])
|
| 379 |
-
|
| 380 |
-
# Display Citations if available
|
| 381 |
-
if turn.get("citations"):
|
| 382 |
-
with st.expander(f"π View {len(turn['citations'])} Source Citations & Evidence Quotes"):
|
| 383 |
-
for idx, c in enumerate(turn["citations"], 1):
|
| 384 |
-
score_info = f"(Score: `{c.get('score', 0.0):.3f}`)" if 'score' in c else ""
|
| 385 |
-
st.markdown(
|
| 386 |
-
f"**Citation {idx}:** `{c['file_name']}` | **Page {c['page']}**, Section *\"{c['section']}\"* {score_info}"
|
| 387 |
-
)
|
| 388 |
-
st.markdown(f"> *\"{c['text']}\"*")
|
| 389 |
-
st.divider()
|
| 390 |
-
|
| 391 |
-
# Display Verification details if available
|
| 392 |
-
if turn.get("verification"):
|
| 393 |
-
ver = turn["verification"]
|
| 394 |
-
faithfulness = ver.get("faithfulness_score", 1.0)
|
| 395 |
-
status_class = "verified-pill" if faithfulness >= 0.8 else "unverified-pill"
|
| 396 |
-
status_label = f"Faithfulness: {int(faithfulness * 100)}%"
|
| 397 |
-
|
| 398 |
-
with st.expander(f"π‘οΈ Second-Pass Verification Audit ({status_label})"):
|
| 399 |
-
st.markdown(f"Status: <span class='{status_class}'>{status_label}</span>", unsafe_allow_html=True)
|
| 400 |
-
st.markdown(f"β’ **Supported Claims:** {len(ver.get('supported_claims', []))}")
|
| 401 |
-
for sc in ver.get("supported_claims", []):
|
| 402 |
-
st.caption(f"β {sc}")
|
| 403 |
-
if ver.get("unsupported_claims"):
|
| 404 |
-
st.markdown(f"β’ **β οΈ Flagged / Unsupported Claims:** {len(ver.get('unsupported_claims', []))}")
|
| 405 |
-
for uc in ver.get("unsupported_claims", []):
|
| 406 |
-
st.caption(f"β οΈ {uc}")
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
# User Input (via chat input or prompt chip)
|
| 410 |
-
chat_input_val = st.chat_input("Ask a question grounded strictly in your uploaded documents...")
|
| 411 |
-
active_prompt = selected_prompt or chat_input_val
|
| 412 |
-
|
| 413 |
-
if active_prompt:
|
| 414 |
-
st.session_state.messages.append({"role": "user", "content": active_prompt})
|
| 415 |
-
generator.memory.add_user_message(active_prompt)
|
| 416 |
-
|
| 417 |
-
with st.chat_message("user"):
|
| 418 |
-
st.markdown(active_prompt)
|
| 419 |
-
|
| 420 |
-
with st.chat_message("assistant"):
|
| 421 |
-
with st.spinner("Retrieving evidence, reranking passages & generating grounded response..."):
|
| 422 |
-
conv_history = generator.memory.get_messages_for_prompt()[:-1]
|
| 423 |
-
ret_result = retriever.retrieve(
|
| 424 |
-
query=active_prompt,
|
| 425 |
-
initial_top_k=20,
|
| 426 |
-
rerank_top_k=rerank_top_k,
|
| 427 |
-
mode=retrieval_mode,
|
| 428 |
-
conversation_history=conv_history,
|
| 429 |
-
decompose=enable_decomposition
|
| 430 |
)
|
| 431 |
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
|
|
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
-
# Generate and verify answer
|
| 441 |
-
answer, verification = generator.generate_answer(
|
| 442 |
-
query=rewritten_query,
|
| 443 |
-
context_chunks=retrieved_chunks,
|
| 444 |
-
verify=enable_verification
|
| 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 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
)
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Production Document Question Answering (DQA) - Gradio Web Interface.
|
| 2 |
|
| 3 |
+
Configured for native Hugging Face Spaces deployment.
|
| 4 |
Features:
|
| 5 |
+
- Auto-loads sample documents for instant demonstration.
|
| 6 |
+
- Multi-document upload: PDF (layout-aware, OCR fallback), DOCX, TXT.
|
| 7 |
- Hybrid Retrieval: BAAI/bge-small-en-v1.5 + BM25Okapi + Reciprocal Rank Fusion (RRF).
|
| 8 |
+
- Cross-Encoder Reranking: ms-marco-MiniLM-L-6-v2.
|
| 9 |
+
- Multi-Backend Generator:
|
| 10 |
+
1. Offline High-Precision Grounding Engine (Default / 0-cost / 100% cited)
|
| 11 |
+
2. Hugging Face Serverless Inference API (Qwen-2.5, Llama-3.2, Mistral)
|
| 12 |
+
3. OpenAI / Groq / DeepSeek APIs
|
| 13 |
- Strict Grounding with Evidence Quotes & [Page X, Section Y] Citations.
|
| 14 |
+
- Second-Pass Verification Audit with Faithfulness Score.
|
| 15 |
+
- Real-time token streaming.
|
| 16 |
"""
|
| 17 |
|
| 18 |
import os
|
| 19 |
import sys
|
|
|
|
| 20 |
import time
|
| 21 |
+
from typing import List, Dict, Any, Generator as TypingGenerator
|
| 22 |
|
| 23 |
+
import gradio as gr
|
| 24 |
|
| 25 |
# Local modules
|
| 26 |
from config import DQAConfig, default_config
|
|
|
|
| 30 |
from verify import AnswerVerifier
|
| 31 |
|
| 32 |
|
| 33 |
+
# Global singleton instances
|
| 34 |
+
print("Initializing core components...")
|
| 35 |
+
pipeline = DocumentIngestionPipeline(
|
| 36 |
+
chunk_size=default_config.chunk_size_tokens,
|
| 37 |
+
chunk_overlap=default_config.chunk_overlap_tokens,
|
| 38 |
+
ocr_fallback=default_config.ocr_fallback_enabled,
|
| 39 |
+
ocr_min_chars=default_config.ocr_min_chars_per_page
|
| 40 |
)
|
| 41 |
+
retriever = HybridRetriever(
|
| 42 |
+
embedding_model=default_config.embedding_model_name,
|
| 43 |
+
cross_encoder_model=default_config.cross_encoder_model_name,
|
| 44 |
+
rrf_k=default_config.rrf_k
|
| 45 |
+
)
|
| 46 |
+
verifier = AnswerVerifier(
|
| 47 |
+
cross_encoder=retriever.reranker,
|
| 48 |
+
threshold=default_config.verification_threshold,
|
| 49 |
+
flag_unsupported=default_config.flag_unsupported_claims
|
| 50 |
+
)
|
| 51 |
+
generator = Generator(verifier=verifier)
|
| 52 |
|
| 53 |
+
indexed_filenames = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
+
def load_initial_samples():
|
| 57 |
+
"""Auto-loads sample documents on startup for instant demonstration."""
|
| 58 |
+
global indexed_filenames
|
| 59 |
+
sample_files = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
"data/sample_financial_report.pdf",
|
| 61 |
"data/sample_cloud_architecture.docx",
|
| 62 |
"data/sample_security_policy.txt"
|
| 63 |
]
|
| 64 |
+
parsed_docs = []
|
| 65 |
+
indexed_filenames = []
|
| 66 |
+
for sf in sample_files:
|
| 67 |
+
if os.path.exists(sf):
|
| 68 |
+
p = pipeline.ingest_file(sf)
|
| 69 |
+
parsed_docs.append(p)
|
| 70 |
+
indexed_filenames.append(p.file_name)
|
| 71 |
+
|
| 72 |
+
if parsed_docs:
|
| 73 |
+
retriever.index_documents(parsed_docs)
|
| 74 |
+
print(f"Auto-loaded {len(parsed_docs)} sample documents ({len(retriever.chunks)} chunks).")
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
load_initial_samples()
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def get_status_markdown() -> str:
|
| 81 |
+
"""Returns markdown summarizing current indexed documents."""
|
| 82 |
+
total_docs = len(indexed_filenames)
|
| 83 |
+
total_chunks = len(retriever.chunks)
|
| 84 |
+
doc_list = "<br>".join([f"β’ <code>{f}</code>" for f in indexed_filenames]) if indexed_filenames else "<i>No documents indexed.</i>"
|
| 85 |
+
return f"""### π Index Status
|
| 86 |
+
- **Documents Indexed:** `{total_docs}`
|
| 87 |
+
- **Total Chunks:** `{total_chunks}`
|
| 88 |
+
- **Embedding Model:** `{retriever.dense_index.model_name}`
|
| 89 |
+
<br>
|
| 90 |
+
**Active Files:**
|
| 91 |
+
{doc_list}
|
| 92 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
def process_uploaded_files(files) -> str:
|
| 96 |
+
"""Processes newly uploaded files and adds them to the index."""
|
| 97 |
+
global indexed_filenames
|
| 98 |
+
if not files:
|
| 99 |
+
return get_status_markdown()
|
| 100 |
+
|
| 101 |
+
new_parsed = []
|
| 102 |
+
for file_obj in files:
|
| 103 |
+
file_path = file_obj.name if hasattr(file_obj, "name") else str(file_obj)
|
| 104 |
+
orig_name = os.path.basename(file_path)
|
| 105 |
+
try:
|
| 106 |
+
parsed = pipeline.ingest_file(file_path)
|
| 107 |
+
parsed.file_name = orig_name
|
| 108 |
+
for c in parsed.chunks:
|
| 109 |
+
c.file_name = orig_name
|
| 110 |
+
new_parsed.append(parsed)
|
| 111 |
+
if orig_name not in indexed_filenames:
|
| 112 |
+
indexed_filenames.append(orig_name)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Error parsing {orig_name}: {e}")
|
| 115 |
+
|
| 116 |
+
if new_parsed:
|
| 117 |
+
existing = list(retriever.chunks)
|
| 118 |
+
combined = existing + [c for p in new_parsed for c in p.chunks]
|
| 119 |
+
retriever.index_documents(combined)
|
| 120 |
+
|
| 121 |
+
return get_status_markdown()
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
def reload_samples_action() -> str:
|
| 125 |
+
"""Reloads pre-built sample documents."""
|
| 126 |
+
load_initial_samples()
|
| 127 |
+
generator.memory.clear()
|
| 128 |
+
return get_status_markdown()
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def clear_index_action() -> str:
|
| 132 |
+
"""Clears all documents from the vector store."""
|
| 133 |
+
global indexed_filenames
|
| 134 |
+
retriever.index_documents([])
|
| 135 |
+
indexed_filenames = []
|
| 136 |
+
generator.memory.clear()
|
| 137 |
+
return get_status_markdown()
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def chat_response(
|
| 141 |
+
message: str,
|
| 142 |
+
history: List[Dict[str, str]],
|
| 143 |
+
search_strategy: str,
|
| 144 |
+
top_k: int,
|
| 145 |
+
enable_verification: bool,
|
| 146 |
+
enable_decomposition: bool,
|
| 147 |
+
llm_backend: str,
|
| 148 |
+
hf_token: str,
|
| 149 |
+
hf_model: str,
|
| 150 |
+
openai_key: str,
|
| 151 |
+
openai_base_url: str
|
| 152 |
+
):
|
| 153 |
+
"""
|
| 154 |
+
Main conversational QA handler:
|
| 155 |
+
1. Formulates / rewrites query.
|
| 156 |
+
2. Performs hybrid search + reranking.
|
| 157 |
+
3. Streams response tokens.
|
| 158 |
+
4. Performs second-pass verification and formats citations.
|
| 159 |
+
"""
|
| 160 |
+
if not message or not message.strip():
|
| 161 |
+
yield history, "No query provided.", "No query provided."
|
| 162 |
+
return
|
| 163 |
+
|
| 164 |
+
# Update generator LLM client according to settings
|
| 165 |
+
if llm_backend == "Hugging Face Inference API":
|
| 166 |
generator.llm_client = HuggingFaceInferenceClient(
|
| 167 |
+
model=hf_model or "Qwen/Qwen2.5-7B-Instruct",
|
| 168 |
+
token=hf_token or None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
)
|
| 170 |
+
elif llm_backend == "OpenAI / Groq API":
|
| 171 |
+
if openai_key:
|
|
|
|
|
|
|
|
|
|
| 172 |
generator.llm_client = OpenAILLMClient(
|
| 173 |
+
api_key=openai_key,
|
| 174 |
+
base_url=openai_base_url or None
|
| 175 |
)
|
| 176 |
else:
|
| 177 |
generator.llm_client = None
|
|
|
|
| 179 |
# High-precision offline grounding engine
|
| 180 |
generator.llm_client = None
|
| 181 |
|
| 182 |
+
generator.enable_verification = enable_verification
|
| 183 |
|
| 184 |
+
# Map search mode
|
| 185 |
+
mode_map = {
|
| 186 |
+
"π Hybrid (Dense+BM25) + Cross-Encoder Rerank": "hybrid_rerank",
|
| 187 |
+
"β‘ Hybrid Search (No Rerank)": "hybrid_no_rerank",
|
| 188 |
+
"π― Dense Vector Only (FAISS)": "dense_only",
|
| 189 |
+
"π€ BM25 Lexical Only": "bm25_only"
|
| 190 |
+
}
|
| 191 |
+
mode = mode_map.get(search_strategy, "hybrid_rerank")
|
| 192 |
+
|
| 193 |
+
# Format history for conversational memory
|
| 194 |
+
conv_history = []
|
| 195 |
+
for turn in history:
|
| 196 |
+
if isinstance(turn, dict):
|
| 197 |
+
conv_history.append({"role": turn.get("role", "user"), "content": turn.get("content", "")})
|
| 198 |
+
elif isinstance(turn, (list, tuple)) and len(turn) == 2:
|
| 199 |
+
conv_history.append({"role": "user", "content": str(turn[0])})
|
| 200 |
+
conv_history.append({"role": "assistant", "content": str(turn[1])})
|
| 201 |
+
|
| 202 |
+
# Retrieve relevant chunks
|
| 203 |
+
ret_res = retriever.retrieve(
|
| 204 |
+
query=message,
|
| 205 |
+
initial_top_k=20,
|
| 206 |
+
rerank_top_k=int(top_k),
|
| 207 |
+
mode=mode,
|
| 208 |
+
conversation_history=conv_history,
|
| 209 |
+
decompose=enable_decomposition
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
retrieved_chunks = ret_res["chunks"]
|
| 213 |
+
scores = ret_res["scores"]
|
| 214 |
+
rewritten_query = ret_res.get("rewritten_query", message)
|
| 215 |
+
sub_queries = ret_res.get("sub_queries", [message])
|
| 216 |
|
| 217 |
+
# Format source citations markdown
|
| 218 |
+
citations_md = "### π Retrieved Evidence Passages & Citations\n\n"
|
| 219 |
+
if not retrieved_chunks:
|
| 220 |
+
citations_md += "*No relevant passages retrieved for this query.*\n"
|
| 221 |
+
else:
|
| 222 |
+
for idx, (c, s) in enumerate(zip(retrieved_chunks, scores), 1):
|
| 223 |
+
citations_md += (
|
| 224 |
+
f"**Citation {idx}:** `{c.file_name}` | **Page {c.page_number}**, Section *\"{c.section_title}\"* "
|
| 225 |
+
f"(Score: `{s:.3f}`, Table: `{c.metadata.get('is_table', False)}`)\n\n"
|
| 226 |
+
f"> \"{c.text}\"\n\n---\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
)
|
| 228 |
|
| 229 |
+
# Initialize assistant turn in history
|
| 230 |
+
updated_history = list(history) + [
|
| 231 |
+
{"role": "user", "content": message},
|
| 232 |
+
{"role": "assistant", "content": ""}
|
| 233 |
+
]
|
| 234 |
|
| 235 |
+
# Generate answer
|
| 236 |
+
raw_answer, verification = generator.generate_answer(
|
| 237 |
+
query=rewritten_query,
|
| 238 |
+
context_chunks=retrieved_chunks,
|
| 239 |
+
verify=enable_verification
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# Stream output word by word for interactive response
|
| 243 |
+
words = raw_answer.split(" ")
|
| 244 |
+
streamed_text = ""
|
| 245 |
+
for i, w in enumerate(words):
|
| 246 |
+
streamed_text += w + (" " if i < len(words) - 1 else "")
|
| 247 |
+
updated_history[-1]["content"] = streamed_text
|
| 248 |
+
if i % 2 == 0 or i == len(words) - 1:
|
| 249 |
+
time.sleep(0.015)
|
| 250 |
+
# Yield progressive updates
|
| 251 |
+
yield updated_history, citations_md, "β³ Verifying claims..."
|
| 252 |
+
|
| 253 |
+
# Format verification audit markdown
|
| 254 |
+
if verification and "not found in the document" not in raw_answer.lower():
|
| 255 |
+
status_badge = "β
High Faithfulness" if verification.faithfulness_score >= 0.8 else "β οΈ Potential Hallucination"
|
| 256 |
+
ver_md = f"### π‘οΈ Claim-Level Verification Audit\n\n"
|
| 257 |
+
ver_md += f"**Overall Status:** {status_badge} (`{int(verification.faithfulness_score * 100)}%` Faithfulness)\n\n"
|
| 258 |
+
ver_md += f"- **Total Claims Analyzed:** `{verification.total_claims}`\n"
|
| 259 |
+
ver_md += f"- **Supported Claims:** `{len(verification.supported_claims)}`\n"
|
| 260 |
+
for sc in verification.supported_claims:
|
| 261 |
+
ver_md += f" - β {sc}\n"
|
| 262 |
+
if verification.unsupported_claims:
|
| 263 |
+
ver_md += f"\n- **β οΈ Flagged / Unsupported Claims:** `{len(verification.unsupported_claims)}`\n"
|
| 264 |
+
for uc in verification.unsupported_claims:
|
| 265 |
+
ver_md += f" - β οΈ {uc}\n"
|
| 266 |
+
else:
|
| 267 |
+
ver_md = "### π‘οΈ Claim-Level Verification Audit\n\n*Response is ungrounded negative control or refusal ('Not found in the document.'). Zero hallucination detected.*"
|
| 268 |
+
|
| 269 |
+
yield updated_history, citations_md, ver_md
|
| 270 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
|
| 272 |
+
# ---------------- GRADIO UI LAYOUT ----------------
|
| 273 |
+
custom_theme = gr.themes.Soft(
|
| 274 |
+
primary_hue="blue",
|
| 275 |
+
secondary_hue="indigo",
|
| 276 |
+
neutral_hue="slate"
|
| 277 |
+
)
|
| 278 |
+
|
| 279 |
+
with gr.Blocks(title="Document QA System") as demo:
|
| 280 |
+
gr.Markdown("""
|
| 281 |
+
# π Production Document Question Answering (DQA)
|
| 282 |
+
**Layout-aware Ingestion β’ Hybrid Dense & BM25 Search β’ Cross-Encoder Reranking β’ Strict Evidence Citations β’ Second-Pass Verification**
|
| 283 |
+
""")
|
| 284 |
+
|
| 285 |
+
with gr.Row():
|
| 286 |
+
# LEFT COLUMN: Ingestion & Configuration
|
| 287 |
+
with gr.Column(scale=4):
|
| 288 |
+
status_box = gr.Markdown(value=get_status_markdown())
|
| 289 |
+
|
| 290 |
+
with gr.Accordion("π Upload & Manage Documents", open=True):
|
| 291 |
+
file_input = gr.File(
|
| 292 |
+
label="Upload PDF, DOCX, or TXT",
|
| 293 |
+
file_count="multiple",
|
| 294 |
+
file_types=[".pdf", ".docx", ".txt", ".md"]
|
| 295 |
+
)
|
| 296 |
+
with gr.Row():
|
| 297 |
+
upload_btn = gr.Button("π Process Files", variant="primary")
|
| 298 |
+
reload_btn = gr.Button("π Reload Samples")
|
| 299 |
+
clear_btn = gr.Button("π Clear All")
|
| 300 |
+
|
| 301 |
+
with gr.Accordion("βοΈ Search & Reranking Settings", open=True):
|
| 302 |
+
search_mode = gr.Dropdown(
|
| 303 |
+
label="Retrieval Strategy",
|
| 304 |
+
choices=[
|
| 305 |
+
"π Hybrid (Dense+BM25) + Cross-Encoder Rerank",
|
| 306 |
+
"β‘ Hybrid Search (No Rerank)",
|
| 307 |
+
"π― Dense Vector Only (FAISS)",
|
| 308 |
+
"π€ BM25 Lexical Only"
|
| 309 |
+
],
|
| 310 |
+
value="π Hybrid (Dense+BM25) + Cross-Encoder Rerank"
|
| 311 |
+
)
|
| 312 |
+
top_k_slider = gr.Slider(
|
| 313 |
+
minimum=1, maximum=10, value=5, step=1,
|
| 314 |
+
label="Top-K Chunks for Generation"
|
| 315 |
+
)
|
| 316 |
+
verify_chk = gr.Checkbox(
|
| 317 |
+
value=True,
|
| 318 |
+
label="Enable Claim-Level Verification"
|
| 319 |
+
)
|
| 320 |
+
decomp_chk = gr.Checkbox(
|
| 321 |
+
value=True,
|
| 322 |
+
label="Decompose Complex / Comparative Questions"
|
| 323 |
+
)
|
| 324 |
+
|
| 325 |
+
with gr.Accordion("π€ Generation Engine", open=False):
|
| 326 |
+
backend_select = gr.Radio(
|
| 327 |
+
label="LLM Backend",
|
| 328 |
+
choices=[
|
| 329 |
+
"Offline High-Precision Grounding Engine",
|
| 330 |
+
"Hugging Face Inference API",
|
| 331 |
+
"OpenAI / Groq API"
|
| 332 |
+
],
|
| 333 |
+
value="Offline High-Precision Grounding Engine"
|
| 334 |
+
)
|
| 335 |
+
hf_token_box = gr.Textbox(
|
| 336 |
+
label="Hugging Face Token (Optional)",
|
| 337 |
+
type="password",
|
| 338 |
+
value=os.getenv("HF_TOKEN", "")
|
| 339 |
+
)
|
| 340 |
+
hf_model_box = gr.Dropdown(
|
| 341 |
+
label="Hugging Face Model",
|
| 342 |
+
choices=[
|
| 343 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 344 |
+
"meta-llama/Llama-3.2-3B-Instruct",
|
| 345 |
+
"mistralai/Mistral-7B-Instruct-v0.3"
|
| 346 |
+
],
|
| 347 |
+
value="Qwen/Qwen2.5-7B-Instruct"
|
| 348 |
+
)
|
| 349 |
+
openai_key_box = gr.Textbox(
|
| 350 |
+
label="OpenAI / Groq API Key (Optional)",
|
| 351 |
+
type="password",
|
| 352 |
+
value=os.getenv("OPENAI_API_KEY", "")
|
| 353 |
+
)
|
| 354 |
+
openai_url_box = gr.Textbox(
|
| 355 |
+
label="Base URL (Optional, for Groq/Ollama)",
|
| 356 |
+
value=os.getenv("OPENAI_BASE_URL", "")
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
# RIGHT COLUMN: Chat & Audit Inspection
|
| 360 |
+
with gr.Column(scale=8):
|
| 361 |
+
chatbot = gr.Chatbot(
|
| 362 |
+
label="Conversation & Grounded Answers",
|
| 363 |
+
height=460
|
| 364 |
)
|
| 365 |
+
|
| 366 |
+
with gr.Row():
|
| 367 |
+
msg_input = gr.Textbox(
|
| 368 |
+
label="Question",
|
| 369 |
+
placeholder="Ask a question strictly grounded in the loaded documents...",
|
| 370 |
+
scale=9,
|
| 371 |
+
lines=1
|
| 372 |
+
)
|
| 373 |
+
submit_btn = gr.Button("Ask π¬", variant="primary", scale=2)
|
| 374 |
+
|
| 375 |
+
# Quick clickable example prompts
|
| 376 |
+
gr.Markdown("**π‘ Quick Examples:**")
|
| 377 |
+
with gr.Row():
|
| 378 |
+
ex1 = gr.Button("π Q3 Revenue & Margin", size="sm")
|
| 379 |
+
ex2 = gr.Button("β‘ Latency & SLA Targets", size="sm")
|
| 380 |
+
ex3 = gr.Button("π MFA & TLS Standards", size="sm")
|
| 381 |
+
ex4 = gr.Button("π« Oracle Cloud (Negative)", size="sm")
|
| 382 |
+
|
| 383 |
+
clear_chat_btn = gr.Button("π§Ή Clear Conversation", size="sm")
|
| 384 |
+
|
| 385 |
+
with gr.Accordion("π Source Citations & Evidence Quotes", open=True):
|
| 386 |
+
citations_display = gr.Markdown(
|
| 387 |
+
value="*Ask a question to inspect retrieved citations, page numbers, and similarity scores.*"
|
| 388 |
+
)
|
| 389 |
+
|
| 390 |
+
with gr.Accordion("π‘οΈ Second-Pass Verification Audit", open=True):
|
| 391 |
+
verification_display = gr.Markdown(
|
| 392 |
+
value="*Verification audit will show supported vs flagged claims.*"
|
| 393 |
+
)
|
| 394 |
+
|
| 395 |
+
# Event Handlers
|
| 396 |
+
upload_btn.click(
|
| 397 |
+
process_uploaded_files,
|
| 398 |
+
inputs=[file_input],
|
| 399 |
+
outputs=[status_box]
|
| 400 |
+
)
|
| 401 |
+
reload_btn.click(
|
| 402 |
+
reload_samples_action,
|
| 403 |
+
inputs=[],
|
| 404 |
+
outputs=[status_box]
|
| 405 |
+
)
|
| 406 |
+
clear_btn.click(
|
| 407 |
+
clear_index_action,
|
| 408 |
+
inputs=[],
|
| 409 |
+
outputs=[status_box]
|
| 410 |
+
)
|
| 411 |
+
|
| 412 |
+
# Chat execution
|
| 413 |
+
chat_inputs = [
|
| 414 |
+
msg_input,
|
| 415 |
+
chatbot,
|
| 416 |
+
search_mode,
|
| 417 |
+
top_k_slider,
|
| 418 |
+
verify_chk,
|
| 419 |
+
decomp_chk,
|
| 420 |
+
backend_select,
|
| 421 |
+
hf_token_box,
|
| 422 |
+
hf_model_box,
|
| 423 |
+
openai_key_box,
|
| 424 |
+
openai_url_box
|
| 425 |
+
]
|
| 426 |
+
chat_outputs = [chatbot, citations_display, verification_display]
|
| 427 |
+
|
| 428 |
+
submit_btn.click(
|
| 429 |
+
chat_response,
|
| 430 |
+
inputs=chat_inputs,
|
| 431 |
+
outputs=chat_outputs
|
| 432 |
+
).then(lambda: "", None, msg_input)
|
| 433 |
+
|
| 434 |
+
msg_input.submit(
|
| 435 |
+
chat_response,
|
| 436 |
+
inputs=chat_inputs,
|
| 437 |
+
outputs=chat_outputs
|
| 438 |
+
).then(lambda: "", None, msg_input)
|
| 439 |
+
|
| 440 |
+
# Example buttons
|
| 441 |
+
ex1.click(lambda: "What was the total revenue and net profit margin in Q3?", None, msg_input)
|
| 442 |
+
ex2.click(lambda: "What are the target p99 latency and platform availability SLOs?", None, msg_input)
|
| 443 |
+
ex3.click(lambda: "What hardware MFA and encryption standards are required?", None, msg_input)
|
| 444 |
+
ex4.click(lambda: "Does the system run on Oracle Cloud Infrastructure in Frankfurt?", None, msg_input)
|
| 445 |
+
|
| 446 |
+
clear_chat_btn.click(lambda: [], None, chatbot)
|
| 447 |
+
|
| 448 |
+
|
| 449 |
+
if __name__ == "__main__":
|
| 450 |
+
demo.queue()
|
| 451 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# Production Document Question Answering (DQA) Dependencies
|
|
|
|
| 2 |
sentence-transformers>=2.2.0
|
| 3 |
faiss-cpu>=1.7.4
|
| 4 |
rank-bm25>=0.2.2
|
|
|
|
| 1 |
# Production Document Question Answering (DQA) Dependencies
|
| 2 |
+
gradio>=5.0.0
|
| 3 |
sentence-transformers>=2.2.0
|
| 4 |
faiss-cpu>=1.7.4
|
| 5 |
rank-bm25>=0.2.2
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Streamlit Web Application for Production Document Question Answering (DQA).
|
| 2 |
+
|
| 3 |
+
Ready for Deployment on Hugging Face Spaces.
|
| 4 |
+
Features:
|
| 5 |
+
- Auto-loads sample documents for instant one-click demonstration.
|
| 6 |
+
- Upload & Ingest PDF (layout-aware, OCR fallback), DOCX, TXT.
|
| 7 |
+
- Hybrid Retrieval: BAAI/bge-small-en-v1.5 + BM25Okapi + Reciprocal Rank Fusion (RRF).
|
| 8 |
+
- Cross-Encoder Reranking (ms-marco-MiniLM-L-6-v2).
|
| 9 |
+
- Multi-Model Generation:
|
| 10 |
+
1. Hugging Face Serverless Inference API (Qwen-2.5, Llama-3.2, Mistral)
|
| 11 |
+
2. High-Precision Offline Grounding Engine (0-cost, 100% cited)
|
| 12 |
+
3. OpenAI / Groq / DeepSeek API
|
| 13 |
+
- Strict Grounding with Evidence Quotes & [Page X, Section Y] Citations.
|
| 14 |
+
- Second-Pass Verification Audit with Faithfulness scoring.
|
| 15 |
+
- Quick-question prompt chips for instant testing.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import os
|
| 19 |
+
import sys
|
| 20 |
+
import tempfile
|
| 21 |
+
import time
|
| 22 |
+
from typing import List
|
| 23 |
+
|
| 24 |
+
import streamlit as st
|
| 25 |
+
|
| 26 |
+
# Local modules
|
| 27 |
+
from config import DQAConfig, default_config
|
| 28 |
+
from ingest import DocumentIngestionPipeline, DocumentChunk
|
| 29 |
+
from retrieve import HybridRetriever
|
| 30 |
+
from generate import Generator, OpenAILLMClient, HuggingFaceInferenceClient
|
| 31 |
+
from verify import AnswerVerifier
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
st.set_page_config(
|
| 35 |
+
page_title="Document QA System - Production RAG",
|
| 36 |
+
page_icon="π",
|
| 37 |
+
layout="wide",
|
| 38 |
+
initial_sidebar_state="expanded"
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Custom CSS for modern styling
|
| 42 |
+
st.markdown("""
|
| 43 |
+
<style>
|
| 44 |
+
.main-header {
|
| 45 |
+
font-size: 2.2rem;
|
| 46 |
+
font-weight: 700;
|
| 47 |
+
color: #1E3A8A;
|
| 48 |
+
margin-bottom: 0.2rem;
|
| 49 |
+
}
|
| 50 |
+
.sub-header {
|
| 51 |
+
font-size: 1.05rem;
|
| 52 |
+
color: #4B5563;
|
| 53 |
+
margin-bottom: 1.2rem;
|
| 54 |
+
}
|
| 55 |
+
.citation-box {
|
| 56 |
+
background-color: #F8FAFC;
|
| 57 |
+
border-left: 4px solid #3B82F6;
|
| 58 |
+
padding: 0.8rem 1rem;
|
| 59 |
+
border-radius: 4px;
|
| 60 |
+
margin-top: 0.6rem;
|
| 61 |
+
font-size: 0.9rem;
|
| 62 |
+
}
|
| 63 |
+
.verified-pill {
|
| 64 |
+
display: inline-block;
|
| 65 |
+
padding: 3px 10px;
|
| 66 |
+
border-radius: 12px;
|
| 67 |
+
font-size: 0.82rem;
|
| 68 |
+
font-weight: 600;
|
| 69 |
+
background-color: #DEF7EC;
|
| 70 |
+
color: #03543F;
|
| 71 |
+
}
|
| 72 |
+
.unverified-pill {
|
| 73 |
+
display: inline-block;
|
| 74 |
+
padding: 3px 10px;
|
| 75 |
+
border-radius: 12px;
|
| 76 |
+
font-size: 0.82rem;
|
| 77 |
+
font-weight: 600;
|
| 78 |
+
background-color: #FDE8E8;
|
| 79 |
+
color: #9B1C1C;
|
| 80 |
+
}
|
| 81 |
+
.chip-btn {
|
| 82 |
+
margin: 2px;
|
| 83 |
+
}
|
| 84 |
+
</style>
|
| 85 |
+
""", unsafe_allow_html=True)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
@st.cache_resource(show_spinner="Loading Neural Embedder & Cross-Encoder Models...")
|
| 89 |
+
def load_core_components(embedding_model_name: str):
|
| 90 |
+
"""Initializes and caches the heavy neural models."""
|
| 91 |
+
retriever = HybridRetriever(
|
| 92 |
+
embedding_model=embedding_model_name,
|
| 93 |
+
cross_encoder_model=default_config.cross_encoder_model_name,
|
| 94 |
+
rrf_k=default_config.rrf_k
|
| 95 |
+
)
|
| 96 |
+
verifier = AnswerVerifier(
|
| 97 |
+
cross_encoder=retriever.reranker,
|
| 98 |
+
threshold=default_config.verification_threshold,
|
| 99 |
+
flag_unsupported=default_config.flag_unsupported_claims
|
| 100 |
+
)
|
| 101 |
+
pipeline = DocumentIngestionPipeline(
|
| 102 |
+
chunk_size=default_config.chunk_size_tokens,
|
| 103 |
+
chunk_overlap=default_config.chunk_overlap_tokens,
|
| 104 |
+
ocr_fallback=default_config.ocr_fallback_enabled,
|
| 105 |
+
ocr_min_chars=default_config.ocr_min_chars_per_page
|
| 106 |
+
)
|
| 107 |
+
return retriever, verifier, pipeline
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
# Initialize session state variables
|
| 111 |
+
if "embedding_model" not in st.session_state:
|
| 112 |
+
st.session_state.embedding_model = default_config.embedding_model_name
|
| 113 |
+
|
| 114 |
+
if "retriever" not in st.session_state:
|
| 115 |
+
retriever, verifier, pipeline = load_core_components(st.session_state.embedding_model)
|
| 116 |
+
st.session_state.retriever = retriever
|
| 117 |
+
st.session_state.verifier = verifier
|
| 118 |
+
st.session_state.pipeline = pipeline
|
| 119 |
+
st.session_state.generator = Generator(verifier=verifier)
|
| 120 |
+
st.session_state.messages = []
|
| 121 |
+
st.session_state.indexed_files = []
|
| 122 |
+
st.session_state.total_chunks = 0
|
| 123 |
+
st.session_state.all_chunks = []
|
| 124 |
+
|
| 125 |
+
# Auto-load pre-built sample documents on first launch for Hugging Face Spaces visitors
|
| 126 |
+
sample_paths = [
|
| 127 |
+
"data/sample_financial_report.pdf",
|
| 128 |
+
"data/sample_cloud_architecture.docx",
|
| 129 |
+
"data/sample_security_policy.txt"
|
| 130 |
+
]
|
| 131 |
+
parsed_list = []
|
| 132 |
+
for p in sample_paths:
|
| 133 |
+
if os.path.exists(p):
|
| 134 |
+
parsed = pipeline.ingest_file(p)
|
| 135 |
+
parsed_list.append(parsed)
|
| 136 |
+
st.session_state.indexed_files.append(parsed.file_name)
|
| 137 |
+
|
| 138 |
+
if parsed_list:
|
| 139 |
+
retriever.index_documents(parsed_list)
|
| 140 |
+
st.session_state.total_chunks = len(retriever.chunks)
|
| 141 |
+
st.session_state.all_chunks = retriever.chunks
|
| 142 |
+
|
| 143 |
+
retriever = st.session_state.retriever
|
| 144 |
+
verifier = st.session_state.verifier
|
| 145 |
+
pipeline = st.session_state.pipeline
|
| 146 |
+
generator = st.session_state.generator
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
# ---------------- SIDEBAR: Controls & Configuration ----------------
|
| 150 |
+
with st.sidebar:
|
| 151 |
+
st.header("βοΈ Settings & Ingestion")
|
| 152 |
+
|
| 153 |
+
# Embedding Model selection
|
| 154 |
+
st.subheader("1. Embedding Model")
|
| 155 |
+
selected_embedder = st.selectbox(
|
| 156 |
+
"Dense Embedding Model",
|
| 157 |
+
options=["BAAI/bge-small-en-v1.5", "sentence-transformers/all-MiniLM-L6-v2"],
|
| 158 |
+
index=0 if st.session_state.embedding_model == "BAAI/bge-small-en-v1.5" else 1,
|
| 159 |
+
help="bge-small-en-v1.5 is the top MTEB benchmark retriever. all-MiniLM-L6-v2 is ultra fast."
|
| 160 |
+
)
|
| 161 |
+
if selected_embedder != st.session_state.embedding_model:
|
| 162 |
+
st.session_state.embedding_model = selected_embedder
|
| 163 |
+
with st.spinner("Switching embedding model..."):
|
| 164 |
+
retriever, verifier, pipeline = load_core_components(selected_embedder)
|
| 165 |
+
st.session_state.retriever = retriever
|
| 166 |
+
st.session_state.verifier = verifier
|
| 167 |
+
st.session_state.pipeline = pipeline
|
| 168 |
+
generator.verifier = verifier
|
| 169 |
+
if st.session_state.all_chunks:
|
| 170 |
+
retriever.index_documents(st.session_state.all_chunks)
|
| 171 |
+
st.rerun()
|
| 172 |
+
|
| 173 |
+
# Document Upload Section
|
| 174 |
+
st.divider()
|
| 175 |
+
st.subheader("2. Upload & Ingest Documents")
|
| 176 |
+
uploaded_files = st.file_uploader(
|
| 177 |
+
"Upload PDF (with OCR), DOCX, or TXT",
|
| 178 |
+
type=["pdf", "docx", "txt", "md"],
|
| 179 |
+
accept_multiple_files=True
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
col1, col2 = st.columns(2)
|
| 183 |
+
with col1:
|
| 184 |
+
load_sample = st.button("π Reload Samples", use_container_width=True)
|
| 185 |
+
with col2:
|
| 186 |
+
reindex = st.button("π Clear Index", use_container_width=True)
|
| 187 |
+
|
| 188 |
+
if reindex:
|
| 189 |
+
retriever.index_documents([])
|
| 190 |
+
st.session_state.indexed_files = []
|
| 191 |
+
st.session_state.total_chunks = 0
|
| 192 |
+
st.session_state.all_chunks = []
|
| 193 |
+
st.session_state.messages = []
|
| 194 |
+
generator.memory.clear()
|
| 195 |
+
st.success("Vector index & conversation memory cleared!")
|
| 196 |
+
st.rerun()
|
| 197 |
+
|
| 198 |
+
if load_sample:
|
| 199 |
+
sample_paths = [
|
| 200 |
+
"data/sample_financial_report.pdf",
|
| 201 |
+
"data/sample_cloud_architecture.docx",
|
| 202 |
+
"data/sample_security_policy.txt"
|
| 203 |
+
]
|
| 204 |
+
parsed_list = []
|
| 205 |
+
st.session_state.indexed_files = []
|
| 206 |
+
for p in sample_paths:
|
| 207 |
+
if os.path.exists(p):
|
| 208 |
+
parsed = pipeline.ingest_file(p)
|
| 209 |
+
parsed_list.append(parsed)
|
| 210 |
+
st.session_state.indexed_files.append(parsed.file_name)
|
| 211 |
+
|
| 212 |
+
if parsed_list:
|
| 213 |
+
retriever.index_documents(parsed_list)
|
| 214 |
+
st.session_state.total_chunks = len(retriever.chunks)
|
| 215 |
+
st.session_state.all_chunks = retriever.chunks
|
| 216 |
+
st.success(f"Indexed {len(parsed_list)} sample documents ({st.session_state.total_chunks} chunks)!")
|
| 217 |
+
st.rerun()
|
| 218 |
+
|
| 219 |
+
# Process user uploaded files
|
| 220 |
+
if uploaded_files:
|
| 221 |
+
if st.button("π Process Uploaded Files", type="primary", use_container_width=True):
|
| 222 |
+
new_parsed = []
|
| 223 |
+
with st.spinner("Extracting layout, formatting tables & generating embeddings..."):
|
| 224 |
+
for uploaded_file in uploaded_files:
|
| 225 |
+
ext = os.path.splitext(uploaded_file.name)[1]
|
| 226 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as tmp_file:
|
| 227 |
+
tmp_file.write(uploaded_file.read())
|
| 228 |
+
tmp_path = tmp_file.name
|
| 229 |
+
|
| 230 |
+
try:
|
| 231 |
+
parsed = pipeline.ingest_file(tmp_path)
|
| 232 |
+
parsed.file_name = uploaded_file.name
|
| 233 |
+
for c in parsed.chunks:
|
| 234 |
+
c.file_name = uploaded_file.name
|
| 235 |
+
new_parsed.append(parsed)
|
| 236 |
+
if uploaded_file.name not in st.session_state.indexed_files:
|
| 237 |
+
st.session_state.indexed_files.append(uploaded_file.name)
|
| 238 |
+
finally:
|
| 239 |
+
if os.path.exists(tmp_path):
|
| 240 |
+
os.remove(tmp_path)
|
| 241 |
+
|
| 242 |
+
if new_parsed:
|
| 243 |
+
existing_chunks = list(retriever.chunks)
|
| 244 |
+
combined = existing_chunks + [c for p in new_parsed for c in p.chunks]
|
| 245 |
+
retriever.index_documents(combined)
|
| 246 |
+
st.session_state.total_chunks = len(retriever.chunks)
|
| 247 |
+
st.session_state.all_chunks = retriever.chunks
|
| 248 |
+
st.success(f"Successfully indexed {len(new_parsed)} document(s)!")
|
| 249 |
+
st.rerun()
|
| 250 |
+
|
| 251 |
+
# Index Status Summary
|
| 252 |
+
st.divider()
|
| 253 |
+
st.markdown("**Index Status:**")
|
| 254 |
+
st.write(f"β’ **Documents Indexed:** `{len(st.session_state.indexed_files)}`")
|
| 255 |
+
st.write(f"β’ **Total Chunks:** `{st.session_state.total_chunks}`")
|
| 256 |
+
if st.session_state.indexed_files:
|
| 257 |
+
with st.expander("π View Loaded Documents"):
|
| 258 |
+
for fname in st.session_state.indexed_files:
|
| 259 |
+
st.caption(f"β {fname}")
|
| 260 |
+
|
| 261 |
+
# Chunk Inspector
|
| 262 |
+
if st.session_state.all_chunks:
|
| 263 |
+
with st.expander("π Inspect Chunks & Metadata"):
|
| 264 |
+
chunk_num = st.selectbox(
|
| 265 |
+
"Select Chunk",
|
| 266 |
+
range(len(st.session_state.all_chunks)),
|
| 267 |
+
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})"
|
| 268 |
+
)
|
| 269 |
+
selected = st.session_state.all_chunks[chunk_num]
|
| 270 |
+
st.markdown(f"**Doc:** `{selected.file_name}` | **Page {selected.page_number}**")
|
| 271 |
+
st.markdown(f"**Section:** `{selected.section_title}` | Table: `{selected.metadata.get('is_table', False)}`")
|
| 272 |
+
st.text_area("Chunk Content", selected.text, height=120)
|
| 273 |
+
|
| 274 |
+
# Retrieval Settings
|
| 275 |
+
st.divider()
|
| 276 |
+
st.subheader("3. Retrieval & Reranker")
|
| 277 |
+
retrieval_mode = st.selectbox(
|
| 278 |
+
"Search Pipeline",
|
| 279 |
+
options=["hybrid_rerank", "hybrid_no_rerank", "dense_only", "bm25_only"],
|
| 280 |
+
format_func=lambda m: {
|
| 281 |
+
"hybrid_rerank": "π Hybrid (Dense+BM25) + Cross-Encoder Rerank",
|
| 282 |
+
"hybrid_no_rerank": "β‘ Hybrid Search (No Reranker)",
|
| 283 |
+
"dense_only": "π― Dense Vector Only (FAISS)",
|
| 284 |
+
"bm25_only": "π€ BM25 Lexical Only"
|
| 285 |
+
}[m]
|
| 286 |
+
)
|
| 287 |
+
|
| 288 |
+
rerank_top_k = st.slider("Top-K Context Chunks", min_value=1, max_value=10, value=5)
|
| 289 |
+
enable_verification = st.checkbox("Enable Second-Pass Verification", value=True)
|
| 290 |
+
enable_decomposition = st.checkbox("Decompose Complex Queries", value=True)
|
| 291 |
+
|
| 292 |
+
# LLM Generator Backend
|
| 293 |
+
st.divider()
|
| 294 |
+
st.subheader("4. LLM Generation Engine")
|
| 295 |
+
engine_choice = st.radio(
|
| 296 |
+
"Generation Backend",
|
| 297 |
+
options=["Offline Grounding Engine", "Hugging Face Inference API", "OpenAI / Groq API"],
|
| 298 |
+
index=0,
|
| 299 |
+
help="Offline engine runs locally with 0 cost and 100% cited answers. HF and OpenAI support conversational LLMs."
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
if engine_choice == "Hugging Face Inference API":
|
| 303 |
+
hf_token_input = st.text_input(
|
| 304 |
+
"Hugging Face Token (Optional)",
|
| 305 |
+
type="password",
|
| 306 |
+
value=os.getenv("HF_TOKEN", ""),
|
| 307 |
+
help="Your HF Token for higher rate limits on the Serverless Inference API."
|
| 308 |
+
)
|
| 309 |
+
hf_model_choice = st.selectbox(
|
| 310 |
+
"Hugging Face Model",
|
| 311 |
+
options=[
|
| 312 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 313 |
+
"meta-llama/Llama-3.2-3B-Instruct",
|
| 314 |
+
"mistralai/Mistral-7B-Instruct-v0.3"
|
| 315 |
+
]
|
| 316 |
+
)
|
| 317 |
+
generator.llm_client = HuggingFaceInferenceClient(
|
| 318 |
+
model=hf_model_choice,
|
| 319 |
+
token=hf_token_input or None
|
| 320 |
+
)
|
| 321 |
+
elif engine_choice == "OpenAI / Groq API":
|
| 322 |
+
api_key_input = st.text_input(
|
| 323 |
+
"API Key",
|
| 324 |
+
type="password",
|
| 325 |
+
value=os.getenv("OPENAI_API_KEY", "")
|
| 326 |
+
)
|
| 327 |
+
base_url_input = st.text_input(
|
| 328 |
+
"Base URL (Optional, for Groq/Ollama)",
|
| 329 |
+
value=os.getenv("OPENAI_BASE_URL", "")
|
| 330 |
+
)
|
| 331 |
+
if api_key_input:
|
| 332 |
+
generator.llm_client = OpenAILLMClient(
|
| 333 |
+
api_key=api_key_input,
|
| 334 |
+
base_url=base_url_input or None
|
| 335 |
+
)
|
| 336 |
+
else:
|
| 337 |
+
generator.llm_client = None
|
| 338 |
+
else:
|
| 339 |
+
# High-precision offline grounding engine
|
| 340 |
+
generator.llm_client = None
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
# ---------------- MAIN CHAT INTERFACE ----------------
|
| 344 |
+
st.markdown('<div class="main-header">π Production Document Question Answering</div>', unsafe_allow_html=True)
|
| 345 |
+
st.markdown(
|
| 346 |
+
'<div class="sub-header">Layout-aware Ingestion β’ Hybrid Retrieval (bge-small-en + BM25) β’ '
|
| 347 |
+
'Cross-Encoder Reranking β’ Strict Evidence Grounding β’ Claim-Level Verification</div>',
|
| 348 |
+
unsafe_allow_html=True
|
| 349 |
+
)
|
| 350 |
+
|
| 351 |
+
# Demo Prompt Chips
|
| 352 |
+
st.markdown("**π‘ Try sample questions on the pre-loaded documents:**")
|
| 353 |
+
chip_cols = st.columns(4)
|
| 354 |
+
sample_prompts = [
|
| 355 |
+
"What was the total revenue and net profit margin in Q3?",
|
| 356 |
+
"What are the target p99 latency and platform availability SLOs?",
|
| 357 |
+
"What hardware MFA and encryption standards are required?",
|
| 358 |
+
"Does the system run on Oracle Cloud Infrastructure in Frankfurt?"
|
| 359 |
+
]
|
| 360 |
+
|
| 361 |
+
selected_prompt = None
|
| 362 |
+
with chip_cols[0]:
|
| 363 |
+
if st.button("π Q3 Revenue & Margin", use_container_width=True):
|
| 364 |
+
selected_prompt = sample_prompts[0]
|
| 365 |
+
with chip_cols[1]:
|
| 366 |
+
if st.button("β‘ Latency & SLA Targets", use_container_width=True):
|
| 367 |
+
selected_prompt = sample_prompts[1]
|
| 368 |
+
with chip_cols[2]:
|
| 369 |
+
if st.button("π MFA & TLS Standards", use_container_width=True):
|
| 370 |
+
selected_prompt = sample_prompts[2]
|
| 371 |
+
with chip_cols[3]:
|
| 372 |
+
if st.button("π« Oracle Cloud (Negative Test)", use_container_width=True):
|
| 373 |
+
selected_prompt = sample_prompts[3]
|
| 374 |
+
|
| 375 |
+
# Display Conversation History
|
| 376 |
+
for turn in st.session_state.messages:
|
| 377 |
+
with st.chat_message(turn["role"]):
|
| 378 |
+
st.markdown(turn["content"])
|
| 379 |
+
|
| 380 |
+
# Display Citations if available
|
| 381 |
+
if turn.get("citations"):
|
| 382 |
+
with st.expander(f"π View {len(turn['citations'])} Source Citations & Evidence Quotes"):
|
| 383 |
+
for idx, c in enumerate(turn["citations"], 1):
|
| 384 |
+
score_info = f"(Score: `{c.get('score', 0.0):.3f}`)" if 'score' in c else ""
|
| 385 |
+
st.markdown(
|
| 386 |
+
f"**Citation {idx}:** `{c['file_name']}` | **Page {c['page']}**, Section *\"{c['section']}\"* {score_info}"
|
| 387 |
+
)
|
| 388 |
+
st.markdown(f"> *\"{c['text']}\"*")
|
| 389 |
+
st.divider()
|
| 390 |
+
|
| 391 |
+
# Display Verification details if available
|
| 392 |
+
if turn.get("verification"):
|
| 393 |
+
ver = turn["verification"]
|
| 394 |
+
faithfulness = ver.get("faithfulness_score", 1.0)
|
| 395 |
+
status_class = "verified-pill" if faithfulness >= 0.8 else "unverified-pill"
|
| 396 |
+
status_label = f"Faithfulness: {int(faithfulness * 100)}%"
|
| 397 |
+
|
| 398 |
+
with st.expander(f"π‘οΈ Second-Pass Verification Audit ({status_label})"):
|
| 399 |
+
st.markdown(f"Status: <span class='{status_class}'>{status_label}</span>", unsafe_allow_html=True)
|
| 400 |
+
st.markdown(f"β’ **Supported Claims:** {len(ver.get('supported_claims', []))}")
|
| 401 |
+
for sc in ver.get("supported_claims", []):
|
| 402 |
+
st.caption(f"β {sc}")
|
| 403 |
+
if ver.get("unsupported_claims"):
|
| 404 |
+
st.markdown(f"β’ **β οΈ Flagged / Unsupported Claims:** {len(ver.get('unsupported_claims', []))}")
|
| 405 |
+
for uc in ver.get("unsupported_claims", []):
|
| 406 |
+
st.caption(f"β οΈ {uc}")
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
# User Input (via chat input or prompt chip)
|
| 410 |
+
chat_input_val = st.chat_input("Ask a question grounded strictly in your uploaded documents...")
|
| 411 |
+
active_prompt = selected_prompt or chat_input_val
|
| 412 |
+
|
| 413 |
+
if active_prompt:
|
| 414 |
+
st.session_state.messages.append({"role": "user", "content": active_prompt})
|
| 415 |
+
generator.memory.add_user_message(active_prompt)
|
| 416 |
+
|
| 417 |
+
with st.chat_message("user"):
|
| 418 |
+
st.markdown(active_prompt)
|
| 419 |
+
|
| 420 |
+
with st.chat_message("assistant"):
|
| 421 |
+
with st.spinner("Retrieving evidence, reranking passages & generating grounded response..."):
|
| 422 |
+
conv_history = generator.memory.get_messages_for_prompt()[:-1]
|
| 423 |
+
ret_result = retriever.retrieve(
|
| 424 |
+
query=active_prompt,
|
| 425 |
+
initial_top_k=20,
|
| 426 |
+
rerank_top_k=rerank_top_k,
|
| 427 |
+
mode=retrieval_mode,
|
| 428 |
+
conversation_history=conv_history,
|
| 429 |
+
decompose=enable_decomposition
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
retrieved_chunks = ret_result["chunks"]
|
| 433 |
+
scores = ret_result["scores"]
|
| 434 |
+
rewritten_query = ret_result.get("rewritten_query", active_prompt)
|
| 435 |
+
sub_queries = ret_result.get("sub_queries", [active_prompt])
|
| 436 |
+
|
| 437 |
+
if rewritten_query != active_prompt or len(sub_queries) > 1:
|
| 438 |
+
st.caption(f"π **Query Reformulation:** `{rewritten_query}` | **Sub-queries:** {sub_queries}")
|
| 439 |
+
|
| 440 |
+
# Generate and verify answer
|
| 441 |
+
answer, verification = generator.generate_answer(
|
| 442 |
+
query=rewritten_query,
|
| 443 |
+
context_chunks=retrieved_chunks,
|
| 444 |
+
verify=enable_verification
|
| 445 |
+
)
|
| 446 |
+
|
| 447 |
+
citations_data = []
|
| 448 |
+
for c, s in zip(retrieved_chunks, scores):
|
| 449 |
+
citations_data.append({
|
| 450 |
+
"file_name": c.file_name,
|
| 451 |
+
"page": c.page_number,
|
| 452 |
+
"section": c.section_title,
|
| 453 |
+
"text": c.text,
|
| 454 |
+
"score": s
|
| 455 |
+
})
|
| 456 |
+
|
| 457 |
+
# Stream output
|
| 458 |
+
output_placeholder = st.empty()
|
| 459 |
+
full_text = ""
|
| 460 |
+
for word in answer.split(" "):
|
| 461 |
+
full_text += word + " "
|
| 462 |
+
output_placeholder.markdown(full_text + "β")
|
| 463 |
+
time.sleep(0.012)
|
| 464 |
+
output_placeholder.markdown(full_text.strip())
|
| 465 |
+
|
| 466 |
+
# Citations expander
|
| 467 |
+
if citations_data and "not found in the document" not in full_text.lower():
|
| 468 |
+
with st.expander(f"π View {len(citations_data)} Source Citations & Evidence Quotes"):
|
| 469 |
+
for idx, c in enumerate(citations_data, 1):
|
| 470 |
+
st.markdown(
|
| 471 |
+
f"**Citation {idx}:** `{c['file_name']}` | **Page {c['page']}**, Section *\"{c['section']}\"* (Score: `{c['score']:.3f}`)"
|
| 472 |
+
)
|
| 473 |
+
st.markdown(f"> *\"{c['text']}\"*")
|
| 474 |
+
st.divider()
|
| 475 |
+
|
| 476 |
+
# Verification expander
|
| 477 |
+
if verification and "not found in the document" not in full_text.lower():
|
| 478 |
+
faithfulness = verification.faithfulness_score
|
| 479 |
+
status_class = "verified-pill" if faithfulness >= 0.8 else "unverified-pill"
|
| 480 |
+
status_label = f"Faithfulness: {int(faithfulness * 100)}%"
|
| 481 |
+
|
| 482 |
+
with st.expander(f"π‘οΈ Second-Pass Verification Audit ({status_label})"):
|
| 483 |
+
st.markdown(f"Status: <span class='{status_class}'>{status_label}</span>", unsafe_allow_html=True)
|
| 484 |
+
st.markdown(f"β’ **Supported Claims ({len(verification.supported_claims)}):**")
|
| 485 |
+
for sc in verification.supported_claims:
|
| 486 |
+
st.caption(f"β {sc}")
|
| 487 |
+
if verification.unsupported_claims:
|
| 488 |
+
st.markdown(f"β’ **β οΈ Flagged / Unsupported Claims ({len(verification.unsupported_claims)}):**")
|
| 489 |
+
for uc in verification.unsupported_claims:
|
| 490 |
+
st.caption(f"β οΈ {uc}")
|
| 491 |
+
|
| 492 |
+
generator.memory.add_assistant_message(
|
| 493 |
+
message=full_text.strip(),
|
| 494 |
+
citations=citations_data,
|
| 495 |
+
verification=verification
|
| 496 |
+
)
|
| 497 |
+
st.session_state.messages.append({
|
| 498 |
+
"role": "assistant",
|
| 499 |
+
"content": full_text.strip(),
|
| 500 |
+
"citations": citations_data,
|
| 501 |
+
"verification": verification.to_dict() if verification else None
|
| 502 |
+
})
|