import os import sys import subprocess def push_to_huggingface(token, space_id): """ Pushes the local directory to a Hugging Face Space. space_id should be in the format 'username/space_name' """ try: from huggingface_hub import HfApi except ImportError: print("Installing huggingface_hub...") subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub"]) from huggingface_hub import HfApi print(f"Pushing current directory to Hugging Face Space: {space_id}...") api = HfApi(token=token) try: # Create space if it doesn't exist api.create_repo( repo_id=space_id, repo_type="space", space_sdk="docker", exist_ok=True ) print("Space created or already exists.") except Exception as e: print(f"Note: Could not create space. It may already exist. ({e})") try: # Upload folder api.upload_folder( folder_path=".", repo_id=space_id, repo_type="space", commit_message="OpenEnv Hackathon Submission - CivicAI Environment", ignore_patterns=[".git", ".venv", "__pycache__", "push_to_hf.py", ".DS_Store"] ) print(f"\nāœ… Successfully pushed to Hugging Face Spaces!") print(f"🌐 View your space at: https://huggingface.co/spaces/{space_id}") print(f"šŸ‘‰ Don't forget to update the README.md with this URL!") except Exception as e: print(f"\nāŒ Error pushing to space: {e}") if __name__ == "__main__": print("šŸš€ CivicAI -> Hugging Face Spaces Deployer") print("="*45) # Get user input token = input("Enter your Hugging Face Token (from https://huggingface.co/settings/tokens): ").strip() space_id = input("Enter the target Space ID (e.g. your_username/civicai-env): ").strip() if not token or not space_id: print("Error: Token and Space ID are required.") sys.exit(1) push_to_huggingface(token, space_id)