File size: 2,817 Bytes
f507ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324eb2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f507ca1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import argparse
import json
from pathlib import Path

from huggingface_hub import HfApi


ROOT = Path(__file__).resolve().parent
CREDENTIALS_FILE = ROOT / "credentials.json"


def read_token():
    if not CREDENTIALS_FILE.is_file():
        raise FileNotFoundError(
            f"Missing {CREDENTIALS_FILE}. Add your Hugging Face access token "
            'as {"token": "hf_..."} in that file.'
        )

    with CREDENTIALS_FILE.open(encoding="utf-8") as credentials_file:
        credentials = json.load(credentials_file)

    token = credentials.get("token")
    if not isinstance(token, str) or not token.strip():
        raise ValueError(
            f"No token found in {CREDENTIALS_FILE}. Add it as "
            '{"token": "hf_..."} and try again.'
        )
    return token.strip()


def main():
    parser = argparse.ArgumentParser(
        description="Create a Hugging Face Gradio Space and upload this app."
    )
    parser.add_argument(
        "--name",
        default="devcoder-ai",
        help="Space name (default: devcoder-ai)",
    )
    parser.add_argument(
        "--private",
        action="store_true",
        help="Create the Space as private (public by default).",
    )
    args = parser.parse_args()

    api = HfApi(token=read_token())
    account = api.whoami()
    username = account.get("name")
    if not username:
        raise RuntimeError("Could not determine the Hugging Face account name.")

    repo_id = f"{username}/{args.name}"
    if api.repo_exists(repo_id=repo_id, repo_type="space"):
        runtime = api.get_space_runtime(repo_id)
        requested_hardware = runtime.requested_hardware
        requested_hardware = getattr(
            requested_hardware, "value", requested_hardware
        )
        if requested_hardware != "zero-a10g":
            api.request_space_hardware(repo_id, "zero-a10g")
        print(f"Updating Space: https://huggingface.co/spaces/{repo_id}")
    else:
        api.create_repo(
            repo_id=repo_id,
            repo_type="space",
            space_sdk="gradio",
            space_hardware="zero-a10g",
            private=args.private,
        )
        print(f"Created Space: https://huggingface.co/spaces/{repo_id}")

    result = api.upload_folder(
        repo_id=repo_id,
        repo_type="space",
        folder_path=str(ROOT),
        ignore_patterns=[
            ".gitignore",
            "credentials.json",
            "__pycache__/**",
            "*.py[cod]",
        ],
        commit_message="Deploy Qwen2.5 Coder 3B Gradio app",
    )
    print(f"Uploaded app. Deployment status: https://huggingface.co/spaces/{repo_id}")
    print(f"Upload commit: {result.commit_url}")


if __name__ == "__main__":
    main()