File size: 4,151 Bytes
406e494
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"""Kaggle notebook body: director -> TTS in one run, with two interpreters.

The clash: Ming pins transformers 4.46.3, Qwen3.5 needs 5.x, and one
site-packages cannot hold both. So transformers 5 is installed into a private
directory and the director runs with PYTHONPATH pointing at it, while the radio
stage keeps the pinned 4.46.3.

Order matters: Ming's dependencies are installed first and never touched again.

Stages:
  1. Ming environment (repo, pinned deps, weights, bundle)
  2. transformers 5 into /kaggle/working/tf5
  3. fine-tune the director on the 160 scripts in the bundle (skipped if the
     model is already on disk)
  4. director writes one programme to director/script.json
  5. radio stage renders it with the 4-step student and the teacher
"""
import os
import subprocess
import sys
import time

ROOT = "/kaggle/working"
BUNDLE = f"{ROOT}/webfm-eval"
TF5 = f"{ROOT}/tf5"


def log(msg: str) -> None:
    print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True)


def sh(cmd: str, env: dict | None = None) -> int:
    log(f"$ {cmd}")
    rc = subprocess.run(cmd, shell=True, env=env).returncode
    if rc != 0:
        log(f"failed ({rc}): {cmd}")
    return rc


def stage_ming() -> None:
    if not os.path.exists(f"{ROOT}/Ming-3.0-tts"):
        sh(f"git clone https://github.com/inclusionAI/Ming-3.0-tts.git {ROOT}/Ming-3.0-tts")
        sh(f"git -C {ROOT}/Ming-3.0-tts checkout e999c39")
    sh('pip install -q "transformers==4.46.3" tokenizers "accelerate==1.3.0" '
       'huggingface_hub "soundfile==0.12.1" tqdm loguru "x_transformers==2.9.2" '
       '"diffusers==0.33.0" onnxruntime onnx onnx_ir onnxscript inflect einops packaging')
    if not os.path.exists(f"{ROOT}/Ming-3.0-tts-0.5b"):
        sh("python3 -c \"from huggingface_hub import snapshot_download as s; "
           f"s('inclusionAI/Ming-3.0-tts-0.5B', local_dir=r'{ROOT}/Ming-3.0-tts-0.5b', repo_type='model')\"")


def stage_tf5() -> None:
    if os.path.exists(f"{TF5}/transformers"):
        log("transformers 5 already installed")
        return
    # Private target: never touches the pinned environment Ming uses. Only
    # transformers and hub go in; everything else (torch, accelerate) resolves
    # from the main interpreter through PYTHONPATH fallback.
    sh(f"pip install -q --target {TF5} 'transformers>=5' 'huggingface_hub>=1.31,<2'")
    out = subprocess.run([sys.executable, "-c",
                          "import transformers; print(transformers.__version__)"],
                         capture_output=True, text=True,
                         env=dict(os.environ, PYTHONPATH=TF5))
    log(f"tf5 transformers: {out.stdout.strip() or out.stderr[-300:]}")


def main() -> None:
    log("== stage 1: ming ==")
    stage_ming()
    from huggingface_hub import snapshot_download
    snapshot_download("idefinity/webfm-eval", repo_type="dataset", local_dir=BUNDLE)

    log("== stage 2: transformers 5 in a private dir ==")
    stage_tf5()

    env5 = dict(os.environ, WORK_ROOT=ROOT, PYTHONPATH=TF5)
    model = f"{ROOT}/director/model"
    if not os.path.exists(f"{model}/config.json"):
        log("== stage 3: fine-tune the director ==")
        rc = subprocess.run([sys.executable, f"{BUNDLE}/kaggle/director_sft.py"],
                            env=env5).returncode
        log(f"train rc={rc}")
        if rc != 0:
            raise SystemExit("director training failed")

    log("== stage 4: write the programme ==")
    rc = subprocess.run([sys.executable, f"{BUNDLE}/kaggle/director_probe.py"],
                        env=dict(env5, MODE="script",
                                 TOPIC=os.environ.get("TOPIC", "雨夜的末班公交"),
                                 DIRECTOR_MODEL=model)).returncode
    log(f"director rc={rc}")
    script = f"{ROOT}/director/script.json"
    if not os.path.exists(script):
        raise SystemExit("no script.json")

    log("== stage 5: render ==")
    rc = subprocess.run([sys.executable, f"{BUNDLE}/kaggle/radio_demo.py"],
                        env=dict(os.environ, WORK_ROOT=ROOT, SCRIPT_FILE=script)).returncode
    log(f"radio rc={rc}")


if __name__ == "__main__":
    main()