Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Deploy GradeM8 to Hugging Face Spaces via CLI. | |
| Usage: python deploy_hf.py --token YOUR_HF_TOKEN | |
| """ | |
| import argparse | |
| import os | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| def run_cmd(cmd, check=True): | |
| """Run a shell command.""" | |
| print(f"Running: {cmd}") | |
| result = subprocess.run(cmd, shell=True, capture_output=True, text=True) | |
| if result.stdout: | |
| print(result.stdout) | |
| if result.stderr: | |
| print(result.stderr, file=sys.stderr) | |
| if check and result.returncode != 0: | |
| sys.exit(f"Command failed: {cmd}") | |
| return result | |
| def deploy_to_hf(token, username, space_name): | |
| """Deploy to Hugging Face Spaces.""" | |
| # Set HF token for authentication | |
| os.environ['HF_TOKEN'] = token | |
| # Create Space URL | |
| space_url = f"https://huggingface.co/spaces/{username}/{space_name}" | |
| print(f"\n🚀 Deploying GradeM8 to {space_url}") | |
| print("=" * 60) | |
| # Login to Hugging Face | |
| print("\n1. Authenticating with Hugging Face...") | |
| run_cmd(f"huggingface-cli login --token {token}", check=False) | |
| # Create Space using huggingface_hub | |
| print("\n2. Creating Hugging Face Space (if it doesn't exist)...") | |
| try: | |
| from huggingface_hub import HfApi | |
| api = HfApi(token=token) | |
| # Try to create the space | |
| try: | |
| api.create_repo( | |
| repo_id=f"{username}/{space_name}", | |
| repo_type="space", | |
| space_sdk="gradio", | |
| private=False | |
| ) | |
| print(f"✅ Space '{space_name}' created successfully!") | |
| except Exception as e: | |
| if "already exists" in str(e).lower() or "409" in str(e): | |
| print(f"ℹ️ Space '{space_name}' already exists, proceeding with push...") | |
| else: | |
| print(f"⚠️ Note: {e}") | |
| except ImportError: | |
| print("⚠️ huggingface_hub not available, skipping Space creation check") | |
| # Add remote and push | |
| print("\n3. Setting up git remote...") | |
| # Remove existing remote if any | |
| run_cmd("git remote remove origin 2>/dev/null || true", check=False) | |
| # Add new remote with token | |
| remote_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{space_name}" | |
| run_cmd(f"git remote add origin {remote_url}", check=False) | |
| # Add all files | |
| print("\n4. Adding files to git...") | |
| run_cmd("git add -A") | |
| # Commit | |
| print("\n5. Committing changes...") | |
| run_cmd('git commit -m "Initial deployment to Hugging Face Spaces" || echo "Nothing to commit"', check=False) | |
| # Push | |
| print("\n6. Pushing to Hugging Face...") | |
| run_cmd("git push -u origin main --force") | |
| print("\n" + "=" * 60) | |
| print(f"✅ Deployment complete!") | |
| print(f"🌐 Your Space is available at: {space_url}") | |
| print("\n⚠️ Important: Set your DEEPINFRA_API_KEY in the Space settings:") | |
| print(f" {space_url}/settings") | |
| print("=" * 60) | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Deploy GradeM8 to Hugging Face Spaces") | |
| parser.add_argument("--token", required=True, help="Hugging Face access token") | |
| parser.add_argument("--username", required=True, help="Your Hugging Face username") | |
| parser.add_argument("--space-name", default="gradem8", help="Name for your Space (default: gradem8)") | |
| args = parser.parse_args() | |
| # Verify we're in the right directory | |
| if not Path("app.py").exists(): | |
| sys.exit("Error: app.py not found. Please run this script from the project root.") | |
| deploy_to_hf(args.token, args.username, args.space_name) | |
| if __name__ == "__main__": | |
| main() | |