| import os
|
| import sys
|
| from huggingface_hub import HfApi
|
| from dotenv import load_dotenv
|
|
|
|
|
| load_dotenv()
|
|
|
| def deploy():
|
|
|
| token = os.getenv("HF_TOKEN")
|
| if not token:
|
| print("❌ Error: HF_TOKEN not found in .env file.")
|
| print("Please ensure your .env file exists and contains HF_TOKEN=hf_...")
|
| return
|
|
|
| print(f"✅ Loaded HF_TOKEN from .env")
|
|
|
|
|
| if len(sys.argv) > 1:
|
| space_name = sys.argv[1]
|
| else:
|
| space_name = input("Enter your Hugging Face Space name (e.g., username/space-name): ").strip()
|
|
|
| if not space_name:
|
| print("❌ Error: Space name is required.")
|
| return
|
|
|
| print(f"🚀 Preparing to deploy to: {space_name}")
|
| print("Uploading files... (this may take a moment)")
|
|
|
|
|
| api = HfApi(token=token)
|
|
|
| try:
|
|
|
|
|
|
|
| api.upload_folder(
|
| folder_path=".",
|
| repo_id=space_name,
|
| repo_type="space",
|
|
|
| ignore_patterns=[
|
| ".env",
|
| "__pycache__",
|
| "*.pyc",
|
| ".git",
|
| ".vscode",
|
| "deploy_to_spaces.py",
|
| "test_token.py",
|
| "token_debug_log.txt",
|
| "*.bat"
|
| ],
|
| commit_message="Deploying version from local"
|
| )
|
|
|
| print("\n✅ Upload successful!")
|
| print(f"👉 Your Space should be building here: https://huggingface.co/spaces/{space_name}")
|
| print("\n⚠️ IMPORTANT REMINDER ⚠️")
|
| print("1. Go to your Space 'Settings' tab.")
|
| print("2. Scroll to 'Secrets'.")
|
| print("3. Add a new secret named 'HF_TOKEN' with your Hugging Face token value.")
|
| print(" (The app needs this to run the models!)")
|
|
|
| except Exception as e:
|
| print(f"\n❌ Deployment failed: {e}")
|
| if "404" in str(e):
|
| print("Tip: Does the Space exist? You might need to create it first at https://huggingface.co/new-space")
|
| if "403" in str(e):
|
| print("Tip: Permission denied. Check if your token has 'write' access.")
|
|
|
| if __name__ == "__main__":
|
| deploy()
|
|
|