File size: 1,962 Bytes
b49bf13
 
 
 
 
 
 
 
 
2ad74e3
b49bf13
 
 
 
 
 
 
 
 
 
 
 
 
2ad74e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b49bf13
 
 
 
 
 
 
 
 
 
 
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
"""Publish this folder to a free Hugging Face ZeroGPU Gradio Space."""
from __future__ import annotations

import argparse
import getpass
import os
from pathlib import Path

from huggingface_hub import HfApi
from huggingface_hub.utils import HfHubHTTPError, RepositoryNotFoundError

ROOT = Path(__file__).resolve().parents[1]


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("repo_id", help="e.g. username/nextgpt-7b-v0-reproduction")
    args = parser.parse_args()
    token = os.environ.get("HF_TOKEN") or getpass.getpass("Hugging Face token: ")
    if not token:
        raise SystemExit("A write token is required")
    api = HfApi(token=token)
    try:
        existing = api.space_info(args.repo_id)
    except RepositoryNotFoundError:
        existing = None
    if existing is None:
        try:
            api.create_repo(
                repo_id=args.repo_id, repo_type="space", space_sdk="gradio",
                space_hardware="zero-a10g", exist_ok=True,
            )
        except HfHubHTTPError as exc:
            if exc.response is not None and exc.response.status_code == 402:
                raise SystemExit(
                    "Hugging Face refused free ZeroGPU hosting for this account. "
                    "No paid hardware was requested."
                ) from exc
            raise
    elif existing.card_data.get("sdk") != "gradio" or api.get_space_runtime(args.repo_id).hardware != "zero-a10g":
        raise SystemExit("The existing Space must already be a free Gradio ZeroGPU Space")
    api.upload_folder(
        repo_id=args.repo_id, repo_type="space", folder_path=str(ROOT),
        ignore_patterns=["**/__pycache__/**", "**/*.pyc", ".venv/**",
                         "models/**", "outputs/**", ".cache/**"],
        commit_message="Deploy official NExT-GPT 7b_tiva_v0 on free ZeroGPU",
    )
    print(f"https://huggingface.co/spaces/{args.repo_id}")


if __name__ == "__main__":
    main()