| """ |
| Upload handwriting recognition model to Hugging Face Hub |
| """ |
|
|
| import os |
| from huggingface_hub import HfApi, create_repo, upload_file |
|
|
| |
| MODEL_PATH = "best_model.pth" |
| REPO_NAME = "handwriting-recognition-iam" |
| USERNAME = "IsmatS" |
|
|
| |
| 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""" |
|
|
| |
| 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: |
| |
| api = HfApi() |
|
|
| |
| 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() |
|
|
| |
| print("π¦ Creating repository...") |
| try: |
| create_repo( |
| repo_id=repo_id, |
| repo_type="model", |
| exist_ok=True, |
| private=False |
| ) |
| print(f"β Repository created/verified: https://huggingface.co/{repo_id}") |
| except Exception as e: |
| print(f"β οΈ Repository may already exist: {e}") |
|
|
| print() |
|
|
| |
| 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() |
|
|