Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,330 +1,330 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import orjson
|
| 3 |
-
import concurrent.futures
|
| 4 |
-
import random
|
| 5 |
-
import torch
|
| 6 |
-
import threading
|
| 7 |
-
import time
|
| 8 |
-
import uuid
|
| 9 |
-
import glob
|
| 10 |
-
import gradio as gr
|
| 11 |
-
import pandas as pd
|
| 12 |
-
import matplotlib.pyplot as plt
|
| 13 |
-
from huggingface_hub import snapshot_download, hf_hub_download, HfApi
|
| 14 |
-
|
| 15 |
-
from riichienv import RiichiEnv, GameRule
|
| 16 |
-
|
| 17 |
-
# 分别导入两个不同架构的加载函数,防止命名冲突
|
| 18 |
-
from model3pLOCAL import load_model as load_model_local
|
| 19 |
-
from model3pNEW import load_model as load_model_new
|
| 20 |
-
|
| 21 |
-
# ==========================================
|
| 22 |
-
# 0. 核心对抗配置开关 (在这里切换模式)
|
| 23 |
-
# ==========================================
|
| 24 |
-
# True: 1个 NEW架构(TEST_MODEL) VS 2个 LOCAL架构(EXAMINER_MODEL)
|
| 25 |
-
# False: 1个 LOCAL架构(TEST_MODEL) VS 2个 NEW架构(EXAMINER_MODEL)
|
| 26 |
-
ONE_NEW_VS_TWO_LOCAL = True
|
| 27 |
-
|
| 28 |
-
# ==========================================
|
| 29 |
-
# 0. 分布式多开与云端持久化配置
|
| 30 |
-
# ==========================================
|
| 31 |
-
DATA_REPO_ID = "ffzeroHua/mj-eval-results" # 📊 战绩数据集仓库
|
| 32 |
-
MODEL_REPO_ID = "ffzeroHua/Riichi-Model-Repo" # 🧠 模型权重仓库
|
| 33 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 34 |
-
|
| 35 |
-
# 为当前节点生成唯一的 ID
|
| 36 |
-
WORKER_ID = os.getenv("WORKER_ID", str(uuid.uuid4())[:6])
|
| 37 |
-
|
| 38 |
-
# 根据开关状态自动调整保存的文件前缀
|
| 39 |
-
BASE_REPORT_PREFIX = '
|
| 40 |
-
if ONE_NEW_VS_TWO_LOCAL:
|
| 41 |
-
REPORT_FILE_PREFIX = BASE_REPORT_PREFIX
|
| 42 |
-
else:
|
| 43 |
-
REPORT_FILE_PREFIX = f"inverse_{BASE_REPORT_PREFIX}"
|
| 44 |
-
|
| 45 |
-
REPORT_FILE = f"{REPORT_FILE_PREFIX}_{WORKER_ID}.txt"
|
| 46 |
-
|
| 47 |
-
api = HfApi()
|
| 48 |
-
EVAL_RUNNING = True
|
| 49 |
-
|
| 50 |
-
# 🚀 设定要从云端拉取并进行对抗的两个模型
|
| 51 |
-
TEST_MODEL = "
|
| 52 |
-
EXAMINER_MODEL = "Elite4z9070.pth"
|
| 53 |
-
|
| 54 |
-
def sync_models_from_hub():
|
| 55 |
-
"""启动时从指定的模型仓库拉取对战双方的权重文件"""
|
| 56 |
-
if HF_TOKEN and "你的用户名" not in MODEL_REPO_ID:
|
| 57 |
-
print(f"☁️ 正在从模型仓库 [{MODEL_REPO_ID}] 拉取评估模型...")
|
| 58 |
-
try:
|
| 59 |
-
hf_hub_download(repo_id=MODEL_REPO_ID, filename=TEST_MODEL, repo_type="model", local_dir=".", token=HF_TOKEN)
|
| 60 |
-
print(f"✅ 成功拉取测试模型: {TEST_MODEL}")
|
| 61 |
-
|
| 62 |
-
hf_hub_download(repo_id=MODEL_REPO_ID, filename=EXAMINER_MODEL, repo_type="model", local_dir=".", token=HF_TOKEN)
|
| 63 |
-
print(f"✅ 成功拉取考官模型: {EXAMINER_MODEL}")
|
| 64 |
-
|
| 65 |
-
print("🎉 模型环境准备完毕!")
|
| 66 |
-
except Exception as e:
|
| 67 |
-
print(f"❌ 拉取模型失败,请检查文件名或仓库权限: {e}")
|
| 68 |
-
else:
|
| 69 |
-
print("⚠️ 未配置有效 HF_TOKEN 或未修改 MODEL_REPO_ID,将尝试使用本地已存在的模型文件。")
|
| 70 |
-
|
| 71 |
-
def sync_data_from_hub():
|
| 72 |
-
"""启动时从数据集下载所有节点的战绩分片文件"""
|
| 73 |
-
if HF_TOKEN and "你的用户名" not in DATA_REPO_ID:
|
| 74 |
-
try:
|
| 75 |
-
print(f"🔄 正在从 Hub 拉取全局历史战绩数据 (前缀匹配: {REPORT_FILE_PREFIX})...")
|
| 76 |
-
snapshot_download(
|
| 77 |
-
repo_id=DATA_REPO_ID,
|
| 78 |
-
repo_type="dataset",
|
| 79 |
-
local_dir=".",
|
| 80 |
-
allow_patterns=REPORT_FILE_PREFIX + "_*.txt",
|
| 81 |
-
token=HF_TOKEN
|
| 82 |
-
)
|
| 83 |
-
print("✅ 历史数据拉取完成。")
|
| 84 |
-
except Exception as e:
|
| 85 |
-
print(f"⚠️ 拉取历史战绩失败: {e}")
|
| 86 |
-
|
| 87 |
-
def sync_data_to_hub():
|
| 88 |
-
"""将当前节点的战绩文件备份到数据集"""
|
| 89 |
-
if HF_TOKEN and "你的用户名" not in DATA_REPO_ID:
|
| 90 |
-
try:
|
| 91 |
-
api.upload_file(
|
| 92 |
-
path_or_fileobj=REPORT_FILE,
|
| 93 |
-
path_in_repo=REPORT_FILE,
|
| 94 |
-
repo_id=DATA_REPO_ID,
|
| 95 |
-
repo_type="dataset",
|
| 96 |
-
token=HF_TOKEN
|
| 97 |
-
)
|
| 98 |
-
print(f"☁️ 节点 {WORKER_ID} 战绩已同步至 Hub: {time.strftime('%H:%M:%S')}")
|
| 99 |
-
except Exception as e:
|
| 100 |
-
print(f"❌ 同步失败: {e}")
|
| 101 |
-
|
| 102 |
-
# ==========================================
|
| 103 |
-
# 1. 高频及模型加载逻辑
|
| 104 |
-
# ==========================================
|
| 105 |
-
def patch_event_fast(event_str):
|
| 106 |
-
if '"kita"' in event_str:
|
| 107 |
-
event_str = event_str.replace('"kita"', '"nukidora"')
|
| 108 |
-
|
| 109 |
-
if '"start_kyoku"' in event_str or '"deltas"' in event_str:
|
| 110 |
-
event = orjson.loads(event_str)
|
| 111 |
-
if event.get('type') == 'start_kyoku':
|
| 112 |
-
scores = event.setdefault('scores', [])
|
| 113 |
-
while len(scores) < 4: scores.append(0)
|
| 114 |
-
tehais = event.setdefault('tehais', [])
|
| 115 |
-
while len(tehais) < 4: tehais.append(["?" for _ in range(13)])
|
| 116 |
-
if 'deltas' in event:
|
| 117 |
-
deltas = event['deltas']
|
| 118 |
-
while len(deltas) < 4: deltas.append(0)
|
| 119 |
-
return orjson.dumps(event).decode('utf-8')
|
| 120 |
-
return event_str
|
| 121 |
-
|
| 122 |
-
def patch_resp_fast(resp_str):
|
| 123 |
-
if not resp_str: return resp_str
|
| 124 |
-
return resp_str.replace('"nukidora"', '"kita"')
|
| 125 |
-
|
| 126 |
-
_MODEL_CACHE = {}
|
| 127 |
-
|
| 128 |
-
def get_cached_model(player_id: int, model_file: str, arch_type: str):
|
| 129 |
-
"""根据指定的架构类型 (new 或 local) 加载模型"""
|
| 130 |
-
key = (player_id, model_file, arch_type)
|
| 131 |
-
if key not in _MODEL_CACHE:
|
| 132 |
-
torch.set_num_threads(1)
|
| 133 |
-
if arch_type == 'new':
|
| 134 |
-
_MODEL_CACHE[key] = load_model_new(player_id, model_file)
|
| 135 |
-
else:
|
| 136 |
-
_MODEL_CACHE[key] = load_model_local(player_id, model_file)
|
| 137 |
-
return _MODEL_CACHE[key]
|
| 138 |
-
|
| 139 |
-
class MortalAgent:
|
| 140 |
-
def __init__(self, player_id: int, model_file: str, arch_type: str):
|
| 141 |
-
self.player_id = player_id
|
| 142 |
-
self.arch_type = arch_type
|
| 143 |
-
self.model = get_cached_model(player_id, model_file, arch_type)
|
| 144 |
-
|
| 145 |
-
def act(self, obs):
|
| 146 |
-
resp = None
|
| 147 |
-
for event in obs.new_events():
|
| 148 |
-
event_patched = patch_event_fast(event)
|
| 149 |
-
resp = patch_resp_fast(self.model.react(event_patched))
|
| 150 |
-
action = obs.select_action_from_mjai(resp)
|
| 151 |
-
assert action is not None, "Mortal must return a legal action"
|
| 152 |
-
return action
|
| 153 |
-
|
| 154 |
-
# ==========================================
|
| 155 |
-
# 2. 核心对局任务
|
| 156 |
-
# ==========================================
|
| 157 |
-
def play_one_game(game_index):
|
| 158 |
-
env = RiichiEnv(game_mode="3p-red-half", rule=GameRule.default_tenhou())
|
| 159 |
-
new_seat = random.randrange(3)
|
| 160 |
-
|
| 161 |
-
agents = {}
|
| 162 |
-
for i in range(3):
|
| 163 |
-
if i == new_seat:
|
| 164 |
-
# 🚀 挑战者位
|
| 165 |
-
model_file = TEST_MODEL
|
| 166 |
-
arch = 'new' if ONE_NEW_VS_TWO_LOCAL else 'local'
|
| 167 |
-
else:
|
| 168 |
-
# 🚀 考官位
|
| 169 |
-
model_file = EXAMINER_MODEL
|
| 170 |
-
arch = 'local' if ONE_NEW_VS_TWO_LOCAL else 'new'
|
| 171 |
-
|
| 172 |
-
agents[i] = MortalAgent(i, model_file, arch)
|
| 173 |
-
|
| 174 |
-
obs_dict = env.reset()
|
| 175 |
-
while not env.done():
|
| 176 |
-
actions = {pid: agents[pid].act(obs) for pid, obs in obs_dict.items()}
|
| 177 |
-
obs_dict = env.step(actions)
|
| 178 |
-
|
| 179 |
-
scores = env.scores()
|
| 180 |
-
ranks = env.ranks()
|
| 181 |
-
return ranks[new_seat], scores[new_seat]
|
| 182 |
-
|
| 183 |
-
# ==========================================
|
| 184 |
-
# 3. 后台独立评估线程
|
| 185 |
-
# ==========================================
|
| 186 |
-
def background_eval_loop():
|
| 187 |
-
sync_models_from_hub() # 🚀 启动时从 Riichi-Model-Repo 拉取对战模型
|
| 188 |
-
sync_data_from_hub() # 🚀 启动时从战绩仓库拉取历史战绩
|
| 189 |
-
|
| 190 |
-
NUM_WORKERS = 1
|
| 191 |
-
|
| 192 |
-
mode_str = "1只 NEW 挑战 2只 LOCAL" if ONE_NEW_VS_TWO_LOCAL else "1只 LOCAL 挑战 2只 NEW"
|
| 193 |
-
print(f"🚀 节点 [{WORKER_ID}] 后台对战线程已启动: 模式为 [{mode_str}]")
|
| 194 |
-
|
| 195 |
-
if not os.path.exists(REPORT_FILE):
|
| 196 |
-
open(REPORT_FILE, 'w').close()
|
| 197 |
-
|
| 198 |
-
games_since_last_sync = 0
|
| 199 |
-
|
| 200 |
-
with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_WORKERS) as executor:
|
| 201 |
-
futures = {executor.submit(play_one_game, i) for i in range(NUM_WORKERS * 2)}
|
| 202 |
-
games_completed = 0
|
| 203 |
-
|
| 204 |
-
while EVAL_RUNNING and futures:
|
| 205 |
-
done, futures = concurrent.futures.wait(
|
| 206 |
-
futures, return_when=concurrent.futures.FIRST_COMPLETED
|
| 207 |
-
)
|
| 208 |
-
|
| 209 |
-
with open(REPORT_FILE, "a") as f:
|
| 210 |
-
for future in done:
|
| 211 |
-
try:
|
| 212 |
-
rank, score = future.result()
|
| 213 |
-
f.write(f"{rank} {score}\n")
|
| 214 |
-
f.flush()
|
| 215 |
-
games_completed += 1
|
| 216 |
-
games_since_last_sync += 1
|
| 217 |
-
print(f"[节点 {WORKER_ID}] 完成 {games_completed} 局: 顺位 {rank}, 得点 {score}")
|
| 218 |
-
except Exception as e:
|
| 219 |
-
print(f"对局异常: {e}")
|
| 220 |
-
|
| 221 |
-
if EVAL_RUNNING:
|
| 222 |
-
futures.add(executor.submit(play_one_game, games_completed))
|
| 223 |
-
|
| 224 |
-
if games_since_last_sync >= 100:
|
| 225 |
-
sync_data_to_hub()
|
| 226 |
-
sync_data_from_hub()
|
| 227 |
-
games_since_last_sync = 0
|
| 228 |
-
|
| 229 |
-
# ==========================================
|
| 230 |
-
# 4. 前端 Gradio 实时展示面板 (全局汇总)
|
| 231 |
-
# ==========================================
|
| 232 |
-
def read_and_analyze():
|
| 233 |
-
all_files = glob.glob(f"{REPORT_FILE_PREFIX}_*.txt")
|
| 234 |
-
|
| 235 |
-
main_arch = "NEW架构" if ONE_NEW_VS_TWO_LOCAL else "LOCAL架构"
|
| 236 |
-
opp_arch = "LOCAL架构" if ONE_NEW_VS_TWO_LOCAL else "NEW架构"
|
| 237 |
-
|
| 238 |
-
if not all_files:
|
| 239 |
-
return f"⏳ 正在拉取模型并等待 [{main_arch}] `{TEST_MODEL}` VS [{opp_arch}] `{EXAMINER_MODEL}` 第一局完成...", None
|
| 240 |
-
|
| 241 |
-
ranks, scores = [], []
|
| 242 |
-
try:
|
| 243 |
-
for file in all_files:
|
| 244 |
-
with open(file, "r") as f:
|
| 245 |
-
lines = f.readlines()
|
| 246 |
-
for line in lines:
|
| 247 |
-
parts = line.strip().split()
|
| 248 |
-
if len(parts) == 2:
|
| 249 |
-
ranks.append(int(float(parts[0])))
|
| 250 |
-
scores.append(float(parts[1]))
|
| 251 |
-
total = len(ranks)
|
| 252 |
-
if total == 0:
|
| 253 |
-
return f"⏳ 模型已就绪,正在进行第一局对抗...", None
|
| 254 |
-
|
| 255 |
-
avg_rank = sum(ranks) / total
|
| 256 |
-
avg_score = sum(scores) / total
|
| 257 |
-
rank1_rate = ranks.count(1) / total * 100
|
| 258 |
-
rank2_rate = ranks.count(2) / total * 100
|
| 259 |
-
rank3_rate = ranks.count(3) / total * 100
|
| 260 |
-
|
| 261 |
-
last_update = time.strftime('%Y-%m-%d %H:%M:%S')
|
| 262 |
-
|
| 263 |
-
md_text = f"""
|
| 264 |
-
### 📊 对战简报
|
| 265 |
-
- ⚔️ **对抗阵容:** 1只 `{TEST_MODEL}` ({main_arch}) **VS** 2只 `{EXAMINER_MODEL}` ({opp_arch})
|
| 266 |
-
- 🧮 **总对局数:** {total} 局 (跨节点全局汇集)
|
| 267 |
-
- 🏆 **平均顺位:** {avg_rank:.3f}
|
| 268 |
-
- 💰 **平均得点:** {avg_score:.0f}
|
| 269 |
-
---
|
| 270 |
-
- 🥇 **一位率:** {rank1_rate:.1f}%
|
| 271 |
-
- 🥈 **二位率:** {rank2_rate:.1f}%
|
| 272 |
-
- 🥉 **三位率:** {rank3_rate:.1f}%
|
| 273 |
-
---
|
| 274 |
-
- 🌐 **当前节点 ID:** `{WORKER_ID}`
|
| 275 |
-
- 🕒 **刷新时间:** {last_update}
|
| 276 |
-
"""
|
| 277 |
-
|
| 278 |
-
fig = plt.figure(figsize=(10, 4))
|
| 279 |
-
|
| 280 |
-
ax1 = fig.add_subplot(121)
|
| 281 |
-
ax1.bar(['1st', '2nd', '3rd'], [rank1_rate, rank2_rate, rank3_rate], color=['#FFD700', '#C0C0C0', '#CD7F32'])
|
| 282 |
-
ax1.set_title(f'Rank Distribution for {TEST_MODEL}')
|
| 283 |
-
ax1.set_ylim(0, max(100, max([rank1_rate, rank2_rate, rank3_rate] + [0]) + 10))
|
| 284 |
-
for i, v in enumerate([rank1_rate, rank2_rate, rank3_rate]):
|
| 285 |
-
ax1.text(i, v + 2, f"{v:.1f}%", ha='center')
|
| 286 |
-
|
| 287 |
-
ax2 = fig.add_subplot(122)
|
| 288 |
-
df = pd.DataFrame({'score': scores})
|
| 289 |
-
df['ma'] = df['score'].rolling(window=min(10, max(1, len(df))), min_periods=1).mean()
|
| 290 |
-
ax2.plot(df['score'], alpha=0.3, color='gray', label='Raw Score')
|
| 291 |
-
ax2.plot(df['ma'], color='crimson', linewidth=2, label='Moving Avg (10)')
|
| 292 |
-
ax2.set_title('Score Trend')
|
| 293 |
-
ax2.legend()
|
| 294 |
-
|
| 295 |
-
plt.tight_layout()
|
| 296 |
-
return md_text, fig
|
| 297 |
-
|
| 298 |
-
except Exception as e:
|
| 299 |
-
return f"❌ 数据解析出错: {e}", None
|
| 300 |
-
|
| 301 |
-
# ==========================================
|
| 302 |
-
# 5. 启动 Gradio 应用
|
| 303 |
-
# ==========================================
|
| 304 |
-
with gr.Blocks() as demo:
|
| 305 |
-
gr.Markdown("# 🀄 Mahjong AI 基准评估舱")
|
| 306 |
-
|
| 307 |
-
header_main = "NEW架构" if ONE_NEW_VS_TWO_LOCAL else "LOCAL架构"
|
| 308 |
-
header_opp = "LOCAL架构" if ONE_NEW_VS_TWO_LOCAL else "NEW架构"
|
| 309 |
-
|
| 310 |
-
gr.Markdown(f"当前正在评估: 1名 **{TEST_MODEL} ({header_main})** 单挑 2名 **{EXAMINER_MODEL} ({header_opp})**。启动时会自动拉取权重。")
|
| 311 |
-
|
| 312 |
-
with gr.Row():
|
| 313 |
-
with gr.Column(scale=1):
|
| 314 |
-
stats_output = gr.Markdown("🚀 正在初始化基准环境并连接模型仓库...")
|
| 315 |
-
refresh_btn = gr.Button("🔄 手动刷新全局战绩")
|
| 316 |
-
with gr.Column(scale=2):
|
| 317 |
-
plot_output = gr.Plot()
|
| 318 |
-
|
| 319 |
-
demo.load(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 320 |
-
|
| 321 |
-
timer = gr.Timer(15)
|
| 322 |
-
timer.tick(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 323 |
-
|
| 324 |
-
refresh_btn.click(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 325 |
-
|
| 326 |
-
if __name__ == "__main__":
|
| 327 |
-
t = threading.Thread(target=background_eval_loop, daemon=True)
|
| 328 |
-
t.start()
|
| 329 |
-
|
| 330 |
-
demo.queue().launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import orjson
|
| 3 |
+
import concurrent.futures
|
| 4 |
+
import random
|
| 5 |
+
import torch
|
| 6 |
+
import threading
|
| 7 |
+
import time
|
| 8 |
+
import uuid
|
| 9 |
+
import glob
|
| 10 |
+
import gradio as gr
|
| 11 |
+
import pandas as pd
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
from huggingface_hub import snapshot_download, hf_hub_download, HfApi
|
| 14 |
+
|
| 15 |
+
from riichienv import RiichiEnv, GameRule
|
| 16 |
+
|
| 17 |
+
# 分别导入两个不同架构的加载函数,防止命名冲突
|
| 18 |
+
from model3pLOCAL import load_model as load_model_local
|
| 19 |
+
from model3pNEW import load_model as load_model_new
|
| 20 |
+
|
| 21 |
+
# ==========================================
|
| 22 |
+
# 0. 核心对抗配置开关 (在这里切换模式)
|
| 23 |
+
# ==========================================
|
| 24 |
+
# True: 1个 NEW架构(TEST_MODEL) VS 2个 LOCAL架构(EXAMINER_MODEL)
|
| 25 |
+
# False: 1个 LOCAL架构(TEST_MODEL) VS 2个 NEW架构(EXAMINER_MODEL)
|
| 26 |
+
ONE_NEW_VS_TWO_LOCAL = True
|
| 27 |
+
|
| 28 |
+
# ==========================================
|
| 29 |
+
# 0. 分布式多开与云端持久化配置
|
| 30 |
+
# ==========================================
|
| 31 |
+
DATA_REPO_ID = "ffzeroHua/mj-eval-results" # 📊 战绩数据集仓库
|
| 32 |
+
MODEL_REPO_ID = "ffzeroHua/Riichi-Model-Repo" # 🧠 模型权重仓库
|
| 33 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 34 |
+
|
| 35 |
+
# 为当前节点生成唯一的 ID
|
| 36 |
+
WORKER_ID = os.getenv("WORKER_ID", str(uuid.uuid4())[:6])
|
| 37 |
+
|
| 38 |
+
# 根据开关状态自动调整保存的文件前缀
|
| 39 |
+
BASE_REPORT_PREFIX = 'D57k_vs_9070_eval_report'
|
| 40 |
+
if ONE_NEW_VS_TWO_LOCAL:
|
| 41 |
+
REPORT_FILE_PREFIX = BASE_REPORT_PREFIX
|
| 42 |
+
else:
|
| 43 |
+
REPORT_FILE_PREFIX = f"inverse_{BASE_REPORT_PREFIX}"
|
| 44 |
+
|
| 45 |
+
REPORT_FILE = f"{REPORT_FILE_PREFIX}_{WORKER_ID}.txt"
|
| 46 |
+
|
| 47 |
+
api = HfApi()
|
| 48 |
+
EVAL_RUNNING = True
|
| 49 |
+
|
| 50 |
+
# 🚀 设定要从云端拉取并进行对抗的两个模型
|
| 51 |
+
TEST_MODEL = "StudentSanma_Distilled_Step57000.pth"
|
| 52 |
+
EXAMINER_MODEL = "Elite4z9070.pth"
|
| 53 |
+
|
| 54 |
+
def sync_models_from_hub():
|
| 55 |
+
"""启动时从指定的模型仓库拉取对战双方的权重文件"""
|
| 56 |
+
if HF_TOKEN and "你的用户名" not in MODEL_REPO_ID:
|
| 57 |
+
print(f"☁️ 正在从模型仓库 [{MODEL_REPO_ID}] 拉取评估模型...")
|
| 58 |
+
try:
|
| 59 |
+
hf_hub_download(repo_id=MODEL_REPO_ID, filename=TEST_MODEL, repo_type="model", local_dir=".", token=HF_TOKEN)
|
| 60 |
+
print(f"✅ 成功拉取测试模型: {TEST_MODEL}")
|
| 61 |
+
|
| 62 |
+
hf_hub_download(repo_id=MODEL_REPO_ID, filename=EXAMINER_MODEL, repo_type="model", local_dir=".", token=HF_TOKEN)
|
| 63 |
+
print(f"✅ 成功拉取考官模型: {EXAMINER_MODEL}")
|
| 64 |
+
|
| 65 |
+
print("🎉 模型环境准备完毕!")
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"❌ 拉取模型失败,请检查文件名或仓库权限: {e}")
|
| 68 |
+
else:
|
| 69 |
+
print("⚠️ 未配置有效 HF_TOKEN 或未修改 MODEL_REPO_ID,将尝试使用本地已存在的模型文件。")
|
| 70 |
+
|
| 71 |
+
def sync_data_from_hub():
|
| 72 |
+
"""启动时从数据集下载所有节点的战绩分片文件"""
|
| 73 |
+
if HF_TOKEN and "你的用户名" not in DATA_REPO_ID:
|
| 74 |
+
try:
|
| 75 |
+
print(f"🔄 正在从 Hub 拉取全局历史战绩数据 (前缀匹配: {REPORT_FILE_PREFIX})...")
|
| 76 |
+
snapshot_download(
|
| 77 |
+
repo_id=DATA_REPO_ID,
|
| 78 |
+
repo_type="dataset",
|
| 79 |
+
local_dir=".",
|
| 80 |
+
allow_patterns=REPORT_FILE_PREFIX + "_*.txt",
|
| 81 |
+
token=HF_TOKEN
|
| 82 |
+
)
|
| 83 |
+
print("✅ 历史数据拉取完成。")
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"⚠️ 拉取历史战绩失败: {e}")
|
| 86 |
+
|
| 87 |
+
def sync_data_to_hub():
|
| 88 |
+
"""将当前节点的战绩文件备份到数据集"""
|
| 89 |
+
if HF_TOKEN and "你的用户名" not in DATA_REPO_ID:
|
| 90 |
+
try:
|
| 91 |
+
api.upload_file(
|
| 92 |
+
path_or_fileobj=REPORT_FILE,
|
| 93 |
+
path_in_repo=REPORT_FILE,
|
| 94 |
+
repo_id=DATA_REPO_ID,
|
| 95 |
+
repo_type="dataset",
|
| 96 |
+
token=HF_TOKEN
|
| 97 |
+
)
|
| 98 |
+
print(f"☁️ 节点 {WORKER_ID} 战绩已同步至 Hub: {time.strftime('%H:%M:%S')}")
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"❌ 同步失败: {e}")
|
| 101 |
+
|
| 102 |
+
# ==========================================
|
| 103 |
+
# 1. 高频及模型加载逻辑
|
| 104 |
+
# ==========================================
|
| 105 |
+
def patch_event_fast(event_str):
|
| 106 |
+
if '"kita"' in event_str:
|
| 107 |
+
event_str = event_str.replace('"kita"', '"nukidora"')
|
| 108 |
+
|
| 109 |
+
if '"start_kyoku"' in event_str or '"deltas"' in event_str:
|
| 110 |
+
event = orjson.loads(event_str)
|
| 111 |
+
if event.get('type') == 'start_kyoku':
|
| 112 |
+
scores = event.setdefault('scores', [])
|
| 113 |
+
while len(scores) < 4: scores.append(0)
|
| 114 |
+
tehais = event.setdefault('tehais', [])
|
| 115 |
+
while len(tehais) < 4: tehais.append(["?" for _ in range(13)])
|
| 116 |
+
if 'deltas' in event:
|
| 117 |
+
deltas = event['deltas']
|
| 118 |
+
while len(deltas) < 4: deltas.append(0)
|
| 119 |
+
return orjson.dumps(event).decode('utf-8')
|
| 120 |
+
return event_str
|
| 121 |
+
|
| 122 |
+
def patch_resp_fast(resp_str):
|
| 123 |
+
if not resp_str: return resp_str
|
| 124 |
+
return resp_str.replace('"nukidora"', '"kita"')
|
| 125 |
+
|
| 126 |
+
_MODEL_CACHE = {}
|
| 127 |
+
|
| 128 |
+
def get_cached_model(player_id: int, model_file: str, arch_type: str):
|
| 129 |
+
"""根据指定的架构类型 (new 或 local) 加载模型"""
|
| 130 |
+
key = (player_id, model_file, arch_type)
|
| 131 |
+
if key not in _MODEL_CACHE:
|
| 132 |
+
torch.set_num_threads(1)
|
| 133 |
+
if arch_type == 'new':
|
| 134 |
+
_MODEL_CACHE[key] = load_model_new(player_id, model_file)
|
| 135 |
+
else:
|
| 136 |
+
_MODEL_CACHE[key] = load_model_local(player_id, model_file)
|
| 137 |
+
return _MODEL_CACHE[key]
|
| 138 |
+
|
| 139 |
+
class MortalAgent:
|
| 140 |
+
def __init__(self, player_id: int, model_file: str, arch_type: str):
|
| 141 |
+
self.player_id = player_id
|
| 142 |
+
self.arch_type = arch_type
|
| 143 |
+
self.model = get_cached_model(player_id, model_file, arch_type)
|
| 144 |
+
|
| 145 |
+
def act(self, obs):
|
| 146 |
+
resp = None
|
| 147 |
+
for event in obs.new_events():
|
| 148 |
+
event_patched = patch_event_fast(event)
|
| 149 |
+
resp = patch_resp_fast(self.model.react(event_patched))
|
| 150 |
+
action = obs.select_action_from_mjai(resp)
|
| 151 |
+
assert action is not None, "Mortal must return a legal action"
|
| 152 |
+
return action
|
| 153 |
+
|
| 154 |
+
# ==========================================
|
| 155 |
+
# 2. 核心对局任务
|
| 156 |
+
# ==========================================
|
| 157 |
+
def play_one_game(game_index):
|
| 158 |
+
env = RiichiEnv(game_mode="3p-red-half", rule=GameRule.default_tenhou())
|
| 159 |
+
new_seat = random.randrange(3)
|
| 160 |
+
|
| 161 |
+
agents = {}
|
| 162 |
+
for i in range(3):
|
| 163 |
+
if i == new_seat:
|
| 164 |
+
# 🚀 挑战者位
|
| 165 |
+
model_file = TEST_MODEL
|
| 166 |
+
arch = 'new' if ONE_NEW_VS_TWO_LOCAL else 'local'
|
| 167 |
+
else:
|
| 168 |
+
# 🚀 考官位
|
| 169 |
+
model_file = EXAMINER_MODEL
|
| 170 |
+
arch = 'local' if ONE_NEW_VS_TWO_LOCAL else 'new'
|
| 171 |
+
|
| 172 |
+
agents[i] = MortalAgent(i, model_file, arch)
|
| 173 |
+
|
| 174 |
+
obs_dict = env.reset()
|
| 175 |
+
while not env.done():
|
| 176 |
+
actions = {pid: agents[pid].act(obs) for pid, obs in obs_dict.items()}
|
| 177 |
+
obs_dict = env.step(actions)
|
| 178 |
+
|
| 179 |
+
scores = env.scores()
|
| 180 |
+
ranks = env.ranks()
|
| 181 |
+
return ranks[new_seat], scores[new_seat]
|
| 182 |
+
|
| 183 |
+
# ==========================================
|
| 184 |
+
# 3. 后台独立评估线程
|
| 185 |
+
# ==========================================
|
| 186 |
+
def background_eval_loop():
|
| 187 |
+
sync_models_from_hub() # 🚀 启动时从 Riichi-Model-Repo 拉取对战模型
|
| 188 |
+
sync_data_from_hub() # 🚀 启动时从战绩仓库拉取历史战绩
|
| 189 |
+
|
| 190 |
+
NUM_WORKERS = 1
|
| 191 |
+
|
| 192 |
+
mode_str = "1只 NEW 挑战 2只 LOCAL" if ONE_NEW_VS_TWO_LOCAL else "1只 LOCAL 挑战 2只 NEW"
|
| 193 |
+
print(f"🚀 节点 [{WORKER_ID}] 后台对战线程已启动: 模式为 [{mode_str}]")
|
| 194 |
+
|
| 195 |
+
if not os.path.exists(REPORT_FILE):
|
| 196 |
+
open(REPORT_FILE, 'w').close()
|
| 197 |
+
|
| 198 |
+
games_since_last_sync = 0
|
| 199 |
+
|
| 200 |
+
with concurrent.futures.ProcessPoolExecutor(max_workers=NUM_WORKERS) as executor:
|
| 201 |
+
futures = {executor.submit(play_one_game, i) for i in range(NUM_WORKERS * 2)}
|
| 202 |
+
games_completed = 0
|
| 203 |
+
|
| 204 |
+
while EVAL_RUNNING and futures:
|
| 205 |
+
done, futures = concurrent.futures.wait(
|
| 206 |
+
futures, return_when=concurrent.futures.FIRST_COMPLETED
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
with open(REPORT_FILE, "a") as f:
|
| 210 |
+
for future in done:
|
| 211 |
+
try:
|
| 212 |
+
rank, score = future.result()
|
| 213 |
+
f.write(f"{rank} {score}\n")
|
| 214 |
+
f.flush()
|
| 215 |
+
games_completed += 1
|
| 216 |
+
games_since_last_sync += 1
|
| 217 |
+
print(f"[节点 {WORKER_ID}] 完成 {games_completed} 局: 顺位 {rank}, 得点 {score}")
|
| 218 |
+
except Exception as e:
|
| 219 |
+
print(f"对局异常: {e}")
|
| 220 |
+
|
| 221 |
+
if EVAL_RUNNING:
|
| 222 |
+
futures.add(executor.submit(play_one_game, games_completed))
|
| 223 |
+
|
| 224 |
+
if games_since_last_sync >= 100:
|
| 225 |
+
sync_data_to_hub()
|
| 226 |
+
sync_data_from_hub()
|
| 227 |
+
games_since_last_sync = 0
|
| 228 |
+
|
| 229 |
+
# ==========================================
|
| 230 |
+
# 4. 前端 Gradio 实时展示面板 (全局汇总)
|
| 231 |
+
# ==========================================
|
| 232 |
+
def read_and_analyze():
|
| 233 |
+
all_files = glob.glob(f"{REPORT_FILE_PREFIX}_*.txt")
|
| 234 |
+
|
| 235 |
+
main_arch = "NEW架构" if ONE_NEW_VS_TWO_LOCAL else "LOCAL架构"
|
| 236 |
+
opp_arch = "LOCAL架构" if ONE_NEW_VS_TWO_LOCAL else "NEW架构"
|
| 237 |
+
|
| 238 |
+
if not all_files:
|
| 239 |
+
return f"⏳ 正在拉取模型并等待 [{main_arch}] `{TEST_MODEL}` VS [{opp_arch}] `{EXAMINER_MODEL}` 第一局完成...", None
|
| 240 |
+
|
| 241 |
+
ranks, scores = [], []
|
| 242 |
+
try:
|
| 243 |
+
for file in all_files:
|
| 244 |
+
with open(file, "r") as f:
|
| 245 |
+
lines = f.readlines()
|
| 246 |
+
for line in lines:
|
| 247 |
+
parts = line.strip().split()
|
| 248 |
+
if len(parts) == 2:
|
| 249 |
+
ranks.append(int(float(parts[0])))
|
| 250 |
+
scores.append(float(parts[1]))
|
| 251 |
+
total = len(ranks)
|
| 252 |
+
if total == 0:
|
| 253 |
+
return f"⏳ 模型已就绪,正在进行第一局对抗...", None
|
| 254 |
+
|
| 255 |
+
avg_rank = sum(ranks) / total
|
| 256 |
+
avg_score = sum(scores) / total
|
| 257 |
+
rank1_rate = ranks.count(1) / total * 100
|
| 258 |
+
rank2_rate = ranks.count(2) / total * 100
|
| 259 |
+
rank3_rate = ranks.count(3) / total * 100
|
| 260 |
+
|
| 261 |
+
last_update = time.strftime('%Y-%m-%d %H:%M:%S')
|
| 262 |
+
|
| 263 |
+
md_text = f"""
|
| 264 |
+
### 📊 对战简报
|
| 265 |
+
- ⚔️ **对抗阵容:** 1只 `{TEST_MODEL}` ({main_arch}) **VS** 2只 `{EXAMINER_MODEL}` ({opp_arch})
|
| 266 |
+
- 🧮 **总对局数:** {total} 局 (跨节点全局汇集)
|
| 267 |
+
- 🏆 **平均顺位:** {avg_rank:.3f}
|
| 268 |
+
- 💰 **平均得点:** {avg_score:.0f}
|
| 269 |
+
---
|
| 270 |
+
- 🥇 **一位率:** {rank1_rate:.1f}%
|
| 271 |
+
- 🥈 **二位率:** {rank2_rate:.1f}%
|
| 272 |
+
- 🥉 **三位率:** {rank3_rate:.1f}%
|
| 273 |
+
---
|
| 274 |
+
- 🌐 **当前节点 ID:** `{WORKER_ID}`
|
| 275 |
+
- 🕒 **刷新时间:** {last_update}
|
| 276 |
+
"""
|
| 277 |
+
|
| 278 |
+
fig = plt.figure(figsize=(10, 4))
|
| 279 |
+
|
| 280 |
+
ax1 = fig.add_subplot(121)
|
| 281 |
+
ax1.bar(['1st', '2nd', '3rd'], [rank1_rate, rank2_rate, rank3_rate], color=['#FFD700', '#C0C0C0', '#CD7F32'])
|
| 282 |
+
ax1.set_title(f'Rank Distribution for {TEST_MODEL}')
|
| 283 |
+
ax1.set_ylim(0, max(100, max([rank1_rate, rank2_rate, rank3_rate] + [0]) + 10))
|
| 284 |
+
for i, v in enumerate([rank1_rate, rank2_rate, rank3_rate]):
|
| 285 |
+
ax1.text(i, v + 2, f"{v:.1f}%", ha='center')
|
| 286 |
+
|
| 287 |
+
ax2 = fig.add_subplot(122)
|
| 288 |
+
df = pd.DataFrame({'score': scores})
|
| 289 |
+
df['ma'] = df['score'].rolling(window=min(10, max(1, len(df))), min_periods=1).mean()
|
| 290 |
+
ax2.plot(df['score'], alpha=0.3, color='gray', label='Raw Score')
|
| 291 |
+
ax2.plot(df['ma'], color='crimson', linewidth=2, label='Moving Avg (10)')
|
| 292 |
+
ax2.set_title('Score Trend')
|
| 293 |
+
ax2.legend()
|
| 294 |
+
|
| 295 |
+
plt.tight_layout()
|
| 296 |
+
return md_text, fig
|
| 297 |
+
|
| 298 |
+
except Exception as e:
|
| 299 |
+
return f"❌ 数据解析出错: {e}", None
|
| 300 |
+
|
| 301 |
+
# ==========================================
|
| 302 |
+
# 5. 启动 Gradio 应用
|
| 303 |
+
# ==========================================
|
| 304 |
+
with gr.Blocks() as demo:
|
| 305 |
+
gr.Markdown("# 🀄 Mahjong AI 基准评估舱")
|
| 306 |
+
|
| 307 |
+
header_main = "NEW架构" if ONE_NEW_VS_TWO_LOCAL else "LOCAL架构"
|
| 308 |
+
header_opp = "LOCAL架构" if ONE_NEW_VS_TWO_LOCAL else "NEW架构"
|
| 309 |
+
|
| 310 |
+
gr.Markdown(f"当前正在评估: 1名 **{TEST_MODEL} ({header_main})** 单挑 2名 **{EXAMINER_MODEL} ({header_opp})**。启动时会自动拉取权重。")
|
| 311 |
+
|
| 312 |
+
with gr.Row():
|
| 313 |
+
with gr.Column(scale=1):
|
| 314 |
+
stats_output = gr.Markdown("🚀 正在初始化基准环境并连接模型仓库...")
|
| 315 |
+
refresh_btn = gr.Button("🔄 手动刷新全局战绩")
|
| 316 |
+
with gr.Column(scale=2):
|
| 317 |
+
plot_output = gr.Plot()
|
| 318 |
+
|
| 319 |
+
demo.load(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 320 |
+
|
| 321 |
+
timer = gr.Timer(15)
|
| 322 |
+
timer.tick(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 323 |
+
|
| 324 |
+
refresh_btn.click(fn=read_and_analyze, inputs=None, outputs=[stats_output, plot_output])
|
| 325 |
+
|
| 326 |
+
if __name__ == "__main__":
|
| 327 |
+
t = threading.Thread(target=background_eval_loop, daemon=True)
|
| 328 |
+
t.start()
|
| 329 |
+
|
| 330 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, theme=gr.themes.Soft())
|