-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Push to spaces #4033
Merged
Merged
Push to spaces #4033
Changes from all commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
616d259
changes
aliabid94 23e27ec
first commit
aliabid94 ebc5307
Update gradio/upload.py
aliabid94 17f4550
Update gradio/upload.py
aliabid94 9679c51
changes
aliabid94 b6bd159
Merge branch 'main' into push_to_spaces
aliabid94 8280da5
changes
aliabid94 482b580
Merge branch 'push_to_spaces' of https://github.com/gradio-app/gradio…
aliabid94 f19e150
changes
aliabid94 05bca13
changes
aliabid94 ed8147f
changes
aliabid94 f7858b9
changes
aliabid94 bf326a6
changes
aliabid94 17d5fff
chnages
aliabid94 bc1ff14
changes
aliabid94 c28867a
changes
aliabid94 dc6996d
changes
aliabid94 e1fa083
merge conflict
aliabid94 a64d34d
changes
aliabid94 188a869
chnages
aliabid94 1b43bb3
Update 03_sharing-your-app.md
aliabid94 d90815e
changes
aliabid94 764a173
Merge branch 'push_to_spaces' of https://github.com/gradio-app/gradio…
aliabid94 507a2aa
Merge branch 'main' into push_to_spaces
aliabid94 1b9d13c
Merge branch 'main' into push_to_spaces
aliabid94 65cfa57
Merge branch 'main' into push_to_spaces
abidlabs 2aea05a
changes
aliabid94 a628ada
Merge branch 'main' into push_to_spaces
aliabid94 3b37451
merge
aliabid94 b6cd32d
change
aliabid94 355d9eb
changes
aliabid94 4403d5d
changes
aliabid94 11dbc38
changes
aliabid94 58e1f6b
changes
aliabid94 615ce72
Merge branch 'main' into push_to_spaces
abidlabs c3ea7d8
Update gradio/deploy_space.py
aliabid94 397d4a1
changes
aliabid94 efec556
Merge remote-tracking branch 'origin' into push_to_spaces
aliabid94 839d17a
changes
aliabid94 19ae624
changes
aliabid94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import sys | ||
|
||
import gradio.deploy_space | ||
import gradio.reload | ||
|
||
|
||
def cli(): | ||
args = sys.argv[1:] | ||
if len(args) == 0: | ||
raise ValueError("No file specified.") | ||
if args[0] == "deploy": | ||
gradio.deploy_space.deploy() | ||
else: | ||
gradio.reload.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import os | ||
import re | ||
|
||
import huggingface_hub | ||
|
||
import gradio as gr | ||
|
||
repo_directory = os.getcwd() | ||
readme_file = os.path.join(repo_directory, "README.md") | ||
github_action_template = os.path.join( | ||
os.path.dirname(__file__), "deploy_space_action.yaml" | ||
) | ||
|
||
|
||
def add_configuration_to_readme( | ||
title: str | None, | ||
app_file: str | None, | ||
) -> dict: | ||
configuration = {} | ||
|
||
dir_name = os.path.basename(repo_directory) | ||
if title is None: | ||
title = input(f"Enter Spaces app title [{dir_name}]: ") or dir_name | ||
formatted_title = format_title(title) | ||
if formatted_title != title: | ||
print(f"Formatted to {formatted_title}. ") | ||
configuration["title"] = formatted_title | ||
|
||
if app_file is None: | ||
for file in os.listdir(repo_directory): | ||
file_path = os.path.join(repo_directory, file) | ||
if not os.path.isfile(file_path) or not file.endswith(".py"): | ||
continue | ||
|
||
with open(file_path, encoding="utf-8", errors="ignore") as f: | ||
content = f.read() | ||
if "import gradio" in content: | ||
app_file = file | ||
break | ||
|
||
app_file = ( | ||
input(f"Enter Gradio app file {f'[{app_file}]' if app_file else ''}: ") | ||
or app_file | ||
) | ||
if not app_file or not os.path.exists(app_file): | ||
raise FileNotFoundError("Failed to find Gradio app file.") | ||
configuration["app_file"] = app_file | ||
|
||
configuration["sdk"] = "gradio" | ||
configuration["sdk_version"] = gr.__version__ | ||
huggingface_hub.metadata_save(readme_file, configuration) | ||
|
||
configuration["hardware"] = ( | ||
aliabid94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input( | ||
f"Enter Spaces hardware ({', '.join(hardware.value for hardware in huggingface_hub.SpaceHardware)}) [cpu-basic]: " | ||
) | ||
or "cpu-basic" | ||
) | ||
|
||
secrets = {} | ||
if input("Any Spaces secrets (y/n) [n]: ") == "y": | ||
while True: | ||
secret_name = input("Enter secret name (leave blank to end): ") | ||
if not secret_name: | ||
break | ||
secret_value = input(f"Enter secret value for {secret_name}: ") | ||
secrets[secret_name] = secret_value | ||
configuration["secrets"] = secrets | ||
|
||
requirements_file = os.path.join(repo_directory, "requirements.txt") | ||
if ( | ||
not os.path.exists(requirements_file) | ||
and input("Create requirements.txt file? (y/n) [n]: ").lower() == "y" | ||
): | ||
while True: | ||
requirement = input("Enter a dependency (leave blank to end): ") | ||
if not requirement: | ||
break | ||
with open(requirements_file, "a") as f: | ||
f.write(requirement + "\n") | ||
|
||
if ( | ||
input( | ||
"Create Github Action to automatically update Space on 'git push'? [n]: " | ||
).lower() | ||
== "y" | ||
): | ||
track_branch = input("Enter branch to track [main]: ") or "main" | ||
github_action_file = os.path.join( | ||
repo_directory, ".github/workflows/update_space.yml" | ||
) | ||
os.makedirs(os.path.dirname(github_action_file), exist_ok=True) | ||
with open(github_action_template) as f: | ||
github_action_content = f.read() | ||
github_action_content = github_action_content.replace("$branch", track_branch) | ||
with open(github_action_file, "w") as f: | ||
f.write(github_action_content) | ||
|
||
print( | ||
"Github Action created. Add your Hugging Face write token (from https://huggingface.co/settings/tokens) as an Actions Secret named 'hf_token' to your GitHub repository. This can be set in your repository's settings page." | ||
) | ||
|
||
return configuration | ||
|
||
|
||
def format_title(title: str): | ||
title = title.replace(" ", "_") | ||
title = re.sub(r"[^a-zA-Z0-9\-._]", "", title) | ||
title = re.sub("-+", "-", title) | ||
while title.startswith("."): | ||
title = title[1:] | ||
return title | ||
|
||
|
||
def deploy(): | ||
if ( | ||
os.getenv("SYSTEM") == "spaces" | ||
): # in case a repo with this function is uploaded to spaces | ||
return | ||
parser = argparse.ArgumentParser(description="Deploy to Spaces") | ||
parser.add_argument("deploy") | ||
parser.add_argument("--title", type=str, help="Spaces app title") | ||
parser.add_argument("--app-file", type=str, help="File containing the Gradio app") | ||
|
||
args = parser.parse_args() | ||
|
||
hf_api = huggingface_hub.HfApi() | ||
whoami = None | ||
login = False | ||
try: | ||
whoami = hf_api.whoami() | ||
if whoami["auth"]["accessToken"]["role"] != "write": | ||
login = True | ||
except OSError: | ||
login = True | ||
if login: | ||
print("Need 'write' access token to create a Spaces repo.") | ||
huggingface_hub.login(add_to_git_credential=False) | ||
whoami = hf_api.whoami() | ||
|
||
configuration: None | dict = None | ||
if os.path.exists(readme_file): | ||
try: | ||
configuration = huggingface_hub.metadata_load(readme_file) | ||
except ValueError: | ||
pass | ||
|
||
if configuration is None: | ||
print( | ||
f"Creating new Spaces Repo in '{repo_directory}'. Collecting metadata, press Enter to accept default value." | ||
) | ||
configuration = add_configuration_to_readme( | ||
args.title, | ||
args.app_file, | ||
) | ||
|
||
space_id = huggingface_hub.create_repo( | ||
configuration["title"], | ||
space_sdk="gradio", | ||
repo_type="space", | ||
exist_ok=True, | ||
space_hardware=configuration.get("hardware"), | ||
).repo_id | ||
hf_api.upload_folder( | ||
repo_id=space_id, | ||
repo_type="space", | ||
folder_path=repo_directory, | ||
) | ||
if configuration.get("secrets"): | ||
for secret_name, secret_value in configuration["secrets"].items(): | ||
huggingface_hub.add_space_secret(space_id, secret_name, secret_value) | ||
print(f"Space available at https://huggingface.co/spaces/{space_id}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Run Python script | ||
|
||
on: | ||
push: | ||
branches: | ||
- $branch | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.9' | ||
|
||
- name: Install Gradio | ||
run: python -m pip install gradio | ||
|
||
- name: Log in to Hugging Face | ||
run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")' | ||
|
||
- name: Deploy to Spaces | ||
run: gradio deploy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Throws an error on Windows:
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f916' in position 46: character maps to <undefined>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The huggingface_hub.metadata_save line? Can you file an issue on https://github.com/huggingface/huggingface_hub/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR here: https://github.com/huggingface/huggingface_hub/pull/1484/files
cc @Wauplin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 🙏 Merging it :)