File size: 2,459 Bytes
403ced7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python
"""
Simple script to push directly to Hugging Face Space.
This is a streamlined approach when you already have a Space.
"""
import os
import subprocess
import sys
from getpass import getpass

def push_to_huggingface():
    """Push the current directory to Hugging Face Space."""
    print("=" * 50)
    print("Simple Hugging Face Push Tool")
    print("=" * 50)
    
    # Get credentials
    username = input("Enter your Hugging Face username: ")
    token = getpass("Enter your Hugging Face token: ")
    space_name = input("Enter your Space name: ")
    
    # Set environment variables
    os.environ["HUGGINGFACEHUB_API_TOKEN"] = token
    
    # Add the direct remote URL
    remote_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{space_name}"
    
    try:
        # Add remote if not exists
        remotes = subprocess.run(["git", "remote"], capture_output=True, text=True).stdout.strip().split('\n')
        if "hf" not in remotes:
            subprocess.run(["git", "remote", "add", "hf", remote_url], check=True)
        else:
            subprocess.run(["git", "remote", "set-url", "hf", remote_url], check=True)
        
        # Stage all files
        subprocess.run(["git", "add", "."], check=True)
        
        # Commit changes
        try:
            subprocess.run(["git", "commit", "-m", "Fix 403 error by using local models"], check=True)
        except subprocess.CalledProcessError:
            # Check if there are changes to commit
            status = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True).stdout.strip()
            if not status:
                print("No changes to commit.")
            else:
                print("Error making commit. Will try to push existing commits.")
        
        # Force push to Space
        print("Pushing to Hugging Face Space...")
        subprocess.run(["git", "push", "-f", "hf", "HEAD:main"], check=True)
        
        print("\nSuccess! Your code has been pushed to Hugging Face Space.")
        print(f"View your Space at: https://huggingface.co/spaces/{username}/{space_name}")
        print("Note: It may take a few minutes for changes to appear.")
        
    except subprocess.CalledProcessError as e:
        print(f"Error: {e}")
        sys.exit(1)
    except Exception as e:
        print(f"Unexpected error: {e}")
        sys.exit(1)

if __name__ == "__main__":
    push_to_huggingface()