init
Browse files
scripts/upload_instructions.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Upload Model to Hugging Face Hub
|
| 2 |
+
|
| 3 |
+
## Quick Start (3 Steps)
|
| 4 |
+
|
| 5 |
+
### 1. Install Hugging Face Hub
|
| 6 |
+
```bash
|
| 7 |
+
pip install huggingface_hub
|
| 8 |
+
```
|
| 9 |
+
|
| 10 |
+
### 2. Login to Hugging Face
|
| 11 |
+
```bash
|
| 12 |
+
huggingface-cli login
|
| 13 |
+
```
|
| 14 |
+
Enter your Hugging Face token when prompted. Get your token from: https://huggingface.co/settings/tokens
|
| 15 |
+
|
| 16 |
+
### 3. Run Upload Script
|
| 17 |
+
```bash
|
| 18 |
+
python upload_to_huggingface.py
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
## Alternative: Manual Upload via Web Interface
|
| 24 |
+
|
| 25 |
+
1. Go to https://huggingface.co/new
|
| 26 |
+
2. Create a new model repository (e.g., `handwriting-recognition-iam`)
|
| 27 |
+
3. Click "Files" β "Add file" β "Upload files"
|
| 28 |
+
4. Upload:
|
| 29 |
+
- `best_model.pth`
|
| 30 |
+
- `README.md`
|
| 31 |
+
- `requirements.txt`
|
| 32 |
+
- `train_colab.ipynb`
|
| 33 |
+
- `training_history.png`
|
| 34 |
+
|
| 35 |
+
---
|
| 36 |
+
|
| 37 |
+
## Alternative: Upload from Python (Colab/Script)
|
| 38 |
+
|
| 39 |
+
```python
|
| 40 |
+
from huggingface_hub import HfApi, create_repo, upload_file
|
| 41 |
+
|
| 42 |
+
# Login first (in Colab)
|
| 43 |
+
from huggingface_hub import notebook_login
|
| 44 |
+
notebook_login()
|
| 45 |
+
|
| 46 |
+
# Create repository
|
| 47 |
+
api = HfApi()
|
| 48 |
+
repo_id = "your-username/handwriting-recognition-iam"
|
| 49 |
+
create_repo(repo_id, repo_type="model", exist_ok=True)
|
| 50 |
+
|
| 51 |
+
# Upload model
|
| 52 |
+
upload_file(
|
| 53 |
+
path_or_fileobj="best_model.pth",
|
| 54 |
+
path_in_repo="best_model.pth",
|
| 55 |
+
repo_id=repo_id,
|
| 56 |
+
repo_type="model"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
print(f"β Uploaded! View at: https://huggingface.co/{repo_id}")
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
---
|
| 63 |
+
|
| 64 |
+
## What Gets Uploaded
|
| 65 |
+
|
| 66 |
+
- β
`best_model.pth` - Trained model checkpoint (105MB)
|
| 67 |
+
- β
`README.md` - Project documentation
|
| 68 |
+
- β
`requirements.txt` - Dependencies
|
| 69 |
+
- β
`train_colab.ipynb` - Training notebook
|
| 70 |
+
- β
`training_history.png` - Training metrics visualization
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
## Customization
|
| 75 |
+
|
| 76 |
+
Edit `upload_to_huggingface.py` to change:
|
| 77 |
+
- `REPO_NAME` - Your preferred repository name
|
| 78 |
+
- `private=False` - Set to `True` for private repository
|
| 79 |
+
- `FILES_TO_UPLOAD` - Add/remove files to upload
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## Troubleshooting
|
| 84 |
+
|
| 85 |
+
### "Authentication required"
|
| 86 |
+
```bash
|
| 87 |
+
huggingface-cli login
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### "Repository already exists"
|
| 91 |
+
- The script uses `exist_ok=True`, so it will update existing repo
|
| 92 |
+
- Or change `REPO_NAME` to create a new one
|
| 93 |
+
|
| 94 |
+
### Large file upload fails
|
| 95 |
+
- Hugging Face supports files up to 50GB
|
| 96 |
+
- Your model (105MB) should upload fine
|
| 97 |
+
- If it fails, try uploading via web interface
|
scripts/upload_to_huggingface.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Upload handwriting recognition model to Hugging Face Hub
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import os
|
| 6 |
+
from huggingface_hub import HfApi, create_repo, upload_file
|
| 7 |
+
|
| 8 |
+
# Configuration
|
| 9 |
+
MODEL_PATH = "best_model.pth"
|
| 10 |
+
REPO_NAME = "handwriting-recognition-iam" # Change this to your preferred name
|
| 11 |
+
USERNAME = None # Will use your HF username automatically
|
| 12 |
+
|
| 13 |
+
# Files to upload
|
| 14 |
+
FILES_TO_UPLOAD = [
|
| 15 |
+
"best_model.pth",
|
| 16 |
+
"README.md",
|
| 17 |
+
"requirements.txt",
|
| 18 |
+
"train_colab.ipynb",
|
| 19 |
+
"training_history.png"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def upload_model_to_hf():
|
| 23 |
+
"""Upload model and related files to Hugging Face Hub"""
|
| 24 |
+
|
| 25 |
+
# Check if model exists
|
| 26 |
+
if not os.path.exists(MODEL_PATH):
|
| 27 |
+
print(f"β Error: {MODEL_PATH} not found!")
|
| 28 |
+
print(" Please ensure the model file exists in the current directory.")
|
| 29 |
+
return
|
| 30 |
+
|
| 31 |
+
print("π Starting upload to Hugging Face Hub...")
|
| 32 |
+
print(f" Repository: {REPO_NAME}")
|
| 33 |
+
print()
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Initialize API
|
| 37 |
+
api = HfApi()
|
| 38 |
+
|
| 39 |
+
# Get username from token
|
| 40 |
+
user_info = api.whoami()
|
| 41 |
+
username = user_info['name']
|
| 42 |
+
repo_id = f"{username}/{REPO_NAME}"
|
| 43 |
+
|
| 44 |
+
print(f"β Authenticated as: {username}")
|
| 45 |
+
print(f"β Repository ID: {repo_id}")
|
| 46 |
+
print()
|
| 47 |
+
|
| 48 |
+
# Create repository (if it doesn't exist)
|
| 49 |
+
print("π¦ Creating repository...")
|
| 50 |
+
try:
|
| 51 |
+
create_repo(
|
| 52 |
+
repo_id=repo_id,
|
| 53 |
+
repo_type="model",
|
| 54 |
+
exist_ok=True,
|
| 55 |
+
private=False # Set to True if you want a private repo
|
| 56 |
+
)
|
| 57 |
+
print(f"β Repository created/verified: https://huggingface.co/{repo_id}")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"β οΈ Repository may already exist: {e}")
|
| 60 |
+
|
| 61 |
+
print()
|
| 62 |
+
|
| 63 |
+
# Upload files
|
| 64 |
+
print("π€ Uploading files...")
|
| 65 |
+
for file_path in FILES_TO_UPLOAD:
|
| 66 |
+
if os.path.exists(file_path):
|
| 67 |
+
print(f" Uploading {file_path}...", end=" ")
|
| 68 |
+
try:
|
| 69 |
+
upload_file(
|
| 70 |
+
path_or_fileobj=file_path,
|
| 71 |
+
path_in_repo=file_path,
|
| 72 |
+
repo_id=repo_id,
|
| 73 |
+
repo_type="model"
|
| 74 |
+
)
|
| 75 |
+
print("β")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"β Failed: {e}")
|
| 78 |
+
else:
|
| 79 |
+
print(f" β οΈ Skipping {file_path} (not found)")
|
| 80 |
+
|
| 81 |
+
print()
|
| 82 |
+
print("=" * 60)
|
| 83 |
+
print("π Upload complete!")
|
| 84 |
+
print(f"π View your model: https://huggingface.co/{repo_id}")
|
| 85 |
+
print("=" * 60)
|
| 86 |
+
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print(f"β Error during upload: {e}")
|
| 89 |
+
print()
|
| 90 |
+
print("Make sure you're logged in to Hugging Face:")
|
| 91 |
+
print(" Run: huggingface-cli login")
|
| 92 |
+
print(" Or set HF_TOKEN environment variable")
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
upload_model_to_hf()
|