File size: 1,323 Bytes
b334f76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import sys
from pathlib import Path

from huggingface_hub import HfApi

SPACE_NAME = os.environ.get("SPACE_NAME", "wode")
PRIVATE = os.environ.get("PRIVATE", "false").lower() == "true"
FOLDER = Path(os.environ.get("FOLDER", ".")).resolve()


def main():
    token = sys.stdin.read().strip()
    if not token:
        raise SystemExit("missing token on stdin")

    api = HfApi(token=token)
    me = api.whoami()
    username = me.get("name") or me.get("fullname") or me.get("email") or "unknown"

    repo_url = api.create_repo(
        repo_id=SPACE_NAME,
        repo_type="space",
        private=PRIVATE,
        exist_ok=True,
        space_sdk="gradio",
    )

    full_repo_id = repo_url.repo_id if hasattr(repo_url, "repo_id") else str(repo_url).replace("https://huggingface.co/", "")

    api.upload_folder(
        repo_id=full_repo_id,
        repo_type="space",
        folder_path=str(FOLDER),
        ignore_patterns=["*.pyc", "__pycache__/*", ".git/*"],
    )

    try:
        api.restart_space(repo_id=full_repo_id)
    except Exception:
        pass

    out = {
        "username": username,
        "repo_id": full_repo_id,
        "url": f"https://huggingface.co/spaces/{full_repo_id}",
    }
    print(json.dumps(out, ensure_ascii=False))


if __name__ == "__main__":
    main()