Medical_Document_Retrieval / ARCHITECTURE.md
taraky's picture
Upload folder using huggingface_hub
b7f3196 verified
# Medical Q&A Bot - System Architecture
## Visual Overview
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ USER INTERFACE β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Gradio Web UI β”‚ β”‚ Streamlit Web UI β”‚ β”‚
β”‚ β”‚ (app.py) β”‚ OR β”‚ (app_streamlit.py) β”‚ β”‚
β”‚ β”‚ Port: 7860 β”‚ β”‚ Port: 8501 β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Query Processing Layer β”‚
β”‚ β”‚
β”‚ 1. Text Input Validation β”‚
β”‚ 2. Embedding Generation β”‚
β”‚ 3. Model Inference β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CLASSIFIER MODULE β”‚
β”‚ (classifier/) β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ SentenceTransformer β”‚ β”‚
β”‚ β”‚ Embedding Model β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β–Ό β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Classification Head β”‚ β”‚
β”‚ β”‚ (Neural Network) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ MEDICAL β”‚ β”‚ ADMINISTRATIVEβ”‚
β”‚ QUERY β”‚ β”‚ QUERY β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚ β”‚
β”‚ └──► End (No Retrieval)
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RETRIEVAL MODULE β”‚
β”‚ (retriever/) β”‚
β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ BM25 Search β”‚ β”‚
β”‚ β”‚ (Sparse Retrieval) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Dense Search β”‚ β”‚
β”‚ β”‚ (Vector Similarity) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ RRF Fusion β”‚ β”‚
β”‚ β”‚ (Rank Combination) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”‚ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Optional Reranker β”‚ β”‚
β”‚ β”‚ (Cross-Encoder) β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ DATA SOURCES β”‚
β”‚ β”‚
β”‚ β€’ PubMed Articles β”‚
β”‚ β€’ Miriad Q&A β”‚
β”‚ β€’ UniDoc Q&A β”‚
β”‚ β”‚
β”‚ (data/corpora/) β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RESULTS β”‚
β”‚ β”‚
β”‚ β€’ Document Title β”‚
β”‚ β€’ Text Content β”‚
β”‚ β€’ Relevance Scores β”‚
β”‚ β€’ Metadata β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚
β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ UI DISPLAY β”‚
β”‚ β”‚
β”‚ β€’ Formatted Cards β”‚
β”‚ β€’ JSON View β”‚
β”‚ β€’ Score Badges β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
## Data Flow
### 1. User Input
```
User Types Query β†’ Web Interface Captures Input β†’ Sends to Backend
```
### 2. Classification Phase
```
Query Text
↓
Sentence Transformer (Embedding)
↓
Classification Head (Neural Network)
↓
Output: [Medical | Administrative | Other] + Confidence Scores
```
### 3. Retrieval Phase (Medical Queries Only)
```
Medical Query
↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Parallel Retrieval β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ BM25 (Sparse) β”‚ β”‚ ← Top 100 docs
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ Dense (Vector) β”‚ β”‚ ← Top 100 docs
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
↓
RRF Fusion Algorithm
↓
Top K Candidates
↓
Optional: Cross-Encoder Reranking
↓
Final Top N Results
```
## Technology Stack
### Frontend
- **Gradio** - Primary UI framework
- **Streamlit** - Alternative UI framework
- **HTML/CSS** - Custom styling
- **JavaScript** - Auto-generated by frameworks
### Backend
- **Python 3.8+** - Core language
- **PyTorch** - Deep learning framework
- **Sentence-Transformers** - Embedding models
- **scikit-learn** - ML utilities
### Search & Retrieval
- **Rank-BM25** - Sparse retrieval
- **FAISS** - Dense vector search
- **Custom RRF** - Rank fusion
- **Cross-Encoder** - Optional reranking
### Data
- **PubMed** - Medical research articles
- **Miriad** - Medical Q&A database
- **UniDoc** - Unified document corpus
- **JSONL** - Data storage format
## Component Interactions
### 1. Initialization
```python
# Load models once at startup
embedding_model, classifier = classifier_init()
```
### 2. Classification
```python
classification = predict_query(
text=[query],
embedding_model=embedding_model,
classifier_head=classifier
)
```
### 3. Retrieval
```python
hits = get_candidates(
query=query,
k_retrieve=10,
use_reranker=False
)
```
### 4. Display
```python
# Gradio displays results in tabs
# - Formatted HTML view
# - Raw JSON view
```
## Performance Characteristics
### Speed
- **Classification**: ~100-500ms
- **BM25 Search**: ~50-200ms
- **Dense Search**: ~100-300ms
- **Reranking**: ~500-2000ms (if enabled)
### Accuracy
- **Classification**: ~95% accuracy
- **Retrieval**: Depends on corpus and query
- **Reranking**: +5-10% improvement
### Resource Usage
- **Memory**: ~2-4 GB (with models loaded)
- **CPU**: Moderate during inference
- **GPU**: Optional (speeds up inference)
## Scalability Considerations
### Current Setup (Single User)
- βœ… Perfect for demos and development
- βœ… Low latency
- βœ… Easy to debug
### Future Scaling Options
- πŸ”„ Add caching for common queries
- πŸ”„ Deploy on cloud with autoscaling
- πŸ”„ Use model quantization for faster inference
- πŸ”„ Implement request queuing
- πŸ”„ Add load balancing
## Security & Privacy
### Current Implementation
- Local hosting only
- No data persistence
- No user tracking
- No authentication (optional)
### Production Considerations
- Add user authentication
- Implement rate limiting
- Sanitize inputs
- Log access for auditing
- HTTPS for encrypted communication
## Monitoring & Debugging
### Available Information
- Query classification results
- Confidence scores per category
- Retrieval scores (BM25, Dense, RRF)
- Document metadata
- Error messages
### Debug Mode
```python
# In app.py, set:
demo.launch(show_error=True) # Shows detailed errors
```
## Deployment Options
### 1. Local (Current)
```
Pros: Easy, fast, secure
Cons: Single user, not accessible remotely
```
### 2. Hugging Face Spaces
```
Pros: Free, easy deploy, public URL
Cons: Limited resources, public access
```
### 3. Cloud (AWS/GCP/Azure)
```
Pros: Scalable, private, customizable
Cons: Costs money, requires setup
```
### 4. Docker Container
```
Pros: Portable, consistent environment
Cons: Requires Docker knowledge
```
## File Structure
```
health-query-classifier/
β”œβ”€β”€ πŸ–₯️ UI Layer
β”‚ β”œβ”€β”€ app.py # Main Gradio UI
β”‚ β”œβ”€β”€ app_streamlit.py # Alternative Streamlit UI
β”‚ β”œβ”€β”€ launch_ui.bat # Windows launcher
β”‚ └── launch_ui.ps1 # PowerShell launcher
β”‚
β”œβ”€β”€ 🧠 Classifier Layer
β”‚ β”œβ”€β”€ classifier/
β”‚ β”‚ β”œβ”€β”€ infer.py # Inference logic
β”‚ β”‚ β”œβ”€β”€ head.py # Classification head
β”‚ β”‚ β”œβ”€β”€ train.py # Training script
β”‚ β”‚ └── utils.py # Utilities
β”‚
β”œβ”€β”€ πŸ” Retrieval Layer
β”‚ β”œβ”€β”€ retriever/
β”‚ β”‚ β”œβ”€β”€ search.py # Search interface
β”‚ β”‚ β”œβ”€β”€ index_bm25.py # BM25 indexing
β”‚ β”‚ β”œβ”€β”€ index_dense.py # Dense indexing
β”‚ β”‚ └── rrf.py # Rank fusion
β”‚
β”œβ”€β”€ πŸ‘₯ Team Layer
β”‚ β”œβ”€β”€ team/
β”‚ β”‚ β”œβ”€β”€ candidates.py # Candidate retrieval
β”‚ β”‚ └── interfaces.py # Data interfaces
β”‚
β”œβ”€β”€ πŸ“Š Data Layer
β”‚ β”œβ”€β”€ data/
β”‚ β”‚ └── corpora/ # Corpus files
β”‚ β”‚ β”œβ”€β”€ medical_qa.jsonl
β”‚ β”‚ β”œβ”€β”€ miriad_text.jsonl
β”‚ β”‚ └── unidoc_qa.jsonl
β”‚
└── πŸ“š Documentation
β”œβ”€β”€ README.md # Main documentation
β”œβ”€β”€ QUICKSTART.md # Quick start guide
β”œβ”€β”€ UI_README.md # UI documentation
β”œβ”€β”€ UI_IMPLEMENTATION.md # Implementation details
└── ARCHITECTURE.md # This file
```
---
This architecture ensures:
- βœ… Clean separation of concerns
- βœ… Modular design
- βœ… Easy to test and debug
- βœ… Scalable and maintainable
- βœ… Well-documented