builder / deploy.py
mgbam's picture
Update deploy.py
2541fb7 verified
# deploy.py
import os
import base64
import webbrowser
import urllib.parse
import tempfile
from typing import Optional
import gradio as gr
from huggingface_hub import HfApi, duplicate_space
# ------------------------------------------------------------------
# Utilities for live-preview sandbox in the Gradio UI
# ------------------------------------------------------------------
def send_to_sandbox(code: str) -> str:
wrapped = f"""
<!DOCTYPE html>
<html>
<head><meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head>
<body>{code}</body>
</html>
"""
b64 = base64.b64encode(wrapped.encode("utf-8")).decode("utf-8")
return (
f'<iframe src="data:text/html;base64,{b64}" '
'width="100%" height="920px" sandbox="allow-scripts allow-same-origin '
'allow-forms allow-popups allow-modals allow-presentation" allow="display-capture"></iframe>'
)
def demo_card_click(e: gr.EventData) -> str:
try:
idx = (
e._data.get("index")
or e._data.get("component", {}).get("index")
or e._data.get("target", {}).get("index")
)
except Exception:
idx = 0
from constants import DEMO_LIST
if not (0 <= idx < len(DEMO_LIST)):
idx = 0
return DEMO_LIST[idx]["description"]
# ------------------------------------------------------------------
# Functions for importing existing Hugging Face Spaces projects
# ------------------------------------------------------------------
def wrap_html_in_gradio_app(html_code: str) -> str:
safe = html_code.replace('"""', r'\"\"\"')
return (
"import gradio as gr\n\n"
"def show_html():\n"
f' return """{safe}"""\n\n'
"demo = gr.Interface(fn=show_html, inputs=None, outputs=gr.HTML())\n\n"
"if __name__ == '__main__':\n"
" demo.launch()\n"
)
def deploy_to_spaces(code: str) -> None:
if not code.strip():
return
app_py = wrap_html_in_gradio_app(code)
params = urllib.parse.urlencode({
"name": "new-space",
"sdk": "gradio"
})
files = {
"files[0][path]": "app.py",
"files[0][content]": app_py
}
files_params = urllib.parse.urlencode(files)
url = f"https://huggingface.co/new-space?{params}&{files_params}"
webbrowser.open_new_tab(url)
def wrap_html_in_static_app(html_code: str) -> str:
return html_code
def deploy_to_spaces_static(code: str) -> None:
if not code.strip():
return
html = wrap_html_in_static_app(code)
params = urllib.parse.urlencode({
"name": "new-space",
"sdk": "static"
})
files = {
"files[0][path]": "index.html",
"files[0][content]": html
}
files_params = urllib.parse.urlencode(files)
url = f"https://huggingface.co/new-space?{params}&{files_params}"
webbrowser.open_new_tab(url)
# ------------------------------------------------------------------
# Functions for loading and updating user Spaces
# ------------------------------------------------------------------
def check_hf_space_url(url: str):
import re
pattern = re.compile(r'^(?:https?://)?(?:huggingface\.co|hf\.co)/spaces/([\w-]+)/([\w-]+)$', re.IGNORECASE)
m = pattern.match(url.strip())
if not m:
return False, None, None
return True, m.group(1), m.group(2)
def fetch_hf_space_content(username: str, project: str) -> str:
api = HfApi()
path = api.hf_hub_download(repo_id=f"{username}/{project}", filename="app.py", repo_type="space")
with open(path, "r", encoding="utf-8") as f:
return f.read()
def load_project_from_url(url: str):
valid, user, proj = check_hf_space_url(url)
if not valid:
return "Error: Invalid Hugging Face Space URL.", ""
try:
content = fetch_hf_space_content(user, proj)
return f"✅ Imported {user}/{proj}", content
except Exception as e:
return f"Error fetching project: {e}", ""
def handle_load_project(url: str):
status_message, content = load_project_from_url(url)
return status_message, content, "", url, [], []
def deploy_to_user_space(code, space_name, sdk_choice, profile: Optional[gr.OAuthProfile] = None, token: Optional[gr.OAuthToken] = None):
if not profile or not token or not token.token or token.token.startswith("hf_"):
return gr.update(value="Please log in with a valid Hugging Face write token.", visible=True)
api = HfApi(token=token.token)
repo_id = f"{profile.username}/{space_name.strip()}"
api.create_repo(repo_id=repo_id, repo_type="space", space_sdk=sdk_choice, exist_ok=True)
with tempfile.NamedTemporaryFile("w", delete=False) as f:
f.write(code)
api.upload_file(path_or_fileobj=f.name, path_in_repo="app.py", repo_id=repo_id, repo_type="space")
os.unlink(f.name)
return gr.update(value=f"✅ Deployed to https://huggingface.co/spaces/{repo_id}", visible=True)