mukimshardul/inverse_problem
0
๐ฌ Multimodal RAG System for Scientific Papers
A production-grade multimodal Retrieval-Augmented Generation (RAG) system built to query research papers by their figures, plots, and tables โ not just text. It uses deep learning layout detection, tri-modal hybrid search, and a local Vision Language Model (VLM) to synthesize scientific answers.
โก Quick Start
# Step 1 โ Activate environment
conda activate rag_prod
# Step 2 โ Run the app (if already indexed)
streamlit run app.pyApp runs at: http://localhost:8501
๐๏ธ Project Structure
NLP/
โโโ raw/ # Input PDFs go here
โโโ extracted_data/
โ โโโ images/ # Cropped figures/tables (PNG)
โ โโโ images_metadata_d2.json # V1 extraction metadata
โ โโโ images_metadata_v2.json # V2 VLM-enriched metadata (generated by enrich_metadata.py)
โโโ index/
โ โโโ chroma_db/ # ChromaDB โ visual + semantic vector stores
โ โโโ bm25_index.pkl # BM25 lexical index
โโโ src/
โ โโโ extract_detectron2.py # Step 1: Extract figures using Detectron2
โ โโโ build_index.py # Step 2: Embed & index all figures
โ โโโ enrich_metadata.py # Optional: VLM enrichment of captions
โ โโโ retriever.py # Tri-Modal Hybrid Retriever (standalone)
โโโ v1_deployment/ # Self-contained V1.0 scripts (commented)
โโโ model_final.pth # PubLayNet Detectron2 weights
โโโ app.py # Main Streamlit Application
โโโ requirements.txt๐ Full Pipeline
Step 1 โ Extract Figures from PDFs
conda run -n rag_prod python src/extract_detectron2.py- Reads all
.pdffiles inraw/ - Uses
layoutparser+ Detectron2 (mask_rcnn_X_101_32x8d_FPN_3x, PubLayNet) to detect Figure and Table regions - Adds a 20-pixel padding on bounding boxes to preserve plot axis labels
- Extracts spatially-proximate text (captions) for each detected region
- Saves cropped images โ
extracted_data/images/ - Saves metadata โ
extracted_data/images_metadata_d2.json
Step 2 (Optional) โ Enrich Captions via VLM
# Runs in background โ 221 images ร ~10s each
nohup conda run -n rag_prod python src/enrich_metadata.py > enrich.log 2>&1 &- Sends each extracted plot to Llama 3.2 Vision via Ollama
- Generates a detailed scientific description of each figure
- Saves enriched metadata โ
extracted_data/images_metadata_v2.json - Indexer automatically picks this up if it exists
Step 3 โ Build the Search Index
conda run -n rag_prod python src/build_index.pyPopulates two ChromaDB collections and one BM25 index:
Step 4 โ Launch the App
conda run -n rag_prod streamlit run app.py --server.port 8501๐ How Retrieval Works
The system uses Tri-Modal Reciprocal Rank Fusion (RRF):
Query Text
โ
โโโโบ OpenCLIP โโโบ Visual embedding โโโบ ChromaDB (multimodal_figures) โโโบ Rank list A
โ
โโโโบ MiniLM โโโโโบ Text embedding โโโโบ ChromaDB (semantic_texts) โโโบ Rank list B
โ
โโโโบ BM25 โโโโโโโบ Tokenized words โโโบ BM25 Index โโโบ Rank list C
โ
โโโโโโโโโโโโโโโโโโฉโโโโโโโโโโโโโโโโ
โ Reciprocal Rank Fusion (RRF) โ
โ score = ฮฃ weight / (60 + rank)โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
Top-K Merged ResultsThe 3 weights are fully tunable in the UI sidebar in real-time.
๐ง VLM Answer Generation
After retrieval, the top-K figures are:
- Base64-encoded and batched
- Combined with their captions as text context
- Sent to Llama 3.2 Vision running locally via Ollama
- The answer is streamed token-by-token back to the UI
๐ฆ Dependencies
conda install -c conda-forge layoutparser detectron2 sentence-transformers
pip install open_clip_torch chromadb rank_bm25 streamlit ollama pymupdfFull pinned list: requirements.txt
๐ Key Design Decisions
๐ V1.0 Standalone (v1_deployment/)
A clean, fully-commented, dependency-isolated copy of the core pipeline scripts. Each script can be run independently without the src/ module structure.
cd v1_deployment/
python extract_images.py # Extract
python build_index.py # Index
streamlit run retriever_app.py # Run๐ Notes
- The VLM enrichment script (
enrich_metadata.py) may run for 30โ60 minutes given 221 figures. It saves progress every 10 items, so it can be safely interrupted and restarted. - After enrichment finishes, re-run
build_index.pyto incorporate the richer captions into the vector store. - The Streamlit app will automatically use
images_metadata_v2.json(enriched) if present, otherwise falls back toimages_metadata_d2.json.
