gradio

Форк
0
/
upload_website_demos.py 
109 строк · 4.8 Кб
1
from __future__ import annotations
2

3
import argparse
4
import json
5
import os
6
import pathlib
7
import shutil
8
import tempfile
9
import textwrap
10

11
import huggingface_hub
12

13
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
14
DIR = os.path.dirname(__file__)
15
GRADIO_DEMO_DIR = os.path.abspath(os.path.join(ROOT, "demo"))
16

17
# Reasoning:
18
# 1. all_demos includes all demos and is for testing PRs
19
# 2. reset_components includes media files that are only present in all_demos (only for PRs)
20
# 3. custom_path doesn't have .launch since the point is to show how to launch with uvicorn
21
# 4. The same reason as 2 for kitchen_sink_random and blocks_kitchen_sink
22
DEMOS_TO_SKIP = {"all_demos", "clear_components", "custom_path", "kitchen_sink_random", "blocks_kitchen_sink"}
23

24

25
def upload_demo_to_space(
26
    demo_name: str, 
27
    space_id: str, 
28
    hf_token: str, 
29
    gradio_version: str | None, 
30
    gradio_wheel_url: str | None = None,
31
    gradio_client_url: str | None = None
32
):
33
    """Upload a demo in the demo directory to a huggingface space.
34
    Parameters:
35
        demo_name: The name of the demo to upload.
36
        space_id: The id of the space to upload the demo to.
37
        hf_token: HF api token. Need to have permission to write to space_id for this to work.
38
        gradio_version: If not None, will set the gradio version in the created Space to the given version.
39
        gradio_wheel_url: If not None, will install the version of gradio using the wheel url in the created Space.
40
        gradio_client_url: If not None, will install the version of gradio client using the wheel url in the created Space.
41
    """
42

43
    with tempfile.TemporaryDirectory() as tmpdir:
44
        demo_path = pathlib.Path(GRADIO_DEMO_DIR, demo_name)
45
        shutil.copytree(demo_path, tmpdir, dirs_exist_ok=True)
46
        readme = pathlib.Path(tmpdir, "README.md")
47
        readme_content = f"""
48
                            ---
49
                            title: {space_id.split("/")[-1]} 
50
                            emoji: 🔥
51
                            colorFrom: indigo
52
                            colorTo: indigo
53
                            sdk: gradio
54
                            sdk_version: {gradio_version}
55
                            app_file: run.py
56
                            pinned: false
57
                            hf_oauth: true
58
                            ---
59
                            """
60
        readme.open("w").write(textwrap.dedent(readme_content))
61

62
        if gradio_wheel_url and gradio_client_url:
63
            requirements_path = os.path.join(tmpdir, "requirements.txt")
64
            if not os.path.exists(requirements_path):
65
                with open(os.path.join(requirements_path), "w") as f:
66
                    f.write(gradio_client_url + "\n" + gradio_wheel_url)
67
            else:
68
                with open(os.path.join(requirements_path), "r") as f:
69
                    content = f.read()
70
                with open(os.path.join(requirements_path), "w") as f:
71
                    f.seek(0, 0)
72
                    f.write(gradio_client_url + "\n" + gradio_wheel_url + "\n" + content)
73

74
        api = huggingface_hub.HfApi()
75
        huggingface_hub.create_repo(
76
            space_id,
77
            space_sdk="gradio",
78
            repo_type="space",
79
            token=hf_token,
80
            exist_ok=True,
81
        )
82
        api.upload_folder(
83
            token=hf_token,
84
            repo_id=space_id,
85
            repo_type="space",
86
            folder_path=tmpdir,
87
            path_in_repo="",
88
        )
89
    return f"https://huggingface.co/spaces/{space_id}"
90

91
demos = os.listdir(GRADIO_DEMO_DIR)
92

93
demos = [demo for demo in demos if demo not in DEMOS_TO_SKIP and os.path.isdir(os.path.join(GRADIO_DEMO_DIR, demo)) and  os.path.exists(os.path.join(GRADIO_DEMO_DIR, demo, "run.py"))]
94

95
if __name__ == "__main__":
96
    parser = argparse.ArgumentParser()
97
    parser.add_argument("--WHEEL_URL", type=str, help="aws link to gradio wheel")
98
    parser.add_argument("--CLIENT_URL", type=str, help="gradio version")
99
    parser.add_argument("--AUTH_TOKEN", type=str, help="huggingface auth token")
100
    parser.add_argument("--GRADIO_VERSION", type=str, help="gradio version")
101
    args = parser.parse_args()
102
    gradio_wheel_url = args.WHEEL_URL + f"gradio-{args.GRADIO_VERSION}-py3-none-any.whl"
103
    
104
    if args.AUTH_TOKEN is not None:
105
        hello_world_version = str(huggingface_hub.space_info("gradio/hello_world").cardData["sdk_version"])
106
        for demo in demos:
107
            if hello_world_version != args.GRADIO_VERSION:
108
                upload_demo_to_space(demo_name=demo, space_id="gradio/" + demo, hf_token=args.AUTH_TOKEN, gradio_version=args.GRADIO_VERSION)
109
            upload_demo_to_space(demo_name=demo, space_id="gradio/" + demo + "_main", hf_token=args.AUTH_TOKEN, gradio_version=args.GRADIO_VERSION, gradio_wheel_url=gradio_wheel_url, gradio_client_url=args.CLIENT_URL)
110

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.