#!/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)