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