Spaces:
Build error
Build error
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,13 +1,102 @@
|
|
| 1 |
---
|
| 2 |
-
title: Enterprise
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
-
pinned:
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Enterprise RAG System
|
| 3 |
+
emoji: π’
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Enterprise Knowledge Retrieval System
|
| 13 |
+
|
| 14 |
+
A production-oriented Retrieval-Augmented Generation (RAG) pipeline built for enterprise document Q&A. Demonstrates AI engineering depth across ingestion, retrieval, generation, evaluation, and observability β fully free to run.
|
| 15 |
+
|
| 16 |
+
## What This System Does
|
| 17 |
+
|
| 18 |
+
Upload any enterprise PDF (policy documents, financial reports, technical manuals, compliance frameworks) and ask natural language questions. The system retrieves semantically relevant sections and generates grounded answers using Mistral-7B β refusing to answer when evidence is insufficient rather than hallucinating.
|
| 19 |
+
|
| 20 |
+
## System Architecture
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
PDF Upload β Text Extraction β Chunking β Embedding β FAISS Index
|
| 24 |
+
β
|
| 25 |
+
User Query β Embed Query β Cosine Similarity Search β Top-K Chunks
|
| 26 |
+
β
|
| 27 |
+
Relevance Threshold Check
|
| 28 |
+
β β
|
| 29 |
+
Fallback Prompt Assembly
|
| 30 |
+
β
|
| 31 |
+
Mistral-7B via HF API
|
| 32 |
+
β
|
| 33 |
+
Grounded Answer + Evaluation Scores
|
| 34 |
+
β
|
| 35 |
+
Langfuse Trace + Local Log
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
## Tech Stack
|
| 39 |
+
|
| 40 |
+
| Layer | Technology | Rationale |
|
| 41 |
+
|---|---|---|
|
| 42 |
+
| LLM | Mistral-7B-Instruct (HF API) | Free hosted inference, strong instruction following |
|
| 43 |
+
| Embeddings | all-MiniLM-L6-v2 | Runs locally, no API cost, 384-dim, fast on CPU |
|
| 44 |
+
| Vector Store | FAISS IndexFlatIP | Exact cosine search, zero infra, ideal for < 500k chunks |
|
| 45 |
+
| Evaluation | Custom cosine similarity metrics | No LLM calls required, millisecond latency |
|
| 46 |
+
| Observability | Langfuse (optional) + JSONL logs | Free tier, self-hostable, structured traces |
|
| 47 |
+
| UI | Gradio | HF Spaces native, minimal setup |
|
| 48 |
+
|
| 49 |
+
## Engineering Decisions & Tradeoffs
|
| 50 |
+
|
| 51 |
+
**Chunking strategy:** Fixed-size character chunking with sentence-boundary snapping. Faster than semantic chunking (no extra embeddings), cleaner than hard character splits. The 512-token / 64-token overlap defaults work well for most English documents. For financial tables or code, increase chunk size; for FAQ-style documents, decrease it.
|
| 52 |
+
|
| 53 |
+
**Relevance threshold (0.35):** Below this cosine similarity, the retrieved chunks are unlikely to contain the answer. Rather than hallucinate, we return a fallback message. This threshold should be tuned empirically on your document corpus.
|
| 54 |
+
|
| 55 |
+
**Evaluation without an LLM judge:** Ragas-style metrics use a second LLM to evaluate answers β expensive in both cost and latency. Our proxy metrics (answer-context cosine similarity for faithfulness, answer-query similarity for relevance) run in ~20ms and correlate well with human judgment. For a production system, augment with periodic human eval on a golden test set.
|
| 56 |
+
|
| 57 |
+
## Deployment on Hugging Face Spaces
|
| 58 |
+
|
| 59 |
+
1. Create a new Space (SDK: Gradio)
|
| 60 |
+
2. Add files from this repository
|
| 61 |
+
3. Go to **Settings β Repository secrets** and add:
|
| 62 |
+
- `HF_TOKEN` β your HF read token (huggingface.co β Settings β Access Tokens)
|
| 63 |
+
- `LANGFUSE_PUBLIC_KEY` (optional)
|
| 64 |
+
- `LANGFUSE_SECRET_KEY` (optional)
|
| 65 |
+
4. The Space will build and launch automatically
|
| 66 |
+
|
| 67 |
+
## Known Limitations & Production Concerns
|
| 68 |
+
|
| 69 |
+
- **Scanned PDFs:** PyPDF cannot extract text from image-based PDFs. Add Tesseract OCR for production use.
|
| 70 |
+
- **Multi-document retrieval:** Current design supports one document per session. For a multi-document corpus, add document-level metadata to chunks and filter by source.
|
| 71 |
+
- **FAISS persistence:** The index lives in memory and resets when the Space restarts. For production, serialize the index with `faiss.write_index()` and store in persistent storage.
|
| 72 |
+
- **Concurrent users:** Gradio's `gr.State()` isolates per-session state, but the embedding model is shared. Under high concurrency, add a request queue.
|
| 73 |
+
- **HF Inference API rate limits:** Free tier allows ~1000 requests/day per token. For production, deploy a dedicated Inference Endpoint.
|
| 74 |
+
|
| 75 |
+
## Scalability Path
|
| 76 |
+
|
| 77 |
+
| Scale | Recommended Change |
|
| 78 |
+
|---|---|
|
| 79 |
+
| > 500k chunks | Switch FAISS to IndexIVFFlat with nprobe tuning |
|
| 80 |
+
| > 10 users | Add Redis for session state isolation |
|
| 81 |
+
| Production SLA | Move to dedicated HF Inference Endpoint or vLLM |
|
| 82 |
+
| Multi-document | Add metadata filtering layer + document registry |
|
| 83 |
+
| Compliance | Add PII detection before ingestion, audit log retention |
|
| 84 |
+
|
| 85 |
+
## Evaluation Methodology
|
| 86 |
+
|
| 87 |
+
Three proxy metrics computed without an LLM judge:
|
| 88 |
+
|
| 89 |
+
- **Faithfulness (0β1):** For each answer sentence, maximum cosine similarity to any retrieved chunk. Scores < 0.5 indicate potential hallucination.
|
| 90 |
+
- **Answer Relevance (0β1):** Cosine similarity between the answer and the original query embeddings. Measures whether the answer addresses what was asked.
|
| 91 |
+
- **Context Precision (0β1):** Rank-weighted average of retrieval similarity scores. Measures retrieval quality independent of generation.
|
| 92 |
+
|
| 93 |
+
Overall score is an unweighted average. For compliance applications, weight faithfulness higher.
|
| 94 |
+
|
| 95 |
+
## Future Improvements
|
| 96 |
+
|
| 97 |
+
- [ ] Add OCR fallback for scanned PDFs (Tesseract/AWS Textract)
|
| 98 |
+
- [ ] Implement hybrid search (BM25 + dense) for better recall on keyword queries
|
| 99 |
+
- [ ] Add re-ranking layer (cross-encoder) between retrieval and generation
|
| 100 |
+
- [ ] Persist FAISS index to HF dataset for cross-session memory
|
| 101 |
+
- [ ] Add streaming response support for lower perceived latency
|
| 102 |
+
- [ ] Build evaluation dataset from uploaded documents automatically
|