Spaces:
Sleeping
Sleeping
Update ml_factory.py
Browse files- ml_factory.py +34 -17
ml_factory.py
CHANGED
|
@@ -35,11 +35,17 @@ class ModelManager:
|
|
| 35 |
pkl_path = os.path.join(model_dir, 'model_weights.pkl')
|
| 36 |
if not os.path.exists(json_path) or not os.path.exists(pkl_path): return None
|
| 37 |
with open(json_path, 'r') as f: meta = json.load(f)
|
|
|
|
| 38 |
if meta['type'] == "regression":
|
| 39 |
instance = RegressionModel(name=meta['name'], framework=meta['framework'], algo_name=meta['algorithm'], window_size=meta['window_size'], weights_path=pkl_path)
|
| 40 |
else:
|
| 41 |
instance = ClassificationModel(name=meta['name'], framework=meta['framework'], algo_name=meta['algorithm'], window_size=meta['window_size'], threshold=meta['threshold'], weights_path=pkl_path)
|
| 42 |
instance.total_trained = meta.get('total_trained', 0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
self._initialize_features(instance)
|
| 44 |
return instance
|
| 45 |
except Exception as e:
|
|
@@ -56,16 +62,19 @@ class ModelManager:
|
|
| 56 |
X, y = self.db.get_bin_data(instance.window_size, instance.threshold, limit=limit)
|
| 57 |
|
| 58 |
total = len(X)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
self.training_status[instance.name] = f"Initialisation 0/{total} features"
|
| 60 |
|
| 61 |
for i, seq in enumerate(X):
|
| 62 |
self.training_status[instance.name] = f"Initialisation {i+1}/{total} features"
|
| 63 |
instance.last_sequence = seq
|
|
|
|
| 64 |
|
| 65 |
-
if instance.last_sequence
|
| 66 |
-
instance.last_prediction = instance.predict(instance.last_sequence)
|
| 67 |
-
else:
|
| 68 |
-
instance.last_prediction = None
|
| 69 |
|
| 70 |
if instance.name in self.training_status:
|
| 71 |
del self.training_status[instance.name]
|
|
@@ -89,17 +98,24 @@ class ModelManager:
|
|
| 89 |
self.models[instance.name] = instance
|
| 90 |
print(f" ✅ {instance.name} chargé (Données: {instance.total_trained:,}).")
|
| 91 |
|
| 92 |
-
def check_auto_generation(self,
|
|
|
|
| 93 |
for w in config.WINDOWS:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
self.auto_generated
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
def _worker_loop(self):
|
| 105 |
while True:
|
|
@@ -129,6 +145,7 @@ class ModelManager:
|
|
| 129 |
|
| 130 |
total_data = len(X)
|
| 131 |
if total_data < window + 1:
|
|
|
|
| 132 |
return f"❌ Pas assez de données ({total_data} lignes)."
|
| 133 |
|
| 134 |
self.training_status[name] = f"Entraînement 0/{total_data} cotes"
|
|
@@ -174,7 +191,7 @@ class ModelManager:
|
|
| 174 |
# Lancement de l'initialisation des features
|
| 175 |
self._initialize_features(instance)
|
| 176 |
|
| 177 |
-
return f"✅ {name} créé sur {total_data} lignes.
|
| 178 |
except Exception as e:
|
| 179 |
if name in self.training_status: del self.training_status[name]
|
| 180 |
return f"❌ Erreur entraînement {name} : {str(e)}"
|
|
@@ -256,7 +273,7 @@ class ModelManager:
|
|
| 256 |
if init_status: status = init_status
|
| 257 |
elif not model.is_training: status = "🔴 En Pause"
|
| 258 |
elif model.last_prediction is not None: status = f"🟢 Prédit: {model.last_prediction:.2f}x"
|
| 259 |
-
else: status = "🟡 En attente..."
|
| 260 |
|
| 261 |
reg_data.append({
|
| 262 |
"Modèle": name, "Fenêtre (W)": model.window_size,
|
|
@@ -283,7 +300,7 @@ class ModelManager:
|
|
| 283 |
if init_status: status = init_status
|
| 284 |
elif not model.is_training: status = "🔴 En Pause"
|
| 285 |
elif model.last_prediction is not None: status = f"🟢 Prédit: {model.last_prediction}"
|
| 286 |
-
else: status = "🟡 En attente..."
|
| 287 |
|
| 288 |
bin_data.append({
|
| 289 |
"Modèle": name, "Fenêtre (W)": model.window_size, "Seuil (T)": f"{model.threshold}x",
|
|
|
|
| 35 |
pkl_path = os.path.join(model_dir, 'model_weights.pkl')
|
| 36 |
if not os.path.exists(json_path) or not os.path.exists(pkl_path): return None
|
| 37 |
with open(json_path, 'r') as f: meta = json.load(f)
|
| 38 |
+
|
| 39 |
if meta['type'] == "regression":
|
| 40 |
instance = RegressionModel(name=meta['name'], framework=meta['framework'], algo_name=meta['algorithm'], window_size=meta['window_size'], weights_path=pkl_path)
|
| 41 |
else:
|
| 42 |
instance = ClassificationModel(name=meta['name'], framework=meta['framework'], algo_name=meta['algorithm'], window_size=meta['window_size'], threshold=meta['threshold'], weights_path=pkl_path)
|
| 43 |
instance.total_trained = meta.get('total_trained', 0)
|
| 44 |
+
|
| 45 |
+
# Si c'est un modèle auto, on l'ajoute à la liste pour ne pas le recréer
|
| 46 |
+
if meta.get('is_auto', False) or instance.name.startswith("Auto_"):
|
| 47 |
+
self.auto_generated.add(instance.name)
|
| 48 |
+
|
| 49 |
self._initialize_features(instance)
|
| 50 |
return instance
|
| 51 |
except Exception as e:
|
|
|
|
| 62 |
X, y = self.db.get_bin_data(instance.window_size, instance.threshold, limit=limit)
|
| 63 |
|
| 64 |
total = len(X)
|
| 65 |
+
if total < limit:
|
| 66 |
+
# Pas assez de données pour initialiser
|
| 67 |
+
instance.last_prediction = None
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
self.training_status[instance.name] = f"Initialisation 0/{total} features"
|
| 71 |
|
| 72 |
for i, seq in enumerate(X):
|
| 73 |
self.training_status[instance.name] = f"Initialisation {i+1}/{total} features"
|
| 74 |
instance.last_sequence = seq
|
| 75 |
+
time.sleep(0.05) # Mini-pause pour l'UI
|
| 76 |
|
| 77 |
+
instance.last_prediction = instance.predict(instance.last_sequence) if instance.last_sequence else None
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
if instance.name in self.training_status:
|
| 80 |
del self.training_status[instance.name]
|
|
|
|
| 98 |
self.models[instance.name] = instance
|
| 99 |
print(f" ✅ {instance.name} chargé (Données: {instance.total_trained:,}).")
|
| 100 |
|
| 101 |
+
def check_auto_generation(self, current_history_length):
|
| 102 |
+
"""Vérifie pour chaque fenêtre si on a atteint la taille critique pour créer les modèles Auto."""
|
| 103 |
for w in config.WINDOWS:
|
| 104 |
+
if current_history_length >= w + 1:
|
| 105 |
+
# Modèle de Régression
|
| 106 |
+
reg_name = f"Auto_Reg_W{w}"
|
| 107 |
+
if reg_name not in self.auto_generated:
|
| 108 |
+
self.train_queue.put(("regression", reg_name, w, None, "river"))
|
| 109 |
+
self.auto_generated.add(reg_name)
|
| 110 |
+
print(f"🤖 [Auto-Factory] Création de {reg_name} déclenchée (Dataset W{w} prêt) !")
|
| 111 |
+
|
| 112 |
+
# Modèles de Classification Binaire
|
| 113 |
+
for t in config.THRESHOLDS:
|
| 114 |
+
bin_name = f"Auto_Bin_W{w}_T{t}"
|
| 115 |
+
if bin_name not in self.auto_generated:
|
| 116 |
+
self.train_queue.put(("binary", bin_name, w, t, "river"))
|
| 117 |
+
self.auto_generated.add(bin_name)
|
| 118 |
+
print(f"🤖 [Auto-Factory] Création de {bin_name} déclenchée !")
|
| 119 |
|
| 120 |
def _worker_loop(self):
|
| 121 |
while True:
|
|
|
|
| 145 |
|
| 146 |
total_data = len(X)
|
| 147 |
if total_data < window + 1:
|
| 148 |
+
if name in self.training_status: del self.training_status[name]
|
| 149 |
return f"❌ Pas assez de données ({total_data} lignes)."
|
| 150 |
|
| 151 |
self.training_status[name] = f"Entraînement 0/{total_data} cotes"
|
|
|
|
| 191 |
# Lancement de l'initialisation des features
|
| 192 |
self._initialize_features(instance)
|
| 193 |
|
| 194 |
+
return f"✅ {name} créé sur {total_data} lignes. Prédit: {instance.last_prediction}"
|
| 195 |
except Exception as e:
|
| 196 |
if name in self.training_status: del self.training_status[name]
|
| 197 |
return f"❌ Erreur entraînement {name} : {str(e)}"
|
|
|
|
| 273 |
if init_status: status = init_status
|
| 274 |
elif not model.is_training: status = "🔴 En Pause"
|
| 275 |
elif model.last_prediction is not None: status = f"🟢 Prédit: {model.last_prediction:.2f}x"
|
| 276 |
+
else: status = "🟡 En attente de features..."
|
| 277 |
|
| 278 |
reg_data.append({
|
| 279 |
"Modèle": name, "Fenêtre (W)": model.window_size,
|
|
|
|
| 300 |
if init_status: status = init_status
|
| 301 |
elif not model.is_training: status = "🔴 En Pause"
|
| 302 |
elif model.last_prediction is not None: status = f"🟢 Prédit: {model.last_prediction}"
|
| 303 |
+
else: status = "🟡 En attente de features..."
|
| 304 |
|
| 305 |
bin_data.append({
|
| 306 |
"Modèle": name, "Fenêtre (W)": model.window_size, "Seuil (T)": f"{model.threshold}x",
|