CoolFace
Apppublic

mukimshardul/inverse_problem

sourceHugging Faceupdated 7mo agoView on Hugging Face
0likes
App README

๐Ÿ”ฌ 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

bash
# Step 1 โ€“ Activate environment
conda activate rag_prod

# Step 2 โ€“ Run the app (if already indexed)
streamlit run app.py

App 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

bash
conda run -n rag_prod python src/extract_detectron2.py
  • โ€”Reads all .pdf files in raw/
  • โ€”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

bash
# 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

bash
conda run -n rag_prod python src/build_index.py

Populates two ChromaDB collections and one BM25 index:

IndexModelPurpose
multimodal_figures (ChromaDB)OpenCLIP ViT-B-32Visual similarity
semantic_texts (ChromaDB)MiniLM all-MiniLM-L6-v2Semantic caption similarity
bm25_index.pklBM25OkapiKeyword matching

Step 4 โ€” Launch the App

bash
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 Results

The 3 weights are fully tunable in the UI sidebar in real-time.


๐Ÿง  VLM Answer Generation

After retrieval, the top-K figures are:

  1. 1.Base64-encoded and batched
  2. 2.Combined with their captions as text context
  3. 3.Sent to Llama 3.2 Vision running locally via Ollama
  4. 4.The answer is streamed token-by-token back to the UI

๐Ÿ“ฆ Dependencies

bash
conda install -c conda-forge layoutparser detectron2 sentence-transformers
pip install open_clip_torch chromadb rank_bm25 streamlit ollama pymupdf

Full pinned list: requirements.txt


๐Ÿ”‘ Key Design Decisions

DecisionWhy
Detectron2 over PyMuPDF for extractionDetectron2 finds figures semantically, not just embedded image blobs
20px bounding box paddingAxis labels, tick marks, and legends are just outside the detected box
OpenCLIP ViT-B-32 (LAION)Strong general CLIP model, good for visual image-text alignment
MiniLM all-MiniLM-L6-v2Fast & accurate semantic text matching for scientific captions
RRF instead of score blendingDifferent models have incompatible score scales; RRF is rank-safe
Local Llama 3.2 Vision via OllamaPrivacy-preserving, no API cost, multimodal
VLM caption enrichment at index-timeScientific plots rarely have good captions; VLM writes better ones

๐Ÿ“ 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.

bash
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.py to incorporate the richer captions into the vector store.
  • โ€”The Streamlit app will automatically use images_metadata_v2.json (enriched) if present, otherwise falls back to images_metadata_d2.json.