sergiopaniego HF Staff commited on
Commit
b6579af
·
verified ·
1 Parent(s): 1a66ef7

Add the recording rig: serve one model behind a public URL and hold it

Browse files
Files changed (1) hide show
  1. serve_for_recording.py +129 -0
serve_for_recording.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020-2026 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # /// script
16
+ # dependencies = ["vllm"]
17
+ # ///
18
+
19
+ """
20
+ Serve one model behind a public URL, and hold it there, so a local opencode can be recorded against it.
21
+
22
+ This is the recording rig, not a training job. Nothing is captured and nothing is scored: opencode runs
23
+ on your laptop (that is what ends up on screen) and calls this endpoint. Run it twice, once per model,
24
+ and you get two URLs you can switch between without waiting for a server to boot mid-session.
25
+
26
+ ```sh
27
+ MODEL=Qwen/Qwen3-8B \
28
+ hf jobs uv run --flavor a100-large --secrets HF_TOKEN --timeout 7200s serve_for_recording.py
29
+ ```
30
+
31
+ The URL is printed every 60 seconds, so you can find it in the job logs at any point without scrolling
32
+ to the top. The job holds until `--timeout`, so set that to comfortably longer than your recording
33
+ session and cancel it when you are done.
34
+
35
+ `--return-tokens-as-token-ids` is not set here on purpose: nothing is capturing token ids, and leaving
36
+ it off keeps the responses closer to what a normal OpenAI-compatible client expects.
37
+ """
38
+
39
+ import os
40
+ import re
41
+ import signal
42
+ import subprocess
43
+ import time
44
+ import urllib.request
45
+
46
+
47
+ CLOUDFLARED = "https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64"
48
+ MODEL = os.environ.get("MODEL", "Qwen/Qwen3-8B")
49
+ HOLD_S = int(os.environ.get("HOLD_S", "7000"))
50
+
51
+
52
+ def _log(m):
53
+ print(">>> " + m, flush=True)
54
+
55
+
56
+ def _kill_pg(p):
57
+ if p is None:
58
+ return
59
+ try:
60
+ os.killpg(os.getpgid(p.pid), signal.SIGKILL)
61
+ except Exception:
62
+ try:
63
+ p.kill()
64
+ except Exception:
65
+ pass
66
+
67
+
68
+ def main():
69
+ urllib.request.urlretrieve(CLOUDFLARED, "cloudflared")
70
+ os.chmod("cloudflared", 0o755)
71
+
72
+ _log(f"serving {MODEL}...")
73
+ vllm = subprocess.Popen(
74
+ ["vllm", "serve", MODEL, "--max-model-len", "40960",
75
+ "--enable-auto-tool-choice", "--tool-call-parser", "hermes"],
76
+ env={**os.environ, "VLLM_USE_FLASHINFER_SAMPLER": "0"},
77
+ start_new_session=True,
78
+ )
79
+ cf = None
80
+ try:
81
+ for _ in range(300):
82
+ if vllm.poll() is not None:
83
+ _log(f"vLLM died early rc={vllm.returncode}")
84
+ return 1
85
+ try:
86
+ urllib.request.urlopen("http://localhost:8000/health", timeout=3)
87
+ break
88
+ except Exception:
89
+ time.sleep(2)
90
+ else:
91
+ _log("vLLM never became healthy")
92
+ return 1
93
+
94
+ cflog = open("cf.log", "w")
95
+ cf = subprocess.Popen(["./cloudflared", "tunnel", "--no-autoupdate", "--url", "http://localhost:8000"],
96
+ stdout=cflog, stderr=subprocess.STDOUT, start_new_session=True)
97
+ public = None
98
+ for _ in range(60):
99
+ time.sleep(2)
100
+ m = re.search(r"https://[a-z0-9-]+\.trycloudflare\.com", open("cf.log").read())
101
+ if m:
102
+ public = m.group(0)
103
+ break
104
+ if not public:
105
+ _log("cloudflared did not produce a public URL")
106
+ return 1
107
+
108
+ banner = (
109
+ "\n" + "=" * 78
110
+ + f"\n READY. Point opencode at: {public}/v1"
111
+ + f"\n model: {MODEL}"
112
+ + "\n" + "=" * 78 + "\n"
113
+ )
114
+ deadline = time.time() + HOLD_S
115
+ while time.time() < deadline:
116
+ if vllm.poll() is not None:
117
+ _log(f"vLLM exited rc={vllm.returncode}")
118
+ return 1
119
+ print(banner, flush=True)
120
+ time.sleep(60)
121
+ _log("hold time is up, shutting down")
122
+ return 0
123
+ finally:
124
+ _kill_pg(cf)
125
+ _kill_pg(vllm)
126
+
127
+
128
+ if __name__ == "__main__":
129
+ raise SystemExit(main())