"""Jarvis v2 M0: measure the gaming PC once and pack the results into one zip. Started by run_m0.bat -> run_m0.ps1 (which installs uv and Python into the work folder). Each stage writes results/.json; a stage that already has a result is skipped on the next run, so the script can be started again after an interruption. """ from __future__ import annotations import argparse import datetime as dt import json import os import shutil import subprocess import sys import time import zipfile from pathlib import Path KIT = Path(__file__).resolve().parent sys.path.insert(0, str(KIT)) from m0lib import envs # noqa: E402 from m0lib.common import read_json, say, step, write_json # noqa: E402 # key, title, venv ("" = this Python), script, options, free disk needed (GB) STAGES = [ ("sysinfo", "PC 정보", "", "sysinfo.py", [], 0), ("winchecks", "창·디스코드·게임 확인 (직접 답해 주셔야 합니다)", "", "winchecks.py", [], 0), ("vdisplay", "가상 모니터에 봇용 창 두기 (직접 답해 주셔야 합니다)", "", "vdisplay.py", [], 0), ("tts_melo", "MeloTTS 속도", "melo", "tts_melo.py", [], 10), ("tts_cosy2", "CosyVoice2 속도", "cosy", "tts_cosy.py", ["models=cosyvoice2"], 15), ("tts_cosy3", "Fun-CosyVoice3 속도", "cosy", "tts_cosy.py", ["models=cosyvoice3"], 8), ("stt", "STT(faster-whisper turbo) 속도", "stt", "stt.py", [], 5), ("llm_fast", "LLM 빠름 모델 (Qwen3.6-35B-A3B, 약 23GB 받기)", "base", "llm.py", ["model=fast"], 35), ("llm_recommended", "LLM 권장 모델 (Qwen3.8-Flash-Next Q2_0, 약 66GB 받기)", "base", "llm.py", ["model=recommended"], 80), # Tool calling again with Jarvis's fix (prompt rules + planner + skipped-tool guard). # Reuses the models the two stages above downloaded. ("tools_fast", "도구 호출 다시 재기: 빠름 모델", "base", "llm.py", ["model=fast", "only=tools"], 0), ("tools_recommended", "도구 호출 다시 재기: 권장 모델", "base", "llm.py", ["model=recommended", "only=tools"], 0), # M7 PC control: agent-desktop (UI Automation) on calculator, a test page, Discord. ("desktop", "PC 제어 도구(agent-desktop) 확인", "", "desktop.py", [], 0), # Automatic tuning: the one model in each placement (normal, less VRAM for a game). ("gamemode", "게임 중 자동 조절: 모델 배치별 속도", "base", "llm.py", ["model=fast", "only=levels"], 0), # The same placements with the STT loaded next to the LLM (on the GPU and on the CPU). ("gamemode_stt", "게임 중 자동 조절: LLM과 음성 인식을 함께 켰을 때", "base", "llm.py", ["model=fast", "only=together"], 0), # The fixes for what gamemode_stt showed: room for the STT in the normal placement, and fewer # STT partial passes while the LLM answers (normal and the 2 GB game placement). ("gamemode_tune", "자동 조절: STT 자리와 VRAM 여유 1GB, 부분 전사 간격, GPU 95% 제한", "base", "llm.py", ["model=fast", "only=tune"], 0), # With the user's game running. Needs someone at the PC, so only when asked for (--only). ("gamemode_live", "게임을 켠 상태로 재기 (직접 게임을 켜 주셔야 합니다)", "base", "llm.py", ["model=fast", "only=live"], 0), ] # Stages left out of a full run: they need the user there after the unattended part. ON_DEMAND = {"gamemode_live"} # Stages that start the STT worker next to the LLM and need the stt venv's Python. NEEDS_STT = {"gamemode_stt", "gamemode_tune", "gamemode_live"} def free_gb(path: Path) -> float: return shutil.disk_usage(path).free / 1e9 def run_stage(key: str, venv: str, script: str, opts: list[str], work: Path, env: dict) -> None: out = work / "results" / f"{key}.json" log = work / "logs" / f"env_{venv or 'main'}.log" if venv: py = str(envs.ensure_env(venv, work, log)) else: py = sys.executable if venv == "cosy": src = envs.ensure_cosy_source(work, log) opts = opts + [f"src={src}"] if key in NEEDS_STT: stt_py = envs.ensure_env("stt", work, work / "logs" / "env_stt.log") opts = opts + [f"stt_python={stt_py}"] cmd = [py, str(KIT / "m0lib" / script), "--out", str(out), "--work", str(work)] for o in opts: cmd += ["--opt", o] # Inherit the console so interactive stages can ask questions; stage # scripts keep their own heavy output in logs/. proc = subprocess.run(cmd, env=env) if not out.exists(): write_json(out, {"stage": key, "status": "failed", "errors": [ {"where": "process", "error": f"stage exited with {proc.returncode} without a result"}]}) def summarise(work: Path) -> str: lines = ["# Jarvis v2 M0 결과 요약", "", f"만든 시각: {dt.datetime.now():%Y-%m-%d %H:%M}", ""] for key, title, *_ in STAGES: p = work / "results" / f"{key}.json" if not p.exists(): lines.append(f"- {title}: 실행 안 함") continue d = read_json(p) errs = len(d.get("errors") or []) lines.append(f"- {title}: {d.get('status')} ({d.get('seconds', '?')}초, 오류 {errs}개)") lines += ["", "자세한 수치는 같은 zip 안의 results/*.json에 있습니다. 이 zip 파일을 그대로 보내 주시면 됩니다."] return "\n".join(lines) + "\n" def pack(work: Path) -> Path: stamp = dt.datetime.now().strftime("%Y%m%d_%H%M") target = KIT / f"M0_results_{stamp}.zip" summary = summarise(work) (work / "results" / "summary.md").write_text(summary, encoding="utf-8") with zipfile.ZipFile(target, "w", zipfile.ZIP_DEFLATED) as z: for p in sorted((work / "results").rglob("*")): if p.is_file() and p.suffix in (".json", ".wav", ".md"): z.write(p, f"results/{p.relative_to(work / 'results').as_posix()}") for p in sorted((work / "logs").glob("*.log")): data = p.read_bytes() # Keep the end of long logs (download progress) so the zip stays small. z.writestr(f"logs/{p.name}", data[-1_000_000:]) return target def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--work", required=True) ap.add_argument("--only", default="", help="comma list of stage keys") ap.add_argument("--skip", default="", help="comma list of stage keys") ap.add_argument("--redo", action="store_true", help="measure again even if a result exists") ap.add_argument("--llm-url", default="", help="measure this OpenAI-compatible server instead of Strata") a = ap.parse_args() work = Path(a.work).resolve() for d in ("results", "logs", "cache/tmp"): (work / d).mkdir(parents=True, exist_ok=True) env = dict(os.environ, PYTHONUTF8="1", PYTHONIOENCODING="utf-8", HF_HOME=str(work / "models" / "hf"), TEMP=str(work / "cache" / "tmp"), TMP=str(work / "cache" / "tmp"), HF_HUB_DISABLE_TELEMETRY="1", DO_NOT_TRACK="1") only = {x for x in a.only.split(",") if x} skip = {x for x in a.skip.split(",") if x} say("Jarvis v2 M0 측정을 시작합니다.") say(f"작업 폴더: {work} (남은 공간 {free_gb(work):.0f} GB)") say("모든 파일은 이 작업 폴더 안에만 받습니다. 끝나면 결과 zip 파일 위치를 알려 드립니다.") say("앞쪽의 '직접 답해 주셔야 합니다' 단계가 끝나면 자리를 비우셔도 됩니다.") say("측정이 정확하도록 그동안 게임이나 무거운 프로그램은 꺼 두세요.\n") t0 = time.time() for key, title, venv, script, opts, need_gb in STAGES: if (only and key not in only) or key in skip or (not only and key in ON_DEMAND): continue out = work / "results" / f"{key}.json" if out.exists() and not a.redo: status = read_json(out).get("status") if status in ("ok", "partial", "skipped"): say(f"[건너뜀] {title}: 이미 측정했습니다 ({status}). 다시 재려면 --redo") continue step(title) if need_gb and free_gb(work) < need_gb: say(f" 디스크 공간이 부족합니다 (필요 약 {need_gb} GB, 남은 {free_gb(work):.0f} GB). 건너뜁니다.") write_json(out, {"stage": key, "status": "failed", "errors": [{"where": "disk", "error": f"free {free_gb(work):.0f} GB < {need_gb} GB"}]}) continue stage_opts = list(opts) if key.startswith(("llm_", "tools_", "gamemode")) and a.llm_url: stage_opts.append(f"url={a.llm_url}") try: run_stage(key, venv, script, stage_opts, work, env) except Exception as e: say(f" [오류] {title}: {e}") write_json(out, {"stage": key, "status": "failed", "errors": [{"where": "setup", "error": str(e)}]}) if key == "vdisplay": say("\n직접 확인하는 단계는 끝났습니다. 이제 자리를 비우셔도 됩니다.") target = pack(work) say("\n" + summarise(work)) say(f"걸린 시간: {(time.time() - t0) / 60:.0f}분") say(f"결과 파일: {target}") say("이 zip 파일을 디스코드로 보내 주세요.") if os.name == "nt": subprocess.run(["explorer", "/select,", str(target)]) return 0 if __name__ == "__main__": sys.exit(main())