#!/usr/bin/env python3 """ Hugging Face Model Upload Script for Advanced Magnus Chess Model """ import os import sys from pathlib import Path def upload_magnus_model(): """Upload the Advanced Magnus Chess Model to Hugging Face""" try: from huggingface_hub import HfApi, upload_folder, create_repo except ImportError: print("āŒ huggingface_hub not installed!") print("Install with: pip install huggingface_hub") return False # Configuration model_name = "advanced-magnus-chess-model" print("šŸ” Authentication Setup") print("You need a Hugging Face account and access token.") print("Get a token at: https://huggingface.co/settings/tokens") print() username = input("Enter your Hugging Face username: ").strip() if not username: print("āŒ Username required!") return False token = input( "Enter your Hugging Face token (or press Enter to use HF_TOKEN env var): " ).strip() if not token and "HF_TOKEN" in os.environ: token = os.environ["HF_TOKEN"] print("āœ… Using HF_TOKEN from environment") if not token: print("āŒ No Hugging Face token provided!") print("Either enter it above or set HF_TOKEN environment variable") return False repo_id = f"{username}/{model_name}" print(f"\nšŸš€ Uploading model to: {repo_id}") # Initialize API try: api = HfApi(token=token) print("āœ… Authenticated with Hugging Face") except Exception as e: print(f"āŒ Authentication failed: {e}") return False # Create repository try: create_repo( repo_id=repo_id, token=token, repo_type="model", exist_ok=True, private=False, ) print(f"āœ… Repository created/verified: {repo_id}") except Exception as e: print(f"āš ļø Repository creation issue: {e}") print("This might be normal if the repository already exists.") # Prepare README for Hugging Face readme_content = open("README_HF.md", "r").read() with open("README.md", "w") as f: f.write(readme_content) print("āœ… Prepared README for Hugging Face format") # Upload the entire folder print("\nšŸ“¤ Starting upload...") try: upload_folder( folder_path=".", repo_id=repo_id, token=token, repo_type="model", commit_message="Upload Advanced Magnus Chess Model v20250626 - 2.65M parameters trained on Magnus Carlsen games", ignore_patterns=[ ".git", "__pycache__", "*.pyc", ".DS_Store", "upload_instructions.py", ], ) print(f"āœ… Model uploaded successfully!") print(f"\n🌐 View your model at:") print(f" https://huggingface.co/{repo_id}") print(f"\nšŸ“š Users can now install and use your model:") print(f" pip install huggingface_hub torch chess") print(f" # Then download and use your model") except Exception as e: print(f"āŒ Upload failed: {e}") return False return True if __name__ == "__main__": print("šŸŽÆ Advanced Magnus Chess Model - Hugging Face Upload") print("šŸ† 2.65M Parameter Neural Network trained on Magnus Carlsen's games") print("=" * 70) # Check if we're in the right directory if not os.path.exists("model.pth"): print("āŒ model.pth not found in current directory!") print("Please run this script from the huggingface_model directory") exit(1) # Check model file model_path = Path("model.pth") model_size_mb = model_path.stat().st_size / (1024 * 1024) print(f"šŸ“ Model file: {model_path}") print(f"šŸ“Š Model size: {model_size_mb:.2f} MB") # Show model info if os.path.exists("config.yaml"): try: import yaml with open("config.yaml", "r") as f: config = yaml.safe_load(f) print(f"🧠 Architecture: {config['model']['architecture']}") print(f"šŸŽÆ Parameters: {config['training']['total_params']:,}") print(f"šŸ“ˆ Test Accuracy: {config['metrics']['test_accuracy']:.4f}") except ImportError: print("🧠 Architecture: AdvancedMagnusModel") print("šŸŽÆ Parameters: 2,651,538") except Exception as e: print(f"āš ļø Could not read config: {e}") print("\n" + "=" * 70) proceed = input("Proceed with upload? (y/N): ").strip().lower() if proceed == "y": success = upload_magnus_model() if success: print("\nšŸŽ‰ Upload completed successfully!") print("Your Advanced Magnus Chess Model is now available on Hugging Face!") print("The chess community can now benefit from your Magnus AI! šŸ†") else: print("\nāŒ Upload failed. Please check your credentials and try again.") else: print("Upload cancelled.") if __name__ == "__main__": print("šŸŽÆ Advanced Magnus Chess Model - Hugging Face Upload") print("=" * 60) # Check if we're in the right directory if not os.path.exists("model.pth"): print("āŒ model.pth not found in current directory!") print("Please run this script from the huggingface_model directory") exit(1) # Check model file model_path = Path("model.pth") model_size_mb = model_path.stat().st_size / (1024 * 1024) print(f"šŸ“ Model file: {model_path}") print(f"šŸ“Š Model size: {model_size_mb:.2f} MB") # Show model info if os.path.exists("config.yaml"): with open("config.yaml", "r") as f: config = yaml.safe_load(f) print(f"🧠 Architecture: {config['model']['architecture']}") print(f"šŸŽÆ Parameters: {config['training']['total_params']:,}") print(f"šŸ“ˆ Test Accuracy: {config['metrics']['test_accuracy']:.4f}") print("\n" + "=" * 60) proceed = input("Proceed with upload? (y/N): ").strip().lower() if proceed == "y": success = upload_magnus_model() if success: print("\nšŸŽ‰ Upload completed successfully!") print("Your model is now available on Hugging Face!") else: print("\nāŒ Upload failed. Please check your credentials and try again.") else: print("Upload cancelled.")