Spaces:
Running
Running
Download services/model/refresh.py from 2026worldcup/predictor: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/services/model/refresh.py
- Command line
-
hf download hf://spaces/2026worldcup/predictor/services/model/refresh.py
-
curl -L -o refresh.py https://huggingface.co/spaces/2026worldcup/predictor/resolve/main/services/model/refresh.py
2.45 kB
| """每日刷新:下载最新国际比赛结果 → 重新入库(修正分组) → 重训模型 → 重跑模拟/预测。 | |
| 世界杯期间每天跑一次,让积分榜/出线/淘汰赛/预测吃到最新真实战绩。""" | |
| from __future__ import annotations | |
| import io | |
| import sys | |
| import zipfile | |
| import urllib.request | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[2] | |
| RAW = ROOT / "data" / "raw" / "international_results-master" | |
| URL = "https://codeload.github.com/martj42/international_results/zip/refs/heads/master" | |
| def download_latest() -> int: | |
| """拉取最新数据集,覆盖本地 csv。返回新增已赛场次数(估算)。""" | |
| import pandas as pd | |
| before = 0 | |
| f = RAW / "results.csv" | |
| if f.exists(): | |
| before = int(pd.read_csv(f).dropna(subset=["home_score"]).shape[0]) | |
| print("下载最新数据集 ...", flush=True) | |
| data = urllib.request.urlopen(URL, timeout=120).read() | |
| zf = zipfile.ZipFile(io.BytesIO(data)) | |
| RAW.mkdir(parents=True, exist_ok=True) | |
| for name in ("results.csv", "shootouts.csv", "goalscorers.csv", "former_names.csv"): | |
| member = f"international_results-master/{name}" | |
| try: | |
| (RAW / name).write_bytes(zf.read(member)) | |
| except KeyError: | |
| pass | |
| after = int(pd.read_csv(f).dropna(subset=["home_score"]).shape[0]) | |
| print(f"已赛场次 {before} -> {after} (新增 {after - before})", flush=True) | |
| return after - before | |
| if __name__ == "__main__": | |
| import config # noqa: F401 (sets UTF-8) | |
| n_new = download_latest() | |
| force = "--force" in sys.argv | |
| if n_new <= 0 and not force: | |
| print("无新比赛,跳过重训。(--force 可强制重训)") | |
| sys.exit(0) | |
| print("重新入库 ...", flush=True) | |
| import ingest | |
| ingest.load() | |
| print("重训模型 ...", flush=True) | |
| from backtest import tune | |
| tune("2024-01-01") # 网格搜 xi + 用最优 xi 重训保存 | |
| print("重跑模拟/预测 ...", flush=True) | |
| import pipeline | |
| out = pipeline.run(20000) | |
| print(f"完成。model_run_id={out['run_id']} RPS={out['rps']}") | |
| # 热加载后端(若在运行),无需重启 | |
| try: | |
| urllib.request.urlopen("http://127.0.0.1:8000/api/reload", | |
| data=b"", timeout=30) | |
| print(">>> 后端已热加载新模型") | |
| except Exception: | |
| print(">>> 后端未运行或热加载失败(下次启动会自动读到新数据)") | |