File size: 2,854 Bytes
a03b3ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import pathlib
import shutil
import tempfile
import textwrap
from typing import Optional

import huggingface_hub


def upload_demo_to_space(
    demo_name: str, space_id: str, hf_token: str, gradio_version: Optional[str]
):
    """Upload a demo in the demo directory to a huggingface space.
    Args:
        demo_name: The name of the demo to upload.
        space_id: The id of the space to upload the demo to.
        hf_token: HF api token. Need to have permission to write to space_id for this to work.
        gradio_version: If not None, will set the gradio version in the created space to the given version.
    """

    with tempfile.TemporaryDirectory() as tmpdir:
        demo_path = pathlib.Path(pathlib.Path().absolute(), f"demo/{demo_name}")
        shutil.copytree(demo_path, tmpdir, dirs_exist_ok=True)
        app_file = pathlib.Path(tmpdir, "run.py")
        # Rename the app file to be app.py
        app_file.rename(app_file.with_stem("app"))
        if gradio_version:
            readme = pathlib.Path(tmpdir, "README.md")
            readme_content = f"""
                                ---
                                title: {space_id.split("/")[-1]} 
                                emoji: 💩
                                colorFrom: indigo
                                colorTo: indigo
                                sdk: gradio
                                sdk_version: {gradio_version}
                                app_file: app.py
                                pinned: false
                                ---
                                """
            readme.open("w").write(textwrap.dedent(readme_content))

        api = huggingface_hub.HfApi()
        huggingface_hub.create_repo(
            space_id,
            space_sdk="gradio",
            repo_type="space",
            token=hf_token,
            exist_ok=True,
        )
        api.upload_folder(
            token=hf_token,
            repo_id=space_id,
            repo_type="space",
            folder_path=tmpdir,
            path_in_repo="",
        )
    return f"https://huggingface.co/spaces/{space_id}"


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Upload a demo to a space")
    parser.add_argument("demo_name", type=str, help="Name of demo to upload")
    parser.add_argument(
        "space_id", type=str, help="Name of the space to upload the demo to"
    )
    parser.add_argument("hf_token", type=str, help="HF API token")
    parser.add_argument(
        "--gradio-version",
        type=str,
        help="If not None, will set the gradio version in the created space to the given version.",
    )
    args = parser.parse_args()
    new_space = upload_demo_to_space(
        args.demo_name, args.space_id, args.hf_token, args.gradio_version
    )
    print(new_space)