import os import shutil import tempfile import gradio as gr from huggingface_hub import snapshot_download, model_info, dataset_info, space_info from huggingface_hub.utils import RepositoryNotFoundError, HFValidationError def detect_repo_type(repo_id): """Automatically detect repo type: model, dataset, space""" try: model_info(repo_id) return "model" except (RepositoryNotFoundError, HFValidationError): pass try: dataset_info(repo_id) return "dataset" except (RepositoryNotFoundError, HFValidationError): pass try: space_info(repo_id) return "space" except (RepositoryNotFoundError, HFValidationError): pass return None def zip_folder(source_folder): """Zip the folder into a temporary ZIP file""" tmp_zip_fd, tmp_zip_path = tempfile.mkstemp(suffix=".zip") os.close(tmp_zip_fd) # Close fd, just need path shutil.make_archive(tmp_zip_path.replace(".zip", ""), 'zip', source_folder) return tmp_zip_path def download_hf_repo_temp(repo_id, repo_type_override=None): repo_id = repo_id.strip() if not repo_id or "/" not in repo_id: return "❌ Invalid repository ID. Format should be: username/repo_name", None # Use override if provided, otherwise auto-detect if repo_type_override and repo_type_override != "auto": repo_type = repo_type_override log = f"🔍 Using specified repo type: `{repo_type}`\n" else: repo_type = detect_repo_type(repo_id) if not repo_type: return f"❌ Repository '{repo_id}' not found or inaccessible", None log = f"🔍 Auto-detected repo type: `{repo_type}`\n" # Create temporary folder temp_dir = tempfile.mkdtemp() log += f"⬇️ Downloading '{repo_id}' to temporary folder...\n" try: # Download repo snapshot_download( repo_id=repo_id, repo_type=repo_type, local_dir=temp_dir, local_dir_use_symlinks=False ) # Zip temp folder zip_path = zip_folder(temp_dir) log += "✅ Download complete and zipped successfully!\n" log += f"📦 ZIP file created: {os.path.basename(zip_path)}\n" log += "🗑️ Temporary files will be automatically cleaned up." # Cleanup temp folder after creating zip shutil.rmtree(temp_dir) return log, zip_path except Exception as e: # Cleanup on error shutil.rmtree(temp_dir) return f"❌ Error during download: {str(e)}", None # Custom CSS for better styling custom_css = """ #main-header { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 2rem; border-radius: 10px; margin-bottom: 1rem; color: white; } #main-header h1 { margin: 0; font-size: 2.5rem; font-weight: 700; } #main-header p { margin: 0.5rem 0 0 0; opacity: 0.9; font-size: 1.1rem; } .feature-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin: 1.5rem 0; } .feature-card { background: white; padding: 1rem; border-radius: 8px; border-left: 4px solid #667eea; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .download-btn { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important; color: white !important; font-weight: 600 !important; border: none !important; } .download-btn:hover { transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.2); } .log-output { font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace; font-size: 0.9em; } """ # Gradio UI with gr.Blocks(theme=gr.themes.Soft(), css=custom_css) as demo: # Header Section with gr.Column(elem_id="main-header"): gr.HTML("""
Download & Package Hugging Face Repositories
💡 Tip: Large repositories may take several minutes to download and process.
🔒 All temporary files are automatically cleaned up after download.