-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontinuous_learner.py
More file actions
231 lines (183 loc) · 7.81 KB
/
continuous_learner.py
File metadata and controls
231 lines (183 loc) · 7.81 KB
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
#!/usr/bin/env python3
"""
Continuous Learner for Legacy AI
Automatically retrains models when new operational data is available.
Runs on schedule to keep AI current with latest dispatch patterns.
"""
import time
from datetime import datetime, timedelta
from pathlib import Path
from typing import Dict, Any
from training_data_builder import TrainingDataBuilder
from model_trainer import ModelTrainer
from model_manager import ModelManager
from logger import LogiLogger
logger = LogiLogger.get_logger("continuous_learner")
class ContinuousLearner:
"""Automated continuous learning pipeline"""
def __init__(
self,
retrain_interval_days: int = 7,
min_new_examples: int = 50,
auto_deploy: bool = False
):
self.retrain_interval = timedelta(days=retrain_interval_days)
self.min_new_examples = min_new_examples
self.auto_deploy = auto_deploy
# Initialize components
self.data_builder = TrainingDataBuilder()
self.trainer = ModelTrainer()
self.model_manager = ModelManager()
self.last_train_time = None
self.load_state()
logger.info(f"Continuous Learner initialized")
logger.info(f"Retrain interval: {retrain_interval_days} days")
logger.info(f"Min new examples: {min_new_examples}")
logger.info(f"Auto-deploy: {auto_deploy}")
def load_state(self):
"""Load last training timestamp"""
state_file = self.data_builder.output_dir / "learner_state.json"
if state_file.exists():
import json
with open(state_file) as f:
state = json.load(f)
self.last_train_time = datetime.fromisoformat(state.get("last_train_time"))
logger.info(f"Last training: {self.last_train_time}")
def save_state(self):
"""Save training state"""
import json
state_file = self.data_builder.output_dir / "learner_state.json"
with open(state_file, 'w') as f:
json.dump({
"last_train_time": datetime.utcnow().isoformat()
}, f, indent=2)
def should_retrain(self) -> bool:
"""Determine if retraining is needed"""
if self.last_train_time is None:
logger.info("First training run")
return True
time_since_last = datetime.utcnow() - self.last_train_time
if time_since_last >= self.retrain_interval:
logger.info(f"Retrain interval reached ({time_since_last.days} days)")
return True
logger.info(f"Next retrain in {(self.retrain_interval - time_since_last).days} days")
return False
def check_new_data(self) -> int:
"""Check for new training examples"""
# This would check Firestore for new data since last training
# For now, return a placeholder
logger.info("Checking for new operational data...")
# Count new driver/plant records
self.data_builder.initialize_firebase()
drivers = self.data_builder.fetch_driver_data()
plants = self.data_builder.fetch_plant_data()
# Rough estimate of new examples
new_examples = len(drivers) * 3 + len(plants) # 3 examples per driver
logger.info(f"Estimated new examples: {new_examples}")
return new_examples
def run_training_cycle(self) -> bool:
"""Execute full training cycle"""
logger.info("=" * 60)
logger.info("STARTING CONTINUOUS LEARNING CYCLE")
logger.info("=" * 60)
try:
# Step 1: Build fresh datasets
logger.info("Step 1: Building training datasets...")
datasets = self.data_builder.build_all_datasets()
# Step 2: Train model
logger.info("Step 2: Training model...")
success = self.trainer.train_legacy_ai()
if not success:
logger.error("Training failed")
return False
# Step 3: Get latest model name
import subprocess
result = subprocess.run(
["ollama", "list"],
capture_output=True,
text=True
)
# Find latest legacy-ai model
latest_model = None
for line in result.stdout.split('\n'):
if 'legacy-ai-srm' in line:
latest_model = line.split()[0]
break
if not latest_model:
logger.error("Could not find trained model")
return False
# Step 4: Register model
logger.info(f"Step 3: Registering model {latest_model}...")
self.model_manager.register_model(
latest_model,
{
"training_method": "continuous_learning",
"datasets": datasets,
"trained_at": datetime.utcnow().isoformat()
},
set_active=self.auto_deploy
)
# Step 5: Auto-deploy if enabled
if self.auto_deploy:
logger.info("Step 4: Auto-deploying model to production...")
self.model_manager.set_active_model(latest_model)
logger.info(f"✓ Model deployed: {latest_model}")
else:
logger.info("Manual deployment required")
logger.info(f"To deploy: python3 model_manager.py set-active {latest_model}")
# Save state
self.save_state()
self.last_train_time = datetime.utcnow()
logger.info("=" * 60)
logger.info("CONTINUOUS LEARNING CYCLE COMPLETE")
logger.info("=" * 60)
return True
except Exception as e:
logger.error(f"Training cycle failed: {e}", exc_info=True)
return False
def run_once(self):
"""Run single learning cycle"""
if self.should_retrain():
new_examples = self.check_new_data()
if new_examples >= self.min_new_examples:
logger.info(f"Sufficient new data ({new_examples} examples) - starting training")
return self.run_training_cycle()
else:
logger.info(f"Insufficient new data ({new_examples}/{self.min_new_examples}) - skipping")
return False
else:
logger.info("Retrain interval not reached - skipping")
return False
def run_continuous(self, check_interval_hours: int = 24):
"""Run continuous learning loop"""
logger.info(f"Starting continuous learning mode (check every {check_interval_hours}h)")
while True:
try:
self.run_once()
# Wait for next check
logger.info(f"Sleeping for {check_interval_hours} hours...")
time.sleep(check_interval_hours * 3600)
except KeyboardInterrupt:
logger.info("Continuous learning stopped by user")
break
except Exception as e:
logger.error(f"Error in continuous loop: {e}")
time.sleep(3600) # Wait 1 hour on error
def main():
"""Main execution"""
import sys
logger.info("Legacy AI Continuous Learner")
# Parse arguments
mode = sys.argv[1] if len(sys.argv) > 1 else "once"
learner = ContinuousLearner(
retrain_interval_days=7,
min_new_examples=50,
auto_deploy=False # Set True for automatic deployment
)
if mode == "continuous":
learner.run_continuous(check_interval_hours=24)
else:
success = learner.run_once()
sys.exit(0 if success else 1)
if __name__ == "__main__":
main()