#!/usr/bin/env python3 """Steps 2-4 only: build_data + train_ranker. Features already generated.""" import os, sys, gc, warnings, subprocess, time from pathlib import Path warnings.filterwarnings("ignore") import numpy as np import pandas as pd DS = "cedwyh/jinjing-shared-data" hf_token = os.environ.get("HF_TOKEN") from huggingface_hub import HfApi, hf_hub_download api = HfApi() def _download_and_patch(src, dst, patches): p = hf_hub_download(repo_id=DS, filename=src, repo_type="dataset") with open(p) as f: c = f.read() for old, new in patches: c = c.replace(old, new) with open(dst, "w") as f: f.write(c) return dst print("=" * 60) print("Steps 2-4: build_data + train_ranker") print(f"Features: chan_engine_features_v5.5.1.parquet (new engine, beichi fix)") print("=" * 60) # Step 2: build_data print("\n[2/4] Building ranking training data...") bd_patched = _download_and_patch("build_data.py", "/tmp/bd_patched.py", [ ('"chan_engine_features.parquet"', '"chan_engine_features_v5.5.1.parquet"'), ]) bd_result = subprocess.run( [sys.executable, bd_patched, "--output", "/tmp/ranking_train_v8.parquet", "--dataset", DS, "--no-use-priors"], capture_output=True, text=True, timeout=7200 ) print(bd_result.stdout[-800:]) if bd_result.returncode != 0: err = (bd_result.stderr or "")[-500:] print(f"❌ Build data failed (code {bd_result.returncode}): {err}") sys.exit(1) df = pd.read_parquet("/tmp/ranking_train_v8.parquet") print(f"\n✅ Build data: {len(df):,} rows x {len(df.columns)} cols, dates {df['date'].min()}..{df['date'].max()}") n_dates = df["date"].nunique() if n_dates < 200: print(f" ❌ RED LINE: only {n_dates} unique dates — merge silently dropped rows!") sys.exit(1) del df; gc.collect() api.upload_file(path_or_fileobj="/tmp/ranking_train_v8.parquet", path_in_repo="ranking_train_v8.parquet", repo_id=DS, repo_type="dataset") print(" ✅ Uploaded ranking_train_v8.parquet") # Step 3: train_ranker print("\n[3/4] Training LGBMRanker...") tr_patched = _download_and_patch("scripts/train_ranker.py", "/tmp/tr_patched.py", [('TARGET_COL = "label"', 'TARGET_COL = "label_rank"')]) output_dir = "/tmp/v10_ranker" Path(output_dir).mkdir(exist_ok=True) tr_result = subprocess.run( [sys.executable, tr_patched, "--data", "/tmp/ranking_train_v8.parquet", "--output", output_dir], capture_output=True, text=True, timeout=14400 ) out = tr_result.stdout # Show last 40 lines of training output lines = out.split('\n') print('\n'.join(lines[-40:])) if tr_result.returncode != 0: err = (tr_result.stderr or "")[-500:] print(f"❌ Ranker training failed: {err}") sys.exit(1) # Upload models for f in sorted(Path(output_dir).glob("*.txt")): api.upload_file(path_or_fileobj=str(f), path_in_repo=f"models/v10_{f.name}", repo_id=DS, repo_type="dataset") print(f" ✅ models/v10_{f.name}") pred_file = Path(output_dir) / "ranker_predictions.parquet" if pred_file.exists(): api.upload_file(path_or_fileobj=str(pred_file), path_in_repo="models/ranker_v10_predictions.parquet", repo_id=DS, repo_type="dataset") print(" ✅ Uploaded predictions") print("\n" + "=" * 60) print("✅ COMPLETE") print(" Data: ranking_train_v8.parquet") print(" Models: models/v10_lgbm_w*.txt") print("=" * 60)