| |
| """Pipeline: build_data + train_ranker (v4) — fixed HF downloads""" |
| import os, sys, gc, warnings |
| warnings.filterwarnings("ignore") |
| import numpy as np |
| import pandas as pd |
| from pathlib import Path |
|
|
| hf_token = os.environ.get("HF_TOKEN") |
| from huggingface_hub import HfApi, hf_hub_download |
| api = HfApi() |
|
|
| DS = "cedwyh/jinjing-shared-data" |
|
|
| print("=" * 60) |
| print("Pipeline: build_data + train_ranker (v4)") |
| print(" HF downloads via hf_hub_download (fixes urllib 404)") |
| print("=" * 60) |
|
|
| def hub_path(filename): |
| """Get local path for a HF dataset file, downloading if needed""" |
| return hf_hub_download(repo_id=DS, filename=filename, repo_type="dataset") |
|
|
| |
| |
| |
| print("\n[1/2] Building ranking training data...") |
|
|
| build_data_local = hub_path("build_data.py") |
| |
| with open(build_data_local) as f: |
| content = f.read() |
| content = content.replace( |
| "from factor_priors import compute_all_priors, PRIOR_COLUMNS", |
| "try:\n from factor_priors import compute_all_priors, PRIOR_COLUMNS\nexcept ImportError:\n compute_all_priors = None\n PRIOR_COLUMNS = []" |
| ) |
| patched = "/tmp/build_data_patched.py" |
| with open(patched, "w") as f: |
| f.write(content) |
|
|
| sys.argv = ["build_data.py", "--output", "/tmp/ranking_train_v8.parquet", |
| "--dataset", DS, "--no-use-priors"] |
| exec(open(patched).read().replace('if __name__ == "__main__"', 'if True')) |
|
|
| |
| if not Path("/tmp/ranking_train_v8.parquet").exists(): |
| print("\n❌ Build data failed - no output") |
| 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") |
| print(f" Date range: {df['date'].min()} to {df['date'].max()}") |
| 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") |
|
|
| |
| |
| |
| print("\n[2/2] Training LGBMRanker...") |
|
|
| train_ranker_local = hub_path("scripts/train_ranker.py") |
|
|
| output_dir = "/tmp/v10_ranker" |
| Path(output_dir).mkdir(parents=True, exist_ok=True) |
|
|
| sys.argv = ["train_ranker.py", |
| "--data", "/tmp/ranking_train_v8.parquet", |
| "--output", output_dir, |
| ] |
|
|
| |
| |
| with open(train_ranker_local) as f: |
| tr_content = f.read() |
| tr_content = tr_content.replace('TARGET_COL = "label"', 'TARGET_COL = "label_rank"') |
| tr_patched = "/tmp/train_ranker_patched.py" |
| with open(tr_patched, "w") as f: |
| f.write(tr_content) |
|
|
| exec(open(tr_patched).read().replace('if __name__ == "__main__"', 'if True')) |
|
|
| |
| model_files = sorted(Path(output_dir).glob("*.txt")) |
| pred_file = Path(output_dir) / "ranker_predictions.parquet" |
|
|
| print(f"\nModels: {len(model_files)}") |
| for f in model_files: |
| 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}") |
|
|
| 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("=" * 60) |
| print("✅ Pipeline complete!") |
| print(" ranking_train_v8.parquet") |
| print(" models/v10_*.txt") |
| print("=" * 60) |
|
|