import os import sys from huggingface_hub import HfApi from dotenv import load_dotenv # Load environment variables to get the token load_dotenv() def deploy(): # 1. Get the HF Token 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") # 2. Get the Space Name 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)") # 3. Upload files api = HfApi(token=token) try: # Check if repo exists, if not prompt or just try updating # We assume the user created the space. If not, api.create_repo could be used but it's safer to let them do it or assume it exists. api.upload_folder( folder_path=".", repo_id=space_name, repo_type="space", # patterns to IGNORE - Critical to not upload .env 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()