File size: 1,277 Bytes
8770e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Thin public loader for this Space.

The real application code for this Space is not public. It lives in a
private Hugging Face repo and is downloaded at startup using this
Space's HF_TOKEN secret. Only this loader file is public.
"""

import os
import sys
import zipfile
from pathlib import Path

PAYLOAD_REPO = "aabiro/space-payloads"
PAYLOAD_FILE = "mcp-explorer.zip"
TARGET = Path("/tmp/space-app/mcp-explorer")


def _fetch_payload() -> None:
    if (TARGET / "app.py").is_file():
        return
    token = os.environ.get("HF_TOKEN")
    if not token:
        raise RuntimeError(
            "HF_TOKEN secret is not set on this Space, so the private "
            "application code cannot be downloaded."
        )
    from huggingface_hub import hf_hub_download

    zip_path = hf_hub_download(
        repo_id=PAYLOAD_REPO,
        repo_type="model",
        filename=PAYLOAD_FILE,
        token=token,
    )
    TARGET.mkdir(parents=True, exist_ok=True)
    with zipfile.ZipFile(zip_path) as zf:
        zf.extractall(TARGET)


_fetch_payload()
sys.path.insert(0, str(TARGET))
os.chdir(TARGET)
_app_file = TARGET / "app.py"
exec(
    compile(_app_file.read_text(encoding="utf-8"), str(_app_file), "exec"),
    {"__name__": "__main__", "__file__": str(_app_file)},
)