File size: 4,052 Bytes
f8ed285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
"""
Script to fix 403 errors and push changes to Hugging Face Spaces
"""
import os
import subprocess
import sys
from getpass import getpass

def fix_403_errors():
    """Update the app to fix 403 errors and push to Hugging Face Space."""
    print("=" * 50)
    print("Fix 403 Errors and Push to Hugging Face")
    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
    os.environ["HF_API_KEY"] = token
    
    # Add the direct remote URL with credentials embedded
    remote_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{space_name}"
    
    try:
        # Update git remotes
        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)
        
        # Pull the latest changes first to avoid conflicts
        try:
            subprocess.run(["git", "pull", "hf", "main"], check=True)
            print("Successfully pulled latest changes")
        except subprocess.CalledProcessError:
            print("Warning: Could not pull latest changes. Will attempt to push anyway.")
        
        # Stage all files
        subprocess.run(["git", "add", "."], check=True)
        
        # Commit changes
        try:
            subprocess.run(["git", "commit", "-m", "Fix 403 errors by improving model loading and error handling"], check=True)
            print("Changes committed successfully")
        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.")
        
        # Push to Space
        print("Pushing to Hugging Face Space...")
        
        # First try a normal push
        try:
            subprocess.run(["git", "push", "hf", "HEAD:main"], check=True)
        except subprocess.CalledProcessError:
            print("Normal push failed. Trying force push instead...")
            try:
                # Force push if normal push fails
                subprocess.run(["git", "push", "-f", "hf", "HEAD:main"], check=True)
            except subprocess.CalledProcessError as e:
                print(f"Force push also failed: {e}")
                print("Trying alternative push approach...")
                
                # Most reliable way to push to HF Spaces
                api_url = f"https://huggingface.co/spaces/{username}/{space_name}"
                
                try:
                    subprocess.run(["git", "remote", "set-url", "hf", api_url], check=True)
                    subprocess.run(["git", "push", "-f", "--set-upstream", "hf", "HEAD:main"], check=True)
                except subprocess.CalledProcessError as e:
                    print(f"All push attempts failed. Final error: {e}")
                    return False
        
        print("\nSuccess! Your fixes have 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.")
        return True
        
    except Exception as e:
        print(f"Unexpected error: {e}")
        return False

if __name__ == "__main__":
    if fix_403_errors():
        print("403 error fixes successfully deployed!")
    else:
        print("Failed to deploy 403 error fixes. Please check the error messages above.")
        sys.exit(1)