File size: 12,014 Bytes
87d3209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a61282
87d3209
 
6a61282
 
 
 
87d3209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a61282
 
87d3209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a61282
 
 
 
87d3209
 
 
c35aeb5
6a61282
 
 
 
 
 
 
 
87d3209
 
 
 
6a61282
87d3209
 
 
6a61282
 
 
 
 
 
87d3209
 
 
 
 
6a61282
 
87d3209
 
 
 
 
 
c35aeb5
 
87d3209
 
 
 
 
 
 
 
 
 
6a61282
 
87d3209
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""Пускач PLUS Studio: сервер + публичная ссылка.

Одна строка в Colab или Kaggle:

    !python3 plus/launch_studio.py

Что делает:
  1. ставит зависимости (fastapi, uvicorn, python-multipart, soundfile, pretty_midi, ...);
  2. если рядом нет plus_app.py — скачивает plus.zip с Hugging Face и распаковывает;
  3. поднимает plus_app.py на локальном порту и ждёт /health;
  4. перебирает туннели по очереди (localhost.run, serveo, pinggy) и берёт первый,
     который выдал адрес и реально ответил;
  5. печатает ссылку, кладёт её в plus_links.txt и держит всё живым,
     перезапуская сервер или туннель, если они умерли.

Флаги:
  --port 8900          локальный порт
  --device auto        auto | cuda | cpu
  --weights PATH       свои веса; иначе модель скачается с HF при первом запросе
  --no-tunnel          только сервер, без ссылки (для локального запуска)
  --tunnel NAME        начать с конкретного туннеля: localhostrun | serveo | pinggy
  --keep 0             сколько секунд держать (0 = бесконечно)
"""

from __future__ import annotations

import argparse
import collections
import os
import re
import shutil
import subprocess
import sys
import threading
import time
import urllib.request

HERE = os.path.dirname(os.path.abspath(__file__))
REPO = "Nokia123543/muscriptor-large-fork"
ZIP_URL = "https://huggingface.co/%s/resolve/main/plus.zip" % REPO
PACKAGES = ("fastapi", "uvicorn", "python-multipart", "soundfile", "pretty_midi",
            "librosa", "huggingface_hub", "numpy", "scipy")

LINKS_FILE = "plus_links.txt"


def step(index: int, total: int, text: str) -> None:
    print("[%d/%d] %s" % (index, total, text), flush=True)


def ensure_deps() -> None:
    missing = []
    for name in PACKAGES:
        import importlib.util
        if importlib.util.find_spec(name.replace("-", "_")) is None:
            missing.append(name)
    if not missing:
        return
    print("ставлю: " + ", ".join(missing), flush=True)
    subprocess.run([sys.executable, "-m", "pip", "install", "-q", *missing], check=False)


def ensure_pack() -> str:
    target = os.path.join(HERE, "plus_app.py")
    if os.path.isfile(target):
        return HERE
    print("скачиваю пакет с Hugging Face", flush=True)
    import zipfile
    archive = os.path.join(HERE, "plus.zip")
    urllib.request.urlretrieve(ZIP_URL, archive)
    with zipfile.ZipFile(archive) as bundle:
        bundle.extractall(HERE)
    for candidate in (os.path.join(HERE, "plus"), HERE):
        if os.path.isfile(os.path.join(candidate, "plus_app.py")):
            return candidate
    raise SystemExit("в архиве нет plus_app.py")


def healthy(port: int) -> bool:
    try:
        with urllib.request.urlopen("http://127.0.0.1:%d/health" % port, timeout=3) as answer:
            return answer.status == 200
    except Exception:
        return False


def start_server(pack: str, port: int, device: str, weights: str | None) -> subprocess.Popen:
    command = [sys.executable, os.path.join(pack, "plus_app.py"),
               "--port", str(port), "--device", device, "--host", "0.0.0.0"]
    if weights:
        command += ["--weights", weights]
    log = open("plus_studio.log", "a")
    log.write("\n=== %s ===\n" % time.strftime("%Y-%m-%d %H:%M:%S"))
    process = subprocess.Popen(command, stdout=log, stderr=subprocess.STDOUT,
                               stdin=subprocess.DEVNULL)
    for _ in range(90):
        if healthy(port):
            return process
        if process.poll() is not None:
            raise SystemExit("сервер умер при старте, смотри plus_studio.log")
        time.sleep(2)
    raise SystemExit("сервер не ответил за 3 минуты, смотри plus_studio.log")


class Tunnel:
    """Туннель по ssh -R или по http-клиенту; нужен только адрес в stdout."""

    def __init__(self, name: str, argv: list[str], pattern: str):
        self.name, self.pattern, self.tail = name, re.compile(pattern), collections.deque(maxlen=60)
        self.url: str | None = None
        self.proc = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                                     stdin=subprocess.DEVNULL, text=True, bufsize=1)
        threading.Thread(target=self._drain, daemon=True).start()

    def _drain(self) -> None:
        for line in self.proc.stdout:
            self.tail.append(line.rstrip())
            if self.url is None:
                found = self.pattern.search(line)
                if found:
                    self.url = found.group(0)

    def alive(self) -> bool:
        return self.proc.poll() is None

    def kill(self) -> None:
        try:
            self.proc.kill()
        except Exception:
            pass


SSH_OPTS = ["-o", "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null",
            "-o", "ServerAliveInterval=15", "-o", "ServerAliveCountMax=3",
            "-o", "TCPKeepAlive=yes", "-o", "ExitOnForwardFailure=yes"]


def tunnel_specs(port: int) -> list[tuple[str, list[str], str]]:
    target = "127.0.0.1:%d" % port
    specs = [("localhostrun",
              ["ssh", "-T", *SSH_OPTS, "-R", "80:" + target, "nokey@localhost.run"],
              r"https://[a-z0-9]+\.lhr\.life"),
             ("serveo",
              ["ssh", "-T", *SSH_OPTS, "-R", "80:" + target, "serveo.net"],
              r"https://[a-z0-9-]+(?:\.[a-z0-9-]+)*\.(?:serveousercontent|serveo)\.[a-z]+")]
    if shutil.which("npx"):
        specs.append(("localtunnel",
                      ["npx", "-y", "localtunnel", "--port", str(port)],
                      r"https://[a-z0-9-]+\.loca\.lt"))
    return specs


def reachable(url: str, attempts: int = 5) -> bool:
    for _ in range(attempts):
        try:
            with urllib.request.urlopen(url + "/health", timeout=8) as answer:
                if answer.status == 200:
                    return True
        except Exception:
            pass
        time.sleep(3)
    return False


def open_tunnel(port: int, order: list[str]) -> Tunnel:
    specs = {name: (argv, pattern) for name, argv, pattern in tunnel_specs(port)}
    tried = []
    for name in order:
        if name not in specs:
            continue
        argv, pattern = specs[name]
        if not shutil.which(argv[0]):
            print("  [%s] нет команды %s, пропускаю" % (name, argv[0]), flush=True)
            continue
        print("  [%s] открываю" % name, flush=True)
        tunnel = Tunnel(name, argv, pattern)
        deadline = time.time() + 60
        started = time.time()
        while time.time() < deadline and tunnel.url is None and tunnel.alive():
            time.sleep(1)
            waited = int(time.time() - started)
            if waited and waited % 10 == 0:
                print("      [%s] ждём адрес, %d с, жив: %s"
                      % (name, waited, tunnel.alive()), flush=True)
        if tunnel.url and reachable(tunnel.url):
            print("  [%s] работает: %s" % (name, tunnel.url), flush=True)
            return tunnel
        tried.append((name, tunnel.url, list(tunnel.tail)[-3:]))
        tunnel.kill()
    for name, url, tail in tried:
        print("  [%s] не вышло: %s | %s" % (name, url or "нет адреса", " / ".join(tail)), flush=True)
    raise SystemExit("ни один туннель не поднялся: ssh или npx недоступны из этой среды")


def write_links(lines: list[str]) -> None:
    text = "\n".join(lines) + "\n"
    with open(LINKS_FILE, "w", encoding="utf-8") as handle:
        handle.write(text)
    print("\n" + text, flush=True)


def main(argv: list[str] | None = None) -> int:
    print("логи: %s (сервер) и %s (ссылки) в %s"
          % ("plus_studio.log", LINKS_FILE, os.getcwd()), flush=True)
    parser = argparse.ArgumentParser(description="PLUS Studio: сервер и ссылка")
    parser.add_argument("--port", type=int, default=8900)
    parser.add_argument("--device", default="auto")
    parser.add_argument("--weights", default=os.environ.get("MUSCRIPTOR_WEIGHTS"))
    parser.add_argument("--no-tunnel", action="store_true")
    parser.add_argument("--tunnel", default=None)
    parser.add_argument("--keep", type=int, default=0, help="секунд держать, 0 = бесконечно")
    args = parser.parse_args(argv)

    step(1, 5, "зависимости")
    ensure_deps()
    step(2, 5, "пакет PLUS")
    pack = ensure_pack()
    step(3, 5, "сервер на порту %d" % args.port)
    server = start_server(pack, args.port, args.device, args.weights)
    print("      сервер отвечает: http://127.0.0.1:%d" % args.port, flush=True)

    order = ["localhostrun", "serveo", "local" "tunnel"]
    if args.tunnel:
        order = [args.tunnel] + [name for name in order if name != args.tunnel]
    tunnel = None
    if args.no_tunnel:
        write_links(["PLUS Studio " + time.strftime("%H:%M:%S"),
                     "локально: http://127.0.0.1:%d" % args.port])
        print(">>> LOCAL LINK: http://127.0.0.1:%d" % args.port, flush=True)
    else:
        step(4, 5, "публичная ссылка")
        tunnel = open_tunnel(args.port, order)
        write_links(["PLUS Studio " + time.strftime("%H:%M:%S"),
                     "ссылка: " + tunnel.url,
                     "локально: http://127.0.0.1:%d" % args.port])
        if tunnel.url:
            print("\n>>> OPEN THIS LINK: " + tunnel.url, flush=True)

    step(5, 5, "держу живым, Stop останавливает")
    started = time.time()
    fails = 0
    beat = time.time()
    try:
        while True:
            time.sleep(10)
            if time.time() - beat > 60:
                beat = time.time()
                print("      жив: %.1f мин | сервер %s | ссылка %s"
                      % ((time.time() - started) / 60,
                         "ок" if server.poll() is None else "упал",
                         tunnel.url if tunnel else "нет (локально)"), flush=True)
            if args.keep and time.time() - started > args.keep:
                break
            if server.poll() is not None:
                print("сервер упал, поднимаю заново", flush=True)
                server = start_server(pack, args.port, args.device, args.weights)
            if tunnel is None:
                continue
            if not tunnel.alive():
                print("туннель умер, открываю новый", flush=True)
                tunnel.kill()
                tunnel = open_tunnel(args.port, order)
                write_links(["PLUS Studio " + time.strftime("%H:%M:%S"),
                             "новая ссылка: " + tunnel.url])
                if tunnel.url:
                    print(">>> OPEN THIS LINK: " + tunnel.url, flush=True)
                fails = 0
                continue
            if not reachable(tunnel.url, attempts=1):
                fails += 1
                if fails >= 3:
                    print("ссылка перестала отвечать, переоткрываю", flush=True)
                    tunnel.kill()
                    fails = 0
    except KeyboardInterrupt:
        pass
    if tunnel is not None:
        tunnel.kill()
    if server.poll() is None:
        server.terminate()
    print("остановлено", flush=True)
    return 0


if __name__ == "__main__":
    sys.exit(main())