import subprocess, time, urllib.request from pathlib import Path SERVER = Path(__file__).parent / "bin" / "llama-server.exe" MD = Path(__file__).parent / "models" MODEL, MMPROJ = MD / "Qwen3.6-35B-A3B-UD-Q4_K_XL.gguf", MD / "mmproj-BF16.gguf" PORT = 18081 EMBED, EMBED_PORT = Path(__file__).parent / "models" / "bge-m3-Q8_0.gguf", 18082 _open = urllib.request.build_opener(urllib.request.ProxyHandler({})).open # corp proxy must not see localhost def up(port=PORT): try: _open(f"http://127.0.0.1:{port}/health", timeout=2); return True except Exception: return False def _spawn(args, port, log): if up(port): return None p = subprocess.Popen([str(SERVER), *args, "--port", str(port)], stdout=open(log, "w"), stderr=subprocess.STDOUT) for _ in range(600): if up(port): return p assert p.poll() is None, f"llama-server died, see {log}" time.sleep(2) raise TimeoutError("llama-server not ready") def start(model=MODEL, mmproj=MMPROJ, port=PORT, log="llama.log"): """Start llama-server (attention on GPU, MoE experts on CPU: fits 4 GB). None if already running.""" return _spawn(["-m", str(model), "--mmproj", str(mmproj), "-ngl", "99", "--cpu-moe", "-c", "16384", "--jinja"], port, log) def start_embed(log="embed.log"): """bge-m3 embeddings on CPU (never competes with the GPU model). None if already running.""" if not EMBED.exists(): raise FileNotFoundError(f"{EMBED} missing: hf download gpustack/bge-m3-GGUF bge-m3-Q8_0.gguf --local-dir models") return _spawn(["-m", str(EMBED), "--embeddings", "--pooling", "cls", "-ngl", "0", "-c", "8192", "-b", "2048", "-ub", "2048"], EMBED_PORT, log)