|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import asyncio |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from sqlmodel import col, select |
| 9 | + |
| 10 | +# Permite ejecutar el script desde la raiz del repo. |
| 11 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
| 12 | + |
| 13 | + |
| 14 | +def _parse_args() -> argparse.Namespace: |
| 15 | + parser = argparse.ArgumentParser( |
| 16 | + description="Crea/actualiza una cuenta bot de modelo y la deja visible en ladder.", |
| 17 | + ) |
| 18 | + parser.add_argument("--username", default="ub_bogonet_v0") |
| 19 | + parser.add_argument("--model-version-name", default="ub_bogonet_v0_iter039") |
| 20 | + parser.add_argument("--hf-repo-id", default="dieg0code/ataxx-zero") |
| 21 | + parser.add_argument("--hf-revision", default="main") |
| 22 | + parser.add_argument("--checkpoint-uri", default="hf://dieg0code/ataxx-zero/model_iter_039.pt") |
| 23 | + parser.add_argument("--onnx-uri", default="") |
| 24 | + parser.add_argument("--model-mode", choices=["fast", "strong"], default="fast") |
| 25 | + parser.add_argument("--activate-version", action="store_true") |
| 26 | + return parser.parse_args() |
| 27 | + |
| 28 | + |
| 29 | +async def _ensure_model_bot(args: argparse.Namespace) -> None: |
| 30 | + from api.db.enums import AgentType, BotKind |
| 31 | + from api.db.models import BotProfile, ModelVersion, User |
| 32 | + from api.db.session import get_engine, get_sessionmaker |
| 33 | + from api.modules.ranking.repository import RankingRepository |
| 34 | + from api.modules.ranking.service import RankingService |
| 35 | + |
| 36 | + sessionmaker = get_sessionmaker() |
| 37 | + async with sessionmaker() as session: |
| 38 | + version_stmt = select(ModelVersion).where( |
| 39 | + col(ModelVersion.name) == args.model_version_name |
| 40 | + ) |
| 41 | + version = (await session.execute(version_stmt)).scalars().first() |
| 42 | + if version is None: |
| 43 | + version = ModelVersion( |
| 44 | + name=args.model_version_name, |
| 45 | + hf_repo_id=args.hf_repo_id, |
| 46 | + hf_revision=args.hf_revision, |
| 47 | + checkpoint_uri=args.checkpoint_uri, |
| 48 | + onnx_uri=(args.onnx_uri or None), |
| 49 | + is_active=bool(args.activate_version), |
| 50 | + notes="Bot bootstrap script.", |
| 51 | + ) |
| 52 | + session.add(version) |
| 53 | + await session.commit() |
| 54 | + await session.refresh(version) |
| 55 | + else: |
| 56 | + version.hf_repo_id = args.hf_repo_id |
| 57 | + version.hf_revision = args.hf_revision |
| 58 | + version.checkpoint_uri = args.checkpoint_uri |
| 59 | + version.onnx_uri = args.onnx_uri or None |
| 60 | + if args.activate_version: |
| 61 | + version.is_active = True |
| 62 | + session.add(version) |
| 63 | + await session.commit() |
| 64 | + await session.refresh(version) |
| 65 | + |
| 66 | + if args.activate_version: |
| 67 | + await session.execute( |
| 68 | + # Keep one global active version when requested explicitly. |
| 69 | + ModelVersion.__table__.update() |
| 70 | + .where(col(ModelVersion.id) != version.id) |
| 71 | + .values(is_active=False) |
| 72 | + ) |
| 73 | + await session.execute( |
| 74 | + ModelVersion.__table__.update() |
| 75 | + .where(col(ModelVersion.id) == version.id) |
| 76 | + .values(is_active=True) |
| 77 | + ) |
| 78 | + await session.commit() |
| 79 | + |
| 80 | + user_stmt = select(User).where(col(User.username) == args.username) |
| 81 | + user = (await session.execute(user_stmt)).scalars().first() |
| 82 | + if user is None: |
| 83 | + user = User( |
| 84 | + username=args.username, |
| 85 | + email=f"{args.username}@bots.local", |
| 86 | + is_active=True, |
| 87 | + is_admin=False, |
| 88 | + is_bot=True, |
| 89 | + bot_kind=BotKind.MODEL, |
| 90 | + is_hidden_bot=False, |
| 91 | + model_version_id=version.id, |
| 92 | + ) |
| 93 | + session.add(user) |
| 94 | + await session.commit() |
| 95 | + await session.refresh(user) |
| 96 | + |
| 97 | + user.is_active = True |
| 98 | + user.is_bot = True |
| 99 | + user.is_hidden_bot = False |
| 100 | + user.bot_kind = BotKind.MODEL |
| 101 | + user.model_version_id = version.id |
| 102 | + session.add(user) |
| 103 | + await session.commit() |
| 104 | + await session.refresh(user) |
| 105 | + |
| 106 | + profile_stmt = select(BotProfile).where(col(BotProfile.user_id) == user.id) |
| 107 | + profile = (await session.execute(profile_stmt)).scalars().first() |
| 108 | + if profile is None: |
| 109 | + profile = BotProfile( |
| 110 | + user_id=user.id, |
| 111 | + agent_type=AgentType.MODEL, |
| 112 | + model_mode=args.model_mode, |
| 113 | + enabled=True, |
| 114 | + ) |
| 115 | + profile.agent_type = AgentType.MODEL |
| 116 | + profile.model_mode = args.model_mode |
| 117 | + profile.heuristic_level = None |
| 118 | + profile.enabled = True |
| 119 | + session.add(profile) |
| 120 | + await session.commit() |
| 121 | + await session.refresh(profile) |
| 122 | + |
| 123 | + ranking_service = RankingService(ranking_repository=RankingRepository(session=session)) |
| 124 | + season = await ranking_service.get_active_season() |
| 125 | + if season is None: |
| 126 | + raise RuntimeError("No active season found. Run scripts/bootstrap_active_season.py first.") |
| 127 | + rating = await ranking_service.get_or_create_rating(user.id, season.id) |
| 128 | + entries = await ranking_service.recompute_leaderboard(season_id=season.id, limit=500) |
| 129 | + rank = next((entry.rank for entry in entries if entry.user_id == user.id), None) |
| 130 | + |
| 131 | + print("Model bot ready:") |
| 132 | + print(f" user_id={user.id}") |
| 133 | + print(f" username={user.username}") |
| 134 | + print(f" model_version_id={version.id}") |
| 135 | + print(f" checkpoint_uri={version.checkpoint_uri}") |
| 136 | + print(f" model_mode={profile.model_mode}") |
| 137 | + print(f" season_id={season.id}") |
| 138 | + print(f" rating={rating.rating:.1f}") |
| 139 | + print(f" rank={rank}") |
| 140 | + |
| 141 | + await get_engine().dispose() |
| 142 | + |
| 143 | + |
| 144 | +def main() -> None: |
| 145 | + args = _parse_args() |
| 146 | + asyncio.run(_ensure_model_bot(args)) |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + main() |
0 commit comments