Spaces:
Sleeping
Sleeping
File size: 13,051 Bytes
b7f3196 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# 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
|