File size: 2,610 Bytes
60bcf4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
67
68
69
70
71
72
73
74
75
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()