diff --git a/.github/workflows/deploy-spaces.yml b/.github/workflows/deploy-spaces.yml index 1c27e467bd..3b14fcfd54 100644 --- a/.github/workflows/deploy-spaces.yml +++ b/.github/workflows/deploy-spaces.yml @@ -113,7 +113,7 @@ jobs: aws s3 cp $tarball_name s3://gradio-builds/${{ needs.changes.outputs.sha }}/$tarball_name echo "js_tarball_url=https://gradio-builds.s3.amazonaws.com/${{ needs.changes.outputs.sha }}/$tarball_name" >> $GITHUB_OUTPUT - name: Install Hub Client Library - run: pip install huggingface-hub + run: pip install huggingface-hub==0.23.2 - name: Upload demo to spaces if: github.event.workflow_run.event == 'pull_request' id: upload-demo diff --git a/scripts/upload_website_demos.py b/scripts/upload_website_demos.py index 87a43d8e29..0e8f479864 100644 --- a/scripts/upload_website_demos.py +++ b/scripts/upload_website_demos.py @@ -7,6 +7,10 @@ import pathlib import shutil import tempfile import textwrap +import requests +import time +import warnings + import huggingface_hub @@ -21,6 +25,21 @@ GRADIO_DEMO_DIR = os.path.abspath(os.path.join(ROOT, "demo")) # 4. The same reason as 2 for kitchen_sink_random and blocks_kitchen_sink DEMOS_TO_SKIP = {"all_demos", "clear_components", "custom_path", "kitchen_sink_random", "blocks_kitchen_sink"} +api = huggingface_hub.HfApi() + +def space_exists(space_id): + url = f"https://huggingface.co/spaces/{space_id}" + try: + for _ in range(5): + with warnings.catch_warnings(): + warnings.filterwarnings("ignore") + r = requests.head(url, timeout=3, verify=False) + if r.status_code == 200: + return True + time.sleep(0.500) + except requests.ConnectionError: + return False + return False def upload_demo_to_space( demo_name: str, @@ -39,7 +58,7 @@ def upload_demo_to_space( gradio_wheel_url: If not None, will install the version of gradio using the wheel url in the created Space. gradio_client_url: If not None, will install the version of gradio client using the wheel url in the created Space. """ - + print(f"Uploading {demo_name} to {space_id}...") with tempfile.TemporaryDirectory() as tmpdir: demo_path = pathlib.Path(GRADIO_DEMO_DIR, demo_name) shutil.copytree(demo_path, tmpdir, dirs_exist_ok=True) @@ -70,26 +89,31 @@ def upload_demo_to_space( with open(os.path.join(requirements_path), "w") as f: f.seek(0, 0) f.write(gradio_client_url + "\n" + gradio_wheel_url + "\n" + content) + + try: + if not space_exists(space_id): + print(f"Creating space {space_id}") + api.create_repo( + space_id, + space_sdk="gradio", + repo_type="space", + token=hf_token, + exist_ok=True, + ) - 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="", - ) + api.upload_folder( + token=hf_token, + repo_id=space_id, + repo_type="space", + folder_path=tmpdir, + path_in_repo="", + ) + except Exception as e: + print(f"Failed to upload {demo_name} to {space_id}. Error: {e}") + return return f"https://huggingface.co/spaces/{space_id}" demos = os.listdir(GRADIO_DEMO_DIR) - 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"))] if __name__ == "__main__": @@ -104,6 +128,6 @@ if __name__ == "__main__": if args.AUTH_TOKEN is not None: hello_world_version = str(huggingface_hub.space_info("gradio/hello_world").cardData["sdk_version"]) for demo in demos: - if hello_world_version != args.GRADIO_VERSION: - upload_demo_to_space(demo_name=demo, space_id="gradio/" + demo, hf_token=args.AUTH_TOKEN, gradio_version=args.GRADIO_VERSION) + # if hello_world_version != args.GRADIO_VERSION: + upload_demo_to_space(demo_name=demo, space_id="gradio/" + demo, hf_token=args.AUTH_TOKEN, gradio_version=args.GRADIO_VERSION) 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)