omravalccc/rag_final_optimized
0
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
๐ RAG Final Optimized - Knowledge Assistant
A production-ready Retrieval-Augmented Generation (RAG) system built for efficient document processing, semantic search, and intelligent question answering. Optimized for Hugging Face Spaces deployment with Docker.
๐ฏ Project Overview
This system allows users to:
- Upload documents (PDF, DOCX, TXT, Images)
- Automatically extract text with OCR support
- Create semantic chunks using embedding similarity
- Store vectors in FAISS for fast retrieval
- Answer questions using Gemini LLM with retrieved context
๐๏ธ Architecture Choices & Justifications
1. Embedding Model Selection
Why E5-base-v2?
- Perfect balance of size (438MB) and quality
- Supports asymmetric retrieval with
query:andpassage:prefixes - Loads in under 30 seconds on HF Spaces
- MTEB benchmark top performer in its size category
- No API key required - runs fully locally
2. Vector Database Selection
Why FAISS?
- Zero external dependencies - just pip install
- File-based persistence - simple save/load
- Inner Product similarity for normalized embeddings
- Battle-tested by Meta AI for billion-scale search
- Works perfectly in Docker/HF Spaces environment
3. LLM Selection for Answer Generation
Why Gemini 2.5 Flash?
- Free tier with generous quota
- Extremely fast response times
- High quality outputs for RAG tasks
- No model download required - API based
- Automatic fallback to Pro and older versions
- Easy HF Spaces deployment with just API key in secrets
4. Chunking Strategy Comparison
Why Semantic Chunking?
- Groups semantically similar sentences together
- Uses embedding similarity threshold (0.75)
- Preserves topic coherence within chunks
- Better retrieval accuracy than fixed-size chunking
- Respects natural content boundaries
5. OCR and Document Processing
Why pdfplumber + pytesseract?
- Open source and free
- Excellent PDF text extraction
- Tesseract OCR for scanned pages and images
- Works in Docker with apt packages
- Handles mixed documents (text + images)
๐ Performance Benchmarks
Model Loading Times (HF Spaces - 2 vCPU, 16GB RAM)
Query Performance
๐ Why NOT These Alternatives?
Local LLMs (Llama, Mistral, etc.)
OpenAI Embeddings
Google Flan-T5 Models
Heavy Vector DBs (Milvus, Weaviate)
๐ ๏ธ Technical Implementation Details
Embedding Strategy
Documents โ "passage: {text}" โ E5-base-v2 โ Normalized Vectors
Queries โ "query: {text}" โ E5-base-v2 โ Normalized VectorsSimilarity Metric
- Using Inner Product (IP) instead of L2 distance
- Vectors are normalized โ IP equals cosine similarity
- Faster computation for normalized vectors
Chunking Algorithm
1. Split text into sentences
2. Embed each sentence
3. For each sentence:
- If similarity with current chunk โฅ 0.75 AND size < max_size:
โ Add to current chunk
- Else:
โ Start new chunk
4. Merge small chunks (< 100 chars) with neighbors๐ Project Structure
rag_final_optimized/
โโโ utils/
โ โโโ docs_reciever.py # Document ingestion + chunking + embedding
โ โโโ ans_retrieval.py # Vector search + Gemini answer generation
โโโ streamlit.py # Web UI with tabs
โโโ requirements.txt # Python dependencies
โโโ packages.txt # System packages (apt)
โโโ Dockerfile # HF Spaces deployment
โโโ .env # API keys (local only)
โโโ README.md # This file๐ Deployment
Hugging Face Spaces
- Push to HF Space repository
- Add
GEMINI_API_KEYto Space secrets - Wait for Docker build (~5 minutes)
- App is live!
Local Development
pip install -r requirements.txt
streamlit run streamlit.py๐ Environment Variables
๐ Future Improvements
- [ ] Add reranking with cross-encoder
- [ ] Implement hybrid search (BM25 + vector)
- [ ] Add conversation history/memory
- [ ] Support more file formats (Excel, PPT)
- [ ] Add multi-language support
๐ License
MIT License - Feel free to use and modify!
Built with โค๏ธ using Streamlit, FAISS, E5 Embeddings, and Gemini
