File size: 2,374 Bytes
58bf71d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Push this folder to the Hugging Face Space, with a fresh download zip.

Needs huggingface_hub (pip install huggingface_hub). The token comes from $HF_TOKEN or from
~/Dokumente/tokens/HF token, and is never written anywhere.

Usage: python3 tools/deploy_space.py ["commit message"]
"""
import os
import re
import shutil
import sys
import tempfile
import zipfile
from pathlib import Path


REPO = "EHS-Studio/techglass-mint"
ROOT = Path(__file__).resolve().parent.parent
SKIP = {"__pycache__", "menu-icon.svg", ".claude", ".git"}
APP_FILES = ["tgcore.py", "studio.py", "templates", "fx", "icon.svg", "install.sh", "README.md", "LICENSE", "tools"]


def token():
    if os.environ.get("HF_TOKEN"):
        return os.environ["HF_TOKEN"]
    text = (Path.home() / "Dokumente/tokens/HF token").read_text()
    return re.search(r"hf_[A-Za-z0-9]+", text).group(0)


def build_zip(dest):
    readme = re.sub(r"\A---\n.*?\n---\n+", "", (ROOT / "README.md").read_text(), flags=re.S)
    with zipfile.ZipFile(dest, "w", zipfile.ZIP_DEFLATED) as z:
        for name in APP_FILES:
            src = ROOT / name
            paths = [src] if src.is_file() else [p for p in src.rglob("*") if p.is_file()]
            for p in paths:
                if SKIP & set(p.relative_to(ROOT).parts):
                    continue
                arc = "techglass-mint/" + str(p.relative_to(ROOT))
                if p.name == "README.md" and p.parent == ROOT:
                    z.writestr(arc, readme)
                else:
                    info = zipfile.ZipInfo.from_file(p, arc)   # keeps install.sh executable
                    z.writestr(info, p.read_bytes())


def main():
    from huggingface_hub import HfApi

    message = sys.argv[1] if len(sys.argv) > 1 else "Update TechGlass Mint"
    with tempfile.TemporaryDirectory() as tmp:
        stage = Path(tmp) / "stage"
        shutil.copytree(ROOT, stage, ignore=lambda d, names: [n for n in names if n in SKIP or n.endswith(".pyc")])
        build_zip(stage / "techglass-mint.zip")
        api = HfApi(token=token())
        api.create_repo(REPO, repo_type="space", space_sdk="static", exist_ok=True)
        api.upload_folder(folder_path=str(stage), repo_id=REPO, repo_type="space", commit_message=message)
    print(f"Pushed to https://huggingface.co/spaces/{REPO}")


if __name__ == "__main__":
    main()