File size: 3,486 Bytes
b279669
 
 
 
 
 
 
 
 
 
d1d8733
 
b279669
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Publish only the reviewed public client files and pin the release to its commit."""
import argparse
import json
from pathlib import Path

from huggingface_hub import CommitOperationAdd, HfApi


def deploy(space):
    root = Path(__file__).resolve().parents[1]
    names = ["README.md", "index.html", "style.css", "bootstrap.md", "requirements.txt", ".gitignore",
             "docs/agent-memory-design.md", "scripts/deploy_space.py", "tests/test_memory.py", "tests/live_smoke.py"]
    names += [p.relative_to(root).as_posix() for p in (root / "memory-use").rglob("*")
              if p.is_file() and "__pycache__" not in p.parts and p.suffix != ".pyc"]
    api = HfApi()
    try:
        info = api.repo_info(space, repo_type="space")
    except Exception as exc:
        if getattr(getattr(exc, "response", None), "status_code", None) != 404:
            raise
        api.create_repo(space, repo_type="space", space_sdk="static", private=False)
        info = api.repo_info(space, repo_type="space")
    if info.private or info.sdk != "static":
        raise RuntimeError("Existing Space must already be public and static; no settings were changed.")
    existing = set(api.list_repo_files(space, repo_type="space"))
    unknown = existing - set(names) - {".gitattributes", "release.json"}
    if unknown:
        raise RuntimeError("Existing Space contains unrelated files; inspect before publishing.")
    if "release.json" in existing:
        from huggingface_hub import hf_hub_download
        release = json.loads(Path(hf_hub_download(space, "release.json", repo_type="space", revision=info.sha)).read_text())
        if release.get("space") != space or release.get("version") is None:
            raise RuntimeError("Existing release descriptor does not identify this client.")
    operations = []
    for name in names:
        path = root / name
        if path.is_symlink():
            raise RuntimeError("Public release cannot contain symlinks.")
        operations.append(CommitOperationAdd(path_in_repo=name, path_or_fileobj=path))
    source = api.create_commit(space, repo_type="space", operations=operations,
                               parent_commit=info.sha, commit_message="Release portable agent memory client 0.1.0")
    descriptor = {"version": "0.1.0", "space": space, "revision": source.oid}
    payload = (json.dumps(descriptor, indent=2) + "\n").encode()
    release_commit = api.create_commit(space, repo_type="space", parent_commit=source.oid,
                                      operations=[CommitOperationAdd(path_in_repo="release.json", path_or_fileobj=payload)],
                                      commit_message="Pin bootstrap client to the published source revision")
    (root / "release.json").write_bytes(payload)
    return {"space": f"https://huggingface.co/spaces/{space}", "client_revision": source.oid,
            "release_commit": release_commit.oid, "files": len(names)}


if __name__ == "__main__":
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument("--space", required=True)
    args = p.parse_args()
    try:
        print(json.dumps(deploy(args.space), indent=2))
    except Exception as exc:
        # SDK errors may contain signed URLs. Keep deployment diagnostics credential-free.
        if isinstance(exc, RuntimeError):
            print(str(exc))
        else:
            print(f"Deployment failed ({type(exc).__name__}); check HF authentication and permissions.")
        raise SystemExit(1)