Predicting glucose levels 4 hours into the future using LSTM neural networks and reinforcement learning agents trained on simulated human physiology
- Overview
- Key Features
- The Science: Models & Training
- Technical Architecture
- Installation
- API Reference
- Project Structure
- Contributing
- License
Aura is a comprehensive, AI-powered diabetes management system that combines multiple machine learning technologies to provide intelligent, proactive health management. Unlike traditional reactive glucose monitoring, Aura predicts your future glucose levels and provides personalized insulin recommendations.
| Challenge | Impact |
|---|---|
| 537 million people worldwide have diabetes | Global health crisis |
| 67% struggle with daily management | Poor quality of life |
| 150+ daily decisions about food, insulin, activity | Decision fatigue |
| Only 24% achieve optimal glucose control | Suboptimal outcomes |
Aura transforms diabetes management through:
- 🔮 Predictive AI: LSTM neural networks forecast glucose 4 hours ahead
- 🤖 Intelligent Dosing: RL agents trained on simulated human physiology
- 💬 Natural Language: Just type "had a sandwich" — AI handles the rest
- 📊 Clinical Analytics: Professional-grade reports for doctors
|
|
|
|
Our glucose prediction system uses a Long Short-Term Memory (LSTM) neural network, a type of recurrent neural network specifically designed for sequence prediction tasks.
┌─────────────────────────────────────────────────────────┐
│ INPUT LAYER │
│ (12 timesteps × 1 feature) │
│ Last 12 glucose readings (1 hour) │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ LSTM LAYER │
│ 16 hidden units │
│ Learns temporal patterns in glucose dynamics │
└─────────────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────────────┐
│ DENSE LAYER │
│ 1 output neuron │
│ Predicts next glucose value │
└─────────────────────────────────────────────────────────┘
| Parameter | Value | Description |
|---|---|---|
| Training Data | OhioT1DM Dataset | Real CGM data from Type 1 diabetics |
| Look-back Window | 12 timesteps | 1 hour of historical glucose (5-min intervals) |
| Prediction Horizon | 12 steps forward | 1 hour ahead prediction |
| Loss Function | Mean Squared Error (MSE) | Regression optimization |
| Optimizer | Adam | Adaptive learning rate |
| Epochs | 50 | Training iterations |
| Batch Size | 32 | Samples per gradient update |
# Normalization using MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))
dataset_scaled = scaler.fit_transform(glucose_data)
# Sequence creation
def create_sequences(dataset, look_back=12):
X, Y = [], []
for i in range(len(dataset) - look_back - 1):
X.append(dataset[i:(i + look_back), 0])
Y.append(dataset[i + look_back, 0])
return np.array(X), np.array(Y)Predictions are post-processed with physiological constraints:
- Maximum rate of change: ±4 mg/dL per 5-minute interval
- Physiological bounds: 40-400 mg/dL hard limits
- Trend consistency: Predictions follow recent glucose trajectory
The insulin recommendation system uses a Deep Q-Network (DQN) trained using Stable-Baselines3 on a custom simulated human physiology environment.
We built a detailed physiological simulation that models:
class RealisticDiabetesSimulator:
"""
Simulates Type 1 Diabetes glucose dynamics based on
published physiological models and clinical data
"""
def __init__(self, patient_weight=70):
# Insulin Parameters
self.insulin_sensitivity = 50 # mg/dL drop per unit
self.insulin_duration = 4.0 # hours of action
self.carb_ratio = 12 # grams per unit
# Physiological Parameters
self.liver_glucose_rate = 2.0 # mg/dL/hour baseline production
self.glucose_clearance = 0.02 # fraction cleared when >140
# Variability (real-world noise)
self.insulin_variability = 0.15 # 15% day-to-day variation
self.absorption_variability = 0.20 # 20% carb absorption variation| Feature | Implementation | Clinical Basis |
|---|---|---|
| Insulin Action Curve | Exponential decay with peak at 1.5 hours | Matches rapid-acting insulin profile |
| Carb Absorption | Bi-exponential model over 3 hours | Reflects gastric emptying patterns |
| Dawn Phenomenon | Sinusoidal glucose rise 4-8 AM | Cortisol-driven morning hyperglycemia |
| Liver Glucose Production | Baseline rate reduced by insulin | Hepatic glucose output suppression |
| Day-to-Day Variability | 15-20% random variation | Real-world insulin sensitivity changes |
def _insulin_action_curve(self, time_since_injection):
"""
Insulin action peaks around 1.5 hours post-injection
Based on pharmacokinetic profiles of rapid-acting insulin
"""
if time_since_injection <= 0:
return 0
t = time_since_injection
return (t / 1.5) * math.exp(-(t - 1.5) / 1.5)class AuraDiabetesEnv(gym.Env):
"""
OpenAI Gymnasium environment for training RL agents
"""
# Action Space: 31 discrete actions (0-15 units in 0.5 increments)
action_space = gym.spaces.Discrete(31)
# Observation Space: 5 continuous values
observation_space = gym.spaces.Box(
low=np.array([30, -20, 0, 0, 0]),
high=np.array([500, 20, 24, 30, 8]),
dtype=np.float32
)
# [glucose, trend, time_of_day, active_insulin, time_since_meal]The reward function encourages safe, effective glucose control:
def _calculate_reward(self, glucose, insulin_dose, active_insulin):
reward = 0.0
# Primary: Time in Range (70-180 mg/dL)
if 70 <= glucose <= 180:
reward += 20.0
if 80 <= glucose <= 140:
reward += 15.0 # Tight control bonus
if 90 <= glucose <= 120:
reward += 10.0 # Optimal range
# Severe penalties for dangerous levels
elif glucose < 70:
if glucose < 50:
reward -= 300.0 # Life-threatening hypoglycemia
elif glucose < 60:
reward -= 150.0 # Severe hypoglycemia
else:
reward -= 75.0 # Mild hypoglycemia
elif glucose > 180:
if glucose > 300:
reward -= 100.0 # Severe hyperglycemia
else:
reward -= 25.0 # Mild hyperglycemia
# Penalize over-treatment
if insulin_dose > 8:
reward -= (insulin_dose - 8) * 5
return rewardmodel = DQN(
'MlpPolicy',
env,
learning_rate=3e-4,
buffer_size=100000,
learning_starts=1000,
batch_size=64,
gamma=0.99,
target_update_interval=500,
exploration_fraction=0.2,
exploration_final_eps=0.05,
verbose=1,
device='cuda' # GPU acceleration
)
# Training
model.learn(
total_timesteps=50000,
callback=checkpoint_callback
)| Metric | Value |
|---|---|
| Training Episodes | 50,000+ |
| Average Reward | Converges to positive values |
| Time in Range | ~75-85% during evaluation |
| Hypoglycemia Events | Significantly reduced |
The NLP system parses natural language inputs to extract health-relevant information.
User: "Had a turkey sandwich and chips for lunch"
Aura: ✅ Logged: Turkey Sandwich (~35g carbs), Chips (~25g carbs)
📈 Predicted glucose rise to 165 mg/dL in 45 minutes
💡 Consider 5 units rapid insulin
class NaturalLanguageProcessor:
"""
Extracts structured health data from conversational input
"""
def __init__(self):
self.food_database = self._load_food_database()
self.exercise_patterns = self._compile_exercise_patterns()
self.glucose_patterns = self._compile_glucose_patterns()
def process(self, message: str) -> dict:
result = {
'foods': self._extract_foods(message),
'exercise': self._extract_exercise(message),
'glucose': self._extract_glucose(message),
'insulin': self._extract_insulin(message)
}
return result
def _extract_foods(self, text: str) -> list:
"""
Uses pattern matching and fuzzy string matching
to identify food items and estimate carbs
"""
# Pattern-based extraction
# Fuzzy matching against food database
# Carbohydrate estimation
passThe system includes a comprehensive food database with:
- 1000+ common foods with nutritional data
- Regional foods (Indian cuisine support)
- Fuzzy matching for spelling variations
- Portion size estimation
┌─────────────────────────────────────────────────────────────────────────┐
│ FRONTEND │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌───────────┐ │
│ │ HTML5 │ │ CSS3 │ │ JavaScript │ │ GSAP │ │
│ │ Canvas │ │ Glassmorphism│ │ ES6+ │ │ Animations│ │
│ └─────────────┘ └─────────────┘ └─────────────┘ └───────────┘ │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Chart.js │ │ Socket.IO │ │ Lenis │ │
│ │ Graphs │ │ Real-time │ │ Smooth Scroll│ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ BACKEND │
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ Flask Server │ │
│ │ │ │
│ │ ┌───────────────┐ ┌───────────────┐ ┌─────────────────┐ │ │
│ │ │ REST API │ │ WebSocket │ │ Auth/Sessions │ │ │
│ │ │ Endpoints │ │ Real-time │ │ (Bcrypt) │ │ │
│ │ └───────────────┘ └───────────────┘ └─────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ┌────────────────────────────────┴────────────────────────────────┐ │
│ │ INTELLIGENT CORE │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │ │
│ │ │ Predictor │ │ Recommender│ │ NLP Processor │ │ │
│ │ │ (LSTM) │ │ (DQN) │ │ (Food/Exercise) │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────────────┘ │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │ │
│ │ │ Model │ │ Report │ │ Analytics │ │ │
│ │ │ Trainer │ │ Generator │ │ (TIR, GMI, CV) │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ DATA LAYER │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────────────┐ │
│ │ PostgreSQL │ │ Redis │ │ File System │ │
│ │ Database │ │ Cache │ │ (Models, Reports, Temp) │ │
│ └─────────────┘ └─────────────┘ └─────────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────────────┐
│ ML MODELS │
│ │
│ ┌──────────────────────┐ ┌──────────────────────┐ │
│ │ glucose_predictor │ │ insulin_advisor │ │
│ │ .h5 │ │ _model.zip │ │
│ │ │ │ │ │
│ │ ┌──────────────┐ │ │ ┌──────────────┐ │ │
│ │ │ LSTM │ │ │ │ DQN │ │ │
│ │ │ (Keras) │ │ │ │ (SB3 + PyTorch)│ │ │
│ │ └──────────────┘ │ │ └──────────────┘ │ │
│ │ │ │ │ │
│ │ Input: 12 glucose │ │ Input: 5 features │ │
│ │ Output: 12 future │ │ Output: 31 actions │ │
│ └──────────────────────┘ └──────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
- Python 3.9+
- PostgreSQL 12+ (or Docker)
- 4GB RAM minimum
- GPU recommended for training (optional for inference)
# Clone the repository
git clone https://github.com/tripathiji1312/Aura.git
cd Aura
# Create virtual environment
python -m venv venv
source venv/bin/activate # Linux/macOS
# venv\Scripts\activate # Windows
# Install dependencies
pip install -r app/requirements.txt
# Initialize database
python app/database.py
# Start the server
python app/app.pyOpen http://localhost:5001 in your browser.
# Build and run with Docker
docker build -t aura .
docker run -p 7860:7860 aura
# Or with Docker Compose
docker-compose up -d# Database
DATABASE_URL=postgresql://user:password@localhost:5432/aura
# Redis (optional, for caching)
REDIS_URL=redis://localhost:6379
# Supabase (optional, for cloud storage)
SUPABASE_URL=your_supabase_url
SUPABASE_KEY=your_supabase_keyPOST /api/chat
Content-Type: application/json
{
"message": "Had 2 slices of pizza and a coke for dinner",
"user_id": 123
}Response:
{
"success": true,
"message": "✅ Logged: Pizza (70g carbs), Coke (39g carbs)\n📈 Predicted peak: 185 mg/dL in 1.5 hours\n💡 Suggested: 9 units rapid insulin",
"data": {
"foods": [
{"name": "Pizza", "carbs": 70, "confidence": 0.92},
{"name": "Coke", "carbs": 39, "confidence": 0.95}
],
"total_carbs": 109
},
"predictions": {
"next_hour": [145, 162, 178, 185, 182, 175, 168, 160, 152, 145, 140, 135]
}
}GET /api/dashboard?user_id=123Response:
{
"current_glucose": 127,
"trend": "stable",
"time_in_range": 78.5,
"recent_meals": [...],
"predictions": [...],
"health_score": 85
}POST /api/ai/calibrate
Content-Type: application/json
{
"user_id": 123
}This endpoint fine-tunes the prediction model using the user's historical data.
POST /api/report/generate
Content-Type: application/json
{
"user_id": 123,
"period": "7days"
}Returns a PDF report with:
- Average glucose and GMI
- Time in Range breakdown
- Glucose variability (CV%)
- Meal/insulin patterns
- Recommendations
aura/
├── app/
│ ├── app.py # Flask server, routes, WebSocket
│ ├── config.py # Configuration and environment
│ ├── database.py # PostgreSQL schema and operations
│ │
│ ├── intelligent_core.py # AI orchestration and decisions
│ ├── prediction_service.py # LSTM glucose forecasting
│ ├── recommendation_service.py # DQN insulin advisor
│ ├── natural_language_processor.py # NLP for chat parsing
│ ├── model_trainer.py # User-specific fine-tuning
│ ├── report_generator.py # PDF clinical reports
│ ├── simulator.py # Demo data generation
│ │
│ ├── models/
│ │ ├── glucose_predictor.h5 # Trained LSTM model
│ │ └── scaler.gz # Feature normalization
│ │
│ ├── static/
│ │ ├── index.html # Single-page application
│ │ ├── css/style.css # Styling (7500+ lines)
│ │ └── js/script.js # Frontend logic (4000+ lines)
│ │
│ ├── data/
│ │ └── 559-ws-training.xml # OhioT1DM training data
│ │
│ └── temp_reports/ # Generated PDFs and charts
│
├── notebooks/
│ ├── lstm_training.ipynb # LSTM model training notebook
│ └── rl_training.ipynb # DQN agent training notebook
│
├── Dockerfile # Container configuration
├── requirements.txt # Python dependencies
└── README.md # This file
- Local-first processing: All AI inference happens on your machine
- No cloud uploads: Health data never leaves your environment
- Encrypted storage: Database encryption at rest
- Open source: Full code transparency and auditability
| Feature | Implementation |
|---|---|
| Password Hashing | Bcrypt with salt |
| SQL Injection | Parameterized queries |
| XSS Protection | Input sanitization |
| Session Management | Secure cookies |
| HTTPS Support | TLS configuration |
We welcome contributions from developers, healthcare professionals, and ML engineers!
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
| Area | Description |
|---|---|
| 🧠 AI/ML | Improve prediction accuracy, add new models |
| 🎨 Frontend | UI/UX improvements, accessibility |
| 📱 Mobile | React Native or Flutter apps |
| 🔒 Security | Privacy enhancements |
| 🌍 i18n | Multi-language support |
| 📚 Docs | Documentation improvements |
| 🧪 Testing | Unit tests, integration tests |
- Mobile-responsive PWA
- CGM API integrations (Dexcom, Libre)
- Multi-user household support
- Meal photo recognition
- Native iOS/Android apps
- Healthcare provider portal
- Federated learning for privacy-preserving model updates
- Voice interface
- Genomic personalization: AI tailored to genetic risk factors
- Autonomous insulin delivery: Integration with pump systems
- Clinical decision support: Tools for endocrinologists
- Global health impact: Democratizing diabetes care worldwide
This project is licensed under the MIT License — see the LICENSE file for details.
This software is not intended to replace professional medical advice, diagnosis, or treatment. Always consult with qualified healthcare providers for medical decisions. The developers are not responsible for any health outcomes related to the use of this software.
- OhioT1DM Dataset: For providing real CGM data for model training
- Stable-Baselines3: For the excellent RL framework
- TensorFlow/Keras: For deep learning infrastructure
- The open-source community: For continuous inspiration