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