File size: 3,099 Bytes
c5c3f12 d44a6b5 c5c3f12 3c6035c c5c3f12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | """
Upload handwriting recognition model to Hugging Face Hub
"""
import os
from huggingface_hub import HfApi, create_repo, upload_file
# Configuration
MODEL_PATH = "best_model.pth"
REPO_NAME = "handwriting-recognition-iam" # Change this to your preferred name
USERNAME = "IsmatS" # Will use your HF username automatically
# Files to upload
FILES_TO_UPLOAD = [
"best_model.pth",
"README.md",
"requirements.txt",
"train_colab.ipynb",
"training_history.png",
"charts/01_sample_images.png",
"charts/02_text_length_distribution.png",
"charts/03_image_dimensions.png",
"charts/04_character_frequency.png",
"charts/05_summary_statistics.png"
]
def upload_model_to_hf():
"""Upload model and related files to Hugging Face Hub"""
# Check if model exists
if not os.path.exists(MODEL_PATH):
print(f"β Error: {MODEL_PATH} not found!")
print(" Please ensure the model file exists in the current directory.")
return
print("π Starting upload to Hugging Face Hub...")
print(f" Repository: {REPO_NAME}")
print()
try:
# Initialize API
api = HfApi()
# Get username from token
user_info = api.whoami()
username = user_info['name']
repo_id = f"{username}/{REPO_NAME}"
print(f"β Authenticated as: {username}")
print(f"β Repository ID: {repo_id}")
print()
# Create repository (if it doesn't exist)
print("π¦ Creating repository...")
try:
create_repo(
repo_id=repo_id,
repo_type="model",
exist_ok=True,
private=False # Set to True if you want a private repo
)
print(f"β Repository created/verified: https://huggingface.co/{repo_id}")
except Exception as e:
print(f"β οΈ Repository may already exist: {e}")
print()
# Upload files
print("π€ Uploading files...")
for file_path in FILES_TO_UPLOAD:
if os.path.exists(file_path):
print(f" Uploading {file_path}...", end=" ")
try:
upload_file(
path_or_fileobj=file_path,
path_in_repo=file_path,
repo_id=repo_id,
repo_type="model"
)
print("β")
except Exception as e:
print(f"β Failed: {e}")
else:
print(f" β οΈ Skipping {file_path} (not found)")
print()
print("=" * 60)
print("π Upload complete!")
print(f"π View your model: https://huggingface.co/{repo_id}")
print("=" * 60)
except Exception as e:
print(f"β Error during upload: {e}")
print()
print("Make sure you're logged in to Hugging Face:")
print(" Run: huggingface-cli login")
print(" Or set HF_TOKEN environment variable")
if __name__ == "__main__":
upload_model_to_hf()
|