Refactor: production-grade error handling, progress bars, input validation, SDK scoring, Gradio 6 compat
Browse files
app.py
CHANGED
|
@@ -1,178 +1,552 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import re
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def validate_github_url(url):
|
| 11 |
-
"""Validate and normalize GitHub URL."""
|
| 12 |
url = url.strip()
|
| 13 |
-
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
url = url.rstrip("/")
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
parts = github_url.rstrip("/").split("/")
|
| 25 |
-
if len(parts) >= 5:
|
| 26 |
-
return parts[3], parts[4].replace(".git", "")
|
| 27 |
-
return None, None
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
-
def detect_sdk(tmpdir):
|
| 31 |
-
"""Auto-detect the best SDK for the project."""
|
| 32 |
-
files = os.listdir(tmpdir)
|
| 33 |
-
filenames_lower = [f.lower() for f in files]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return "static"
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
lines = []
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
if level > 0:
|
|
|
|
| 82 |
lines.append(f"{indent}π {dirname}/")
|
| 83 |
-
for f in sorted(files):
|
| 84 |
-
if count >= max_files:
|
| 85 |
-
lines.append(f"\n... and more files (showing first {max_files})")
|
| 86 |
-
return "\n".join(lines)
|
| 87 |
-
file_indent = " " * (level + 1)
|
| 88 |
-
fpath = os.path.join(root, f)
|
| 89 |
-
size = os.path.getsize(fpath)
|
| 90 |
-
size_str = f"{size / 1024:.1f}KB" if size > 1024 else f"{size}B"
|
| 91 |
-
lines.append(f"{file_indent}π {f} ({size_str})")
|
| 92 |
-
count += 1
|
| 93 |
-
return "\n".join(lines) if lines else "(empty repository)"
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def import_github_to_hf(github_url, hf_space_id, sdk_choice, hf_token, private, branch):
|
| 97 |
-
"""Clone a GitHub repo and push it to a Hugging Face Space."""
|
| 98 |
-
# Validate inputs
|
| 99 |
-
if not github_url or not github_url.strip():
|
| 100 |
-
yield "β Please enter a GitHub repository URL.", ""
|
| 101 |
-
return
|
| 102 |
-
|
| 103 |
-
github_url = validate_github_url(github_url)
|
| 104 |
-
if github_url is None:
|
| 105 |
-
yield "β Invalid GitHub URL. Expected format: `https://github.com/owner/repo`", ""
|
| 106 |
-
return
|
| 107 |
-
|
| 108 |
-
if not hf_token or not hf_token.strip():
|
| 109 |
-
yield "β Please enter your Hugging Face token.", ""
|
| 110 |
-
return
|
| 111 |
-
|
| 112 |
-
owner, repo_name = extract_repo_name(github_url)
|
| 113 |
-
if not owner or not repo_name:
|
| 114 |
-
yield "β Could not parse owner/repo from URL.", ""
|
| 115 |
-
return
|
| 116 |
-
|
| 117 |
-
# Default space name
|
| 118 |
-
if not hf_space_id or not hf_space_id.strip():
|
| 119 |
-
try:
|
| 120 |
-
api = HfApi(token=hf_token.strip())
|
| 121 |
-
user_info = api.whoami()
|
| 122 |
-
username = user_info.get("name", user_info.get("user", "user"))
|
| 123 |
-
hf_space_id = f"{username}/{repo_name}"
|
| 124 |
-
except Exception as e:
|
| 125 |
-
yield f"β Could not determine your HF username: {e}", ""
|
| 126 |
-
return
|
| 127 |
-
|
| 128 |
-
hf_space_id = hf_space_id.strip()
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
-
# Clone
|
| 133 |
-
tmpdir = tempfile.mkdtemp()
|
| 134 |
try:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
clone_cmd += [github_url, tmpdir]
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
if result.returncode != 0:
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
# Remove .git directory
|
| 149 |
git_dir = os.path.join(tmpdir, ".git")
|
| 150 |
-
if os.path.
|
| 151 |
-
shutil.rmtree(git_dir)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
-
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
#
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
sdk_to_use = detected_sdk
|
| 162 |
-
sdk_msg = f"Auto-detected
|
| 163 |
else:
|
| 164 |
sdk_to_use = sdk_choice
|
| 165 |
-
sdk_msg = f"Using selected: **{sdk_choice}**"
|
| 166 |
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
)
|
|
|
|
| 173 |
|
| 174 |
-
|
| 175 |
-
api = HfApi(token=hf_token.strip())
|
| 176 |
try:
|
| 177 |
api.create_repo(
|
| 178 |
repo_id=hf_space_id,
|
|
@@ -181,136 +555,266 @@ def import_github_to_hf(github_url, hf_space_id, sdk_choice, hf_token, private,
|
|
| 181 |
private=private,
|
| 182 |
exist_ok=True,
|
| 183 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
except Exception as e:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
# Upload
|
| 197 |
try:
|
| 198 |
api.upload_folder(
|
| 199 |
folder_path=tmpdir,
|
| 200 |
repo_id=hf_space_id,
|
| 201 |
repo_type="space",
|
| 202 |
commit_message=f"Import from {github_url}",
|
| 203 |
-
ignore_patterns=
|
| 204 |
-
"*.pyc", "__pycache__/", ".git/", ".gitignore",
|
| 205 |
-
".env", ".env.*", "*.log", ".DS_Store", "node_modules/",
|
| 206 |
-
],
|
| 207 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
except Exception as e:
|
| 209 |
-
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
|
| 212 |
space_url = f"https://huggingface.co/spaces/{hf_space_id}"
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
)
|
| 225 |
|
| 226 |
-
except
|
| 227 |
-
|
|
|
|
|
|
|
| 228 |
except Exception as e:
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
|
| 235 |
-
# βββ Gradio UI βββββββββββββββββββββββββββββββββββββββββββ
|
| 236 |
|
|
|
|
|
|
|
|
|
|
| 237 |
with gr.Blocks(
|
| 238 |
title="π GitHub β HF Spaces Importer",
|
| 239 |
-
theme=gr.themes.Soft(),
|
| 240 |
) as demo:
|
| 241 |
gr.Markdown("""
|
| 242 |
# π GitHub β Hugging Face Spaces Importer
|
| 243 |
|
| 244 |
-
Import any public GitHub repository directly into a Hugging Face Space.
|
| 245 |
-
The tool clones the repo, auto-detects the
|
| 246 |
""")
|
| 247 |
|
| 248 |
-
with gr.
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
with gr.Row():
|
| 278 |
with gr.Column(scale=2):
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
placeholder="hf_...",
|
| 283 |
-
info="Needs write access. Get one at huggingface.co/settings/tokens",
|
| 284 |
)
|
| 285 |
with gr.Column(scale=1):
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
)
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
import_btn.click(
|
| 310 |
fn=import_github_to_hf,
|
| 311 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
outputs=[status_output, files_output],
|
|
|
|
|
|
|
|
|
|
| 313 |
)
|
| 314 |
|
|
|
|
|
|
|
| 315 |
if __name__ == "__main__":
|
| 316 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
π GitHub β Hugging Face Spaces Importer β Production-Grade
|
| 3 |
+
|
| 4 |
+
Features:
|
| 5 |
+
- Import any public GitHub repo into an HF Space with one click
|
| 6 |
+
- Auto-detect SDK (Gradio, Streamlit, Docker, Static) by scanning project structure
|
| 7 |
+
- Smart validation of all inputs before any network calls
|
| 8 |
+
- Streaming progress with step-by-step status updates
|
| 9 |
+
- Robust cleanup of temp files even on failure
|
| 10 |
+
- Token format validation and permission checking
|
| 11 |
+
- Branch validation and fallback
|
| 12 |
+
- Concurrency-limited to prevent abuse
|
| 13 |
+
- Detailed file tree preview with size calculations
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import logging
|
| 19 |
import os
|
| 20 |
import re
|
| 21 |
+
import shutil
|
| 22 |
+
import subprocess
|
| 23 |
+
import tempfile
|
| 24 |
+
import traceback
|
| 25 |
+
from dataclasses import dataclass
|
| 26 |
+
from enum import Enum
|
| 27 |
+
from typing import Optional, Generator
|
| 28 |
|
| 29 |
+
import gradio as gr
|
| 30 |
+
from huggingface_hub import HfApi
|
| 31 |
+
from huggingface_hub.utils import (
|
| 32 |
+
HfHubHTTPError,
|
| 33 |
+
RepositoryNotFoundError,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
+
# Configuration
|
| 38 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 39 |
+
CLONE_TIMEOUT_SECONDS = 180
|
| 40 |
+
MAX_REPO_SIZE_MB = 500
|
| 41 |
+
MAX_FILES_TO_UPLOAD = 5_000
|
| 42 |
+
CONCURRENCY_LIMIT = 2
|
| 43 |
+
|
| 44 |
+
UPLOAD_IGNORE_PATTERNS = [
|
| 45 |
+
"*.pyc", "__pycache__/", ".git/", ".gitmodules",
|
| 46 |
+
".env", ".env.*", "*.log",
|
| 47 |
+
".DS_Store", "Thumbs.db", "desktop.ini",
|
| 48 |
+
"node_modules/", ".venv/", "venv/", "env/",
|
| 49 |
+
".tox/", ".nox/", ".mypy_cache/", ".pytest_cache/",
|
| 50 |
+
"*.egg-info/", "dist/", "build/",
|
| 51 |
+
".idea/", ".vscode/", "*.swp", "*.swo", "*~",
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 55 |
+
# Logging
|
| 56 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 57 |
+
logger = logging.getLogger("github_importer")
|
| 58 |
+
logging.basicConfig(
|
| 59 |
+
level=logging.INFO,
|
| 60 |
+
format="%(asctime)s | %(levelname)s | %(message)s",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 65 |
+
# Data types
|
| 66 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 67 |
+
class SDK(str, Enum):
|
| 68 |
+
GRADIO = "gradio"
|
| 69 |
+
STREAMLIT = "streamlit"
|
| 70 |
+
DOCKER = "docker"
|
| 71 |
+
STATIC = "static"
|
| 72 |
+
AUTO = "auto-detect"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@dataclass
|
| 76 |
+
class ImportResult:
|
| 77 |
+
success: bool
|
| 78 |
+
space_url: str = ""
|
| 79 |
+
sdk_used: str = ""
|
| 80 |
+
file_count: int = 0
|
| 81 |
+
total_size: str = ""
|
| 82 |
+
error: Optional[str] = None
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 86 |
+
# Input validators
|
| 87 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 88 |
+
GITHUB_URL_PATTERN = re.compile(
|
| 89 |
+
r"^https?://github\.com/"
|
| 90 |
+
r"(?P<owner>[a-zA-Z0-9\-_.]+)/"
|
| 91 |
+
r"(?P<repo>[a-zA-Z0-9\-_.]+)"
|
| 92 |
+
r"(/.*)?$"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
HF_SPACE_ID_PATTERN = re.compile(
|
| 96 |
+
r"^[a-zA-Z0-9\-_.]+/[a-zA-Z0-9\-_.]+$"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
def validate_github_url(url: str) -> tuple[str, str, str]:
|
| 101 |
+
"""Validate and normalize a GitHub URL. Returns (clean_url, owner, repo_name)."""
|
| 102 |
+
if not url or not url.strip():
|
| 103 |
+
raise gr.Error("π Please enter a GitHub repository URL.")
|
| 104 |
|
|
|
|
|
|
|
| 105 |
url = url.strip()
|
| 106 |
+
|
| 107 |
+
if url.startswith("git@github.com:"):
|
| 108 |
+
raise gr.Error(
|
| 109 |
+
"π SSH URLs are not supported. Please use the HTTPS URL instead.\n"
|
| 110 |
+
f"Try: https://github.com/{url.split(':')[1].replace('.git', '')}"
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
if not url.startswith(("http://", "https://")):
|
| 114 |
+
if "/" in url and " " not in url:
|
| 115 |
+
url = f"https://github.com/{url}"
|
| 116 |
+
gr.Info(f"Auto-prepended https://github.com/ β {url}")
|
| 117 |
+
else:
|
| 118 |
+
raise gr.Error(
|
| 119 |
+
"π Invalid URL format. Expected: https://github.com/owner/repo"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
url = url.rstrip("/")
|
| 123 |
+
url = re.sub(r"\.git$", "", url)
|
| 124 |
+
url = re.sub(r"/(tree|blob|commits|pull|issues|releases|actions|wiki)(/.*)?$", "", url)
|
| 125 |
+
|
| 126 |
+
match = GITHUB_URL_PATTERN.match(url)
|
| 127 |
+
if not match:
|
| 128 |
+
raise gr.Error(
|
| 129 |
+
"π Could not parse GitHub URL. Expected format:\n"
|
| 130 |
+
"`https://github.com/owner/repository`\n\n"
|
| 131 |
+
f"Got: `{url}`"
|
| 132 |
+
)
|
| 133 |
|
| 134 |
+
owner = match.group("owner")
|
| 135 |
+
repo_name = match.group("repo")
|
| 136 |
|
| 137 |
+
if len(repo_name) > 100:
|
| 138 |
+
raise gr.Error("π Repository name seems unusually long. Please verify the URL.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
+
return url, owner, repo_name
|
| 141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
def validate_hf_token(token: str) -> str:
|
| 144 |
+
"""Validate HF token format and permissions. Returns cleaned token."""
|
| 145 |
+
if not token or not token.strip():
|
| 146 |
+
raise gr.Error(
|
| 147 |
+
"π Please enter your Hugging Face token.\n\n"
|
| 148 |
+
"Get one at: https://huggingface.co/settings/tokens\n"
|
| 149 |
+
"Make sure it has **write** permissions."
|
| 150 |
+
)
|
| 151 |
|
| 152 |
+
token = token.strip()
|
| 153 |
+
|
| 154 |
+
if not token.startswith("hf_"):
|
| 155 |
+
raise gr.Error(
|
| 156 |
+
"π Invalid token format. HF tokens start with `hf_`.\n\n"
|
| 157 |
+
"Get a valid token at: https://huggingface.co/settings/tokens"
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
if len(token) < 10:
|
| 161 |
+
raise gr.Error("π Token is too short. Please paste the full token.")
|
| 162 |
+
|
| 163 |
+
try:
|
| 164 |
+
api = HfApi(token=token)
|
| 165 |
+
user_info = api.whoami()
|
| 166 |
+
except Exception as e:
|
| 167 |
+
error_msg = str(e).lower()
|
| 168 |
+
if "401" in error_msg or "unauthorized" in error_msg or "invalid" in error_msg:
|
| 169 |
+
raise gr.Error(
|
| 170 |
+
"π Token is invalid or expired. Please generate a new token at:\n"
|
| 171 |
+
"https://huggingface.co/settings/tokens"
|
| 172 |
+
)
|
| 173 |
+
raise gr.Error(f"π Could not verify token: {type(e).__name__}: {e}")
|
| 174 |
+
|
| 175 |
+
username = user_info.get("name") or user_info.get("user") or ""
|
| 176 |
+
if not username:
|
| 177 |
+
raise gr.Error("π Could not determine your username from the token.")
|
| 178 |
+
|
| 179 |
+
return token
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def validate_space_id(space_id: str, repo_name: str, token: str) -> str:
|
| 183 |
+
"""Validate or auto-generate the HF Space ID."""
|
| 184 |
+
if space_id and space_id.strip():
|
| 185 |
+
space_id = space_id.strip()
|
| 186 |
+
if not HF_SPACE_ID_PATTERN.match(space_id):
|
| 187 |
+
raise gr.Error(
|
| 188 |
+
"π Invalid Space ID format. Expected: `username/space-name`\n\n"
|
| 189 |
+
"- Only letters, numbers, hyphens, underscores, and dots are allowed\n"
|
| 190 |
+
f"- Got: `{space_id}`"
|
| 191 |
+
)
|
| 192 |
+
return space_id
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
api = HfApi(token=token)
|
| 196 |
+
user_info = api.whoami()
|
| 197 |
+
username = user_info.get("name") or user_info.get("user") or "user"
|
| 198 |
+
except Exception as e:
|
| 199 |
+
raise gr.Error(f"Could not determine your HF username for auto-naming: {e}")
|
| 200 |
+
|
| 201 |
+
safe_name = re.sub(r"[^a-zA-Z0-9\-_.]", "-", repo_name)
|
| 202 |
+
safe_name = re.sub(r"-+", "-", safe_name).strip("-")
|
| 203 |
+
if not safe_name:
|
| 204 |
+
safe_name = "imported-repo"
|
| 205 |
+
|
| 206 |
+
auto_id = f"{username}/{safe_name}"
|
| 207 |
+
gr.Info(f"Auto-generated Space ID: **{auto_id}**")
|
| 208 |
+
return auto_id
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
def validate_branch(branch: str) -> Optional[str]:
|
| 212 |
+
"""Validate branch name. Returns cleaned branch or None."""
|
| 213 |
+
if not branch or not branch.strip():
|
| 214 |
+
return None
|
| 215 |
+
|
| 216 |
+
branch = branch.strip()
|
| 217 |
|
| 218 |
+
if ".." in branch or branch.startswith("/") or branch.endswith("/"):
|
| 219 |
+
raise gr.Error(f"πΏ Invalid branch name: `{branch}`")
|
|
|
|
| 220 |
|
| 221 |
+
if len(branch) > 250:
|
| 222 |
+
raise gr.Error("πΏ Branch name too long.")
|
| 223 |
|
| 224 |
+
if re.search(r'[;&|`$(){}[\]<>!]', branch):
|
| 225 |
+
raise gr.Error(f"πΏ Branch name contains invalid characters: `{branch}`")
|
| 226 |
|
| 227 |
+
return branch
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 231 |
+
# SDK detection
|
| 232 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 233 |
+
def detect_sdk(project_dir: str) -> tuple[str, str]:
|
| 234 |
+
"""Auto-detect SDK by examining project files. Returns (sdk_name, reason)."""
|
| 235 |
+
files_at_root = set(os.listdir(project_dir))
|
| 236 |
+
files_at_root_lower = {f.lower() for f in files_at_root}
|
| 237 |
+
|
| 238 |
+
if "Dockerfile" in files_at_root or "dockerfile" in files_at_root_lower:
|
| 239 |
+
return SDK.DOCKER.value, "Found Dockerfile in project root"
|
| 240 |
+
|
| 241 |
+
gradio_score = 0
|
| 242 |
+
streamlit_score = 0
|
| 243 |
+
scanned_files = 0
|
| 244 |
+
scan_errors = 0
|
| 245 |
+
|
| 246 |
+
for root, dirs, fnames in os.walk(project_dir):
|
| 247 |
+
dirs[:] = [
|
| 248 |
+
d for d in dirs
|
| 249 |
+
if not d.startswith(".") and d not in {
|
| 250 |
+
"node_modules", "__pycache__", ".git", "venv", ".venv",
|
| 251 |
+
"env", ".tox", ".nox", "dist", "build",
|
| 252 |
+
}
|
| 253 |
+
]
|
| 254 |
+
|
| 255 |
+
for fname in fnames:
|
| 256 |
+
if not fname.endswith(".py"):
|
| 257 |
+
continue
|
| 258 |
+
|
| 259 |
+
scanned_files += 1
|
| 260 |
+
fpath = os.path.join(root, fname)
|
| 261 |
+
try:
|
| 262 |
+
with open(fpath, "r", errors="replace") as f:
|
| 263 |
+
content = f.read(50_000)
|
| 264 |
+
|
| 265 |
+
if "import gradio" in content or "from gradio" in content:
|
| 266 |
+
gradio_score += 2
|
| 267 |
+
if "gr.Blocks" in content or "gr.Interface" in content:
|
| 268 |
+
gradio_score += 3
|
| 269 |
+
if ".launch(" in content:
|
| 270 |
+
gradio_score += 1
|
| 271 |
+
|
| 272 |
+
if "import streamlit" in content or "from streamlit" in content:
|
| 273 |
+
streamlit_score += 2
|
| 274 |
+
if "st.title" in content or "st.write" in content:
|
| 275 |
+
streamlit_score += 3
|
| 276 |
+
|
| 277 |
+
except PermissionError:
|
| 278 |
+
scan_errors += 1
|
| 279 |
+
except Exception as e:
|
| 280 |
+
scan_errors += 1
|
| 281 |
+
logger.debug(f"SDK scan error on {fpath}: {e}")
|
| 282 |
+
|
| 283 |
+
if gradio_score > 0 and gradio_score >= streamlit_score:
|
| 284 |
+
return SDK.GRADIO.value, f"Detected Gradio imports (score: {gradio_score}, scanned {scanned_files} .py files)"
|
| 285 |
+
if streamlit_score > 0:
|
| 286 |
+
return SDK.STREAMLIT.value, f"Detected Streamlit imports (score: {streamlit_score}, scanned {scanned_files} .py files)"
|
| 287 |
+
|
| 288 |
+
for req_file in ["requirements.txt", "pyproject.toml", "setup.cfg", "setup.py"]:
|
| 289 |
+
req_path = os.path.join(project_dir, req_file)
|
| 290 |
+
if os.path.exists(req_path):
|
| 291 |
+
try:
|
| 292 |
+
with open(req_path, "r", errors="replace") as f:
|
| 293 |
+
content = f.read().lower()
|
| 294 |
+
if "gradio" in content:
|
| 295 |
+
return SDK.GRADIO.value, f"Found 'gradio' in {req_file}"
|
| 296 |
+
if "streamlit" in content:
|
| 297 |
+
return SDK.STREAMLIT.value, f"Found 'streamlit' in {req_file}"
|
| 298 |
+
except Exception:
|
| 299 |
+
pass
|
| 300 |
+
|
| 301 |
+
if "index.html" in files_at_root_lower:
|
| 302 |
+
return SDK.STATIC.value, "Found index.html in project root"
|
| 303 |
+
|
| 304 |
+
return SDK.GRADIO.value, f"No framework detected (scanned {scanned_files} .py files) β defaulting to Gradio"
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 308 |
+
# File tree builder
|
| 309 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 310 |
+
def build_file_tree(project_dir: str, max_files: int = 80) -> tuple[str, int, int]:
|
| 311 |
+
"""Build a visual file tree. Returns (tree_string, file_count, total_size_bytes)."""
|
| 312 |
lines = []
|
| 313 |
+
file_count = 0
|
| 314 |
+
total_size = 0
|
| 315 |
+
truncated = False
|
| 316 |
+
|
| 317 |
+
for root, dirs, files in os.walk(project_dir):
|
| 318 |
+
dirs[:] = sorted(d for d in dirs if not d.startswith(".") and d not in {
|
| 319 |
+
"node_modules", "__pycache__", ".git", "venv", ".venv", ".tox",
|
| 320 |
+
})
|
| 321 |
+
files = sorted(files)
|
| 322 |
+
|
| 323 |
+
level = root.replace(project_dir, "").count(os.sep)
|
| 324 |
+
indent = "β " * level
|
| 325 |
+
|
| 326 |
if level > 0:
|
| 327 |
+
dirname = os.path.basename(root)
|
| 328 |
lines.append(f"{indent}π {dirname}/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
+
for fname in files:
|
| 331 |
+
if file_count >= max_files:
|
| 332 |
+
truncated = True
|
| 333 |
+
break
|
| 334 |
+
|
| 335 |
+
fpath = os.path.join(root, fname)
|
| 336 |
+
try:
|
| 337 |
+
fsize = os.path.getsize(fpath)
|
| 338 |
+
except OSError:
|
| 339 |
+
fsize = 0
|
| 340 |
+
|
| 341 |
+
total_size += fsize
|
| 342 |
+
file_count += 1
|
| 343 |
+
|
| 344 |
+
file_indent = "β " * (level + 1)
|
| 345 |
+
size_str = format_size(fsize)
|
| 346 |
+
lines.append(f"{file_indent}π {fname} ({size_str})")
|
| 347 |
+
|
| 348 |
+
if truncated:
|
| 349 |
+
break
|
| 350 |
+
|
| 351 |
+
if truncated:
|
| 352 |
+
lines.append(f"\n... and more files (showing first {max_files})")
|
| 353 |
+
|
| 354 |
+
tree = "\n".join(lines) if lines else "(empty repository)"
|
| 355 |
+
return tree, file_count, total_size
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
def format_size(size_bytes: int) -> str:
|
| 359 |
+
if size_bytes < 1024:
|
| 360 |
+
return f"{size_bytes} B"
|
| 361 |
+
elif size_bytes < 1024 ** 2:
|
| 362 |
+
return f"{size_bytes / 1024:.1f} KB"
|
| 363 |
+
elif size_bytes < 1024 ** 3:
|
| 364 |
+
return f"{size_bytes / (1024 ** 2):.1f} MB"
|
| 365 |
+
else:
|
| 366 |
+
return f"{size_bytes / (1024 ** 3):.2f} GB"
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 370 |
+
# Status builder (accumulates steps for the streaming UI)
|
| 371 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 372 |
+
class StatusBuilder:
|
| 373 |
+
"""Accumulates step statuses and renders as markdown."""
|
| 374 |
+
|
| 375 |
+
def __init__(self):
|
| 376 |
+
self.steps: list[tuple[str, str, str]] = []
|
| 377 |
+
|
| 378 |
+
def add(self, emoji: str, label: str, detail: str = ""):
|
| 379 |
+
self.steps.append((emoji, label, detail))
|
| 380 |
+
|
| 381 |
+
def update_last(self, emoji: str, label: str, detail: str = ""):
|
| 382 |
+
if self.steps:
|
| 383 |
+
self.steps[-1] = (emoji, label, detail)
|
| 384 |
+
|
| 385 |
+
def render(self) -> str:
|
| 386 |
+
lines = []
|
| 387 |
+
for emoji, label, detail in self.steps:
|
| 388 |
+
line = f"{emoji} **{label}**"
|
| 389 |
+
if detail:
|
| 390 |
+
line += f" β {detail}"
|
| 391 |
+
lines.append(line)
|
| 392 |
+
return "\n\n".join(lines)
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 396 |
+
# Core import logic (generator for streaming updates)
|
| 397 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 398 |
+
def import_github_to_hf(
|
| 399 |
+
github_url: str,
|
| 400 |
+
hf_space_id: str,
|
| 401 |
+
sdk_choice: str,
|
| 402 |
+
hf_token: str,
|
| 403 |
+
private: bool,
|
| 404 |
+
branch: str,
|
| 405 |
+
progress=gr.Progress(),
|
| 406 |
+
) -> Generator[tuple[str, str], None, None]:
|
| 407 |
+
"""Clone a GitHub repo and push it to a Hugging Face Space."""
|
| 408 |
+
status = StatusBuilder()
|
| 409 |
+
tmpdir: Optional[str] = None
|
| 410 |
|
|
|
|
|
|
|
| 411 |
try:
|
| 412 |
+
# ββ Step 0: Validate all inputs ββββββββββββββββββββββββββββββ
|
| 413 |
+
progress(0.02, desc="Validating inputs...")
|
| 414 |
+
status.add("π", "Validating inputs")
|
| 415 |
+
yield status.render(), ""
|
| 416 |
+
|
| 417 |
+
github_url, owner, repo_name = validate_github_url(github_url)
|
| 418 |
+
hf_token = validate_hf_token(hf_token)
|
| 419 |
+
hf_space_id = validate_space_id(hf_space_id, repo_name, hf_token)
|
| 420 |
+
branch_name = validate_branch(branch)
|
| 421 |
+
|
| 422 |
+
status.update_last("β
", "Inputs validated",
|
| 423 |
+
f"`{owner}/{repo_name}` β `{hf_space_id}`")
|
| 424 |
+
yield status.render(), ""
|
| 425 |
+
|
| 426 |
+
# ββ Step 1: Clone βββββββββββββββββββββββββββββββββββββββββββββ
|
| 427 |
+
progress(0.10, desc="Cloning repository...")
|
| 428 |
+
status.add("π", "Step 1/4: Cloning repository",
|
| 429 |
+
f"`{github_url}`" + (f" (branch: `{branch_name}`)" if branch_name else ""))
|
| 430 |
+
yield status.render(), ""
|
| 431 |
+
|
| 432 |
+
tmpdir = tempfile.mkdtemp(prefix="ghimport_")
|
| 433 |
+
logger.info(f"Cloning {github_url} to {tmpdir}")
|
| 434 |
+
|
| 435 |
+
clone_cmd = ["git", "clone", "--depth=1", "--single-branch"]
|
| 436 |
+
if branch_name:
|
| 437 |
+
clone_cmd += ["-b", branch_name]
|
| 438 |
clone_cmd += [github_url, tmpdir]
|
| 439 |
|
| 440 |
+
try:
|
| 441 |
+
result = subprocess.run(
|
| 442 |
+
clone_cmd,
|
| 443 |
+
capture_output=True,
|
| 444 |
+
text=True,
|
| 445 |
+
timeout=CLONE_TIMEOUT_SECONDS,
|
| 446 |
+
env={**os.environ, "GIT_TERMINAL_PROMPT": "0"},
|
| 447 |
+
)
|
| 448 |
+
except subprocess.TimeoutExpired:
|
| 449 |
+
raise gr.Error(
|
| 450 |
+
f"β° Git clone timed out after {CLONE_TIMEOUT_SECONDS} seconds.\n\n"
|
| 451 |
+
"The repository may be too large or the server may be unreachable.\n"
|
| 452 |
+
"Try cloning a specific branch with fewer files."
|
| 453 |
+
)
|
| 454 |
+
except FileNotFoundError:
|
| 455 |
+
raise gr.Error(
|
| 456 |
+
"π§ `git` is not installed on this server. "
|
| 457 |
+
"This is a server configuration issue."
|
| 458 |
+
)
|
| 459 |
+
except OSError as e:
|
| 460 |
+
raise gr.Error(f"π§ System error during clone: {e}")
|
| 461 |
+
|
| 462 |
if result.returncode != 0:
|
| 463 |
+
stderr = result.stderr.strip()
|
| 464 |
+
logger.error(f"Git clone failed: {stderr}")
|
| 465 |
+
|
| 466 |
+
if "not found" in stderr.lower() or "does not exist" in stderr.lower():
|
| 467 |
+
raise gr.Error(
|
| 468 |
+
f"π Repository not found: `{github_url}`\n\n"
|
| 469 |
+
"- Check the URL for typos\n"
|
| 470 |
+
"- Make sure the repository is **public**\n"
|
| 471 |
+
"- Private repos require GitHub authentication (not supported)"
|
| 472 |
+
)
|
| 473 |
+
elif "could not read" in stderr.lower() and "branch" in stderr.lower():
|
| 474 |
+
raise gr.Error(
|
| 475 |
+
f"πΏ Branch `{branch_name}` not found in `{owner}/{repo_name}`.\n\n"
|
| 476 |
+
"Leave the branch field empty to use the default branch, "
|
| 477 |
+
"or check the branch name on GitHub."
|
| 478 |
+
)
|
| 479 |
+
elif "authentication" in stderr.lower() or "permission" in stderr.lower():
|
| 480 |
+
raise gr.Error(
|
| 481 |
+
f"π Repository requires authentication: `{github_url}`\n\n"
|
| 482 |
+
"This tool only supports **public** GitHub repositories."
|
| 483 |
+
)
|
| 484 |
+
elif "ssl" in stderr.lower() or "certificate" in stderr.lower():
|
| 485 |
+
raise gr.Error(
|
| 486 |
+
"π SSL/TLS error connecting to GitHub. "
|
| 487 |
+
"This is likely a temporary network issue. Please try again."
|
| 488 |
+
)
|
| 489 |
+
else:
|
| 490 |
+
raise gr.Error(
|
| 491 |
+
f"β Git clone failed:\n```\n{stderr[:500]}\n```\n\n"
|
| 492 |
+
"Check the URL and try again."
|
| 493 |
+
)
|
| 494 |
|
|
|
|
| 495 |
git_dir = os.path.join(tmpdir, ".git")
|
| 496 |
+
if os.path.isdir(git_dir):
|
| 497 |
+
shutil.rmtree(git_dir, ignore_errors=True)
|
| 498 |
+
|
| 499 |
+
progress(0.30, desc="Analyzing repository...")
|
| 500 |
+
file_tree, file_count, total_size = build_file_tree(tmpdir)
|
| 501 |
+
total_size_mb = total_size / (1024 ** 2)
|
| 502 |
|
| 503 |
+
if file_count == 0:
|
| 504 |
+
raise gr.Error("β The cloned repository is empty (no files found).")
|
| 505 |
+
|
| 506 |
+
if total_size_mb > MAX_REPO_SIZE_MB:
|
| 507 |
+
raise gr.Error(
|
| 508 |
+
f"π¦ Repository too large: {total_size_mb:.0f} MB "
|
| 509 |
+
f"(limit: {MAX_REPO_SIZE_MB} MB).\n\n"
|
| 510 |
+
"Try a smaller repository or fork with reduced history."
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
if file_count > MAX_FILES_TO_UPLOAD:
|
| 514 |
+
gr.Warning(
|
| 515 |
+
f"π¦ Repository has {file_count} files (limit: {MAX_FILES_TO_UPLOAD}). "
|
| 516 |
+
"Some files may be excluded."
|
| 517 |
+
)
|
| 518 |
+
|
| 519 |
+
files_md = (
|
| 520 |
+
f"### π Repository Files ({file_count} files, {format_size(total_size)})\n"
|
| 521 |
+
f"```\n{file_tree}\n```"
|
| 522 |
+
)
|
| 523 |
|
| 524 |
+
status.update_last("β
", "Step 1/4: Repository cloned",
|
| 525 |
+
f"{file_count} files, {format_size(total_size)}")
|
| 526 |
+
yield status.render(), files_md
|
| 527 |
|
| 528 |
+
# ββ Step 2: Detect SDK ββββββββββββββββββββββββββββββββββββββββ
|
| 529 |
+
progress(0.40, desc="Detecting SDK...")
|
| 530 |
+
status.add("π", "Step 2/4: Detecting SDK")
|
| 531 |
+
yield status.render(), files_md
|
| 532 |
+
|
| 533 |
+
if sdk_choice == SDK.AUTO.value:
|
| 534 |
+
detected_sdk, detection_reason = detect_sdk(tmpdir)
|
| 535 |
sdk_to_use = detected_sdk
|
| 536 |
+
sdk_msg = f"Auto-detected **{detected_sdk}** ({detection_reason})"
|
| 537 |
else:
|
| 538 |
sdk_to_use = sdk_choice
|
| 539 |
+
sdk_msg = f"Using selected SDK: **{sdk_choice}**"
|
| 540 |
|
| 541 |
+
status.update_last("β
", "Step 2/4: SDK determined", sdk_msg)
|
| 542 |
+
yield status.render(), files_md
|
| 543 |
+
|
| 544 |
+
# ββ Step 3: Create Space ββββββββββββββββββββββββββββββββββββββ
|
| 545 |
+
progress(0.50, desc="Creating HF Space...")
|
| 546 |
+
status.add("π", "Step 3/4: Creating Space", f"`{hf_space_id}` ({sdk_to_use})")
|
| 547 |
+
yield status.render(), files_md
|
| 548 |
|
| 549 |
+
api = HfApi(token=hf_token)
|
|
|
|
| 550 |
try:
|
| 551 |
api.create_repo(
|
| 552 |
repo_id=hf_space_id,
|
|
|
|
| 555 |
private=private,
|
| 556 |
exist_ok=True,
|
| 557 |
)
|
| 558 |
+
except HfHubHTTPError as e:
|
| 559 |
+
status_code = getattr(e.response, "status_code", None) if hasattr(e, "response") else None
|
| 560 |
+
if status_code == 403:
|
| 561 |
+
raise gr.Error(
|
| 562 |
+
f"π Permission denied creating `{hf_space_id}`.\n\n"
|
| 563 |
+
"Your token may not have write access, or you may not have "
|
| 564 |
+
"permission to create Spaces in that namespace."
|
| 565 |
+
)
|
| 566 |
+
elif status_code == 409:
|
| 567 |
+
gr.Warning(f"Space `{hf_space_id}` already exists β will overwrite files.")
|
| 568 |
+
else:
|
| 569 |
+
raise gr.Error(f"β Failed to create Space: {e}")
|
| 570 |
except Exception as e:
|
| 571 |
+
logger.error(f"Space creation error: {e}")
|
| 572 |
+
traceback.print_exc()
|
| 573 |
+
raise gr.Error(
|
| 574 |
+
f"β Failed to create Space `{hf_space_id}`:\n"
|
| 575 |
+
f"{type(e).__name__}: {e}"
|
| 576 |
+
)
|
| 577 |
+
|
| 578 |
+
status.update_last("β
", "Step 3/4: Space created",
|
| 579 |
+
f"[{hf_space_id}](https://huggingface.co/spaces/{hf_space_id})")
|
| 580 |
+
yield status.render(), files_md
|
| 581 |
+
|
| 582 |
+
# ββ Step 4: Upload files ββββββββββββββββββββββββββββββββββββββ
|
| 583 |
+
progress(0.60, desc="Uploading files...")
|
| 584 |
+
status.add("π", "Step 4/4: Uploading files",
|
| 585 |
+
f"{file_count} files to `{hf_space_id}`")
|
| 586 |
+
yield status.render(), files_md
|
| 587 |
|
|
|
|
| 588 |
try:
|
| 589 |
api.upload_folder(
|
| 590 |
folder_path=tmpdir,
|
| 591 |
repo_id=hf_space_id,
|
| 592 |
repo_type="space",
|
| 593 |
commit_message=f"Import from {github_url}",
|
| 594 |
+
ignore_patterns=UPLOAD_IGNORE_PATTERNS,
|
|
|
|
|
|
|
|
|
|
| 595 |
)
|
| 596 |
+
except HfHubHTTPError as e:
|
| 597 |
+
status_code = getattr(e.response, "status_code", None) if hasattr(e, "response") else None
|
| 598 |
+
if status_code == 413:
|
| 599 |
+
raise gr.Error(
|
| 600 |
+
"π¦ Upload rejected β files too large for the HF Hub.\n\n"
|
| 601 |
+
"Try a smaller repository or exclude large binary files."
|
| 602 |
+
)
|
| 603 |
+
raise gr.Error(f"β Upload failed: {e}")
|
| 604 |
except Exception as e:
|
| 605 |
+
logger.error(f"Upload error: {e}")
|
| 606 |
+
traceback.print_exc()
|
| 607 |
+
raise gr.Error(
|
| 608 |
+
f"β Failed to upload files:\n{type(e).__name__}: {e}"
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
+
progress(0.95, desc="Finalizing...")
|
| 612 |
|
| 613 |
space_url = f"https://huggingface.co/spaces/{hf_space_id}"
|
| 614 |
|
| 615 |
+
status.update_last("β
", "Step 4/4: Files uploaded")
|
| 616 |
+
|
| 617 |
+
# ββ Success βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 618 |
+
progress(1.0, desc="Import complete!")
|
| 619 |
+
final_status = status.render() + f"""
|
| 620 |
+
|
| 621 |
+
---
|
| 622 |
+
|
| 623 |
+
## β
Import Complete!
|
| 624 |
+
|
| 625 |
+
| Detail | Value |
|
| 626 |
+
|--------|-------|
|
| 627 |
+
| **Source** | [{github_url}]({github_url}) |
|
| 628 |
+
| **Branch** | {branch_name or '(default)'} |
|
| 629 |
+
| **Space** | [{hf_space_id}]({space_url}) |
|
| 630 |
+
| **SDK** | {sdk_to_use} |
|
| 631 |
+
| **Files** | {file_count} |
|
| 632 |
+
| **Size** | {format_size(total_size)} |
|
| 633 |
+
| **Visibility** | {'π Private' if private else 'π Public'} |
|
| 634 |
+
|
| 635 |
+
### π **[Open your Space β]({space_url})**
|
| 636 |
+
|
| 637 |
+
> The Space may take a minute to build. Refresh the link above if it shows "Building".
|
| 638 |
+
"""
|
| 639 |
+
yield final_status, files_md
|
| 640 |
+
gr.Info(f"β
Import complete! Space: {space_url}")
|
| 641 |
+
|
| 642 |
+
except gr.Error:
|
| 643 |
+
raise
|
| 644 |
+
|
| 645 |
+
except MemoryError:
|
| 646 |
+
logger.error("MemoryError during import")
|
| 647 |
+
raise gr.Error(
|
| 648 |
+
"π₯ Out of memory! The repository is too large to process. "
|
| 649 |
+
"Try a smaller repository."
|
| 650 |
)
|
| 651 |
|
| 652 |
+
except KeyboardInterrupt:
|
| 653 |
+
logger.warning("Import interrupted by user")
|
| 654 |
+
raise gr.Error("π Import was interrupted.")
|
| 655 |
+
|
| 656 |
except Exception as e:
|
| 657 |
+
logger.error(f"Unexpected error: {type(e).__name__}: {e}")
|
| 658 |
+
traceback.print_exc()
|
| 659 |
+
raise gr.Error(
|
| 660 |
+
f"π₯ An unexpected error occurred:\n"
|
| 661 |
+
f"{type(e).__name__}: {e}\n\n"
|
| 662 |
+
"If this persists, please report it as a bug."
|
| 663 |
+
)
|
| 664 |
|
| 665 |
+
finally:
|
| 666 |
+
if tmpdir and os.path.exists(tmpdir):
|
| 667 |
+
try:
|
| 668 |
+
shutil.rmtree(tmpdir, ignore_errors=True)
|
| 669 |
+
logger.info(f"Cleaned up temp directory: {tmpdir}")
|
| 670 |
+
except Exception as e:
|
| 671 |
+
logger.warning(f"Cleanup warning: {e}")
|
| 672 |
|
|
|
|
| 673 |
|
| 674 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 675 |
+
# Gradio UI
|
| 676 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββοΏ½οΏ½ββββββββββ
|
| 677 |
with gr.Blocks(
|
| 678 |
title="π GitHub β HF Spaces Importer",
|
|
|
|
| 679 |
) as demo:
|
| 680 |
gr.Markdown("""
|
| 681 |
# π GitHub β Hugging Face Spaces Importer
|
| 682 |
|
| 683 |
+
Import any **public** GitHub repository directly into a Hugging Face Space.
|
| 684 |
+
The tool clones the repo, auto-detects the framework, creates the Space, and uploads all files.
|
| 685 |
""")
|
| 686 |
|
| 687 |
+
with gr.Group():
|
| 688 |
+
gr.Markdown("### π Source Repository")
|
| 689 |
+
with gr.Row():
|
| 690 |
+
with gr.Column(scale=3):
|
| 691 |
+
github_url_input = gr.Textbox(
|
| 692 |
+
label="GitHub Repository URL",
|
| 693 |
+
placeholder="https://github.com/owner/repo",
|
| 694 |
+
info="Public repo URL. Also accepts owner/repo format.",
|
| 695 |
+
max_lines=1,
|
| 696 |
+
)
|
| 697 |
+
with gr.Column(scale=1):
|
| 698 |
+
branch_input = gr.Textbox(
|
| 699 |
+
label="Branch (optional)",
|
| 700 |
+
placeholder="main",
|
| 701 |
+
info="Leave empty for the default branch",
|
| 702 |
+
max_lines=1,
|
| 703 |
+
)
|
| 704 |
+
|
| 705 |
+
with gr.Group():
|
| 706 |
+
gr.Markdown("### π€ Destination Space")
|
| 707 |
+
with gr.Row():
|
| 708 |
+
with gr.Column(scale=3):
|
| 709 |
+
space_id_input = gr.Textbox(
|
| 710 |
+
label="HF Space ID (optional)",
|
| 711 |
+
placeholder="your-username/space-name",
|
| 712 |
+
info="Leave empty to auto-generate from the repo name",
|
| 713 |
+
max_lines=1,
|
| 714 |
+
)
|
| 715 |
+
with gr.Column(scale=1):
|
| 716 |
+
sdk_dropdown = gr.Dropdown(
|
| 717 |
+
choices=[
|
| 718 |
+
("π Auto-detect", "auto-detect"),
|
| 719 |
+
("π Gradio", "gradio"),
|
| 720 |
+
("π΄ Streamlit", "streamlit"),
|
| 721 |
+
("π³ Docker", "docker"),
|
| 722 |
+
("π Static HTML", "static"),
|
| 723 |
+
],
|
| 724 |
+
value="auto-detect",
|
| 725 |
+
label="Space SDK",
|
| 726 |
+
info="Auto-detect scans imports, Dockerfile, and index.html",
|
| 727 |
+
)
|
| 728 |
+
|
| 729 |
+
with gr.Group():
|
| 730 |
+
gr.Markdown("### π Authentication & Options")
|
| 731 |
+
with gr.Row():
|
| 732 |
+
with gr.Column(scale=3):
|
| 733 |
+
token_input = gr.Textbox(
|
| 734 |
+
label="Hugging Face Token",
|
| 735 |
+
type="password",
|
| 736 |
+
placeholder="hf_...",
|
| 737 |
+
info="Needs **write** access Β· [Get a token β](https://huggingface.co/settings/tokens)",
|
| 738 |
+
max_lines=1,
|
| 739 |
+
)
|
| 740 |
+
with gr.Column(scale=1):
|
| 741 |
+
private_checkbox = gr.Checkbox(
|
| 742 |
+
label="π Private Space",
|
| 743 |
+
value=False,
|
| 744 |
+
info="Only you (and your org) can see it",
|
| 745 |
+
)
|
| 746 |
+
|
| 747 |
+
import_btn = gr.Button(
|
| 748 |
+
"π Import to Hugging Face",
|
| 749 |
+
variant="primary",
|
| 750 |
+
size="lg",
|
| 751 |
+
)
|
| 752 |
|
| 753 |
with gr.Row():
|
| 754 |
with gr.Column(scale=2):
|
| 755 |
+
status_output = gr.Markdown(
|
| 756 |
+
value="*Enter a GitHub URL and click Import to get started.*",
|
| 757 |
+
label="Import Status",
|
|
|
|
|
|
|
| 758 |
)
|
| 759 |
with gr.Column(scale=1):
|
| 760 |
+
files_output = gr.Markdown(
|
| 761 |
+
value="",
|
| 762 |
+
label="Repository Files",
|
| 763 |
)
|
| 764 |
|
| 765 |
+
with gr.Accordion("βΉοΈ Notes & Troubleshooting", open=False):
|
| 766 |
+
gr.Markdown(f"""
|
| 767 |
+
### Supported Repositories
|
| 768 |
+
- **Public** GitHub repositories only (private repos require GitHub auth, which is not supported)
|
| 769 |
+
- Maximum repository size: **{MAX_REPO_SIZE_MB} MB** after cloning
|
| 770 |
+
- Clone timeout: **{CLONE_TIMEOUT_SECONDS} seconds**
|
| 771 |
+
|
| 772 |
+
### SDK Auto-Detection
|
| 773 |
+
The auto-detector scans your project in this priority order:
|
| 774 |
+
1. **Dockerfile** in root β Docker
|
| 775 |
+
2. **Python imports** (`import gradio` / `import streamlit`) β matching framework
|
| 776 |
+
3. **requirements.txt / pyproject.toml** β checks for framework dependencies
|
| 777 |
+
4. **index.html** in root β Static
|
| 778 |
+
5. **Default** β Gradio (if nothing detected)
|
| 779 |
+
|
| 780 |
+
### Excluded Files
|
| 781 |
+
These patterns are excluded during upload:
|
| 782 |
+
`{', '.join(UPLOAD_IGNORE_PATTERNS[:10])}`, ...
|
| 783 |
+
|
| 784 |
+
### Common Issues
|
| 785 |
+
| Problem | Solution |
|
| 786 |
+
|---------|----------|
|
| 787 |
+
| "Repository not found" | Check URL, ensure repo is public |
|
| 788 |
+
| "Branch not found" | Leave branch empty for default, or verify branch name |
|
| 789 |
+
| "Permission denied" | Ensure your HF token has write access |
|
| 790 |
+
| "Clone timed out" | Repository may be very large; try a specific branch |
|
| 791 |
+
| Space shows "Building" | Wait 1β2 minutes for the Space to build and deploy |
|
| 792 |
+
""")
|
| 793 |
|
| 794 |
import_btn.click(
|
| 795 |
fn=import_github_to_hf,
|
| 796 |
+
inputs=[
|
| 797 |
+
github_url_input,
|
| 798 |
+
space_id_input,
|
| 799 |
+
sdk_dropdown,
|
| 800 |
+
token_input,
|
| 801 |
+
private_checkbox,
|
| 802 |
+
branch_input,
|
| 803 |
+
],
|
| 804 |
outputs=[status_output, files_output],
|
| 805 |
+
concurrency_limit=CONCURRENCY_LIMIT,
|
| 806 |
+
concurrency_id="github_import",
|
| 807 |
+
trigger_mode="once",
|
| 808 |
)
|
| 809 |
|
| 810 |
+
demo.queue(default_concurrency_limit=CONCURRENCY_LIMIT, max_size=10)
|
| 811 |
+
|
| 812 |
if __name__ == "__main__":
|
| 813 |
+
demo.launch(
|
| 814 |
+
show_error=True,
|
| 815 |
+
theme=gr.themes.Soft(),
|
| 816 |
+
css="""
|
| 817 |
+
footer { display: none !important; }
|
| 818 |
+
.info-box { background: #f0f7ff; border-radius: 8px; padding: 12px; margin: 8px 0; }
|
| 819 |
+
""",
|
| 820 |
+
)
|