| |
| """ |
| 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) |
| |
| |
| username = input("Enter your Hugging Face username: ") |
| token = getpass("Enter your Hugging Face token: ") |
| space_name = input("Enter your Space name: ") |
| |
| |
| os.environ["HUGGINGFACEHUB_API_TOKEN"] = token |
| os.environ["HF_API_KEY"] = token |
| |
| |
| remote_url = f"https://{username}:{token}@huggingface.co/spaces/{username}/{space_name}" |
| |
| try: |
| |
| 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) |
| |
| |
| 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.") |
| |
| |
| subprocess.run(["git", "add", "."], check=True) |
| |
| |
| 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: |
| |
| 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.") |
| |
| |
| print("Pushing to Hugging Face Space...") |
| |
| |
| try: |
| subprocess.run(["git", "push", "hf", "HEAD:main"], check=True) |
| except subprocess.CalledProcessError: |
| print("Normal push failed. Trying force push instead...") |
| try: |
| |
| 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...") |
| |
| |
| 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) |