advanced-magnus-chess-model / upload_to_hf.py
Levdalba's picture
Upload Advanced Magnus Chess Model v20250626 - 2.65M parameters trained on Magnus Carlsen games
6dc8c30 verified
#!/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.")