CoolFace
Apppublic

ITNovaML/PCAgentinAI

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
train_all_agents.py146 linesDownload Raw Back to root
1"""2══════════════════════════════════════════════════════════════════════════════3PolicyBridge — Master Agent Training Runner4══════════════════════════════════════════════════════════════════════════════5Trains all 4 ML agents in sequence from the Bronze layer data.6Run this ONCE after loading bronze_historical_data.sql.7 8Usage:9    cd agents/10    pip install pymysql pandas scikit-learn xgboost sqlalchemy11    python train_all_agents.py12 13Output models saved to agents/models/:14    agent1_kyc_classifier.pkl     — KYC binary classifier15    agent2_property_risk.pkl      — Property peril regressors + risk classifier16    agent3_underwriting.pkl       — UW binary classifier17    agent4_pricing.pkl            — Premium XGBoost + GLM ensemble18 19Agent 5 (Issuance) is deterministic — no training required.20 21Environment:22    Local         → connects to localhost:3306 / bronze  (root / root@123)23    HuggingFace   → reads MYSQL_ADDON_* or MYSQL_* Secrets → Clever Cloud24══════════════════════════════════════════════════════════════════════════════25"""26 27import sys28import os29import time30import datetime31from pathlib import Path32 33sys.path.insert(0, str(Path(__file__).parent))34 35# ── Dynamic environment detection (mirrors agent DB config) ───────────────────36def _is_huggingface() -> bool:37    return (38        os.environ.get("SPACE_ID")            is not None39        or os.environ.get("HUGGINGFACE_SPACE") is not None40        or os.environ.get("MYSQL_ADDON_HOST")  is not None41        or os.environ.get("MYSQL_HOST")        is not None42    )43 44def _env(addon_key: str, generic_key: str, default: str = "") -> str:45    return os.environ.get(addon_key) or os.environ.get(generic_key) or default46 47def _db_display() -> str:48    """Returns a human-readable DB connection string for the startup banner."""49    if _is_huggingface():50        host = _env("MYSQL_ADDON_HOST", "MYSQL_HOST", "clever-cloud")51        port = _env("MYSQL_ADDON_PORT", "MYSQL_PORT", "3306")52        db   = _env("MYSQL_ADDON_DB",   "MYSQL_DATABASE", "btvbbpqhvnttzvptguj3")53        return f"Clever Cloud  {host}:{port}/{db}  (bronze_* prefix)"54    return "localhost:3306/bronze  (separate schemas)"55 56# ─────────────────────────────────────────────────────────────────────────────57 58def print_banner(title):59    print(f"\n{'═'*65}")60    print(f"  {title}")61    print(f"{'═'*65}")62 63def train_all():64    env_label = "HuggingFace → Clever Cloud" if _is_huggingface() else "Local → MySQL"65 66    print_banner("PolicyBridge — Agent Training Pipeline")67    print(f"  Started:     {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")68    print(f"  Environment: {env_label}")69    print(f"  DB Target:   {_db_display()}")70    print(f"  Records:     500 historical submissions (2024)")71 72    results = {}73    total_start = time.time()74 75    # ── Agent 1: KYC ─────────────────────────────────────────────────────────76    try:77        from agent1_ssn_kyc import train_kyc_model78        t0  = time.time()79        art = train_kyc_model()80        results["Agent1_KYC"] = {"status": "OK", "elapsed": round(time.time()-t0, 1)}81    except Exception as e:82        results["Agent1_KYC"] = {"status": f"FAILED: {e}", "elapsed": 0}83 84    # ── Agent 2: Property Risk ────────────────────────────────────────────────85    try:86        from agent2_property_risk import train_property_model87        t0  = time.time()88        art = train_property_model()89        results["Agent2_Property"] = {"status": "OK", "elapsed": round(time.time()-t0, 1)}90    except Exception as e:91        results["Agent2_Property"] = {"status": f"FAILED: {e}", "elapsed": 0}92 93    # ── Agent 3: Underwriting ────────────────────────────────────────────────94    try:95        from agent3_underwriting import train_uw_model96        t0  = time.time()97        art = train_uw_model()98        results["Agent3_UW"] = {"status": "OK", "elapsed": round(time.time()-t0, 1)}99    except Exception as e:100        results["Agent3_UW"] = {"status": f"FAILED: {e}", "elapsed": 0}101 102    # ── Agent 4: Pricing ─────────────────────────────────────────────────────103    try:104        from agent4_pricing import train_pricing_model105        t0  = time.time()106        art = train_pricing_model()107        results["Agent4_Pricing"] = {"status": "OK", "elapsed": round(time.time()-t0, 1)}108    except Exception as e:109        results["Agent4_Pricing"] = {"status": f"FAILED: {e}", "elapsed": 0}110 111    # ── Summary ───────────────────────────────────────────────────────────────112    total = round(time.time() - total_start, 1)113    print_banner("Training Summary")114 115    all_ok = True116    for agent, res in results.items():117        status = res["status"]118        icon   = "✓" if status == "OK" else "✗"119        print(f"  {icon}  {agent:<25}  {status:<10}  {res['elapsed']}s")120        if status != "OK":121            all_ok = False122 123    print(f"\n  Total elapsed: {total}s")124    print(f"\n  Models saved to: agents/models/")125    for pkl in sorted(Path("models").glob("*.pkl")):126        size = pkl.stat().st_size // 1024127        print(f"    {pkl.name:<40} {size} KB")128 129    if all_ok:130        print(f"\n  All 4 agents trained successfully.")131        print(f"  Run the pipeline:")132        print(f"    python agent5_issuance_orchestrator.py")133        print(f"    python agent5_issuance_orchestrator.py --submission SUB-2024-00001")134        print(f"    python agent5_issuance_orchestrator.py --batch --limit 100")135    else:136        print(f"\n  Some agents failed. Check errors above.")137        if _is_huggingface():138            print(f"  HuggingFace: verify MYSQL_ADDON_* Secrets are set in Space Settings.")139        else:140            print(f"  Local: ensure MySQL is running and bronze_historical_data.sql is loaded.")141 142    return results143 144if __name__ == "__main__":145    train_all()146