1
from __future__ import annotations
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"))
22
DEMOS_TO_SKIP = {"all_demos", "clear_components", "custom_path", "kitchen_sink_random", "blocks_kitchen_sink"}
25
def upload_demo_to_space(
29
gradio_version: str | None,
30
gradio_wheel_url: str | None = None,
31
gradio_client_url: str | None = None
33
"""Upload a demo in the demo directory to a huggingface space.
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.
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")
49
title: {space_id.split("/")[-1]}
54
sdk_version: {gradio_version}
60
readme.open("w").write(textwrap.dedent(readme_content))
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)
68
with open(os.path.join(requirements_path), "r") as f:
70
with open(os.path.join(requirements_path), "w") as f:
72
f.write(gradio_client_url + "\n" + gradio_wheel_url + "\n" + content)
74
api = huggingface_hub.HfApi()
75
huggingface_hub.create_repo(
89
return f"https://huggingface.co/spaces/{space_id}"
91
demos = os.listdir(GRADIO_DEMO_DIR)
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"))]
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"
104
if args.AUTH_TOKEN is not None:
105
hello_world_version = str(huggingface_hub.space_info("gradio/hello_world").cardData["sdk_version"])
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)