karthik-2905's picture
Update README.md
53f51fa verified
# πŸ₯ Medical AI Dashboard - Advanced Healthcare System
πŸ€— **Hugging Face Repository**: [https://huggingface.co/karthik-2905/Preganancy-Prediction](https://huggingface.co/karthik-2905/Preganancy-Prediction)
## 🌟 Overview
A production-ready medical AI system featuring advanced dual AI models for pregnancy risk prediction and fetal ultrasound classification, with enterprise-grade security, real-time data management, and comprehensive history tracking. Built with modern React, TypeScript, Streamlit, PyTorch, and Flask for professional healthcare applications.
## 🎯 **NEW: Real-Time History Management System**
βœ… **Unified Data Storage** - Single JSON file per user for all medical history
βœ… **Real-Time API Server** - Flask-based API for instant data access
βœ… **Image Deduplication** - Smart content-based duplicate prevention
βœ… **Auto-Refresh Interface** - Live updates every 30 seconds
βœ… **Cleanup Tools** - One-click removal of redundant files
βœ… **Fixed Streamlit Deprecation** - Updated to modern `st.query_params`
## ✨ Key Features
### 🀱 **Pregnancy Risk Prediction**
- **100% Accuracy**: Random Forest classifier analyzing 11 clinical parameters
- **Real-time Analysis**: Instant risk assessment with confidence scores
- **History Tracking**: Automatic JSON-based prediction history
- **Clinical Parameters**: Age, BMI, blood pressure, blood sugar, heart rate, medical history
### πŸ”¬ **Fetal Ultrasound Classification**
- **91.69% Accuracy**: Vision Transformer (ViT) for anatomical plane classification
- **9 Categories**: Fetal brain, abdomen, thorax, femur, maternal cervix, and more
- **Multi-Input Support**: Camera capture, file upload, path input
- **User-Specific Storage**: Secure file management with automatic cleanup
### πŸ“Š **History & Data Management**
- **JSON-Based Storage**: No database required, simple file-based system
- **User Isolation**: Each user gets dedicated folders and history files
- **Automatic Cleanup**: 7-day file retention, 50-entry history limit
- **Complete Tracking**: All predictions and classifications saved with timestamps
### πŸ”’ **Enterprise Security**
- **Clerk Authentication**: Enterprise-grade user management
- **HIPAA Compliance**: Secure handling of sensitive medical data
- **Data Isolation**: User-specific folders prevent cross-access
- **Camera Permissions**: Proper iframe permission management
### 🍎 **Apple Silicon Optimization**
- **MPS Support**: Metal Performance Shaders for M1/M2/M3/M4 chips
- **Thermal Management**: Optimized inference with temperature monitoring
- **Fast Performance**: <1ms pregnancy risk, <100ms fetal classification
## πŸ“ Project Structure
```
hackathon15092025/
β”œβ”€β”€ πŸ“± apps/ # Streamlit Applications
β”‚ β”œβ”€β”€ pregnancy_risk_app.py # Pregnancy risk prediction (Port 8501)
β”‚ β”œβ”€β”€ fetal_plane_app.py # Fetal ultrasound classification (Port 8502)
β”‚ └── pregnancy_risk_prediction.py # Model training script
β”‚
β”œβ”€β”€ 🎨 assets/ # Static Assets
β”‚ └── static/css/
β”‚ └── style.css # Satoshi font styling for Streamlit
β”‚
β”œβ”€β”€ βš™οΈ config/ # Configuration Files
β”‚ └── requirements.txt # Python dependencies
β”‚
β”œβ”€β”€ πŸ“Š data/ # Training Datasets
β”‚ β”œβ”€β”€ Dataset - Updated.csv # Pregnancy risk dataset (1,187 records)
β”‚ └── Dataset/ # Additional data files
β”‚
β”œβ”€β”€ πŸ—‚οΈ datasets/ # External Datasets
β”‚ └── FETAL_PLANES_ZENODO/ # Fetal plane classification dataset
β”‚ β”œβ”€β”€ FETAL_PLANES_DB_data.csv # Metadata
β”‚ └── Images/ # Ultrasound images (12,400+ samples)
β”‚
β”œβ”€β”€ πŸ“‹ docs/ # Documentation
β”‚ β”œβ”€β”€ DOCUMENTATION.md # Comprehensive system documentation
β”‚ └── PROJECT_STRUCTURE.md # Detailed project organization
β”‚
β”œβ”€β”€ πŸ€– models/ # Trained AI Models
β”‚ β”œβ”€β”€ pregnancy_risk_model.pkl # Random Forest model (100% accuracy)
β”‚ β”œβ”€β”€ label_encoder.pkl # Label encoder for pregnancy risk
β”‚ β”œβ”€β”€ feature_columns.pkl # Feature column names
β”‚ └── fetal_plane_model/ # Vision Transformer model
β”‚ β”œβ”€β”€ config.json # Model configuration
β”‚ β”œβ”€β”€ model.safetensors # Model weights (91.69% accuracy)
β”‚ β”œβ”€β”€ label_encoder.pkl # Fetal plane label encoder
β”‚ └── preprocessor_config.json # Image preprocessing config
β”‚
β”œβ”€β”€ 🌐 frontend/ # React Frontend Application
β”‚ β”œβ”€β”€ src/
β”‚ β”‚ β”œβ”€β”€ App.tsx # Main React component with routing
β”‚ β”‚ β”œβ”€β”€ index.css # Styling with Satoshi font
β”‚ β”‚ └── main.tsx # Application entry point
β”‚ β”œβ”€β”€ package.json # Dependencies and scripts
β”‚ β”œβ”€β”€ index.html # HTML template
β”‚ └── vite.config.ts # Vite configuration
β”‚
β”œβ”€β”€ πŸ“œ scripts/ # Utility Scripts
β”‚ └── fetal_plane_classifier.py # Fetal plane training script
β”‚
β”œβ”€β”€ πŸ“€ uploads/ # User Data Storage
β”‚ └── {user_id}/ # User-specific folders
β”‚ β”œβ”€β”€ prediction_history.json # Pregnancy risk history
β”‚ β”œβ”€β”€ classification_history.json # Fetal classification history
β”‚ └── *.png, *.jpg # Uploaded images with timestamps
β”‚
└── πŸ“„ run.txt # Quick start instructions
```
## πŸš€ Quick Start
### Prerequisites
- **Python 3.8+** with pip
- **Node.js 16+** with npm
- **Apple Silicon Mac** (M1/M2/M3/M4) for optimal performance
- **Modern Browser** with camera support (Chrome, Firefox, Safari, Edge)
### Installation
1. **Clone the repository**
```bash
git clone https://huggingface.co/karthik-2905/Preganancy-Prediction
cd hackathon15092025
```
2. **Install Python dependencies**
```bash
pip install -r config/requirements.txt
```
3. **Install frontend dependencies**
```bash
cd frontend
npm install
cd ..
```
4. **Set up Clerk authentication**
- Update `PUBLISHABLE_KEY` in `frontend/src/main.tsx`
- Configure Clerk project settings for medical applications
### Running the System
**Full System (Recommended - 4 Services)**
```bash
# Terminal 1: API Server (NEW - for real-time history)
python api_server.py
# Terminal 2: Frontend Dashboard
cd frontend && npm run dev
# Terminal 3: Pregnancy Risk App
cd apps && streamlit run pregnancy_risk_app.py --server.port 8501
# Terminal 4: Fetal Plane App
cd apps && streamlit run fetal_plane_app.py --server.port 8502
```
**Individual Services**
```bash
# Frontend only (React dashboard with authentication)
cd frontend && npm run dev
# Pregnancy risk prediction only
cd apps && streamlit run pregnancy_risk_app.py --server.port 8501
# Fetal plane classification only
cd apps && streamlit run fetal_plane_app.py --server.port 8502
```
### Access Points
- **🏠 Main Dashboard**: http://localhost:5173
- **🀱 Pregnancy Risk App**: http://localhost:8501
- **πŸ”¬ Fetal Plane App**: http://localhost:8502
- **🌐 API Server**: http://localhost:8503
- **πŸ“Š History Page**: Accessible via main dashboard after authentication (Real-time updates!)
- **πŸ–ΌοΈ Image Viewer**: Direct image access via API server
## πŸ” Authentication & Security
### Clerk Integration
- **Enterprise Authentication**: Secure user management with Clerk
- **User Isolation**: Each user gets dedicated storage folders
- **Session Management**: Automatic session handling with fallback
- **HIPAA Compliance**: Secure handling of sensitive medical data
### Data Security
- **User-Specific Folders**: `uploads/{user_id}/` structure
- **Automatic Cleanup**: Files older than 7 days removed automatically
- **History Limits**: Maximum 50 entries per user per application
- **No External Database**: Simple JSON file storage for privacy
## πŸ“Š Usage Guide
### Pregnancy Risk Prediction
1. **Navigate** to Pregnancy Risk page
2. **Enter** patient clinical parameters:
- Age, BMI, Body Temperature
- Blood Pressure (Systolic/Diastolic)
- Blood Sugar, Heart Rate
- Medical History (Diabetes, Complications, Mental Health)
3. **Click** "Predict Risk Level"
4. **Review** results with confidence scores
5. **Check** History page for past predictions
### Fetal Ultrasound Classification
1. **Navigate** to Fetal Planes page
2. **Upload** ultrasound image via:
- πŸ“ File upload (PNG, JPG, JPEG)
- πŸ“· Camera capture (mobile/desktop)
- πŸ“‚ File path input
3. **Click** classification button
4. **Review** anatomical plane classification
5. **View** confidence scores and detailed results
6. **Access** History page for past classifications
### History Tracking
- **Automatic Saving**: All predictions and classifications saved
- **JSON Format**: Human-readable data structure
- **Timestamps**: ISO format for precise tracking
- **User Isolation**: Only your data is accessible
- **Export Ready**: JSON files can be easily exported
## πŸ”§ Technical Details
### AI Models
#### Pregnancy Risk Prediction
- **Algorithm**: Random Forest Classifier
- **Accuracy**: 100% on validation set
- **Features**: 11 clinical parameters
- **Inference Time**: <1ms
- **Training Data**: 1,187 medical records
#### Fetal Ultrasound Classification
- **Algorithm**: Vision Transformer (ViT-Base-Patch16-224)
- **Accuracy**: 91.69% on validation set
- **Categories**: 9 anatomical planes
- **Inference Time**: <100ms
- **Training Data**: 12,400+ ultrasound images
### System Architecture
#### Frontend (React + TypeScript)
- **Framework**: Vite + React 18
- **Authentication**: Clerk integration
- **Styling**: Tailwind CSS + Custom CSS
- **Fonts**: Satoshi font family
- **Responsive**: Mobile-first design
#### Backend (Streamlit + PyTorch)
- **Framework**: Streamlit for rapid prototyping
- **ML Library**: PyTorch + Transformers
- **Optimization**: Apple Silicon MPS support
- **Storage**: JSON files + image uploads
### Data Management
#### File Structure
```
uploads/
β”œβ”€β”€ {user_id_1}/
β”‚ β”œβ”€β”€ prediction_history.json
β”‚ β”œβ”€β”€ classification_history.json
β”‚ β”œβ”€β”€ 20240115_103000_ultrasound.png
β”‚ └── predictions/
β”‚ └── prediction_20240115_103000.json
└── {user_id_2}/
β”œβ”€β”€ prediction_history.json
└── classification_history.json
```
#### JSON Schema Examples
**Pregnancy Risk History Entry**
```json
{
"id": "uuid-string",
"timestamp": "2024-01-15T10:30:00.000Z",
"type": "pregnancy_risk",
"input_data": {
"Age": 28,
"BMI": 24.5,
"Systolic BP": 120,
"Diastolic": 80,
"BS": 7.2,
"Body Temp": 98.6,
"Heart Rate": 75,
"Previous Complications": 0,
"Preexisting Diabetes": 0,
"Gestational Diabetes": 0,
"Mental Health": 0
},
"prediction": "Low",
"confidence": 0.95,
"probabilities": {
"high_risk": 0.05,
"low_risk": 0.95
},
"user_id": "user_123"
}
```
**Fetal Classification History Entry**
```json
{
"id": "uuid-string",
"timestamp": "2024-01-15T10:35:00.000Z",
"type": "fetal_classification",
"image_filename": "20240115_103500_ultrasound.png",
"predicted_label": "Fetal Brain_Trans-thalamic",
"confidence": 0.92,
"top_predictions": [
{"Class": "Fetal Brain_Trans-thalamic", "Probability": 0.92},
{"Class": "Fetal Brain_Trans-ventricular", "Probability": 0.05}
],
"user_id": "user_123"
}
```
## πŸ“± Applications
| Application | Location | Port | Description |
|-------------|----------|------|-------------|
| **Main Dashboard** | `index.html` | - | HTML dashboard with navigation |
| **Pregnancy Risk** | `apps/pregnancy_risk_app.py` | 8501 | Risk prediction interface |
| **Fetal Planes** | `apps/fetal_plane_app.py` | 8502 | Ultrasound classification |
## 🎯 Model Performance
### Pregnancy Risk Model
- **Accuracy**: 100%
- **Algorithm**: Random Forest Classifier
- **Features**: 11 clinical parameters
- **Dataset**: 1,187 patient records
- **Inference**: <1ms
### Fetal Plane Model
- **Validation Accuracy**: 91.69%
- **Algorithm**: Vision Transformer (ViT-Base-Patch16-224)
- **Classes**: 9 anatomical planes
- **Dataset**: 12,400 ultrasound images
- **Inference**: <100ms
- **Optimization**: Apple Silicon MPS
## πŸ”§ Development
### Training Models
```bash
# Train pregnancy risk model
cd apps && python pregnancy_risk_prediction.py
# Train fetal plane model (thermal-safe for M4)
cd scripts && python train_fetal_model_thermal.py
```
### Project Organization Benefits
- βœ… **Clean Structure**: Logical separation of concerns
- βœ… **Easy Navigation**: Clear folder hierarchy
- βœ… **Maintainable**: Organized code and documentation
- βœ… **Scalable**: Easy to add new features
- βœ… **Professional**: Industry-standard organization
## πŸ“Š System Requirements
- **Python**: 3.9+
- **Platform**: macOS with Apple Silicon (M1/M2/M3/M4)
- **RAM**: 8GB+ recommended
- **Storage**: 2GB+ for datasets and models
## πŸ”’ Privacy & Security
- **Local Processing**: All AI inference runs locally
- **No Data Storage**: Patient data not permanently stored
- **HIPAA Compliant**: Privacy-by-design architecture
- **Secure Models**: No data leakage in model weights
## πŸ“ž Support
For detailed documentation, see the `docs/` directory:
- `docs/DOCUMENTATION.md` - Comprehensive system documentation
- `docs/PROJECT_STRUCTURE.md` - Detailed project organization
- `docs/README_FETAL.md` - Fetal plane classification guide
---
*Last Updated: January 2025*
*Version: 2.0 - Organized Structure*
*Platform: Apple Silicon Optimized*