MultiModalRag / README.md
irajkoohi's picture
chore: update app [space deploy]
6c21523
|
Raw
History Blame Contribute Delete
5.11 kB
---
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.