File size: 1,907 Bytes
294928d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from huggingface_hub import HfApi, login
import os
import sys

def deploy():
    # Use the token provided by the user in the next step
    # For now, this script will try to use the stored token or prompt
    api = HfApi()
    
    try:
        user_info = api.whoami()
        print(f"Logged in as: {user_info['name']}")
    except Exception as e:
        print(f"Authentication failed: {e}")
        print("Please provide a valid WRITE token from https://huggingface.co/settings/tokens")
        token = input("Enter your Hugging Face Write Token: ")
        try:
            login(token=token, add_to_git_credential=True)
            user_info = api.whoami()
            print(f"Successfully logged in as: {user_info['name']}")
        except Exception as login_e:
            print(f"Login failed even with token: {login_e}")
            return

    repo_id = f"{user_info['name']}/CNN2"
    print(f"Creating/Checking Space: {repo_id}")
    
    try:
        api.create_repo(
            repo_id=repo_id,
            repo_type="space",
            space_sdk="docker",
            exist_ok=True
        )
        print(f"Space {repo_id} is ready.")
        
        print("Uploading files...")
        # Uploading the files from the current directory
        # We upload Dockerfile, requirements.txt and everything in web_app/
        
        # Upload everything in the directory
        api.upload_folder(
            folder_path=".",
            repo_id=repo_id,
            repo_type="space",
            ignore_patterns=[".git*", "data*", "__pycache__*", "venv*", "*.pyc", "web_app*"]
        )
        
        print(f"\nDeployment Successful!")
        print(f"View your Space at: https://huggingface.co/spaces/{repo_id}")
        
    except Exception as e:
        print(f"Deployment failed: {e}")

if __name__ == "__main__":
    deploy()