Spaces:
Paused

m3kb / launch.py
LahiruSe's picture
Upload launch.py with huggingface_hub
7569963 verified
Raw History Blame Contribute Delete
1.74 kB
"""
m3kb Space launcher.
The public Space image contains ONLY this tiny stub plus dependencies. The real
MCP server (server.py) and ALL data (graph, facts, labels, source archive) live
in the PRIVATE dataset and are pulled at boot with a read token. So the public
Space repo reveals nothing about the tools, the corpus, or how the API works -
just "download the private dataset and run whatever is inside".
Because the image itself never changes, every future update - server code OR
data - is just a dataset upload + a Space restart (~1 min): no Docker rebuild,
no build queue.
"""
import os
import sys
import tarfile
from pathlib import Path
from huggingface_hub import snapshot_download
DATA = Path(os.environ.get("M3KB_DATA", "/app/data"))
DATASET = os.environ.get("M3KB_DATASET", "").strip()
if not DATASET:
sys.exit("launcher: M3KB_DATASET not set - cannot locate the private data repo")
print(f"launcher: pulling private dataset {DATASET} ...", file=sys.stderr)
snapshot_download(DATASET, repo_type="dataset", local_dir=str(DATA),
token=os.environ.get("HF_TOKEN"))
# Bulk file trees (src ~23k, docs ~1.7k) ship as single archives so boot is a
# handful of downloads, not thousands. Extract whatever archives are present.
for _name in ("src.tar.gz", "docs.tar.gz"):
_arch = DATA / _name
if _arch.exists():
print(f"launcher: extracting {_name} ...", file=sys.stderr)
with tarfile.open(_arch, "r:gz") as tf:
tf.extractall(str(DATA))
server = DATA / "server.py"
if not server.exists():
sys.exit("launcher: server.py not found in the private dataset")
print("launcher: starting server ...", file=sys.stderr)
os.execv(sys.executable, [sys.executable, str(server)])