feat(ml): add isolated embeddings service and scoped client
This commit is contained in:
1 parent
46c185d928
commit
0c7897d695
13 files changed
+1112
No files matched your search
@@ -0,0 +1,71 @@
|
||||
"""Synthetic training -> Gunicorn HTTP -> Go client. No Docker or paid API.
|
||||
|
||||
Run using the same pinned dependencies as the service. All artifacts are placed
|
||||
in a temporary directory and removed on exit. Requires localhost listening.
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import urllib.request
|
||||
|
||||
from train import train
|
||||
|
||||
DOMAIN = "11111111-1111-4111-8111-111111111111"
|
||||
SCOPE = "22222222-2222-4222-8222-222222222222"
|
||||
|
||||
|
||||
def main():
|
||||
service = Path(__file__).resolve().parent
|
||||
with tempfile.TemporaryDirectory(prefix="pages-ml-smoke-") as root:
|
||||
root = Path(root)
|
||||
source = root / "fixture.jsonl"
|
||||
source.write_text("\n".join(json.dumps({"text": t}) for t in [
|
||||
"合成文書 library books reading", "図書館 本 library books documents",
|
||||
"network server routing 文書", "サーバー network server machines"]))
|
||||
manifest = train(source, root / "models", DOMAIN, SCOPE, 8)
|
||||
token_path = root / "token"
|
||||
token_path.write_text("t" * 40)
|
||||
token_path.chmod(0o600)
|
||||
env = dict(os.environ, PAGES_ML_TOKEN_FILE=str(token_path),
|
||||
PAGES_ML_MODEL_DIR=str(root / "models"), PAGES_ML_ALLOW_OPENAI="false")
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0)); sock.listen()
|
||||
origin = "http://127.0.0.1:" + str(sock.getsockname()[1])
|
||||
process = subprocess.Popen([
|
||||
sys.executable, "-m", "gunicorn", "--bind", f"fd://{sock.fileno()}",
|
||||
"--workers", "1", "--worker-class", "gthread", "--threads", "2",
|
||||
"--timeout", "60", "--no-control-socket", "pages_ml:create_app()"],
|
||||
pass_fds=(sock.fileno(),), cwd=service, env=env)
|
||||
try:
|
||||
deadline = time.monotonic() + 15
|
||||
while True:
|
||||
if process.poll() is not None:
|
||||
raise RuntimeError("ML service exited before readiness")
|
||||
try:
|
||||
with urllib.request.urlopen(origin + "/healthz", timeout=1) as response:
|
||||
if json.load(response)["protocol"] == "1":
|
||||
break
|
||||
except OSError:
|
||||
pass
|
||||
if time.monotonic() > deadline:
|
||||
raise RuntimeError("ML service did not become ready")
|
||||
time.sleep(0.1)
|
||||
env.update(PAGES_ML_TEST_ORIGIN=origin, PAGES_ML_TEST_MODEL=manifest["model_sha256"])
|
||||
subprocess.run(["go", "test", "./internal/machinelearning", "-run", "TestLocalDoc2VecService", "-count=1", "-v"],
|
||||
cwd=service.parent.parent, env=env, check=True, timeout=120)
|
||||
print("Synthetic Doc2Vec -> HTTP -> Go client: PASS")
|
||||
finally:
|
||||
process.terminate()
|
||||
try:
|
||||
process.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
process.kill(); process.wait()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user