Spaces:
Paused
Paused
| title: Multimodal RAG | |
| emoji: π§ | |
| colorFrom: green | |
| colorTo: purple | |
| sdk: docker | |
| pinned: true | |
| license: mit | |
| short_description: Multimodal RAG β PDFs, scans, tables, charts, URLs. | |
| sleep_time: -1 | |
| tags: | |
| - rag | |
| - multimodal | |
| - groq | |
| - ollama | |
| - chromadb | |
| ## ⬑ Multimodal RAG System | |
| A deployable **Multimodal Retrieval-Augmented Generation** system that answers questions **strictly** from your uploaded documents. Runs locally or on HuggingFace Spaces. | |
| ## Features | |
| | Feature | Details | | |
| | --- | --- | | |
| | π **Document types** | PDF (text + embedded images), scanned images (OCR), XLSX, DOCX, CSV, TXT | | |
| | π **URL indexing** | Paste any URL β crawls up to 2 levels deep (same domain, max 50 pages); linked PDFs downloaded and indexed automatically | | |
| | π **Multimodal** | Extracts text, tables, charts, and images from PDFs | | |
| | π§ **Strict grounding** | Answers ONLY from documents β responds "I DON'T KNOW" otherwise | | |
| | πΎ **Persistent vector store** | ChromaDB with cosine similarity (persists across restarts) | | |
| | β‘ **LLM backends** | Groq (default on HF Space) β HF Inference (opt-in) β Ollama (local fallback) | | |
| | π¬ **Conversation memory** | Remembers context; auto-summarizes when context window fills up | | |
| | π **Document management** | Add/remove documents and URLs via UI; files synced to HF Dataset for persistence | | |
| | π **Text-to-Speech** | Read aloud any answer; works on desktop and mobile; tap again to stop | | |
| | π‘ **Sample questions** | 12 one-click question chips for quick exploration | | |
| ## LLM Backend Priority | |
| | Priority | Backend | Condition | | |
| | --- | --- | --- | | |
| | 1 | **Groq** (`llama-3.3-70b-versatile`) | `GROQ_API_KEY` is set | | |
| | 2 | **HF Inference API** (`Llama-3.1-8B-Instruct`) | `USE_HF_LLM=1` + `HF_TOKEN` set | | |
| | 3 | **Ollama** (`llama3.2`) | fallback / local dev | | |
| ## Setup | |
| ### Prerequisites | |
| 1. **Install Tesseract** (for OCR): | |
| - macOS: `brew install tesseract` | |
| - Ubuntu: `sudo apt-get install tesseract-ocr` | |
| - Windows: <https://github.com/UB-Mannheim/tesseract/wiki> | |
| 2. **For local Ollama fallback** β Install Ollama and run `ollama pull llama3.2` | |
| ### Installation | |
| ```bash | |
| pip install -r requirements.txt | |
| ``` | |
| ### Run | |
| ```bash | |
| python app.py | |
| ``` | |
| - Gradio UI: <http://localhost:7860> | |
| - FastAPI docs: <http://localhost:8000/docs> | |
| ## Environment Variables | |
| | Variable | Default | Description | | |
| | --- | --- | --- | | |
| | `GROQ_API_KEY` | β | Groq API key (enables Groq backend, priority 1) | | |
| | `USE_HF_LLM` | `0` | Set to `1` to use HF Inference API instead of Ollama | | |
| | `HF_TOKEN` | β | HuggingFace token (required for dataset sync + HF Inference) | | |
| | `HF_DATASET_REPO` | β | HF Dataset repo ID for persistent file storage (e.g. `user/repo`) | | |
| | `DATA_DIR` | `./data` | Directory for uploaded documents | | |
| | `VECTORSTORE_DIR` | `./vectorstore` | ChromaDB persistence directory | | |
| | `OLLAMA_MODEL` | `llama3.2` | Ollama model to use (local fallback) | | |
| | `API_BASE` | `http://localhost:8000` | FastAPI backend URL | | |
| ## Project Structure | |
| ```text | |
| multimodal-rag/ | |
| βββ app.py # Entrypoint (local dev + HuggingFace Spaces) | |
| βββ frontend.py # Gradio UI | |
| βββ backend.py # FastAPI backend | |
| βββ Dockerfile # HF Spaces Docker build | |
| βββ deploy_changes.sh # One-command deploy to GitHub + HF Space | |
| βββ requirements.txt | |
| βββ data/ # Uploaded documents (synced to HF Dataset) | |
| βββ vectorstore/ # ChromaDB persistent storage | |
| βββ utils/ | |
| βββ document_processor.py # PDF/image/DOCX/XLSX extraction | |
| βββ url_processor.py # Web crawler + linked PDF downloader | |
| βββ vector_store.py # ChromaDB manager | |
| βββ rag_engine.py # RAG + Groq/HF/Ollama integration | |
| βββ memory.py # Conversation memory manager | |
| ``` | |
| ## API Endpoints | |
| | Method | Endpoint | Description | | |
| | --- | --- | --- | | |
| | GET | `/status` | System status, indexed docs, chunk count | | |
| | POST | `/documents/upload` | Upload and index a document | | |
| | POST | `/documents/url` | Start background crawl of a URL (returns immediately) | | |
| | GET | `/documents/url/status?url=` | Poll crawl job status (`crawling` / `done` / `error`) | | |
| | DELETE | `/documents/{filename}` | Remove a document or URL from the index | | |
| | DELETE | `/documents` | Remove ALL documents from index, disk, and HF Dataset | | |
| | POST | `/query` | Query the RAG system | | |
| | POST | `/memory/clear` | Clear conversation memory | | |
| | GET | `/memory/stats` | Memory usage stats | | |
| ## HuggingFace Spaces Deployment | |
| 1. Set Space secrets: `GROQ_API_KEY`, `HF_TOKEN`, `HF_DATASET_REPO` | |
| 2. Deploy with: | |
| ```bash | |
| ./deploy_changes.sh | |
| ``` | |
| The script creates an orphan branch (no binary files in git history) and force-pushes it to the Space. Uploaded documents are persisted in the HF Dataset repo and re-downloaded on every container restart. | |
| > **Note**: Binary files (PDFs, images, etc.) are stored in the HF Dataset, not in the Space git repo. This satisfies HF's Xet storage requirement for large files. | |