How to push fine-tuned model to Hugging Face hub
Quick answer
Use the
huggingface_hub Python library to push your fine-tuned model to the Hugging Face hub by logging in with your token and calling push_to_hub() on your model or repository. This process uploads model files, configuration, and tokenizer for easy sharing and deployment.PREREQUISITES
Python 3.8+pip install huggingface_hub transformersHugging Face account with an access token
Setup
Install the huggingface_hub and transformers libraries and set your Hugging Face access token as an environment variable for authentication.
pip install huggingface_hub transformers Step by step
This example shows how to push a fine-tuned transformers model to the Hugging Face hub using the push_to_hub() method.
import os
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# Load your fine-tuned model and tokenizer
model_name_or_path = "./fine_tuned_model"
model = AutoModelForSequenceClassification.from_pretrained(model_name_or_path)
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
# Set your HF token in environment variable HF_TOKEN
hf_token = os.environ["HF_TOKEN"]
# Define your repo name on Hugging Face hub
repo_name = "your-username/fine-tuned-model"
# Push model and tokenizer to the hub
model.push_to_hub(repo_name, use_auth_token=hf_token)
tokenizer.push_to_hub(repo_name, use_auth_token=hf_token)
print(f"Model and tokenizer pushed to https://huggingface.co/{repo_name}") output
Model and tokenizer pushed to https://huggingface.co/your-username/fine-tuned-model
Common variations
- Use
huggingface_hub.Repositoryclass to manage repo files and commits manually. - Push other model types like
AutoModelForCausalLMsimilarly. - Use
use_auth_token=Trueif your token is set in~/.huggingface/token.
from huggingface_hub import Repository
repo = Repository(local_dir="./fine_tuned_model", clone_from=repo_name, use_auth_token=hf_token)
repo.push_to_hub(commit_message="Add fine-tuned model files") Troubleshooting
- If you get authentication errors, verify your
HF_TOKENenvironment variable is set correctly. - Ensure your repo name includes your username and is unique on Hugging Face hub.
- Check your internet connection and firewall settings if upload fails.
Key Takeaways
- Use the huggingface_hub Python library's push_to_hub() method to upload fine-tuned models.
- Set your Hugging Face access token in an environment variable for secure authentication.
- You can push both model weights and tokenizer files to the Hugging Face hub.
- The Repository class allows manual control over repo commits and file management.
- Verify repo naming and token permissions to avoid common push errors.