File size: 11,027 Bytes
202f345
5aafb3a
202f345
5aafb3a
202f345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aafb3a
202f345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aafb3a
202f345
5aafb3a
 
202f345
 
5aafb3a
 
202f345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5aafb3a
 
202f345
5aafb3a
202f345
5aafb3a
 
202f345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
351
352
353
354
355
356
357
358
359
360
361
# HNTAI - Medical Data Extraction & AI Processing Platform

A comprehensive, scalable AI platform for medical data extraction, processing, and analysis. Built with FastAPI, supporting multiple AI model backends including Transformers, OpenVINO, and GGUF models with automatic GPU/CPU optimization.

## πŸ₯ Overview

HNTAI is a production-ready medical AI platform that provides:
- **Medical Document Processing**: PDF, DOCX, image, and audio transcription
- **Protected Health Information (PHI) Scrubbing**: HIPAA-compliant data anonymization
- **AI-Powered Summarization**: Multi-model support with automatic device optimization
- **Patient Summary Generation**: Comprehensive clinical assessments
- **Scalable Architecture**: Kubernetes-ready with monitoring and security features

## πŸš€ Key Features

### πŸ€– Multi-Model AI Support
- **Transformers Models**: Hugging Face models with automatic GPU/CPU detection
- **OpenVINO Optimization**: Intel-optimized models for production performance
- **GGUF Models**: Quantized models for efficient inference
- **Automatic Device Selection**: GPU when available, CPU fallback
- **Model Caching**: Intelligent model management and caching

### πŸ“„ Document Processing
- **Multi-format Support**: PDF, DOCX, images, audio files
- **OCR Integration**: Tesseract-based text extraction
- **Audio Transcription**: Whisper-based speech-to-text
- **Batch Processing**: Async processing for scalability

### πŸ”’ Security & Compliance
- **HIPAA Compliance**: PHI scrubbing with audit logging
- **Data Encryption**: Secure data handling and storage
- **Audit Trails**: Comprehensive logging for compliance
- **Non-root Containers**: Security-hardened deployments

### πŸ“Š Monitoring & Observability
- **Health Endpoints**: `/health/live`, `/health/ready`
- **Prometheus Metrics**: `/metrics` endpoint
- **Structured Logging**: Comprehensive application monitoring
- **Performance Tracking**: Model inference metrics

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI       β”‚    β”‚   AI Models     β”‚    β”‚   PostgreSQL    β”‚
β”‚   Web Server    │◄──►│   (Multi-backend)β”‚    β”‚   Database      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Redis Cache   β”‚    β”‚   File Storage  β”‚    β”‚   Audit Logs    β”‚
β”‚   (PHI Stats)   β”‚    β”‚   (Documents)   β”‚    β”‚   (Compliance)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

## πŸ› οΈ Installation

### Prerequisites
- Python 3.11+
- CUDA 11.8+ (for GPU support)
- Docker (for containerized deployment)
- PostgreSQL 13+
- Redis 6+

### Local Development

1. **Clone the repository**:
```bash
git clone <repository-url>
cd HNTAI
```

2. **Create virtual environment**:
```bash
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
```

3. **Install dependencies**:
```bash
pip install -r requirements.txt
```

4. **Set up environment variables**:
```bash
export DATABASE_URL="postgresql://user:password@localhost:5432/hntai"
export REDIS_URL="redis://localhost:6379"
export SECRET_KEY="your-secret-key"
export JWT_SECRET_KEY="your-jwt-secret"
export HF_HOME="/tmp/huggingface"
```

5. **Run the application**:
```bash
# Development server
python -m uvicorn services.ai-service.src.ai_med_extract.main:app --reload --host 0.0.0.0 --port 7860

# Or using the service directly
cd services/ai-service
python src/ai_med_extract/main.py
```

### Docker Deployment

1. **Build the image**:
```bash
docker build -t hntai:latest .
```

2. **Run with Docker Compose**:
```bash
docker-compose up -d
```

### Kubernetes Deployment

1. **Apply Kubernetes manifests**:
```bash
kubectl apply -f infra/k8s/secure_deployment.yaml
```

2. **Check deployment status**:
```bash
kubectl get pods -l app=hntai
```

## πŸ“š API Documentation

### Core Endpoints

#### Health & Monitoring
- `GET /health/live` - Liveness probe
- `GET /health/ready` - Readiness probe  
- `GET /metrics` - Prometheus metrics

#### Document Processing
- `POST /upload` - Upload and process documents
- `POST /transcribe` - Transcribe audio files
- `GET /get_updated_medical_data` - Retrieve processed data
- `PUT /update_medical_data` - Update medical data

#### AI Processing
- `POST /generate_patient_summary` - Generate comprehensive patient summaries
- `POST /api/generate_summary` - Generate text summaries
- `POST /api/patient_summary_openvino` - OpenVINO-optimized summaries
- `POST /extract_medical_data` - Extract structured medical data

### Model Management
- `POST /api/load_model` - Load specific AI models
- `GET /api/model_info` - Get model information
- `POST /api/switch_model` - Switch between models

## πŸ€– AI Model Configuration

### Supported Model Types

#### 1. Transformers Models
```python
{
    "model_name": "microsoft/Phi-3-mini-4k-instruct",
    "model_type": "text-generation"
}
```

#### 2. OpenVINO Models
```python
{
    "model_name": "OpenVINO/Phi-3-mini-4k-instruct-fp16-ov",
    "model_type": "openvino"
}
```

#### 3. GGUF Models
```python
{
    "model_name": "microsoft/Phi-3-mini-4k-instruct-gguf",
    "model_type": "gguf"
}
```

### Automatic Device Detection
The system automatically detects and uses:
- **GPU**: When CUDA is available
- **CPU**: Fallback when GPU is not available
- **Optimization**: Intel OpenVINO for production performance

## πŸ”§ Configuration

### Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `DATABASE_URL` | PostgreSQL connection string | Required |
| `REDIS_URL` | Redis connection string | Required |
| `SECRET_KEY` | Application secret key | Required |
| `JWT_SECRET_KEY` | JWT signing key | Required |
| `HF_HOME` | Hugging Face cache directory | `/tmp/huggingface` |
| `TORCH_HOME` | PyTorch cache directory | `/tmp/torch` |
| `WHISPER_CACHE` | Whisper model cache | `/tmp/whisper` |
| `HF_SPACES` | Hugging Face Spaces mode | `false` |
| `PRELOAD_GGUF` | Preload GGUF models | `false` |

### Model Configuration

The system supports flexible model configuration through `model_config.py`:

```python
# Default models for different tasks
DEFAULT_MODELS = {
    "text-generation": {
        "primary": "microsoft/Phi-3-mini-4k-instruct",
        "fallback": "facebook/bart-base"
    },
    "openvino": {
        "primary": "OpenVINO/Phi-3-mini-4k-instruct-fp16-ov",
        "fallback": "microsoft/Phi-3-mini-4k-instruct"
    },
    "gguf": {
        "primary": "microsoft/Phi-3-mini-4k-instruct-gguf",
        "fallback": "microsoft/Phi-3-mini-4k-instruct-gguf"
    }
}
```

## πŸ§ͺ Testing

### Run Tests
```bash
# Unit tests
python -m pytest tests/

# Smoke test (no model loading)
cd services/ai-service
python run_smoke_test.py

# Integration tests
python -m pytest tests/integration/
```

### Code Quality
```bash
# Format code
black .
isort .

# Lint code
flake8 .
mypy .

# Type checking
mypy services/ai-service/src/ai_med_extract/
```

## πŸ“Š Monitoring

### Health Checks
- **Liveness**: `GET /health/live` - Application is running
- **Readiness**: `GET /health/ready` - Application is ready to serve requests

### Metrics
- **Prometheus**: `GET /metrics` - Application and model metrics
- **Custom Metrics**: Model inference time, success rates, error rates

### Logging
- **Structured Logging**: JSON-formatted logs
- **Audit Trails**: PHI access and modification logs
- **Performance Logs**: Model loading and inference timing

## πŸ”’ Security Features

### HIPAA Compliance
- **PHI Scrubbing**: Automatic removal of protected health information
- **Audit Logging**: Comprehensive access and modification logs
- **Data Encryption**: Secure data handling and storage
- **Access Controls**: Role-based access to sensitive data

### Container Security
- **Non-root Containers**: Security-hardened container images
- **Resource Limits**: CPU and memory limits
- **Network Policies**: Secure network communication
- **Secrets Management**: Secure handling of sensitive configuration

## πŸš€ Deployment Options

### 1. Local Development
```bash
python -m uvicorn services.ai-service.src.ai_med_extract.main:app --reload
```

### 2. Docker
```bash
docker run -p 7860:7860 hntai:latest
```

### 3. Kubernetes
```bash
kubectl apply -f infra/k8s/secure_deployment.yaml
```

### 4. Hugging Face Spaces
```bash
# Configure for HF Spaces
export HF_SPACES=true
python start_hf_spaces.py
```

## πŸ“ Project Structure

```
HNTAI/
β”œβ”€β”€ services/
β”‚   └── ai-service/
β”‚       β”œβ”€β”€ src/ai_med_extract/
β”‚       β”‚   β”œβ”€β”€ agents/           # AI agents and processors
β”‚       β”‚   β”œβ”€β”€ api/             # FastAPI routes and management
β”‚       β”‚   β”œβ”€β”€ utils/           # Utilities and model management
β”‚       β”‚   β”œβ”€β”€ app.py          # Main application
β”‚       β”‚   └── main.py         # Application entry point
β”‚       β”œβ”€β”€ docker-compose.yml  # Docker services
β”‚       └── Dockerfile          # Container image
β”œβ”€β”€ infra/
β”‚   └── k8s/                   # Kubernetes manifests
β”œβ”€β”€ monitoring/
β”‚   └── prometheus.yml         # Monitoring configuration
β”œβ”€β”€ database/
β”‚   └── postgresql/           # Database schemas
└── requirements.txt          # Python dependencies
```

## 🀝 Contributing

1. **Fork the repository**
2. **Create a feature branch**: `git checkout -b feature/amazing-feature`
3. **Make your changes**
4. **Run tests**: `python -m pytest`
5. **Commit changes**: `git commit -m 'Add amazing feature'`
6. **Push to branch**: `git push origin feature/amazing-feature`
7. **Open a Pull Request**

## πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

## πŸ†˜ Support

- **Documentation**: Check the `/docs` endpoint for interactive API documentation
- **Issues**: Report bugs and feature requests via GitHub Issues
- **Discussions**: Join community discussions for questions and support

## πŸ”„ Changelog

### Latest Updates
- βœ… **Fixed OpenVINO GPU/CPU auto-detection**
- βœ… **Improved model loading with fallback mechanisms**
- βœ… **Enhanced security and HIPAA compliance**
- βœ… **Added comprehensive monitoring and health checks**
- βœ… **Optimized for production deployment**

---

**Built with ❀️ for the medical AI community**