| """Gradio demo — RAG Compliance Intelligence. |
| |
| Natural-language Q&A across AML / GDPR / SOX compliance documents with cited |
| sources. FAISS + sentence-transformers + offline generator (no API key). |
| """ |
| from __future__ import annotations |
|
|
| import sys |
| from pathlib import Path |
|
|
| import gradio as gr |
|
|
| sys.path.insert(0, str(Path(__file__).parent)) |
| from rag.pipeline import RAGPipeline |
|
|
| |
| PIPELINE = RAGPipeline() |
| INDEXED = PIPELINE.index_directory(Path(__file__).parent / "data" / "sample_docs") |
|
|
| SAMPLES = [ |
| "What is the SAR filing threshold?", |
| "When must a data breach be reported to authorities?", |
| "What are the GDPR rights of data subjects?", |
| "What is the CTR reporting threshold?", |
| "Explain SOX section 404 requirements", |
| "What is Enhanced Due Diligence?", |
| "How long must AML records be retained?", |
| ] |
|
|
|
|
| def answer(question: str): |
| if not question or not question.strip(): |
| return "Enter a question above.", "", "" |
| r = PIPELINE.query(question) |
| meta = ( |
| f"**Confidence:** {r.confidence:.0%} · " |
| f"**Latency:** {r.latency_ms:.0f} ms · " |
| f"**Citations:** {len(r.citations)}" |
| ) |
| sources = "" |
| for c in r.citations: |
| sources += f"- **{c.marker}** — {c.excerpt[:180].strip()}…\n" |
| scores = "" |
| for res in (r.retrieval_results or [])[:5]: |
| bar = "█" * max(1, round(res.score * 20)) |
| scores += f"`{res.score:.3f}` {bar} {res.source_doc} §{res.section[:40]}\n" |
| body = f"### Answer\n{r.answer}\n\n{meta}" |
| src = f"### Sources\n{sources}" if sources else "" |
| sc = f"### Retrieval scores\n{scores}" if scores else "" |
| return body, src, sc |
|
|
|
|
| with gr.Blocks(title="RAG Compliance Intelligence", theme=gr.themes.Soft()) as demo: |
| gr.Markdown( |
| "# ⚖️ RAG Compliance Intelligence\n" |
| "Ask questions across **AML, GDPR, and SOX** compliance documents — " |
| "answers come with cited sources. FAISS + sentence-transformers, no API key. \n" |
| f"*{INDEXED} document chunks indexed · " |
| "[source](https://github.com/shaikn6/rag-compliance-intelligence)*" |
| ) |
| with gr.Row(): |
| q = gr.Textbox( |
| label="Your question", |
| placeholder="e.g. What is the threshold for filing a Currency Transaction Report?", |
| scale=4, |
| ) |
| btn = gr.Button("Ask", variant="primary", scale=1) |
| gr.Examples(SAMPLES, inputs=q, label="Sample questions") |
| out = gr.Markdown() |
| src = gr.Markdown() |
| sc = gr.Markdown() |
| btn.click(answer, inputs=q, outputs=[out, src, sc]) |
| q.submit(answer, inputs=q, outputs=[out, src, sc]) |
|
|
| if __name__ == "__main__": |
| demo.launch() |
|
|