suppa09 commited on
Commit
08ad5d6
·
verified ·
1 Parent(s): 5e97314

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -13
app.py CHANGED
@@ -1,16 +1,17 @@
1
  """
2
- PolyBloom ML Service — v19.0.0
3
- Hugging Face Space: OMEGA COUNCIL (6 models)
4
 
5
  Model roster:
6
  ROW 1 FOUNDATION Chronos (zero-shot, CPU-optimized)
7
- ROW 2 TRAINED XGBoost · LightGBM (bootstrap train on startup)
8
  ROW 3 NEURAL-TS PatchTST · N-HiTS · iTransformer (NeuralForecast)
9
 
10
  v19 changes (cleanup):
11
  - REMOVED: MOIRAI (requires GPU, duplicate of Chronos)
12
  - REMOVED: MOMENT (representation model, not prediction-first)
13
  - REMOVED: Lag-Llama (LLM for TS, CPU timeout)
 
14
  - REMOVED: DAG (moved to frontend)
15
  - REMOVED: TiDE (moved to frontend)
16
  - Health endpoint now returns clean booleans only
@@ -18,6 +19,11 @@ v19 changes (cleanup):
18
  - Reduced ensemble to 6 high-quality models for CPU inference
19
  - Updated weights renormalized for 6-model ensemble
20
 
 
 
 
 
 
21
  Health endpoint GET /
22
  Omega endpoint POST /omega
23
  """
@@ -41,7 +47,7 @@ logger = logging.getLogger("polybloom")
41
  # App
42
  # ─────────────────────────────────────────────────────────────────────────────
43
 
44
- app = FastAPI(title="PolyBloom ML Service", version="19.0.0")
45
  app.add_middleware(
46
  CORSMiddleware,
47
  allow_origins=["*"],
@@ -57,6 +63,7 @@ _flags: dict = {
57
  "chronos": False,
58
  "xgboost": False,
59
  "lgbm": False,
 
60
  "patchtst": False,
61
  "nhits": False,
62
  "itransformer": False,
@@ -64,14 +71,15 @@ _flags: dict = {
64
 
65
  _models: dict = {} # name → loaded object
66
 
67
- # Weights — renormalized for 6 models (v19)
68
  WEIGHTS = {
69
- "Chronos": 0.22,
70
- "XGBoost": 0.20,
71
- "LightGBM": 0.18,
72
- "PatchTST": 0.15,
73
- "N-HiTS": 0.13,
74
- "iTransformer": 0.12,
 
75
  }
76
 
77
  # ─────────────────────────────────────────────────────────────────────────────
@@ -315,6 +323,7 @@ def build_neural_models(horizon: int = NF_HORIZON):
315
  async def startup_event():
316
  _load_xgboost()
317
  _load_lgbm()
 
318
  _load_neuralforecast()
319
  _load_chronos()
320
  logger.info("Startup complete. flags=%s", _flags)
@@ -373,6 +382,37 @@ def _load_lgbm():
373
  logger.warning("LightGBM unavailable: %s", exc)
374
 
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  def _load_neuralforecast():
377
  """Bootstrap-train PatchTST, N-HiTS, and iTransformer on synthetic candle data."""
378
  try:
@@ -493,6 +533,27 @@ def _predict_lightgbm(closes: list, feats: dict) -> dict:
493
  return _predict_chronos_bootstrap(closes, feats)
494
 
495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
  # ─────────────────────────────────────────────────────────────────────────────
497
  # Model inference — NeuralForecast (PatchTST, N-HiTS, iTransformer)
498
  # ─────────────────────────────────────────────────────────────────────────────
@@ -619,14 +680,15 @@ def _aggregate(predictions: list) -> dict:
619
  async def health_check():
620
  return {
621
  "ok": True,
622
- "version": "19.0.0",
623
  "chronos": _flags["chronos"],
624
  "xgboost": _flags["xgboost"],
625
  "lgbm": _flags["lgbm"],
 
626
  "patchtst": _flags["patchtst"],
627
  "nhits": _flags["nhits"],
628
  "itransformer": _flags["itransformer"],
629
- "models": ["Chronos", "XGBoost", "LightGBM", "PatchTST", "N-HiTS", "iTransformer"],
630
  "removed": ["MOIRAI", "MOMENT", "TimesNet", "Lag-Llama", "DAG", "TiDE", "TabPFN-TS", "N-BEATS", "AROpt"],
631
  "hf_space": True,
632
  }
@@ -661,6 +723,7 @@ async def omega(request: Request):
661
  # Trained Classifiers
662
  predictions.append(_predict_xgboost(closes, feats))
663
  predictions.append(_predict_lightgbm(closes, feats))
 
664
 
665
  # NeuralForecast (PatchTST, N-HiTS, iTransformer)
666
  predictions.extend(_predict_neuralforecast(closes, feats))
 
1
  """
2
+ PolyBloom ML Service — v19.1.0
3
+ Hugging Face Space: OMEGA COUNCIL (7 models)
4
 
5
  Model roster:
6
  ROW 1 FOUNDATION Chronos (zero-shot, CPU-optimized)
7
+ ROW 2 TRAINED XGBoost · LightGBM · CatBoost (bootstrap train on startup)
8
  ROW 3 NEURAL-TS PatchTST · N-HiTS · iTransformer (NeuralForecast)
9
 
10
  v19 changes (cleanup):
11
  - REMOVED: MOIRAI (requires GPU, duplicate of Chronos)
12
  - REMOVED: MOMENT (representation model, not prediction-first)
13
  - REMOVED: Lag-Llama (LLM for TS, CPU timeout)
14
+ - REMOVED: N-BEATS (NeuralForecast NBEATS OOM on free HF tier; dropped)
15
  - REMOVED: DAG (moved to frontend)
16
  - REMOVED: TiDE (moved to frontend)
17
  - Health endpoint now returns clean booleans only
 
19
  - Reduced ensemble to 6 high-quality models for CPU inference
20
  - Updated weights renormalized for 6-model ensemble
21
 
22
+ v19.1 changes:
23
+ - ADDED: CatBoost classifier (bootstrap-trained alongside XGBoost/LightGBM)
24
+ - Health endpoint now emits "catboost" boolean
25
+ - Ensemble expanded to 7 models; weights renormalized
26
+
27
  Health endpoint GET /
28
  Omega endpoint POST /omega
29
  """
 
47
  # App
48
  # ─────────────────────────────────────────────────────────────────────────────
49
 
50
+ app = FastAPI(title="PolyBloom ML Service", version="19.1.0")
51
  app.add_middleware(
52
  CORSMiddleware,
53
  allow_origins=["*"],
 
63
  "chronos": False,
64
  "xgboost": False,
65
  "lgbm": False,
66
+ "catboost": False,
67
  "patchtst": False,
68
  "nhits": False,
69
  "itransformer": False,
 
71
 
72
  _models: dict = {} # name → loaded object
73
 
74
+ # Weights — renormalized for 7 models (v19.1)
75
  WEIGHTS = {
76
+ "Chronos": 0.20,
77
+ "XGBoost": 0.18,
78
+ "LightGBM": 0.16,
79
+ "CatBoost": 0.14,
80
+ "PatchTST": 0.13,
81
+ "N-HiTS": 0.11,
82
+ "iTransformer": 0.08,
83
  }
84
 
85
  # ─────────────────────────────────────────────────────────────────────────────
 
323
  async def startup_event():
324
  _load_xgboost()
325
  _load_lgbm()
326
+ _load_catboost()
327
  _load_neuralforecast()
328
  _load_chronos()
329
  logger.info("Startup complete. flags=%s", _flags)
 
382
  logger.warning("LightGBM unavailable: %s", exc)
383
 
384
 
385
+ def _load_catboost():
386
+ try:
387
+ from catboost import CatBoostClassifier
388
+ import pickle
389
+
390
+ path = os.environ.get("CATBOOST_MODEL_PATH", "/app/catboost_model.cbm")
391
+ if os.path.exists(path):
392
+ clf = CatBoostClassifier()
393
+ clf.load_model(path)
394
+ logger.info("CatBoost loaded from %s", path)
395
+ else:
396
+ logger.info("CatBoost: bootstrap training…")
397
+ X, y = _synthetic_training_data(2000)
398
+ clf = CatBoostClassifier(
399
+ iterations=300,
400
+ depth=6,
401
+ learning_rate=0.07,
402
+ loss_function="Logloss",
403
+ eval_metric="AUC",
404
+ random_seed=42,
405
+ thread_count=2,
406
+ verbose=0,
407
+ )
408
+ clf.fit(X, y)
409
+ logger.info("CatBoost: bootstrap trained")
410
+ _models["catboost"] = clf
411
+ _flags["catboost"] = True
412
+ except Exception as exc:
413
+ logger.warning("CatBoost unavailable: %s", exc)
414
+
415
+
416
  def _load_neuralforecast():
417
  """Bootstrap-train PatchTST, N-HiTS, and iTransformer on synthetic candle data."""
418
  try:
 
533
  return _predict_chronos_bootstrap(closes, feats)
534
 
535
 
536
+ def _predict_catboost(closes: list, feats: dict) -> dict:
537
+ clf = _models.get("catboost")
538
+ if clf is None:
539
+ return _predict_chronos_bootstrap(closes, feats)
540
+ try:
541
+ fv = _feature_vector(feats, closes).reshape(1, -1)
542
+ prob = float(clf.predict_proba(fv)[0][1])
543
+ score = (prob - 0.5) * 2.0
544
+ direction = "UP" if prob > 0.55 else ("DOWN" if prob < 0.45 else "NEUTRAL")
545
+ return {
546
+ "name": "CatBoost",
547
+ "direction": direction,
548
+ "confidence": _clamp(round(45.0 + abs(score) * 50.0), 40, 95),
549
+ "slope_pct": round(score * 0.5, 6),
550
+ "reasoning": f"CatBoost: P(UP)={prob:.4f}",
551
+ }
552
+ except Exception as exc:
553
+ logger.warning("CatBoost inference error: %s", exc)
554
+ return _predict_chronos_bootstrap(closes, feats)
555
+
556
+
557
  # ─────────────────────────────────────────────────────────────────────────────
558
  # Model inference — NeuralForecast (PatchTST, N-HiTS, iTransformer)
559
  # ─────────────────────────────────────────────────────────────────────────────
 
680
  async def health_check():
681
  return {
682
  "ok": True,
683
+ "version": "19.1.0",
684
  "chronos": _flags["chronos"],
685
  "xgboost": _flags["xgboost"],
686
  "lgbm": _flags["lgbm"],
687
+ "catboost": _flags["catboost"],
688
  "patchtst": _flags["patchtst"],
689
  "nhits": _flags["nhits"],
690
  "itransformer": _flags["itransformer"],
691
+ "models": ["Chronos", "XGBoost", "LightGBM", "CatBoost", "PatchTST", "N-HiTS", "iTransformer"],
692
  "removed": ["MOIRAI", "MOMENT", "TimesNet", "Lag-Llama", "DAG", "TiDE", "TabPFN-TS", "N-BEATS", "AROpt"],
693
  "hf_space": True,
694
  }
 
723
  # Trained Classifiers
724
  predictions.append(_predict_xgboost(closes, feats))
725
  predictions.append(_predict_lightgbm(closes, feats))
726
+ predictions.append(_predict_catboost(closes, feats))
727
 
728
  # NeuralForecast (PatchTST, N-HiTS, iTransformer)
729
  predictions.extend(_predict_neuralforecast(closes, feats))